55import datadog .trace .bootstrap .instrumentation .api .AgentSpan ;
66import datadog .trace .bootstrap .instrumentation .api .AgentTracer ;
77import datadog .trace .bootstrap .instrumentation .api .AttachableWrapper ;
8+ import datadog .trace .bootstrap .instrumentation .api .BaggageContext ;
89import io .opentelemetry .api .trace .Span ;
910import io .opentelemetry .context .Context ;
1011import io .opentelemetry .context .ContextKey ;
@@ -22,22 +23,29 @@ public class OtelContext implements Context {
2223
2324 private static final String OTEL_CONTEXT_SPAN_KEY = "opentelemetry-trace-span-key" ;
2425 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" ;
2527
2628 /** Keep track of propagated context that has not been captured on the scope stack. */
2729 private static final ThreadLocal <OtelContext > lastPropagated = new ThreadLocal <>();
2830
2931 private final Span currentSpan ;
3032 private final Span rootSpan ;
33+ private final BaggageContext baggageContext ;
3134
3235 private final Object [] entries ;
3336
3437 public OtelContext (Span currentSpan , Span rootSpan ) {
35- this (currentSpan , rootSpan , NO_ENTRIES );
38+ this (currentSpan , rootSpan , null , NO_ENTRIES );
3639 }
3740
38- public OtelContext (Span currentSpan , Span rootSpan , Object [] entries ) {
41+ public OtelContext (Span currentSpan , Span rootSpan , BaggageContext baggageContext ) {
42+ this (currentSpan , rootSpan , baggageContext , NO_ENTRIES );
43+ }
44+
45+ public OtelContext (Span currentSpan , Span rootSpan , BaggageContext baggageContext , Object [] entries ) {
3946 this .currentSpan = currentSpan ;
4047 this .rootSpan = rootSpan ;
48+ this .baggageContext = baggageContext ;
4149 this .entries = entries ;
4250 }
4351
@@ -49,6 +57,8 @@ public <V> V get(ContextKey<V> key) {
4957 return (V ) this .currentSpan ;
5058 } else if (OTEL_CONTEXT_ROOT_SPAN_KEY .equals (key .toString ())) {
5159 return (V ) this .rootSpan ;
60+ } else if (OTEL_CONTEXT_BAGGAGE_KEY .equals (key .toString ())){
61+ return (V ) this .baggageContext ;
5262 }
5363 for (int i = 0 ; i < this .entries .length ; i += 2 ) {
5464 if (this .entries [i ] == key ) {
@@ -61,9 +71,11 @@ public <V> V get(ContextKey<V> key) {
6171 @ Override
6272 public <V > Context with (ContextKey <V > key , V value ) {
6373 if (OTEL_CONTEXT_SPAN_KEY .equals (key .toString ())) {
64- return new OtelContext ((Span ) value , this .rootSpan , this .entries );
74+ return new OtelContext ((Span ) value , this .rootSpan , null , this .entries );
6575 } else if (OTEL_CONTEXT_ROOT_SPAN_KEY .equals (key .toString ())) {
66- return new OtelContext (this .currentSpan , (Span ) value , this .entries );
76+ return new OtelContext (this .currentSpan , (Span ) value , null , this .entries );
77+ } else if (OTEL_CONTEXT_BAGGAGE_KEY .equals (key .toString ())){
78+ return new OtelContext (null , null , this .baggageContext , this .entries );
6779 }
6880 Object [] newEntries = null ;
6981 int oldEntriesLength = this .entries .length ;
@@ -82,7 +94,7 @@ public <V> Context with(ContextKey<V> key, V value) {
8294 newEntries [oldEntriesLength ] = key ;
8395 newEntries [oldEntriesLength + 1 ] = value ;
8496 }
85- return new OtelContext (this .currentSpan , this .rootSpan , newEntries );
97+ return new OtelContext (this .currentSpan , this .rootSpan , null , newEntries );
8698 }
8799
88100 @ Override
@@ -149,7 +161,7 @@ public static Context current() {
149161 contextEntries = ((OtelScope ) wrapper ).contextEntries ();
150162 }
151163 }
152- return new OtelContext (otelCurrentSpan , otelRootSpan , contextEntries );
164+ return new OtelContext (otelCurrentSpan , otelRootSpan , null , contextEntries );
153165 }
154166
155167 /** Last propagated context not on the scope stack; {@code null} if there's no such context. */
@@ -158,6 +170,10 @@ public static Context lastPropagated() {
158170 return lastPropagated .get ();
159171 }
160172
173+ public static String getOtelContextBaggageKey (){
174+ return OTEL_CONTEXT_BAGGAGE_KEY ;
175+ }
176+
161177 @ Override
162178 public String toString () {
163179 return "OtelContext{"
0 commit comments