Skip to content

Commit cf78cef

Browse files
committed
fix otel baggage prop
1 parent 50ead96 commit cf78cef

2 files changed

Lines changed: 18 additions & 10 deletions

File tree

opentelemetry/src/main/java/io/grpc/opentelemetry/GrpcOpenTelemetry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private GrpcOpenTelemetry(Builder builder) {
101101
this.optionalLabels = ImmutableList.copyOf(builder.optionalLabels);
102102
this.openTelemetryMetricsModule = new OpenTelemetryMetricsModule(
103103
STOPWATCH_SUPPLIER, resource, optionalLabels, builder.plugins,
104-
builder.targetFilter);
104+
openTelemetrySdk.getPropagators(), builder.targetFilter);
105105
this.openTelemetryTracingModule = new OpenTelemetryTracingModule(openTelemetrySdk);
106106
this.sink = new OpenTelemetryMetricSink(meter, enableMetrics, disableDefault, optionalLabels);
107107
}

opentelemetry/src/main/java/io/grpc/opentelemetry/OpenTelemetryMetricsModule.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import io.opentelemetry.api.baggage.Baggage;
5050
import io.opentelemetry.api.common.AttributesBuilder;
5151
import io.opentelemetry.context.Context;
52+
import io.opentelemetry.context.propagation.ContextPropagators;
5253
import java.util.ArrayList;
5354
import java.util.Collection;
5455
import java.util.Collections;
@@ -100,24 +101,28 @@ final class OpenTelemetryMetricsModule {
100101
private final boolean localityEnabled;
101102
private final boolean backendServiceEnabled;
102103
private final ImmutableList<OpenTelemetryPlugin> plugins;
104+
private final ContextPropagators aggregators;
103105
@Nullable
104106
private final TargetFilter targetAttributeFilter;
105107

106108
OpenTelemetryMetricsModule(Supplier<Stopwatch> stopwatchSupplier,
107109
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 aggregators) {
112+
this(stopwatchSupplier, resource, optionalLabels, plugins, aggregators, null);
110113
}
111114

112115
OpenTelemetryMetricsModule(Supplier<Stopwatch> stopwatchSupplier,
113116
OpenTelemetryMetricsResource resource,
114117
Collection<String> optionalLabels, List<OpenTelemetryPlugin> plugins,
118+
ContextPropagators aggregators,
115119
@Nullable TargetFilter targetAttributeFilter) {
116120
this.resource = checkNotNull(resource, "resource");
117121
this.stopwatchSupplier = checkNotNull(stopwatchSupplier, "stopwatchSupplier");
118122
this.localityEnabled = optionalLabels.contains(LOCALITY_KEY.getKey());
119123
this.backendServiceEnabled = optionalLabels.contains(BACKEND_SERVICE_KEY.getKey());
120124
this.plugins = ImmutableList.copyOf(plugins);
125+
this.aggregators = checkNotNull(aggregators, "aggregators");
121126
this.targetAttributeFilter = targetAttributeFilter;
122127
}
123128

@@ -159,8 +164,7 @@ static String recordMethodName(String fullMethodName, boolean isGeneratedMethod)
159164
return isGeneratedMethod ? fullMethodName : "other";
160165
}
161166

162-
private static Context otelContextWithBaggage() {
163-
Baggage baggage = BAGGAGE_KEY.get();
167+
private static Context otelContextWithBaggage(Baggage baggage) {
164168
if (baggage == null) {
165169
return Context.current();
166170
}
@@ -282,7 +286,7 @@ public void streamClosed(Status status) {
282286
}
283287

284288
void recordFinishedAttempt() {
285-
Context otelContext = otelContextWithBaggage();
289+
Context otelContext = otelContextWithBaggage(BAGGAGE_KEY.get());
286290
AttributesBuilder builder = io.opentelemetry.api.common.Attributes.builder()
287291
.put(METHOD_KEY, fullMethodName)
288292
.put(TARGET_KEY, target)
@@ -448,7 +452,7 @@ void callEnded(Status status) {
448452
}
449453

450454
void recordFinishedCall() {
451-
Context otelContext = otelContextWithBaggage();
455+
Context otelContext = otelContextWithBaggage(BAGGAGE_KEY.get());
452456
if (attemptsPerCall.get() == 0) {
453457
ClientTracer tracer = newClientTracer(null);
454458
tracer.attemptNanos = attemptDelayStopwatch.elapsed(TimeUnit.NANOSECONDS);
@@ -553,13 +557,15 @@ private static final class ServerTracer extends ServerStreamTracer {
553557
private final Stopwatch stopwatch;
554558
private volatile long outboundWireSize;
555559
private volatile long inboundWireSize;
560+
private final Context otelContext;
556561

557562
ServerTracer(OpenTelemetryMetricsModule module, String fullMethodName,
558-
List<OpenTelemetryPlugin.ServerStreamPlugin> streamPlugins) {
563+
List<OpenTelemetryPlugin.ServerStreamPlugin> streamPlugins, Context otelContext) {
559564
this.module = checkNotNull(module, "module");
560565
this.fullMethodName = fullMethodName;
561566
this.streamPlugins = checkNotNull(streamPlugins, "streamPlugins");
562567
this.stopwatch = module.stopwatchSupplier.get().start();
568+
this.otelContext = checkNotNull(otelContext, "otelContext");
563569
}
564570

565571
@Override
@@ -606,7 +612,6 @@ public void inboundWireSize(long bytes) {
606612
*/
607613
@Override
608614
public void streamClosed(Status status) {
609-
Context otelContext = otelContextWithBaggage();
610615
if (streamClosedUpdater != null) {
611616
if (streamClosedUpdater.getAndSet(this, 1) != 0) {
612617
return;
@@ -657,7 +662,10 @@ public ServerStreamTracer newServerStreamTracer(String fullMethodName, Metadata
657662
}
658663
streamPlugins = Collections.unmodifiableList(streamPluginsMutable);
659664
}
660-
return new ServerTracer(OpenTelemetryMetricsModule.this, fullMethodName, streamPlugins);
665+
Context context = aggregators.getTextMapPropagator().extract(
666+
Context.current(), headers, MetadataGetter.getInstance());
667+
return new ServerTracer(OpenTelemetryMetricsModule.this, fullMethodName, streamPlugins,
668+
context);
661669
}
662670
}
663671

0 commit comments

Comments
 (0)