Skip to content

Commit 8480558

Browse files
committed
otel: baggage prop
1 parent 04daa07 commit 8480558

3 files changed

Lines changed: 217 additions & 573 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-
openTelemetrySdk.getPropagators(), builder.targetFilter);
104+
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: 40 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@
4848
import io.grpc.opentelemetry.GrpcOpenTelemetry.TargetFilter;
4949
import io.opentelemetry.api.baggage.Baggage;
5050
import io.opentelemetry.api.common.AttributesBuilder;
51-
import io.opentelemetry.context.Context;
52-
import io.opentelemetry.context.propagation.ContextPropagators;
5351
import java.util.ArrayList;
5452
import java.util.Collection;
5553
import java.util.Collections;
@@ -101,28 +99,24 @@ final class OpenTelemetryMetricsModule {
10199
private final boolean localityEnabled;
102100
private final boolean backendServiceEnabled;
103101
private final ImmutableList<OpenTelemetryPlugin> plugins;
104-
private final ContextPropagators contextPropagators;
105102
@Nullable
106103
private final TargetFilter targetAttributeFilter;
107104

108105
OpenTelemetryMetricsModule(Supplier<Stopwatch> stopwatchSupplier,
109106
OpenTelemetryMetricsResource resource,
110-
Collection<String> optionalLabels, List<OpenTelemetryPlugin> plugins,
111-
ContextPropagators contextPropagators) {
112-
this(stopwatchSupplier, resource, optionalLabels, plugins, contextPropagators, null);
107+
Collection<String> optionalLabels, List<OpenTelemetryPlugin> plugins) {
108+
this(stopwatchSupplier, resource, optionalLabels, plugins, null);
113109
}
114110

115111
OpenTelemetryMetricsModule(Supplier<Stopwatch> stopwatchSupplier,
116112
OpenTelemetryMetricsResource resource,
117113
Collection<String> optionalLabels, List<OpenTelemetryPlugin> plugins,
118-
ContextPropagators contextPropagators,
119-
@Nullable TargetFilter targetAttributeFilter) {
114+
@Nullable TargetFilter targetAttributeFilter) {
120115
this.resource = checkNotNull(resource, "resource");
121116
this.stopwatchSupplier = checkNotNull(stopwatchSupplier, "stopwatchSupplier");
122117
this.localityEnabled = optionalLabels.contains(LOCALITY_KEY.getKey());
123118
this.backendServiceEnabled = optionalLabels.contains(BACKEND_SERVICE_KEY.getKey());
124119
this.plugins = ImmutableList.copyOf(plugins);
125-
this.contextPropagators = checkNotNull(contextPropagators, "contextPropagators");
126120
this.targetAttributeFilter = targetAttributeFilter;
127121
}
128122

@@ -164,13 +158,6 @@ static String recordMethodName(String fullMethodName, boolean isGeneratedMethod)
164158
return isGeneratedMethod ? fullMethodName : "other";
165159
}
166160

