diff --git a/circuitbreaker-reactive/src/main/java/com/example/micrometer/ReactiveCircuitBreakerApplication.java b/circuitbreaker-reactive/src/main/java/com/example/micrometer/ReactiveCircuitBreakerApplication.java index 7ecc658..d6c75c9 100644 --- a/circuitbreaker-reactive/src/main/java/com/example/micrometer/ReactiveCircuitBreakerApplication.java +++ b/circuitbreaker-reactive/src/main/java/com/example/micrometer/ReactiveCircuitBreakerApplication.java @@ -1,6 +1,7 @@ package com.example.micrometer; import io.micrometer.context.ContextSnapshot; +import io.micrometer.context.ContextSnapshotFactory; import io.micrometer.observation.Observation; import io.micrometer.observation.ObservationRegistry; import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor; @@ -47,6 +48,8 @@ class CircuitService { private final ObservationRegistry observationRegistry; + private final ContextSnapshotFactory contextSnapshotFactory = ContextSnapshotFactory.builder().build(); + CircuitService(ReactiveCircuitBreakerFactory factory, Tracer tracer, ObservationRegistry observationRegistry) { this.factory = factory; this.tracer = tracer; @@ -74,7 +77,7 @@ Mono call() { } private void scoped(ContextView contextView, Runnable runnable) { - try (ContextSnapshot.Scope scope = ContextSnapshot.setThreadLocalsFrom(contextView, + try (ContextSnapshot.Scope scope = this.contextSnapshotFactory.setThreadLocalsFrom(contextView, ObservationThreadLocalAccessor.KEY)) { runnable.run(); } diff --git a/data-reactive/src/main/java/com/example/micrometer/ReactiveContinuedTransactionService.java b/data-reactive/src/main/java/com/example/micrometer/ReactiveContinuedTransactionService.java index a6d5c7a..5e81516 100644 --- a/data-reactive/src/main/java/com/example/micrometer/ReactiveContinuedTransactionService.java +++ b/data-reactive/src/main/java/com/example/micrometer/ReactiveContinuedTransactionService.java @@ -1,6 +1,7 @@ package com.example.micrometer; import io.micrometer.context.ContextSnapshot; +import io.micrometer.context.ContextSnapshotFactory; import io.micrometer.observation.ObservationRegistry; import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor; import io.micrometer.tracing.Tracer; @@ -23,6 +24,8 @@ public class ReactiveContinuedTransactionService { private final ReactiveNestedTransactionService reactiveNestedTransactionService; + private final ContextSnapshotFactory contextSnapshotFactory = ContextSnapshotFactory.builder().build(); + public ReactiveContinuedTransactionService(ReactiveCustomerRepository repository, Tracer tracer, ObservationRegistry observationRegistry, ReactiveNestedTransactionService reactiveNestedTransactionService) { @@ -35,7 +38,7 @@ public ReactiveContinuedTransactionService(ReactiveCustomerRepository repository @Transactional public Mono continuedTransaction() { return Mono.deferContextual(contextView -> { - try (ContextSnapshot.Scope scope = ContextSnapshot.setThreadLocalsFrom(contextView, + try (ContextSnapshot.Scope scope = this.contextSnapshotFactory.setThreadLocalsFrom(contextView, ObservationThreadLocalAccessor.KEY)) { log.info(" Hello from consumer", tracer.currentSpan().context().traceId()); } @@ -43,7 +46,7 @@ public Mono continuedTransaction() { }) .transformDeferredContextual( (reactiveCustomerMono, contextView) -> reactiveCustomerMono.doOnNext(customer -> { - try (ContextSnapshot.Scope scope = ContextSnapshot.setThreadLocalsFrom(contextView, + try (ContextSnapshot.Scope scope = this.contextSnapshotFactory.setThreadLocalsFrom(contextView, ObservationThreadLocalAccessor.KEY)) { // fetch an individual customer by ID log.info("Customer found with findById(1L):"); @@ -55,7 +58,7 @@ public Mono continuedTransaction() { log.info("--------------------------------------------"); } }).flatMapMany(customer -> repository.findByLastName("Bauer")).doOnNext(reactiveCustomer -> { - try (ContextSnapshot.Scope scope = ContextSnapshot.setThreadLocalsFrom(contextView, + try (ContextSnapshot.Scope scope = this.contextSnapshotFactory.setThreadLocalsFrom(contextView, ObservationThreadLocalAccessor.KEY)) { log.info(reactiveCustomer.toString()); } diff --git a/data-reactive/src/main/java/com/example/micrometer/ReactiveNestedTransactionService.java b/data-reactive/src/main/java/com/example/micrometer/ReactiveNestedTransactionService.java index ea48a74..b7b1a43 100644 --- a/data-reactive/src/main/java/com/example/micrometer/ReactiveNestedTransactionService.java +++ b/data-reactive/src/main/java/com/example/micrometer/ReactiveNestedTransactionService.java @@ -1,6 +1,7 @@ package com.example.micrometer; import io.micrometer.context.ContextSnapshot; +import io.micrometer.context.ContextSnapshotFactory; import io.micrometer.observation.ObservationRegistry; import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor; import io.micrometer.tracing.Tracer; @@ -23,6 +24,8 @@ public class ReactiveNestedTransactionService { private final ReactiveCustomerRepository repository; + private final ContextSnapshotFactory contextSnapshotFactory = ContextSnapshotFactory.builder().build(); + public ReactiveNestedTransactionService(Tracer tracer, ObservationRegistry observationRegistry, ReactiveCustomerRepository repository) { this.tracer = tracer; @@ -33,7 +36,7 @@ public ReactiveNestedTransactionService(Tracer tracer, ObservationRegistry obser @Transactional(propagation = Propagation.REQUIRES_NEW) public Mono requiresNew() { return Mono.deferContextual(contextView -> { - try (ContextSnapshot.Scope scope = ContextSnapshot.setThreadLocalsFrom(contextView, + try (ContextSnapshot.Scope scope = this.contextSnapshotFactory.setThreadLocalsFrom(contextView, ObservationThreadLocalAccessor.KEY)) { log.info(" Hello from consumer requires new", tracer.currentSpan().context().traceId()); diff --git a/data-reactive/src/main/java/com/example/micrometer/ReactiveNewTransactionService.java b/data-reactive/src/main/java/com/example/micrometer/ReactiveNewTransactionService.java index 219818e..dd3e3a5 100644 --- a/data-reactive/src/main/java/com/example/micrometer/ReactiveNewTransactionService.java +++ b/data-reactive/src/main/java/com/example/micrometer/ReactiveNewTransactionService.java @@ -1,6 +1,7 @@ package com.example.micrometer; import io.micrometer.context.ContextSnapshot; +import io.micrometer.context.ContextSnapshotFactory; import io.micrometer.observation.ObservationRegistry; import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor; import io.micrometer.tracing.Tracer; @@ -24,6 +25,8 @@ public class ReactiveNewTransactionService { private final ObservationRegistry observationRegistry; + private final ContextSnapshotFactory contextSnapshotFactory = ContextSnapshotFactory.builder().build(); + public ReactiveNewTransactionService(ReactiveCustomerRepository repository, ReactiveContinuedTransactionService reactiveContinuedTransactionService, Tracer tracer, ObservationRegistry observationRegistry) { @@ -36,7 +39,7 @@ public ReactiveNewTransactionService(ReactiveCustomerRepository repository, @Transactional public Mono newTransaction() { return Mono.deferContextual(contextView -> { - try (ContextSnapshot.Scope scope = ContextSnapshot.setThreadLocalsFrom(contextView, + try (ContextSnapshot.Scope scope = this.contextSnapshotFactory.setThreadLocalsFrom(contextView, ObservationThreadLocalAccessor.KEY)) { log.info(" Hello from producer", tracer.currentSpan().context().traceId()); log.info("Customers found with findAll():"); @@ -52,7 +55,7 @@ public Mono newTransaction() { .flatMapMany(reactiveCustomer -> repository.findAll()) .transformDeferredContextual( (reactiveCustomerFlux, contextView) -> reactiveCustomerFlux.doOnNext(reactiveCustomer -> { - try (ContextSnapshot.Scope scope = ContextSnapshot.setThreadLocalsFrom(contextView, + try (ContextSnapshot.Scope scope = this.contextSnapshotFactory.setThreadLocalsFrom(contextView, ObservationThreadLocalAccessor.KEY)) { log.info(reactiveCustomer.toString()); } diff --git a/rsocket-client/src/main/java/com/example/micrometer/RsocketClient.java b/rsocket-client/src/main/java/com/example/micrometer/RsocketClient.java index 15c9fa9..a8f3df9 100644 --- a/rsocket-client/src/main/java/com/example/micrometer/RsocketClient.java +++ b/rsocket-client/src/main/java/com/example/micrometer/RsocketClient.java @@ -1,6 +1,7 @@ package com.example.micrometer; import io.micrometer.context.ContextSnapshot; +import io.micrometer.context.ContextSnapshotFactory; import io.micrometer.observation.Observation; import io.micrometer.observation.ObservationRegistry; import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor; @@ -64,6 +65,8 @@ class RsocketService { private final ObservationRegistry observationRegistry; + private final ContextSnapshotFactory contextSnapshotFactory = ContextSnapshotFactory.builder().build(); + RsocketService(RSocketRequester rSocketRequester, Tracer tracer, ObservationRegistry observationRegistry) { this.rSocketRequester = rSocketRequester; this.tracer = tracer; @@ -76,7 +79,7 @@ Mono call() { // You could use the client Obseravtion directy, but we're trying to show how // you would interact with // setting thread locals from Reactor Context - try (ContextSnapshot.Scope scope = ContextSnapshot.setThreadLocalsFrom(contextView, + try (ContextSnapshot.Scope scope = this.contextSnapshotFactory.setThreadLocalsFrom(contextView, ObservationThreadLocalAccessor.KEY)) { log.info(" Hello from producer", this.tracer.currentSpan().context().traceId()); diff --git a/rsocket-server/src/main/java/com/example/micrometer/RsocketServerApplication.java b/rsocket-server/src/main/java/com/example/micrometer/RsocketServerApplication.java index b610590..93b2f03 100644 --- a/rsocket-server/src/main/java/com/example/micrometer/RsocketServerApplication.java +++ b/rsocket-server/src/main/java/com/example/micrometer/RsocketServerApplication.java @@ -1,6 +1,7 @@ package com.example.micrometer; import io.micrometer.context.ContextSnapshot; +import io.micrometer.context.ContextSnapshotFactory; import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor; import io.micrometer.tracing.Tracer; import org.slf4j.Logger; @@ -27,6 +28,8 @@ class RSocketController { private final Tracer tracer; + private final ContextSnapshotFactory contextSnapshotFactory = ContextSnapshotFactory.builder().build(); + RSocketController(Tracer tracer) { this.tracer = tracer; } @@ -34,7 +37,7 @@ class RSocketController { @MessageMapping("foo") public Mono span() { return Mono.deferContextual(contextView -> { - try (ContextSnapshot.Scope scope = ContextSnapshot.setThreadLocalsFrom(contextView, + try (ContextSnapshot.Scope scope = this.contextSnapshotFactory.setThreadLocalsFrom(contextView, ObservationThreadLocalAccessor.KEY)) { String traceId = this.tracer.currentSpan().context().traceId(); log.info(" Hello from consumer", traceId); diff --git a/vault-webclient/src/main/java/com/example/micrometer/VaultWebClientApplication.java b/vault-webclient/src/main/java/com/example/micrometer/VaultWebClientApplication.java index 790531d..bbb8d67 100644 --- a/vault-webclient/src/main/java/com/example/micrometer/VaultWebClientApplication.java +++ b/vault-webclient/src/main/java/com/example/micrometer/VaultWebClientApplication.java @@ -1,6 +1,7 @@ package com.example.micrometer; import io.micrometer.context.ContextSnapshot; +import io.micrometer.context.ContextSnapshotFactory; import io.micrometer.observation.Observation; import io.micrometer.observation.ObservationRegistry; import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor; @@ -37,6 +38,8 @@ class Config { private static final Logger log = LoggerFactory.getLogger(Config.class); + private final ContextSnapshotFactory contextSnapshotFactory = ContextSnapshotFactory.builder().build(); + @Bean CommandLineRunner myCommandLineRunner(WebClientService webClientService) { return args -> { @@ -54,7 +57,7 @@ CommandLineRunner myCommandLineRunner(WebClientService webClientService) { @Bean WebClientCustomizer testWebClientCustomizer(Tracer tracer, ObservationRegistry observationRegistry) { return webClientBuilder -> webClientBuilder.filter((request, next) -> Mono.deferContextual(contextView -> { - try (ContextSnapshot.Scope scope = ContextSnapshot.setThreadLocalsFrom(contextView, + try (ContextSnapshot.Scope scope = this.contextSnapshotFactory.setThreadLocalsFrom(contextView, ObservationThreadLocalAccessor.KEY)) { log.info(" Hello from producer", tracer.currentSpan().context().traceId()); } diff --git a/webflux/src/main/java/com/example/micrometer/WebFluxApplication.java b/webflux/src/main/java/com/example/micrometer/WebFluxApplication.java index 659a228..a2a6762 100644 --- a/webflux/src/main/java/com/example/micrometer/WebFluxApplication.java +++ b/webflux/src/main/java/com/example/micrometer/WebFluxApplication.java @@ -1,6 +1,7 @@ package com.example.micrometer; import io.micrometer.context.ContextSnapshot; +import io.micrometer.context.ContextSnapshotFactory; import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor; import io.micrometer.tracing.Tracer; import org.slf4j.Logger; @@ -27,6 +28,8 @@ class WebFluxController { private final Tracer tracer; + private final ContextSnapshotFactory contextSnapshotFactory = ContextSnapshotFactory.builder().build(); + WebFluxController(Tracer tracer) { this.tracer = tracer; } @@ -34,7 +37,7 @@ class WebFluxController { @RequestMapping("/") public Mono span() { return Mono.deferContextual(contextView -> { - try (ContextSnapshot.Scope scope = ContextSnapshot.setThreadLocalsFrom(contextView, + try (ContextSnapshot.Scope scope = this.contextSnapshotFactory.setThreadLocalsFrom(contextView, ObservationThreadLocalAccessor.KEY)) { String traceId = this.tracer.currentSpan().context().traceId(); log.info(" Hello from producer", traceId);