Skip to content
Permalink
Browse files

datadog: inject trace/span ids into MDC (#1188)

  • Loading branch information...
mightyguava committed Sep 6, 2019
1 parent 1a27fa4 commit 9713983fe9004f604d606975c6938d392cb86c10
@@ -15,6 +15,7 @@ class DatadogTracingBackendModule : KAbstractModule() {
val tracer = GlobalTracer.get()
if (tracer is Tracer) {
bind<Tracer>().toInstance(tracer)
tracer.addScopeListener(MDCScopeListener())
} else {
logger.error(
"A DDTracer was unexpectedly not installed by the java agent, binding a NoopTracer")
@@ -0,0 +1,38 @@
package misk.tracing.backends.datadog

import datadog.trace.api.CorrelationIdentifier
import datadog.trace.context.ScopeListener
import misk.logging.getLogger
import org.slf4j.MDC

/**
* A scope listener that updates the MDC with the trace and span reference anytime a new scope is
* activated or closed.
*/
class MDCScopeListener : ScopeListener {
companion object {
val log = getLogger<MDCScopeListener>()
const val MDC_TRACE_ID = "trace_id"
const val MDC_SPAN_ID = "span_id"
}

override fun afterScopeActivated() {
try {
MDC.put(MDC_TRACE_ID, CorrelationIdentifier.getTraceId())
MDC.put(MDC_SPAN_ID, CorrelationIdentifier.getSpanId())
} catch (e: Exception) {
log.debug("Exception setting log context context", e)
}

}

override fun afterScopeClosed() {
try {
MDC.remove(MDC_TRACE_ID)
MDC.remove(MDC_SPAN_ID)
} catch (e: Exception) {
log.debug("Exception removing log context context", e)
}

}
}

0 comments on commit 9713983

Please sign in to comment.
You can’t perform that action at this time.