167-
private static Context otelContextWithBaggage(Baggage baggage) {
168-
if (baggage == null) {
169-
return Context.current();
170-
}
171-
return Context.current().with(baggage);
172-
}
173-
174161
private static final class ClientTracer extends ClientStreamTracer {
175162
@Nullable private static final AtomicLongFieldUpdater<ClientTracer> outboundWireSizeUpdater;
176163
@Nullable private static final AtomicLongFieldUpdater<ClientTracer> inboundWireSizeUpdater;
@@ -286,7 +273,6 @@ public void streamClosed(Status status) {
286273
}
287274

288275
void recordFinishedAttempt() {
289-
Context otelContext = otelContextWithBaggage(BAGGAGE_KEY.get());
290276
AttributesBuilder builder = io.opentelemetry.api.common.Attributes.builder()
291277
.put(METHOD_KEY, fullMethodName)
292278
.put(TARGET_KEY, target)
@@ -312,15 +298,15 @@ void recordFinishedAttempt() {
312298

313299
if (module.resource.clientAttemptDurationCounter() != null ) {
314300
module.resource.clientAttemptDurationCounter()
315-
.record(attemptNanos * SECONDS_PER_NANO, attribute, otelContext);
301+
.record(attemptNanos * SECONDS_PER_NANO, attribute);
316302
}
317303
if (module.resource.clientTotalSentCompressedMessageSizeCounter() != null) {
318304
module.resource.clientTotalSentCompressedMessageSizeCounter()
319-
.record(outboundWireSize, attribute, otelContext);
305+
.record(outboundWireSize, attribute);
320306
}
321307
if (module.resource.clientTotalReceivedCompressedMessageSizeCounter() != null) {
322308
module.resource.clientTotalReceivedCompressedMessageSizeCounter()
323-
.record(inboundWireSize, attribute, otelContext);
309+
.record(inboundWireSize, attribute);
324310
}
325311
}
326312
}
@@ -452,7 +438,6 @@ void callEnded(Status status) {
452438
}
453439

454440
void recordFinishedCall() {
455-
Context otelContext = otelContextWithBaggage(BAGGAGE_KEY.get());
456441
if (attemptsPerCall.get() == 0) {
457442
ClientTracer tracer = newClientTracer(null);
458443
tracer.attemptNanos = attemptDelayStopwatch.elapsed(TimeUnit.NANOSECONDS);
@@ -474,8 +459,7 @@ void recordFinishedCall() {
474459
callLatencyNanos * SECONDS_PER_NANO,
475460
baseAttributes.toBuilder()
476461
.put(STATUS_KEY, status.getCode().toString())
477-
.build(),
478-
otelContext
462+
.build()
479463
);
480464
}
481465

@@ -484,7 +468,7 @@ void recordFinishedCall() {
484468
long retriesPerCall = Math.max(attemptsPerCall.get() - 1, 0);
485469
if (retriesPerCall > 0) {
486470
module.resource.clientCallRetriesCounter()
487-
.record(retriesPerCall, baseAttributes, otelContext);
471+
.record(retriesPerCall, baseAttributes);
488472
}
489473
}
490474

@@ -493,7 +477,7 @@ void recordFinishedCall() {
493477
long hedges = hedgedAttemptsPerCall.get();
494478
if (hedges > 0) {
495479
module.resource.clientCallHedgesCounter()
496-
.record(hedges, baseAttributes, otelContext);
480+
.record(hedges, baseAttributes);
497481
}
498482
}
499483

@@ -502,16 +486,15 @@ void recordFinishedCall() {
502486
long transparentRetries = transparentRetriesPerCall.get();
503487
if (transparentRetries > 0) {
504488
module.resource.clientCallTransparentRetriesCounter()
505-
.record(transparentRetries, baseAttributes, otelContext);
489+
.record(transparentRetries, baseAttributes);
506490
}
507491
}
508492

509493
// Retry delay
510494
if (module.resource.clientCallRetryDelayCounter() != null) {
511495
module.resource.clientCallRetryDelayCounter().record(
512496
retryDelayNanos * SECONDS_PER_NANO,
513-
baseAttributes,
514-
otelContext
497+
baseAttributes
515498
);
516499
}
517500
}
@@ -557,15 +540,14 @@ private static final class ServerTracer extends ServerStreamTracer {
557540
private final Stopwatch stopwatch;
558541
private volatile long outboundWireSize;
559542
private volatile long inboundWireSize;
560-
private final Context otelContext;
543+
private volatile Baggage baggage;
561544

562545
ServerTracer(OpenTelemetryMetricsModule module, String fullMethodName,
563-
List<OpenTelemetryPlugin.ServerStreamPlugin> streamPlugins, Context otelContext) {
546+
List<OpenTelemetryPlugin.ServerStreamPlugin> streamPlugins) {
564547
this.module = checkNotNull(module, "module");
565548
this.fullMethodName = fullMethodName;
566549
this.streamPlugins = checkNotNull(streamPlugins, "streamPlugins");
567550
this.stopwatch = module.stopwatchSupplier.get().start();
568-
this.otelContext = checkNotNull(otelContext, "otelContext");
569551
}
570552

571553
@Override
@@ -574,13 +556,23 @@ public void serverCallStarted(ServerCallInfo<?, ?> callInfo) {
574556
// which is true for all generated methods. Otherwise, programmatically
575557
// created methods result in high cardinality metrics.
576558
boolean isSampledToLocalTracing = callInfo.getMethodDescriptor().isSampledToLocalTracing();
559+
baggage = BAGGAGE_KEY.get(io.grpc.Context.current());
577560
isGeneratedMethod = isSampledToLocalTracing;
578-
io.opentelemetry.api.common.Attributes attribute =
579-
io.opentelemetry.api.common.Attributes.of(
580-
METHOD_KEY, recordMethodName(fullMethodName, isSampledToLocalTracing));
561+
562+
AttributesBuilder builder = io.opentelemetry.api.common.Attributes.builder()
563+
.put(METHOD_KEY, recordMethodName(fullMethodName, isSampledToLocalTracing));
564+
565+
if (baggage != null) {
566+
for (java.util.Map.Entry<String, io.opentelemetry.api.baggage.BaggageEntry> entry :
567+
baggage.asMap().entrySet()) {
568+
builder.put(entry.getKey(), entry.getValue().getValue());
569+
}
570+
}
571+
572+
io.opentelemetry.api.common.Attributes attributes = builder.build();
581573

582574
if (module.resource.serverCallCountCounter() != null) {
583-
module.resource.serverCallCountCounter().add(1, attribute, otelContext);
575+
module.resource.serverCallCountCounter().add(1, attributes);
584576
}
585577
}
586578

@@ -627,28 +619,30 @@ public void streamClosed(Status status) {
627619
AttributesBuilder builder = io.opentelemetry.api.common.Attributes.builder()
628620
.put(METHOD_KEY, recordMethodName(fullMethodName, isGeneratedMethod))
629621
.put(STATUS_KEY, status.getCode().toString());
622+
623+
if (baggage != null) {
624+
for (java.util.Map.Entry<String, io.opentelemetry.api.baggage.BaggageEntry> entry :
625+
baggage.asMap().entrySet()) {
626+
builder.put(entry.getKey(), entry.getValue().getValue());
627+
}
628+
}
629+
630630
for (OpenTelemetryPlugin.ServerStreamPlugin plugin : streamPlugins) {
631631
plugin.addLabels(builder);
632632
}
633633
io.opentelemetry.api.common.Attributes attributes = builder.build();
634634

635-
Context ctxToRecord = otelContext;
636-
Baggage currentBaggage = BAGGAGE_KEY.get();
637-
if (currentBaggage != null && !currentBaggage.isEmpty()) {
638-
ctxToRecord = ctxToRecord.with(currentBaggage);
639-
}
640-
641635
if (module.resource.serverCallDurationCounter() != null) {
642636
module.resource.serverCallDurationCounter()
643-
.record(elapsedTimeNanos * SECONDS_PER_NANO, attributes, ctxToRecord);
637+
.record(elapsedTimeNanos * SECONDS_PER_NANO, attributes);
644638
}
645639
if (module.resource.serverTotalSentCompressedMessageSizeCounter() != null) {
646640
module.resource.serverTotalSentCompressedMessageSizeCounter()
647-
.record(outboundWireSize, attributes, ctxToRecord);
641+
.record(outboundWireSize, attributes);
648642
}
649643
if (module.resource.serverTotalReceivedCompressedMessageSizeCounter() != null) {
650644
module.resource.serverTotalReceivedCompressedMessageSizeCounter()
651-
.record(inboundWireSize, attributes, ctxToRecord);
645+
.record(inboundWireSize, attributes);
652646
}
653647
}
654648
}
@@ -668,10 +662,8 @@ public ServerStreamTracer newServerStreamTracer(String fullMethodName, Metadata
668662
}
669663
streamPlugins = Collections.unmodifiableList(streamPluginsMutable);
670664
}
671-
Context context = contextPropagators.getTextMapPropagator().extract(
672-
Context.current(), headers, MetadataGetter.getInstance());
673-
return new ServerTracer(OpenTelemetryMetricsModule.this, fullMethodName, streamPlugins,
674-
context);
665+
return new ServerTracer(OpenTelemetryMetricsModule.this, fullMethodName,
666+
streamPlugins);
675667
}
676668
}
677669

0 commit comments

Comments
 (0)