Skip to content

Commit

Permalink
Prefix baggage key not value when adding it to logback mdc (#8066)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit authored Mar 22, 2023
1 parent 3bb312a commit eac75a1
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
5 changes: 5 additions & 0 deletions instrumentation/logback/logback-mdc-1.0/javaagent/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Settings for the Logback MDC instrumentation

| System property | Type | Default | Description |
|---|---|---|---|
| `otel.instrumentation.logback-mdc.add-baggage` | Boolean | `false` | Enable exposing baggage attributes through MDC. |
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,8 @@ public static void onExit(
// (Java 6 related errors are observed) so relying on for loop instead
for (Map.Entry<String, BaggageEntry> entry : baggage.asMap().entrySet()) {
spanContextData.put(
entry.getKey(),
// prefix all baggage values to avoid clashes with existing context
"baggage." + entry.getValue().getValue());
"baggage." + entry.getKey(), entry.getValue().getValue());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@ public ILoggingEvent wrapEvent(ILoggingEvent event) {
baggage.forEach(
(key, value) ->
contextData.put(
key,
// prefix all baggage values to avoid clashes with existing context
"baggage." + value.getValue()));
"baggage." + key, value.getValue()));
}

if (eventContext == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ abstract class AbstractLogbackTest extends InstrumentationSpecification {
events[0].getMDCPropertyMap().get("trace_id") == null
events[0].getMDCPropertyMap().get("span_id") == null
events[0].getMDCPropertyMap().get("trace_flags") == null
events[0].getMDCPropertyMap().get("baggage_key") == (expectBaggage() ? "baggage.baggage_value" : null)
events[0].getMDCPropertyMap().get("baggage.baggage_key") == (expectBaggage() ? "baggage_value" : null)

events[1].message == "log message 2"
events[1].getMDCPropertyMap().get("trace_id") == null
events[1].getMDCPropertyMap().get("span_id") == null
events[1].getMDCPropertyMap().get("trace_flags") == null
events[1].getMDCPropertyMap().get("baggage_key") == (expectBaggage() ? "baggage.baggage_value" : null)
events[1].getMDCPropertyMap().get("baggage.baggage_key") == (expectBaggage() ? "baggage_value" : null)
}

def "ids when span"() {
Expand All @@ -86,19 +86,19 @@ abstract class AbstractLogbackTest extends InstrumentationSpecification {
events[0].getMDCPropertyMap().get("trace_id") == span1.spanContext.traceId
events[0].getMDCPropertyMap().get("span_id") == span1.spanContext.spanId
events[0].getMDCPropertyMap().get("trace_flags") == "01"
events[0].getMDCPropertyMap().get("baggage_key") == (expectBaggage() ? "baggage.baggage_value" : null)
events[0].getMDCPropertyMap().get("baggage.baggage_key") == (expectBaggage() ? "baggage_value" : null)

events[1].message == "log message 2"
events[1].getMDCPropertyMap().get("trace_id") == null
events[1].getMDCPropertyMap().get("span_id") == null
events[1].getMDCPropertyMap().get("trace_flags") == null
events[1].getMDCPropertyMap().get("baggage_key") == null
events[1].getMDCPropertyMap().get("baggage.baggage_key") == null

events[2].message == "log message 3"
events[2].getMDCPropertyMap().get("trace_id") == span2.spanContext.traceId
events[2].getMDCPropertyMap().get("span_id") == span2.spanContext.spanId
events[2].getMDCPropertyMap().get("trace_flags") == "01"
events[2].getMDCPropertyMap().get("baggage_key") == (expectBaggage() ? "baggage.baggage_value" : null)
events[2].getMDCPropertyMap().get("baggage.baggage_key") == (expectBaggage() ? "baggage_value" : null)
}

Span runWithSpanAndBaggage(String spanName, Baggage baggage, Closure callback) {
Expand Down

0 comments on commit eac75a1

Please sign in to comment.