From 7df79432eb6ae2627a5db9aa3a2362acbe89ff32 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 12 Sep 2023 15:10:19 +0200 Subject: [PATCH] Polish "Upgrade to Micrometer Tracing 1.0.10" See gh-37345 --- .../BaggagePropagationIntegrationTests.java | 40 ++++++++++++------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/BaggagePropagationIntegrationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/BaggagePropagationIntegrationTests.java index 0f2b50f9df..9245f8af96 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/BaggagePropagationIntegrationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/BaggagePropagationIntegrationTests.java @@ -18,6 +18,7 @@ package org.springframework.boot.actuate.autoconfigure.tracing; import java.util.function.Supplier; +import io.micrometer.tracing.BaggageInScope; import io.micrometer.tracing.BaggageManager; import io.micrometer.tracing.Span; import io.micrometer.tracing.Tracer; @@ -60,11 +61,16 @@ class BaggagePropagationIntegrationTests { assertThatTracingContextIsInitialized(autoConfig); try (Tracer.SpanInScope scope = tracer.withSpan(span.start())) { BaggageManager baggageManager = context.getBean(BaggageManager.class); - baggageManager.createBaggage(COUNTRY_CODE).set(span.context(), "FO"); - baggageManager.createBaggage(BUSINESS_PROCESS).set(span.context(), "ALM"); - assertThat(MDC.get("traceId")).isEqualTo(span.context().traceId()); - assertThat(MDC.get(COUNTRY_CODE)).isEqualTo("FO"); - assertThat(MDC.get(BUSINESS_PROCESS)).isEqualTo("ALM"); + try (BaggageInScope fo = baggageManager.createBaggage(COUNTRY_CODE) + .set(span.context(), "FO") + .makeCurrent(); + BaggageInScope alm = baggageManager.createBaggage(BUSINESS_PROCESS) + .set(span.context(), "ALM") + .makeCurrent()) { + assertThat(MDC.get("traceId")).isEqualTo(span.context().traceId()); + assertThat(MDC.get(COUNTRY_CODE)).isEqualTo("FO"); + assertThat(MDC.get(BUSINESS_PROCESS)).isEqualTo("ALM"); + } } finally { span.end(); @@ -84,17 +90,21 @@ class BaggagePropagationIntegrationTests { Span span = createSpan(tracer); assertThatTracingContextIsInitialized(autoConfig); try (Tracer.SpanInScope scope = tracer.withSpan(span.start())) { - context.getBean(BaggageManager.class).createBaggage(COUNTRY_CODE).set(span.context(), "FO"); - assertThat(MDC.get("traceId")).isEqualTo(span.context().traceId()); - assertThat(MDC.get(COUNTRY_CODE)).isEqualTo("FO"); - - try (Tracer.SpanInScope scope2 = tracer.withSpan(null)) { - assertThatMdcContainsUnsetTraceId(); - assertThat(MDC.get(COUNTRY_CODE)).isNull(); + try (BaggageInScope fo = context.getBean(BaggageManager.class) + .createBaggage(COUNTRY_CODE) + .set(span.context(), "FO") + .makeCurrent()) { + assertThat(MDC.get("traceId")).isEqualTo(span.context().traceId()); + assertThat(MDC.get(COUNTRY_CODE)).isEqualTo("FO"); + + try (Tracer.SpanInScope scope2 = tracer.withSpan(null)) { + assertThatMdcContainsUnsetTraceId(); + assertThat(MDC.get(COUNTRY_CODE)).isNull(); + } + + assertThat(MDC.get("traceId")).isEqualTo(span.context().traceId()); + assertThat(MDC.get(COUNTRY_CODE)).isEqualTo("FO"); } - - assertThat(MDC.get("traceId")).isEqualTo(span.context().traceId()); - assertThat(MDC.get(COUNTRY_CODE)).isEqualTo("FO"); } finally { span.end();