Skip to content

Commit ee20859

Browse files
committed
1st attempt at unit test
1 parent cddd78f commit ee20859

File tree

4 files changed

+65
-17
lines changed

4 files changed

+65
-17
lines changed

dd-java-agent/agent-otel/otel-shim/src/main/java/datadog/opentelemetry/shim/context/OtelContext.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class OtelContext implements Context {
2323

2424
private static final String OTEL_CONTEXT_SPAN_KEY = "opentelemetry-trace-span-key";
2525
private static final String OTEL_CONTEXT_ROOT_SPAN_KEY = "opentelemetry-traces-local-root-span";
26-
private static final String OTEL_CONTEXT_BAGGAGE_KEY = "opentelemetry-baggage";
26+
private static final String OTEL_CONTEXT_BAGGAGE_KEY = "opentelemetry-baggage-key";
2727

2828
/** Keep track of propagated context that has not been captured on the scope stack. */
2929
private static final ThreadLocal<OtelContext> lastPropagated = new ThreadLocal<>();
@@ -42,7 +42,8 @@ public OtelContext(Span currentSpan, Span rootSpan, BaggageContext baggageContex
4242
this(currentSpan, rootSpan, baggageContext, NO_ENTRIES);
4343
}
4444

45-
public OtelContext(Span currentSpan, Span rootSpan, BaggageContext baggageContext, Object[] entries) {
45+
public OtelContext(
46+
Span currentSpan, Span rootSpan, BaggageContext baggageContext, Object[] entries) {
4647
this.currentSpan = currentSpan;
4748
this.rootSpan = rootSpan;
4849
this.baggageContext = baggageContext;
@@ -57,7 +58,7 @@ public <V> V get(ContextKey<V> key) {
5758
return (V) this.currentSpan;
5859
} else if (OTEL_CONTEXT_ROOT_SPAN_KEY.equals(key.toString())) {
5960
return (V) this.rootSpan;
60-
} else if (OTEL_CONTEXT_BAGGAGE_KEY.equals(key.toString())){
61+
} else if (OTEL_CONTEXT_BAGGAGE_KEY.equals(key.toString())) {
6162
return (V) this.baggageContext;
6263
}
6364
for (int i = 0; i < this.entries.length; i += 2) {
@@ -74,7 +75,7 @@ public <V> Context with(ContextKey<V> key, V value) {
7475
return new OtelContext((Span) value, this.rootSpan, null, this.entries);
7576
} else if (OTEL_CONTEXT_ROOT_SPAN_KEY.equals(key.toString())) {
7677
return new OtelContext(this.currentSpan, (Span) value, null, this.entries);
77-
} else if (OTEL_CONTEXT_BAGGAGE_KEY.equals(key.toString())){
78+
} else if (OTEL_CONTEXT_BAGGAGE_KEY.equals(key.toString())) {
7879
return new OtelContext(null, null, this.baggageContext, this.entries);
7980
}
8081
Object[] newEntries = null;
@@ -170,7 +171,7 @@ public static Context lastPropagated() {
170171
return lastPropagated.get();
171172
}
172173

173-
public static String getOtelContextBaggageKey(){
174+
public static String getOtelContextBaggageKey() {
174175
return OTEL_CONTEXT_BAGGAGE_KEY;
175176
}
176177

dd-java-agent/agent-otel/otel-shim/src/main/java/datadog/opentelemetry/shim/context/propagation/AgentTextMapPropagator.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import static datadog.context.propagation.Propagators.defaultPropagator;
44
import static datadog.opentelemetry.shim.trace.OtelSpanContext.fromRemote;
55
import static datadog.trace.api.TracePropagationStyle.TRACECONTEXT;
6-
import static datadog.trace.bootstrap.instrumentation.api.AgentPropagation.extractContextAndGetSpanContext;
76

87
import datadog.context.propagation.Propagators;
98
import datadog.opentelemetry.shim.context.OtelContext;
@@ -16,7 +15,6 @@
1615
import datadog.trace.bootstrap.instrumentation.api.BaggageContext;
1716
import datadog.trace.bootstrap.instrumentation.api.TagContext;
1817
import datadog.trace.util.PropagationUtils;
19-
import io.opentelemetry.api.baggage.Baggage;
2018
import io.opentelemetry.api.trace.Span;
2119
import io.opentelemetry.api.trace.SpanContext;
2220
import io.opentelemetry.api.trace.TraceState;
@@ -51,19 +49,25 @@ public <C> Context extract(Context context, @Nullable C carrier, TextMapGetter<C
5149
return context;
5250
}
5351

54-
datadog.context.Context extracted = Propagators.defaultPropagator().extract(datadog.context.Context.root(), carrier, (carrier1, classifier) -> {
55-
for (String key : getter.keys(carrier1)) {
56-
classifier.accept(key, getter.get(carrier1, key));
57-
}
58-
});
52+
datadog.context.Context extracted =
53+
Propagators.defaultPropagator()
54+
.extract(
55+
datadog.context.Context.root(),
56+
carrier,
57+
(carrier1, classifier) -> {
58+
for (String key : getter.keys(carrier1)) {
59+
classifier.accept(key, getter.get(carrier1, key));
60+
}
61+
});
5962
if (extracted == null) {
6063
return context;
61-
}else{
64+
} else {
6265
AgentSpan extractedSpan = AgentSpan.fromContext(extracted);
63-
Extracted extractedSpanContext = extractedSpan == null ? null : (AgentSpanContext.Extracted) extractedSpan.context();
66+
Extracted extractedSpanContext =
67+
extractedSpan == null ? null : (AgentSpanContext.Extracted) extractedSpan.context();
6468

6569
SpanContext spanContext = null;
66-
if (extractedSpanContext != null){
70+
if (extractedSpanContext != null) {
6771
TraceState traceState = extractTraceState(extractedSpanContext, carrier, getter);
6872
spanContext = fromRemote(extractedSpanContext, traceState);
6973
}
@@ -78,7 +82,11 @@ private static datadog.context.Context convertContext(Context context) {
7882
// context if baggage
7983
AgentSpanContext extract = OtelExtractedContext.extract(context);
8084
AgentSpan agentSpan = AgentSpan.fromSpanContext(extract);
81-
return agentSpan.with(BaggageContext.getContextKey(), context.get(ContextKey.named(OtelContext.getOtelContextBaggageKey())));
85+
datadog.context.Context c =
86+
agentSpan.with(
87+
BaggageContext.getContextKey(),
88+
context.get(ContextKey.named(OtelContext.getOtelContextBaggageKey())));
89+
return c;
8290
}
8391

8492
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package opentelemetry14.context.propagation
2+
3+
4+
import datadog.trace.api.DDTraceId
5+
6+
import static datadog.trace.api.sampling.PrioritySampling.SAMPLER_DROP
7+
import static datadog.trace.api.sampling.PrioritySampling.SAMPLER_KEEP
8+
import static datadog.trace.api.sampling.PrioritySampling.UNSET
9+
import static java.lang.Long.parseLong
10+
11+
class BaggagePropagatorTest extends AgentPropagatorTest {
12+
@Override
13+
String style() {
14+
return 'baggage'
15+
}
16+
17+
def values() {
18+
// spotless:off
19+
return [
20+
[["baggage": "key1=val1,key2=val2,foo=bar"], '00000000000000001111111111111111', '2222222222222222', UNSET],
21+
]
22+
// spotless:on
23+
}
24+
25+
void assertInjectedHeaders(Map<String, String> headers, String traceId, String spanId, byte sampling) {
26+
assert headers['x-datadog-trace-id'] == Long.toString(DDTraceId.fromHex(traceId).toLong())
27+
assert headers['x-datadog-parent-id'] == spanId.replaceAll('^0+(?!$)', '')
28+
def tags = []
29+
def samplingPriority = sampling == SAMPLER_DROP ? '0' : '1' // Deterministic sampler with rate to 1 if not explicitly dropped
30+
if (sampling == UNSET) {
31+
tags+= '_dd.p.dm=-1'
32+
}
33+
if (traceId.length() == 32) {
34+
tags+= '_dd.p.tid='+ traceId.substring(0, 16)
35+
}
36+
assert headers['x-datadog-tags'] == tags.join(',')
37+
assert headers['x-datadog-sampling-priority'] == samplingPriority
38+
}
39+
}

internal-api/src/main/java/datadog/trace/bootstrap/instrumentation/api/BaggageContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public Map<String, String> asMap() {
6363
return new HashMap<>(baggage);
6464
}
6565

66-
public static ContextKey<BaggageContext> getContextKey(){
66+
public static ContextKey<BaggageContext> getContextKey() {
6767
return CONTEXT_KEY;
6868
}
6969

0 commit comments

Comments
 (0)