|
59 | 59 | import io.grpc.testing.GrpcCleanupRule; |
60 | 60 | import io.grpc.testing.GrpcServerRule; |
61 | 61 | import io.opentelemetry.api.OpenTelemetry; |
| 62 | +import io.opentelemetry.api.baggage.Baggage; |
62 | 63 | import io.opentelemetry.api.trace.Span; |
63 | 64 | import io.opentelemetry.api.trace.SpanBuilder; |
64 | 65 | import io.opentelemetry.api.trace.SpanId; |
@@ -750,6 +751,48 @@ public void onComplete() { |
750 | 751 | } |
751 | 752 | } |
752 | 753 |
|
| 754 | + @Test |
| 755 | + public void serverSpanPropagationInterceptor_propagatesBaggage() { |
| 756 | + // 1. Arrange: Setup the module, interceptor, and mocks. |
| 757 | + OpenTelemetryTracingModule tracingModule = new OpenTelemetryTracingModule( |
| 758 | + openTelemetryRule.getOpenTelemetry()); |
| 759 | + ServerInterceptor interceptor = tracingModule.getServerSpanPropagationInterceptor(); |
| 760 | + |
| 761 | + @SuppressWarnings("unchecked") |
| 762 | + ServerCallHandler<String, String> handler = mock(ServerCallHandler.class); |
| 763 | + ServerCall<String, String> call = new NoopServerCall<>(); |
| 764 | + Metadata metadata = new Metadata(); |
| 765 | + final AtomicReference<Baggage> capturedBaggage = new AtomicReference<>(); |
| 766 | + |
| 767 | + // Mock the handler to capture the Baggage from the current context when it's called. |
| 768 | + when(handler.startCall(any(), any())).thenAnswer(invocation -> { |
| 769 | + capturedBaggage.set(io.opentelemetry.api.baggage.Baggage.current()); |
| 770 | + return mockServerCallListener; |
| 771 | + }); |
| 772 | + |
| 773 | + // Create a test Span and Baggage to be propagated. |
| 774 | + Span parentSpan = tracerRule.spanBuilder("parent-span").startSpan(); |
| 775 | + io.opentelemetry.api.baggage.Baggage testBaggage = |
| 776 | + io.opentelemetry.api.baggage.Baggage.builder().put("testKey", "testValue").build(); |
| 777 | + |
| 778 | + // Attach the Span and Baggage to the gRPC context. |
| 779 | + io.grpc.Context grpcContext = io.grpc.Context.current() |
| 780 | + .withValue(tracingModule.otelSpan, parentSpan) |
| 781 | + .withValue(tracingModule.baggageKey, testBaggage); |
| 782 | + |
| 783 | + io.grpc.Context previous = grpcContext.attach(); |
| 784 | + try { |
| 785 | + // 2. Act: Call the interceptor. |
| 786 | + interceptor.interceptCall(call, metadata, handler); |
| 787 | + } finally { |
| 788 | + grpcContext.detach(previous); |
| 789 | + } |
| 790 | + |
| 791 | + // 3. Assert: Verify the handler was called and the correct Baggage was propagated. |
| 792 | + verify(handler).startCall(same(call), same(metadata)); |
| 793 | + assertEquals(testBaggage, capturedBaggage.get()); |
| 794 | + } |
| 795 | + |
753 | 796 | @Test |
754 | 797 | public void generateTraceSpanName() { |
755 | 798 | assertEquals( |
|
0 commit comments