|
49 | 49 | import io.opentelemetry.api.baggage.Baggage; |
50 | 50 | import io.opentelemetry.api.common.AttributesBuilder; |
51 | 51 | import io.opentelemetry.context.Context; |
| 52 | +import io.opentelemetry.context.propagation.ContextPropagators; |
52 | 53 | import java.util.ArrayList; |
53 | 54 | import java.util.Collection; |
54 | 55 | import java.util.Collections; |
@@ -100,24 +101,28 @@ final class OpenTelemetryMetricsModule { |
100 | 101 | private final boolean localityEnabled; |
101 | 102 | private final boolean backendServiceEnabled; |
102 | 103 | private final ImmutableList<OpenTelemetryPlugin> plugins; |
| 104 | + private final ContextPropagators contextPropagators; |
103 | 105 | @Nullable |
104 | 106 | private final TargetFilter targetAttributeFilter; |
105 | 107 |
|
106 | 108 | OpenTelemetryMetricsModule(Supplier<Stopwatch> stopwatchSupplier, |
107 | 109 | OpenTelemetryMetricsResource resource, |
108 | | - Collection<String> optionalLabels, List<OpenTelemetryPlugin> plugins) { |
109 | | - this(stopwatchSupplier, resource, optionalLabels, plugins, null); |
| 110 | + Collection<String> optionalLabels, List<OpenTelemetryPlugin> plugins, |
| 111 | + ContextPropagators contextPropagators) { |
| 112 | + this(stopwatchSupplier, resource, optionalLabels, plugins, contextPropagators, null); |
110 | 113 | } |
111 | 114 |
|
112 | 115 | OpenTelemetryMetricsModule(Supplier<Stopwatch> stopwatchSupplier, |
113 | 116 | OpenTelemetryMetricsResource resource, |
114 | 117 | Collection<String> optionalLabels, List<OpenTelemetryPlugin> plugins, |
115 | | - @Nullable TargetFilter targetAttributeFilter) { |
| 118 | + ContextPropagators contextPropagators, |
| 119 | + @Nullable TargetFilter targetAttributeFilter) { |
116 | 120 | this.resource = checkNotNull(resource, "resource"); |
117 | 121 | this.stopwatchSupplier = checkNotNull(stopwatchSupplier, "stopwatchSupplier"); |
118 | 122 | this.localityEnabled = optionalLabels.contains(LOCALITY_KEY.getKey()); |
119 | 123 | this.backendServiceEnabled = optionalLabels.contains(BACKEND_SERVICE_KEY.getKey()); |
120 | 124 | this.plugins = ImmutableList.copyOf(plugins); |
| 125 | + this.contextPropagators = checkNotNull(contextPropagators, "contextPropagators"); |
121 | 126 | this.targetAttributeFilter = targetAttributeFilter; |
122 | 127 | } |
123 | 128 |
|
@@ -159,10 +164,13 @@ static String recordMethodName(String fullMethodName, boolean isGeneratedMethod) |
159 | 164 | return isGeneratedMethod ? fullMethodName : "other"; |
160 | 165 | } |
161 | 166 |
|
162 | | - private static Context otelContextWithBaggage() { |
163 | | - Baggage baggage = BAGGAGE_KEY.get(); |
| 167 | + private static Context otelContextWithBaggage(@Nullable Baggage baggage) { |
164 | 168 | if (baggage == null) { |
165 | | - return Context.current(); |
| 169 | + Baggage currentBaggage = BAGGAGE_KEY.get(); |
| 170 | + if (currentBaggage == null) { |
| 171 | + return Context.current(); |
| 172 | + } |
| 173 | + return Context.current().with(currentBaggage); |
166 | 174 | } |
167 | 175 | return Context.current().with(baggage); |
168 | 176 | } |
@@ -282,7 +290,7 @@ public void streamClosed(Status status) { |
282 | 290 | } |
283 | 291 |
|
284 | 292 | void recordFinishedAttempt() { |
285 | | - Context otelContext = otelContextWithBaggage(); |
| 293 | + Context otelContext = otelContextWithBaggage(null); |
286 | 294 | AttributesBuilder builder = io.opentelemetry.api.common.Attributes.builder() |
287 | 295 | .put(METHOD_KEY, fullMethodName) |
288 | 296 | .put(TARGET_KEY, target) |
@@ -448,7 +456,7 @@ void callEnded(Status status) { |
448 | 456 | } |
449 | 457 |
|
450 | 458 | void recordFinishedCall() { |
451 | | - Context otelContext = otelContextWithBaggage(); |
| 459 | + Context otelContext = otelContextWithBaggage(null); |
452 | 460 | if (attemptsPerCall.get() == 0) { |
453 | 461 | ClientTracer tracer = newClientTracer(null); |
454 | 462 | tracer.attemptNanos = attemptDelayStopwatch.elapsed(TimeUnit.NANOSECONDS); |
@@ -553,13 +561,15 @@ private static final class ServerTracer extends ServerStreamTracer { |
553 | 561 | private final Stopwatch stopwatch; |
554 | 562 | private volatile long outboundWireSize; |
555 | 563 | private volatile long inboundWireSize; |
| 564 | + private final Baggage baggage; |
556 | 565 |
|
557 | 566 | ServerTracer(OpenTelemetryMetricsModule module, String fullMethodName, |
558 | | - List<OpenTelemetryPlugin.ServerStreamPlugin> streamPlugins) { |
| 567 | + List<OpenTelemetryPlugin.ServerStreamPlugin> streamPlugins, @Nullable Baggage baggage) { |
559 | 568 | this.module = checkNotNull(module, "module"); |
560 | 569 | this.fullMethodName = fullMethodName; |
561 | 570 | this.streamPlugins = checkNotNull(streamPlugins, "streamPlugins"); |
562 | 571 | this.stopwatch = module.stopwatchSupplier.get().start(); |
| 572 | + this.baggage = baggage; |
563 | 573 | } |
564 | 574 |
|
565 | 575 | @Override |
@@ -606,7 +616,7 @@ public void inboundWireSize(long bytes) { |
606 | 616 | */ |
607 | 617 | @Override |
608 | 618 | public void streamClosed(Status status) { |
609 | | - Context otelContext = otelContextWithBaggage(); |
| 619 | + Context otelContext = otelContextWithBaggage(baggage); |
610 | 620 | if (streamClosedUpdater != null) { |
611 | 621 | if (streamClosedUpdater.getAndSet(this, 1) != 0) { |
612 | 622 | return; |
@@ -657,7 +667,10 @@ public ServerStreamTracer newServerStreamTracer(String fullMethodName, Metadata |
657 | 667 | } |
658 | 668 | streamPlugins = Collections.unmodifiableList(streamPluginsMutable); |
659 | 669 | } |
660 | | - return new ServerTracer(OpenTelemetryMetricsModule.this, fullMethodName, streamPlugins); |
| 670 | + Context context = contextPropagators.getTextMapPropagator().extract( |
| 671 | + Context.current(), headers, MetadataGetter.getInstance()); |
| 672 | + Baggage baggage = Baggage.fromContext(context); |
| 673 | + return new ServerTracer(OpenTelemetryMetricsModule.this, fullMethodName, streamPlugins, baggage); |
661 | 674 | } |
662 | 675 | } |
663 | 676 |
|
|
0 commit comments