Content-Length: 309673 | pFad | http://github.com/http4s/http4s/pull/7446

41 Server logging middleware: change logAction to `F[String => F[Unit]]` by iRevive · Pull Request #7446 · http4s/http4s · GitHub
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server logging middleware: change logAction to F[String => F[Unit]] #7446

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

iRevive
Copy link
Contributor

@iRevive iRevive commented May 15, 2024

The follow-up to https://discord.com/channels/632277896739946517/632286375311573032/1239565715036442686.
Repo example: https://github.com/alexcardell/otel4s-server-logger-repro-example/tree/master

Motivation

We want to allow capturing the 'state' while we are still within Kleisli closure. This is needed to make the MDC logging work.

Context

When logBody = false, the logging is 'streamlined' - the message will be logged within the Kleisli closure.

However, when logBody = true, the logic changes drastically. The middleware binds logging action to the materialization of the body.

val newBody = Stream.eval(vec.get).flatMap(v => Stream.emits(v)).unchunks
// Cannot Be Done Asynchronously - Otherwise All Chunks May Not Be Appended Previous to Finalization
val logPipe: Pipe[F, Byte, Byte] =
_.observe(_.chunks.flatMap(c => Stream.exec(vec.update(_ :+ c))))
.onFinalizeWeak(logMessage(response.withBodyStream(newBody)))
response.pipeBodyThrough(logPipe)

Since the body's materialization may happen outside of the Kelisli (and/or on a different fiber), the relevant IOLocal context is missing.

Comment on lines +100 to +103
val logF: IO[String => IO[Unit]] =
for {
state <- local.get
} yield (message: String) => logs.update(_ :+ (message, state))
Copy link
Contributor Author

@iRevive iRevive May 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test fails on the current main branch.

@iRevive iRevive force-pushed the topic/1.0/logging-middleware branch from 1bd4833 to 227234b Compare May 15, 2024 07:53
@@ -39,11 +39,11 @@ object Logger {
logBody: Boolean,
fk: F ~> G,
redactHeadersWhen: CIString => Boolean = defaultRedactHeadersWhen,
logAction: Option[String => F[Unit]] = None,
logAction: Option[F[String => F[Unit]]] = None,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also use something more flexible:

sealed trait LogAction[F[_]]

object LogAction {
  def plain[F[_]](f: String => F[Unit]): LogAction[F] = PlainAction(f)
  def deferred[F[_]](f: F[String => F[Unit])]: LogAction[F] = DeferredAction(f)
  
  private[http4s] final case class LogAction[F[_]](f: String => F[Unit]) extends LogAction[F]
  private[http4s] final case class DeferredAction[F[_]](f: F[String => F[Unit]]) extends LogAction[F]
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe even an implicit conversion can be handy:

implicit def toPlainLogAction[F[_]](f: String => F[Unit]): LogAction[F] = LogAction.plain(f)
implicit def toDeferredLogAction[F[_]](f: F[String => F[Unit]]): LogAction[F] = LogAction.deferred(f)

@iRevive iRevive force-pushed the topic/1.0/logging-middleware branch from 227234b to 5c11224 Compare May 15, 2024 08:29
@mergify mergify bot added the docs Relates to our website or tutorials label May 15, 2024
@samspills samspills added this to the 0.23.29 milestone Sep 9, 2024
@samspills samspills modified the milestones: 0.23.29, 0.23.30 Oct 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Relates to our website or tutorials module:server
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/http4s/http4s/pull/7446

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy