|
@@ -14,6 +14,7 @@ import io.opentelemetry.api.baggage.BaggageEntry;
|
|
|
import io.opentelemetry.api.trace.Span;
|
|
|
import io.opentelemetry.api.trace.SpanContext;
|
|
|
import io.opentelemetry.context.Context;
|
|
|
+import io.opentelemetry.javaagent.bootstrap.internal.ConfiguredResourceAttributesHolder;
|
|
|
import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
@@ -28,6 +29,8 @@ public final class SpanDecoratingContextDataInjector implements ContextDataInjec
|
|
|
InstrumentationConfig.get()
|
|
|
.getBoolean("otel.instrumentation.log4j-context-data.add-baggage", false);
|
|
|
|
|
|
+ private static final StringMap staticContextData = getStaticContextData();
|
|
|
+
|
|
|
private final ContextDataInjector delegate;
|
|
|
|
|
|
public SpanDecoratingContextDataInjector(ContextDataInjector delegate) {
|
|
@@ -40,17 +43,17 @@ public final class SpanDecoratingContextDataInjector implements ContextDataInjec
|
|
|
|
|
|
if (contextData.containsKey(TRACE_ID)) {
|
|
|
// Assume already instrumented event if traceId is present.
|
|
|
- return contextData;
|
|
|
+ return staticContextData.isEmpty() ? contextData : newContextData(contextData);
|
|
|
}
|
|
|
|
|
|
Context context = Context.current();
|
|
|
Span span = Span.fromContext(context);
|
|
|
SpanContext currentContext = span.getSpanContext();
|
|
|
if (!currentContext.isValid()) {
|
|
|
- return contextData;
|
|
|
+ return staticContextData.isEmpty() ? contextData : newContextData(contextData);
|
|
|
}
|
|
|
|
|
|
- StringMap newContextData = new SortedArrayStringMap(contextData);
|
|
|
+ StringMap newContextData = newContextData(contextData);
|
|
|
newContextData.putValue(TRACE_ID, currentContext.getTraceId());
|
|
|
newContextData.putValue(SPAN_ID, currentContext.getSpanId());
|
|
|
newContextData.putValue(TRACE_FLAGS, currentContext.getTraceFlags().asHex());
|
|
@@ -69,4 +72,19 @@ public final class SpanDecoratingContextDataInjector implements ContextDataInjec
|
|
|
public ReadOnlyStringMap rawContextData() {
|
|
|
return delegate.rawContextData();
|
|
|
}
|
|
|
+
|
|
|
+ private static StringMap newContextData(StringMap contextData) {
|
|
|
+ StringMap newContextData = new SortedArrayStringMap(contextData);
|
|
|
+ newContextData.putAll(staticContextData);
|
|
|
+ return newContextData;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static StringMap getStaticContextData() {
|
|
|
+ StringMap map = new SortedArrayStringMap();
|
|
|
+ for (Map.Entry<String, String> entry :
|
|
|
+ ConfiguredResourceAttributesHolder.getResourceAttributes().entrySet()) {
|
|
|
+ map.putValue(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ return map;
|
|
|
+ }
|
|
|
}
|