2323import  io .micrometer .tracing .Span ;
2424import  io .micrometer .tracing .Tracer ;
2525import  io .opentelemetry .context .Context ;
26+ import  org .assertj .core .api .ThrowingConsumer ;
2627import  org .junit .jupiter .api .AfterEach ;
2728import  org .junit .jupiter .api .BeforeEach ;
2829import  org .junit .jupiter .params .ParameterizedTest ;
4142 * formats. 
4243 * 
4344 * @author Marcin Grzejszczak 
45+  * @author Moritz Halbritter 
4446 */ 
4547class  BaggagePropagationIntegrationTests  {
4648
47- 	static  final  String  COUNTRY_CODE  = "country-code" ;
48- 	static  final  String  BUSINESS_PROCESS  = "bp" ;
49+ 	private  static  final  String  COUNTRY_CODE  = "country-code" ;
50+ 
51+ 	private  static  final  String  BUSINESS_PROCESS  = "bp" ;
4952
5053	@ BeforeEach 
5154	@ AfterEach 
@@ -61,7 +64,7 @@ void shouldSetEntriesToMdcFromSpanWithBaggage(AutoConfig autoConfig) {
6164			Span  span  = createSpan (tracer );
6265			assertThatTracingContextIsInitialized (autoConfig );
6366			try  (Tracer .SpanInScope  scope  = tracer .withSpan (span .start ())) {
64- 				BaggageManager  baggageManager  = context . getBean ( BaggageManager . class );
67+ 				BaggageManager  baggageManager  = baggageManager ( context );
6568				try  (BaggageInScope  fo  = baggageManager .createBaggageInScope (span .context (), COUNTRY_CODE , "FO" );
6669						BaggageInScope  alm  = baggageManager .createBaggageInScope (span .context (), BUSINESS_PROCESS ,
6770								"ALM" )) {
@@ -73,8 +76,7 @@ void shouldSetEntriesToMdcFromSpanWithBaggage(AutoConfig autoConfig) {
7376			finally  {
7477				span .end ();
7578			}
76- 
77- 			assertThatMdcContainsUnsetTraceId ();
79+ 			assertThatMdcContainsUnsetTraceId (autoConfig );
7880			assertThat (MDC .get (COUNTRY_CODE )).isNull ();
7981			assertThat (MDC .get (BUSINESS_PROCESS )).isNull ();
8082		});
@@ -88,25 +90,22 @@ void shouldRemoveEntriesFromMdcForNullSpan(AutoConfig autoConfig) {
8890			Span  span  = createSpan (tracer );
8991			assertThatTracingContextIsInitialized (autoConfig );
9092			try  (Tracer .SpanInScope  scope  = tracer .withSpan (span .start ())) {
91- 				try  (BaggageInScope  fo  = context .getBean (BaggageManager .class )
92- 					.createBaggageInScope (span .context (), COUNTRY_CODE , "FO" )) {
93- 
93+ 				try  (BaggageInScope  fo  = baggageManager (context ).createBaggageInScope (span .context (), COUNTRY_CODE ,
94+ 						"FO" )) {
9495					assertThat (MDC .get ("traceId" )).isEqualTo (span .context ().traceId ());
9596					assertThat (MDC .get (COUNTRY_CODE )).isEqualTo ("FO" );
96- 
9797					try  (Tracer .SpanInScope  scope2  = tracer .withSpan (null )) {
98- 						assertThatMdcContainsUnsetTraceId ();
98+ 						assertThatMdcContainsUnsetTraceId (autoConfig );
9999						assertThat (MDC .get (COUNTRY_CODE )).isNull ();
100100					}
101- 
102101					assertThat (MDC .get ("traceId" )).isEqualTo (span .context ().traceId ());
103102					assertThat (MDC .get (COUNTRY_CODE )).isEqualTo ("FO" );
104103				}
105104			}
106105			finally  {
107106				span .end ();
108107			}
109- 			assertThatMdcContainsUnsetTraceId ();
108+ 			assertThatMdcContainsUnsetTraceId (autoConfig );
110109			assertThat (MDC .get (COUNTRY_CODE )).isNull ();
111110		});
112111	}
@@ -119,22 +118,28 @@ private Tracer tracer(ApplicationContext context) {
119118		return  context .getBean (Tracer .class );
120119	}
121120
121+ 	private  BaggageManager  baggageManager (ApplicationContext  context ) {
122+ 		return  context .getBean (BaggageManager .class );
123+ 	}
124+ 
122125	private  void  assertThatTracingContextIsInitialized (AutoConfig  autoConfig ) {
123- 		if  (autoConfig  ==  AutoConfig . OTEL_B3 ) {
126+ 		if  (autoConfig . isOtel () ) {
124127			assertThat (Context .current ()).isEqualTo (Context .root ());
125128		}
126129	}
127130
128- 	private  void  assertThatMdcContainsUnsetTraceId () {
129- 		assertThat (isInvalidBraveTraceId () || isInvalidOtelTraceId ()).isTrue ();
130- 	}
131- 
132- 	private  boolean  isInvalidBraveTraceId () {
133- 		return  MDC .get ("traceId" ) == null ;
134- 	}
135- 
136- 	private  boolean  isInvalidOtelTraceId () {
137- 		return  MDC .get ("traceId" ).equals ("00000000000000000000000000000000" );
131+ 	private  void  assertThatMdcContainsUnsetTraceId (AutoConfig  autoConfig ) {
132+ 		boolean  eitherOtelOrBrave  = autoConfig .isOtel () || autoConfig .isBrave ();
133+ 		assertThat (eitherOtelOrBrave ).isTrue ();
134+ 		if  (autoConfig .isOtel ()) {
135+ 			ThrowingConsumer <String > isNull  = (traceId ) -> assertThat (traceId ).isNull ();
136+ 			ThrowingConsumer <String > isZero  = (traceId ) -> assertThat (traceId )
137+ 				.isEqualTo ("00000000000000000000000000000000" );
138+ 			assertThat (MDC .get ("traceId" )).satisfiesAnyOf (isNull , isZero );
139+ 		}
140+ 		if  (autoConfig .isBrave ()) {
141+ 			assertThat (MDC .get ("traceId" )).isNull ();
142+ 		}
138143	}
139144
140145	enum  AutoConfig  implements  Supplier <ApplicationContextRunner > {
@@ -227,6 +232,14 @@ public ApplicationContextRunner get() {
227232							"management.tracing.baggage.remote-fields=x-vcap-request-id,country-code,bp" ,
228233							"management.tracing.baggage.correlation.fields=country-code,bp" );
229234			}
235+ 		};
236+ 
237+ 		boolean  isOtel () {
238+ 			return  name ().startsWith ("OTEL_" );
239+ 		}
240+ 
241+ 		boolean  isBrave () {
242+ 			return  name ().startsWith ("BRAVE_" );
230243		}
231244
232245	}
0 commit comments