Skip to content

Instantly share code, notes, and snippets.

@ndchandar ndchandar/CorrrelationIdCodec.scala Secret
Last active Sep 20, 2019

Embed
What would you like to do?
kamon {
environment {
service = "my-app"
# Arbitrary key-value pairs that further identify the environment where this service instance is running.
tags {
env = "local"
env = ${?ENVIRONMENT}
region = "us-east-1"
region = ${?REGION}
}
}
trace {
# If you are using Zipkin, keep this option enabled. If you are using Jaeger, disable it.
join-remote-parents-with-same-span-id = yes
# Interval at which sampled finished spans will be flushed to SpanReporters.
tick-interval = 1 seconds // default is 10 seconds
}
propagation {
http {
default {
tags {
mappings { correlationId = "X-COMP-ID-CORRELATION" }
}
entries {
incoming {
correlationId = "com.mycompany.myapp.utils.CorrelationIdCodec"
}
outgoing {
correlationId = "com.mycompany.myapp.utils.CorrelationIdCodec"
}
}
}
}
}
instrumentation.logback {
mdc {
trace-id-key = "traceId"
span-id-key = "spanId"
}
copy {
# Controls whether Context information should be copied into the MDC or not.
enabled = yes
# Controls whether Context tags should be copied into the MDC.
tags = yes
# Contains the names of all Context entries that should be copied into the MDC.
entries = [correlationId]
}
}
}
kanela {
modules {
logback { enabled = true }
}
}
package com.mycomp.myapp.utils
import kamon.context.Context
import kamon.context.HttpPropagation.{HeaderReader, HeaderWriter}
import kamon.context.Propagation.{EntryReader, EntryWriter}
object CorrelationIdCodec {
private val HeaderName = "X-COMPID-CORRELATION"
val defaultId = IdGenerator.newId
val correlationKey = Context.key[String]("correlationId", null)
def apply() = new CorrelationIdCodec()
}
// From https://github.com/kamon-io/Kamon/blob/master/kamon-core-tests/src/test/scala/kamon/context/HttpPropagationSpec.scala#L135-L163
class CorrelationIdCodec extends EntryReader[HeaderReader] with EntryWriter[HeaderWriter] {
override def read(reader: HeaderReader, context: Context): Context =
reader.read(HeaderName).map(v => context.withEntry(correlationKey, v)).getOrElse(context)
override def write(context: Context, writer: HeaderWriter): Unit =
Option(context.get(correlationKey)).foreach(v => writer.write(HeaderName, v))
}
object IdGenerator {
def newId: String = UUID.randomUUID.toString
}
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<conversionRule conversionWord="traceId" converterClass="kamon.instrumentation.logback.tools.TraceIDConverter" />
<conversionRule conversionWord="spanId" converterClass="kamon.instrumentation.logback.tools.SpanIDConverter" />
<conversionRule conversionWord="contextTag" converterClass="kamon.instrumentation.logback.tools.ContextTagConverter" />
<conversionRule conversionWord="contextEntry" converterClass="kamon.instrumentation.logback.tools.ContextEntryConverter" />
<conversionRule conversionWord="correlationId" converterClass="com.mycomp.myapp.utils.CorrelationIdConverter" />
<appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/var/log/myapp/myapp.log</file>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>/var/log/myapp/myapp.%i.log.gz</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>3</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>50MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>timestamp=%d{HH:mm:ss.SSS}, level=%-5level, loggingId=%X{correlationId}, traceId=%X{traceId}, logger=%logger{36}, message=%msg,%n
</pattern>
</encoder>
</appender>
<appender name="ROLLING_ASYNC" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="ROLLING"/>
</appender>
<root level="INFO">
<appender-ref ref="ROLLING_ASYNC" />
</root>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.