@@ -159,8 +159,7 @@ static String recordMethodName(String fullMethodName, boolean isGeneratedMethod)
159159 return isGeneratedMethod ? fullMethodName : "other" ;
160160 }
161161
162- private static Context otelContextWithBaggage () {
163- Baggage baggage = BAGGAGE_KEY .get ();
162+ private static Context otelContextWithBaggage (@ Nullable Baggage baggage ) {
164163 if (baggage == null ) {
165164 return Context .current ();
166165 }
@@ -206,16 +205,19 @@ private static final class ClientTracer extends ClientStreamTracer {
206205 volatile String backendService ;
207206 long attemptNanos ;
208207 Code statusCode ;
208+ final Baggage baggage ;
209209
210210 ClientTracer (CallAttemptsTracerFactory attemptsState , OpenTelemetryMetricsModule module ,
211211 StreamInfo info , String target , String fullMethodName ,
212- List <OpenTelemetryPlugin .ClientStreamPlugin > streamPlugins ) {
212+ List <OpenTelemetryPlugin .ClientStreamPlugin > streamPlugins ,
213+ Baggage baggage ) {
213214 this .attemptsState = attemptsState ;
214215 this .module = module ;
215216 this .info = info ;
216217 this .target = target ;
217218 this .fullMethodName = fullMethodName ;
218219 this .streamPlugins = streamPlugins ;
220+ this .baggage = baggage ;
219221 this .stopwatch = module .stopwatchSupplier .get ().start ();
220222 }
221223
@@ -282,7 +284,7 @@ public void streamClosed(Status status) {
282284 }
283285
284286 void recordFinishedAttempt () {
285- Context otelContext = otelContextWithBaggage ();
287+ Context otelContext = otelContextWithBaggage (baggage );
286288 AttributesBuilder builder = io .opentelemetry .api .common .Attributes .builder ()
287289 .put (METHOD_KEY , fullMethodName )
288290 .put (TARGET_KEY , target )
@@ -301,6 +303,10 @@ void recordFinishedAttempt() {
301303 }
302304 builder .put (BACKEND_SERVICE_KEY , savedBackendService );
303305 }
306+ for (java .util .Map .Entry <String , io .opentelemetry .api .baggage .BaggageEntry > entry :
307+ baggage .asMap ().entrySet ()) {
308+ builder .put (entry .getKey (), entry .getValue ().getValue ());
309+ }
304310 for (OpenTelemetryPlugin .ClientStreamPlugin plugin : streamPlugins ) {
305311 plugin .addLabels (builder );
306312 }
@@ -342,6 +348,7 @@ static final class CallAttemptsTracerFactory extends ClientStreamTracer.Factory
342348 private int activeStreams ;
343349 @ GuardedBy ("lock" )
344350 private boolean finishedCallToBeRecorded ;
351+ private final Baggage baggage ;
345352
346353 CallAttemptsTracerFactory (
347354 OpenTelemetryMetricsModule module ,
@@ -354,6 +361,7 @@ static final class CallAttemptsTracerFactory extends ClientStreamTracer.Factory
354361 this .callPlugins = checkNotNull (callPlugins , "callPlugins" );
355362 this .attemptDelayStopwatch = module .stopwatchSupplier .get ();
356363 this .callStopWatch = module .stopwatchSupplier .get ().start ();
364+ this .baggage = Baggage .fromContext (Context .current ());
357365
358366 io .opentelemetry .api .common .Attributes attribute = io .opentelemetry .api .common .Attributes .of (
359367 METHOD_KEY , fullMethodName ,
@@ -407,7 +415,8 @@ private ClientTracer newClientTracer(StreamInfo info) {
407415 }
408416 streamPlugins = Collections .unmodifiableList (streamPlugins );
409417 }
410- return new ClientTracer (this , module , info , target , fullMethodName , streamPlugins );
418+ return new ClientTracer (this , module , info , target , fullMethodName , streamPlugins ,
419+ baggage );
411420 }
412421
413422 // Called whenever each attempt is ended.
@@ -448,7 +457,7 @@ void callEnded(Status status) {
448457 }
449458
450459 void recordFinishedCall () {
451- Context otelContext = otelContextWithBaggage ();
460+ Context otelContext = otelContextWithBaggage (baggage );
452461 if (attemptsPerCall .get () == 0 ) {
453462 ClientTracer tracer = newClientTracer (null );
454463 tracer .attemptNanos = attemptDelayStopwatch .elapsed (TimeUnit .NANOSECONDS );
@@ -463,6 +472,12 @@ void recordFinishedCall() {
463472 METHOD_KEY , fullMethodName ,
464473 TARGET_KEY , target
465474 );
475+ AttributesBuilder baseAttributesBuilder = baseAttributes .toBuilder ();
476+ for (java .util .Map .Entry <String , io .opentelemetry .api .baggage .BaggageEntry > entry
477+ : baggage .asMap ().entrySet ()) {
478+ baseAttributesBuilder .put (entry .getKey (), entry .getValue ().getValue ());
479+ }
480+ baseAttributes = baseAttributesBuilder .build ();
466481
467482 // Duration
468483 if (module .resource .clientCallDurationCounter () != null ) {
@@ -606,7 +621,8 @@ public void inboundWireSize(long bytes) {
606621 */
607622 @ Override
608623 public void streamClosed (Status status ) {
609- Context otelContext = otelContextWithBaggage ();
624+ Baggage baggage = BAGGAGE_KEY .get ();
625+ Context otelContext = otelContextWithBaggage (baggage );
610626 if (streamClosedUpdater != null ) {
611627 if (streamClosedUpdater .getAndSet (this , 1 ) != 0 ) {
612628 return ;
@@ -622,6 +638,12 @@ public void streamClosed(Status status) {
622638 AttributesBuilder builder = io .opentelemetry .api .common .Attributes .builder ()
623639 .put (METHOD_KEY , recordMethodName (fullMethodName , isGeneratedMethod ))
624640 .put (STATUS_KEY , status .getCode ().toString ());
641+ if (baggage != null ) {
642+ for (java .util .Map .Entry <String , io .opentelemetry .api .baggage .BaggageEntry > entry
643+ : baggage .asMap ().entrySet ()) {
644+ builder .put (entry .getKey (), entry .getValue ().getValue ());
645+ }
646+ }
625647 for (OpenTelemetryPlugin .ServerStreamPlugin plugin : streamPlugins ) {
626648 plugin .addLabels (builder );
627649 }
0 commit comments