ZopaPublic / ktor-opentracing

Ktor features for OpenTracing instrumentation.
MIT License
37 stars 8 forks source link

Advice on when/how to insert trace-id into logging context? #7

Closed holyjak closed 3 years ago

holyjak commented 3 years ago

Hello! Thanks a lot for this library!

I would like to use MDC.put to put the trace-id into the logging context so that it gets included in all logs. But when I should do that? I guess I would need to define my own phase and insert it between Features and OpenTracingStart. Or? Thank you!

fstien commented 3 years ago

Hi Jakub, Thanks for your message! As the OpenTracingStart phase is inserted before the Features phase, I recommend that you intercept the Features phase. You can add a code block to the phase in your module function as such:

intercept(ApplicationCallPipeline.Features) {
   MDC.put("traceId", GlobalTracer.get().activeSpan().context().toTraceId())
}

Francois

holyjak commented 3 years ago

Thanks a lot! I fixed that via

    intercept(ApplicationCallPipeline.Features) {
        println("Intercept before: spanContext=" + current().spanContext)
        proceed()
        println("Intercept after: spanContext=" + current().spanContext)
    }

though I am not abandoning OpenTracing for OpenTelemetry. I will steal all the ideas from your code :-)

fstien commented 3 years ago

You're welcome! :)