main.misk.cloud.gcp.tracing.TracingLoggingEnhancer.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of misk-gcp Show documentation
Show all versions of misk-gcp Show documentation
Open source application container in Kotlin
package misk.cloud.gcp.tracing
import com.google.cloud.logging.LogEntry
import com.google.cloud.logging.LoggingEnhancer
import io.opentracing.Tracer
import io.opentracing.util.GlobalTracer
/**
* Add this enhancer to your project's Logback configuration to add trace ids to logs generated by
* GCP's LoggingAppender
*/
class TracingLoggingEnhancer : LoggingEnhancer {
override fun enhanceLogEntry(builder: LogEntry.Builder) {
enhanceLogEntry(GlobalTracer.get(), builder)
}
fun enhanceLogEntry(tracer: Tracer, builder: LogEntry.Builder) {
if (tracer.activeSpan().context().toTraceId().isNotEmpty()) {
builder.addLabel(
"appengine.googleapis.com/trace_id",
tracer.activeSpan().context().toTraceId()
)
}
}
}