@@ -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