I currently have a spring boot project with log4j2
, log4j-layout-template-json
and, micrometer-tracing
The for illustrative purposes, log message that gets created looks like the following: (note that newlines have been added to make this more readable)
OPEN - name='http.server.requests',
contextualName='null',
error='null',
lowCardinalityKeyValues=[exception='none',
method='GET',
outcome='SUCCESS',
status='200',
uri='UNKNOWN'],
highCardinalityKeyValues=[http.url='/elided/observe'],
map=[
class io.micrometer.tracing.handler.TracingObservationHandler$TracingContext='TracingContext{span=SpanFromSpanContext{span=PropagatedSpan{ImmutableSpanContext{traceId=5aa346001bc6b8721d78e1a81b5c0927, spanId=9576f6ad1654f2b1, traceFlags=00, traceState=ArrayBasedTraceState{entries=[]}, remote=false, valid=true}}, newSpanContext=ImmutableSpanContext{traceId=5aa346001bc6b8721d78e1a81b5c0927, spanId=9576f6ad1654f2b1, traceFlags=00, traceState=ArrayBasedTraceState{entries=[]}, remote=false, valid=true}}, scope=null}',
class io.micrometer.core.instrument.Timer$Sample='io.micrometer.core.instrument.Timer$Sample@1e1ef373',
class io.micrometer.core.instrument.LongTaskTimer$Sample='SampleImpl{duration(seconds)=3.405E-4, duration(nanos)=340500.0, startTimeNanos=89066890754375}'
],
parentObservation=null
given the logs above there's a "tracing context" which looks like: (again, spacing added for readability)
TracingContext {
span=SpanFromSpanContext{
span=PropagatedSpan{
ImmutableSpanContext{
traceId=5aa346001bc6b8721d78e1a81b5c0927,
spanId=9576f6ad1654f2b1,
traceFlags=00,
traceState=ArrayBasedTraceState{entries=[]},
remote=false,
valid=true
}
},
newSpanContext=
ImmutableSpanContext{
traceId=5aa346001bc6b8721d78e1a81b5c0927,
spanId=9576f6ad1654f2b1,
traceFlags=00,
traceState=ArrayBasedTraceState{entries=[]},
remote=false,
valid=true}},
scope=null
}
I am using a JSON log formatter https://logging.apache.org/log4j/2.x/manual/json-template-layout.html but am struggling to get the settings in the template which will correctly extract the values of traceId
and spanId
to the top level JSON object which gets logged as opposed to embedded in the broader full message.
effectively I'd like to see the JSON object which gets produced in logs to have:
{
"@timestamp": "2023-04-14T19:55:14.540Z",
"log.level": "INFO",
"message": "[... full message...]",
"thread": "[...thread name...]",
"log.logger": "[...logger name...]",
"parent_id": "null",
"trace_id": "5aa346001bc6b8721d78e1a81b5c0927",
"span_id": "9576f6ad1654f2b1",
}
what resolver do I need to use in order to extract context information in java?