Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,17 @@ private static boolean isConfiguredResourceAttributeAccessible() {
}

/**
* Returns context from the current span when available.
* Returns context from the current span and baggage when available.
*
* @return A map containing string versions of the traceId, spanId, and traceFlags, which can then
* be accessed from layout components
* @return A map containing string versions of the traceId, spanId, traceFlags and baggage entries,
* which can then be accessed from layout components
*/
@Override
public Map<String, String> supplyContextData() {
Context context = Context.current();
Span currentSpan = Span.fromContext(context);
if (!currentSpan.getSpanContext().isValid()) {
SpanContext spanContext = Span.fromContext(context).getSpanContext();
Baggage baggage = Configuration.baggageEnabled ? Baggage.fromContext(context) : Baggage.empty();
if (!spanContext.isValid() && baggage.isEmpty()) {
Comment thread
trask marked this conversation as resolved.
Outdated
return staticContextData;
}

Expand All @@ -77,17 +78,15 @@ public Map<String, String> supplyContextData() {
}

Map<String, String> contextData = new HashMap<>(staticContextData);
SpanContext spanContext = currentSpan.getSpanContext();
contextData.put(contextDataKeys.getTraceIdKey(), spanContext.getTraceId());
contextData.put(contextDataKeys.getSpanIdKey(), spanContext.getSpanId());
contextData.put(contextDataKeys.getTraceFlagsKey(), spanContext.getTraceFlags().asHex());
if (spanContext.isValid()) {
contextData.put(contextDataKeys.getTraceIdKey(), spanContext.getTraceId());
contextData.put(contextDataKeys.getSpanIdKey(), spanContext.getSpanId());
contextData.put(contextDataKeys.getTraceFlagsKey(), spanContext.getTraceFlags().asHex());
}

if (Configuration.baggageEnabled) {
Baggage baggage = Baggage.fromContext(context);
for (Map.Entry<String, BaggageEntry> entry : baggage.asMap().entrySet()) {
// prefix all baggage values to avoid clashes with existing context
contextData.put("baggage." + entry.getKey(), entry.getValue().getValue());
}
for (Map.Entry<String, BaggageEntry> entry : baggage.asMap().entrySet()) {
// prefix all baggage values to avoid clashes with existing context
contextData.put("baggage." + entry.getKey(), entry.getValue().getValue());
}

return contextData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,24 @@ public StringMap injectContextData(List<Property> list, StringMap stringMap) {
}

Context context = Context.current();
Span span = Span.fromContext(context);
SpanContext currentContext = span.getSpanContext();
if (!currentContext.isValid()) {
SpanContext currentContext = Span.fromContext(context).getSpanContext();
Baggage baggage = BAGGAGE_ENABLED ? Baggage.fromContext(context) : Baggage.empty();
if (!currentContext.isValid() && baggage.isEmpty()) {
return staticContextData.isEmpty() ? contextData : newContextData(contextData);
}

StringMap newContextData = newContextData(contextData);
newContextData.putValue(TRACE_ID_KEY, currentContext.getTraceId());
newContextData.putValue(SPAN_ID_KEY, currentContext.getSpanId());
newContextData.putValue(TRACE_FLAGS_KEY, currentContext.getTraceFlags().asHex());
if (currentContext.isValid()) {
newContextData.putValue(TRACE_ID_KEY, currentContext.getTraceId());
newContextData.putValue(SPAN_ID_KEY, currentContext.getSpanId());
newContextData.putValue(TRACE_FLAGS_KEY, currentContext.getTraceFlags().asHex());
}

if (BAGGAGE_ENABLED) {
Baggage baggage = Baggage.fromContext(context);
for (Map.Entry<String, BaggageEntry> entry : baggage.asMap().entrySet()) {
// prefix all baggage values to avoid clashes with existing context
newContextData.putValue("baggage." + entry.getKey(), entry.getValue().getValue());
}
for (Map.Entry<String, BaggageEntry> entry : baggage.asMap().entrySet()) {
// prefix all baggage values to avoid clashes with existing context
newContextData.putValue("baggage." + entry.getKey(), entry.getValue().getValue());
}

return newContextData;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ private String getLoggingKey(String key) {
void testNoIdsWhenNoSpan() {
Logger logger = LogManager.getLogger("TestLogger");

logger.info("log message 1");
logger.info("log message 2");
Baggage baggage = Baggage.empty().toBuilder().put("baggage_key", "baggage_value").build();
try (Scope unusedScope = baggage.makeCurrent()) {
logger.info("log message 1");
logger.info("log message 2");
}

List<ListAppender.LoggedEvent> events = ListAppender.get().getEvents();

Expand All @@ -55,12 +58,16 @@ void testNoIdsWhenNoSpan() {
assertThat(event.getContextData().get(getLoggingKey("trace_id"))).isNull();
assertThat(event.getContextData().get(getLoggingKey("span_id"))).isNull();
assertThat(event.getContextData().get(getLoggingKey("trace_flags"))).isNull();
assertThat(event.getContextData().get("baggage.baggage_key"))
.isEqualTo(expectBaggage() ? "baggage_value" : null);
},
event -> {
assertThat(event.getMessage()).isEqualTo("log message 2");
assertThat(event.getContextData().get(getLoggingKey("trace_id"))).isNull();
assertThat(event.getContextData().get(getLoggingKey("span_id"))).isNull();
assertThat(event.getContextData().get(getLoggingKey("trace_flags"))).isNull();
assertThat(event.getContextData().get("baggage.baggage_key"))
.isEqualTo(expectBaggage() ? "baggage_value" : null);
});
}

Expand Down
Loading