Skip to content

LogbookClient (logbook-ktor) context propagration missing in response log #2186

@grassehh

Description

@grassehh

Description

Using LogbookClient from logbook-ktor, the HTTP response log does not preserves the context (for instance MDC) used in the request.

Expected Behavior

Context should be preserved in the HTTP response log

Actual Behavior

Context is missing in the HTTP response log

Possible Fix

In LogbookClient, maybe pass the context of the current coroutine when launching the new coroutine:

...
val (loggingContent, responseContent) = httpResponse.rawContent.split(httpResponse)
                scope.launch(coroutineContext) {
                    val responseProcessingStage = httpResponse.call.attributes[responseProcessingStageKey]
...

I tested it locally by forking LogbookClient and it works in my scenario.

Steps to Reproduce

  1. Configure a ktor HTTP client and install LogbookClient
  2. Add something in the MDC
  3. Make a request with the HTTP client
  4. If you check the response log, it will not contain the MDC.

Context

I'm using context propagation for observability, for example to have a traceId which I can use to view all logs related to it.

Your Environment

logbook-ktor:3.12.2
ktor-client:: 3.2.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions