Skip to content

Commit c8c10e2

Browse files
committed
baggage propagation
1 parent 9ec107f commit c8c10e2

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ final class OpenTelemetryTracingModule {
6161

6262
@VisibleForTesting
6363
final io.grpc.Context.Key<Span> otelSpan = io.grpc.Context.key("opentelemetry-span-key");
64-
6564
@Nullable
6665
private static final AtomicIntegerFieldUpdater<CallAttemptsTracerFactory> callEndedUpdater;
6766
@Nullable

opentelemetry/src/test/java/io/grpc/opentelemetry/OpenTelemetryTracingModuleTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,48 @@ public void testNullBaggageIsHandledGracefully() {
866866
Baggage.empty(), capturedBaggage.get());
867867
}
868868

869+
@Test
870+
public void serverSpanPropagationInterceptor_propagatesBaggage() {
871+
// 1. Arrange: Setup the module, interceptor, and mocks.
872+
OpenTelemetryTracingModule tracingModule = new OpenTelemetryTracingModule(
873+
openTelemetryRule.getOpenTelemetry());
874+
ServerInterceptor interceptor = tracingModule.getServerSpanPropagationInterceptor();
875+
876+
@SuppressWarnings("unchecked")
877+
ServerCallHandler<String, String> handler = mock(ServerCallHandler.class);
878+
ServerCall<String, String> call = new NoopServerCall<>();
879+
Metadata metadata = new Metadata();
880+
final AtomicReference<Baggage> capturedBaggage = new AtomicReference<>();
881+
882+
// Mock the handler to capture the Baggage from the current context when it's called.
883+
when(handler.startCall(any(), any())).thenAnswer(invocation -> {
884+
capturedBaggage.set(io.opentelemetry.api.baggage.Baggage.current());
885+
return mockServerCallListener;
886+
});
887+
888+
// Create a test Span and Baggage to be propagated.
889+
Span parentSpan = tracerRule.spanBuilder("parent-span").startSpan();
890+
io.opentelemetry.api.baggage.Baggage testBaggage =
891+
io.opentelemetry.api.baggage.Baggage.builder().put("testKey", "testValue").build();
892+
893+
// Attach the Span and Baggage to the gRPC context.
894+
io.grpc.Context grpcContext = io.grpc.Context.current()
895+
.withValue(tracingModule.otelSpan, parentSpan)
896+
.withValue(tracingModule.baggageKey, testBaggage);
897+
898+
io.grpc.Context previous = grpcContext.attach();
899+
try {
900+
// 2. Act: Call the interceptor.
901+
interceptor.interceptCall(call, metadata, handler);
902+
} finally {
903+
grpcContext.detach(previous);
904+
}
905+
906+
// 3. Assert: Verify the handler was called and the correct Baggage was propagated.
907+
verify(handler).startCall(same(call), same(metadata));
908+
assertEquals(testBaggage, capturedBaggage.get());
909+
}
910+
869911
@Test
870912
public void generateTraceSpanName() {
871913
assertEquals(

0 commit comments

Comments
 (0)