1818
1919import static com .google .common .base .Preconditions .checkNotNull ;
2020import static io .grpc .opentelemetry .internal .OpenTelemetryConstants .BACKEND_SERVICE_KEY ;
21+ import static io .grpc .opentelemetry .internal .OpenTelemetryConstants .BAGGAGE_KEY ;
2122import static io .grpc .opentelemetry .internal .OpenTelemetryConstants .LOCALITY_KEY ;
2223import static io .grpc .opentelemetry .internal .OpenTelemetryConstants .METHOD_KEY ;
2324import static io .grpc .opentelemetry .internal .OpenTelemetryConstants .STATUS_KEY ;
4445import io .grpc .Status ;
4546import io .grpc .Status .Code ;
4647import io .grpc .StreamTracer ;
48+ import io .opentelemetry .api .baggage .Baggage ;
4749import io .opentelemetry .api .common .AttributesBuilder ;
50+ import io .opentelemetry .context .Context ;
4851import java .util .ArrayList ;
4952import java .util .Collection ;
5053import java .util .Collections ;
@@ -94,8 +97,8 @@ final class OpenTelemetryMetricsModule {
9497 private final ImmutableList <OpenTelemetryPlugin > plugins ;
9598
9699 OpenTelemetryMetricsModule (Supplier <Stopwatch > stopwatchSupplier ,
97- OpenTelemetryMetricsResource resource , Collection < String > optionalLabels ,
98- List <OpenTelemetryPlugin > plugins ) {
100+ OpenTelemetryMetricsResource resource ,
101+ Collection < String > optionalLabels , List <OpenTelemetryPlugin > plugins ) {
99102 this .resource = checkNotNull (resource , "resource" );
100103 this .stopwatchSupplier = checkNotNull (stopwatchSupplier , "stopwatchSupplier" );
101104 this .localityEnabled = optionalLabels .contains (LOCALITY_KEY .getKey ());
@@ -128,6 +131,14 @@ static String recordMethodName(String fullMethodName, boolean isGeneratedMethod)
128131 return isGeneratedMethod ? fullMethodName : "other" ;
129132 }
130133
134+ private static Context otelContextWithBaggage () {
135+ Baggage baggage = BAGGAGE_KEY .get (io .grpc .Context .current ());
136+ if (baggage == null ) {
137+ return Context .current ();
138+ }
139+ return Context .current ().with (baggage );
140+ }
141+
131142 private static final class ClientTracer extends ClientStreamTracer {
132143 @ Nullable private static final AtomicLongFieldUpdater <ClientTracer > outboundWireSizeUpdater ;
133144 @ Nullable private static final AtomicLongFieldUpdater <ClientTracer > inboundWireSizeUpdater ;
@@ -243,6 +254,7 @@ public void streamClosed(Status status) {
243254 }
244255
245256 void recordFinishedAttempt () {
257+ Context otelContext = otelContextWithBaggage ();
246258 AttributesBuilder builder = io .opentelemetry .api .common .Attributes .builder ()
247259 .put (METHOD_KEY , fullMethodName )
248260 .put (TARGET_KEY , target )
@@ -268,15 +280,15 @@ void recordFinishedAttempt() {
268280
269281 if (module .resource .clientAttemptDurationCounter () != null ) {
270282 module .resource .clientAttemptDurationCounter ()
271- .record (attemptNanos * SECONDS_PER_NANO , attribute );
283+ .record (attemptNanos * SECONDS_PER_NANO , attribute , otelContext );
272284 }
273285 if (module .resource .clientTotalSentCompressedMessageSizeCounter () != null ) {
274286 module .resource .clientTotalSentCompressedMessageSizeCounter ()
275- .record (outboundWireSize , attribute );
287+ .record (outboundWireSize , attribute , otelContext );
276288 }
277289 if (module .resource .clientTotalReceivedCompressedMessageSizeCounter () != null ) {
278290 module .resource .clientTotalReceivedCompressedMessageSizeCounter ()
279- .record (inboundWireSize , attribute );
291+ .record (inboundWireSize , attribute , otelContext );
280292 }
281293 }
282294 }
@@ -408,6 +420,7 @@ void callEnded(Status status) {
408420 }
409421
410422 void recordFinishedCall () {
423+ Context otelContext = otelContextWithBaggage ();
411424 if (attemptsPerCall .get () == 0 ) {
412425 ClientTracer tracer = newClientTracer (null );
413426 tracer .attemptNanos = attemptDelayStopwatch .elapsed (TimeUnit .NANOSECONDS );
@@ -429,15 +442,17 @@ void recordFinishedCall() {
429442 callLatencyNanos * SECONDS_PER_NANO ,
430443 baseAttributes .toBuilder ()
431444 .put (STATUS_KEY , status .getCode ().toString ())
432- .build ()
445+ .build (),
446+ otelContext
433447 );
434448 }
435449
436450 // Retry counts
437451 if (module .resource .clientCallRetriesCounter () != null ) {
438452 long retriesPerCall = Math .max (attemptsPerCall .get () - 1 , 0 );
439453 if (retriesPerCall > 0 ) {
440- module .resource .clientCallRetriesCounter ().record (retriesPerCall , baseAttributes );
454+ module .resource .clientCallRetriesCounter ()
455+ .record (retriesPerCall , baseAttributes , otelContext );
441456 }
442457 }
443458
@@ -446,24 +461,25 @@ void recordFinishedCall() {
446461 long hedges = hedgedAttemptsPerCall .get ();
447462 if (hedges > 0 ) {
448463 module .resource .clientCallHedgesCounter ()
449- .record (hedges , baseAttributes );
464+ .record (hedges , baseAttributes , otelContext );
450465 }
451466 }
452467
453468 // Transparent Retry counts
454469 if (module .resource .clientCallTransparentRetriesCounter () != null ) {
455470 long transparentRetries = transparentRetriesPerCall .get ();
456471 if (transparentRetries > 0 ) {
457- module .resource .clientCallTransparentRetriesCounter (). record (
458- transparentRetries , baseAttributes );
472+ module .resource .clientCallTransparentRetriesCounter ()
473+ . record ( transparentRetries , baseAttributes , otelContext );
459474 }
460475 }
461476
462477 // Retry delay
463478 if (module .resource .clientCallRetryDelayCounter () != null ) {
464479 module .resource .clientCallRetryDelayCounter ().record (
465480 retryDelayNanos * SECONDS_PER_NANO ,
466- baseAttributes
481+ baseAttributes ,
482+ otelContext
467483 );
468484 }
469485 }
@@ -562,6 +578,7 @@ public void inboundWireSize(long bytes) {
562578 */
563579 @ Override
564580 public void streamClosed (Status status ) {
581+ Context otelContext = otelContextWithBaggage ();
565582 if (streamClosedUpdater != null ) {
566583 if (streamClosedUpdater .getAndSet (this , 1 ) != 0 ) {
567584 return ;
@@ -584,15 +601,15 @@ public void streamClosed(Status status) {
584601
585602 if (module .resource .serverCallDurationCounter () != null ) {
586603 module .resource .serverCallDurationCounter ()
587- .record (elapsedTimeNanos * SECONDS_PER_NANO , attributes );
604+ .record (elapsedTimeNanos * SECONDS_PER_NANO , attributes , otelContext );
588605 }
589606 if (module .resource .serverTotalSentCompressedMessageSizeCounter () != null ) {
590607 module .resource .serverTotalSentCompressedMessageSizeCounter ()
591- .record (outboundWireSize , attributes );
608+ .record (outboundWireSize , attributes , otelContext );
592609 }
593610 if (module .resource .serverTotalReceivedCompressedMessageSizeCounter () != null ) {
594611 module .resource .serverTotalReceivedCompressedMessageSizeCounter ()
595- .record (inboundWireSize , attributes );
612+ .record (inboundWireSize , attributes , otelContext );
596613 }
597614 }
598615 }
0 commit comments