Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace deprecated ContextSnapshot.setThreadLocalsFrom() #50

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -74,7 +77,7 @@ Mono<String> 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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand All @@ -35,15 +38,15 @@ public ReactiveContinuedTransactionService(ReactiveCustomerRepository repository
@Transactional
public Mono<Void> continuedTransaction() {
return Mono.deferContextual(contextView -> {
try (ContextSnapshot.Scope scope = ContextSnapshot.setThreadLocalsFrom(contextView,
try (ContextSnapshot.Scope scope = this.contextSnapshotFactory.setThreadLocalsFrom(contextView,
ObservationThreadLocalAccessor.KEY)) {
log.info("<ACCEPTANCE_TEST> <TRACE:{}> Hello from consumer", tracer.currentSpan().context().traceId());
}
return repository.findById(1L);
})
.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):");
Expand All @@ -55,7 +58,7 @@ public Mono<Void> 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());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -33,7 +36,7 @@ public ReactiveNestedTransactionService(Tracer tracer, ObservationRegistry obser
@Transactional(propagation = Propagation.REQUIRES_NEW)
public Mono<Void> requiresNew() {
return Mono.deferContextual(contextView -> {
try (ContextSnapshot.Scope scope = ContextSnapshot.setThreadLocalsFrom(contextView,
try (ContextSnapshot.Scope scope = this.contextSnapshotFactory.setThreadLocalsFrom(contextView,
ObservationThreadLocalAccessor.KEY)) {
log.info("<ACCEPTANCE_TEST> <TRACE:{}> Hello from consumer requires new",
tracer.currentSpan().context().traceId());
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand All @@ -36,7 +39,7 @@ public ReactiveNewTransactionService(ReactiveCustomerRepository repository,
@Transactional
public Mono<Void> newTransaction() {
return Mono.deferContextual(contextView -> {
try (ContextSnapshot.Scope scope = ContextSnapshot.setThreadLocalsFrom(contextView,
try (ContextSnapshot.Scope scope = this.contextSnapshotFactory.setThreadLocalsFrom(contextView,
ObservationThreadLocalAccessor.KEY)) {
log.info("<ACCEPTANCE_TEST> <TRACE:{}> Hello from producer", tracer.currentSpan().context().traceId());
log.info("Customers found with findAll():");
Expand All @@ -52,7 +55,7 @@ public Mono<Void> 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());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -76,7 +79,7 @@ Mono<String> 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("<ACCEPTANCE_TEST> <TRACE:{}> Hello from producer",
this.tracer.currentSpan().context().traceId());
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -27,14 +28,16 @@ class RSocketController {

private final Tracer tracer;

private final ContextSnapshotFactory contextSnapshotFactory = ContextSnapshotFactory.builder().build();

RSocketController(Tracer tracer) {
this.tracer = tracer;
}

@MessageMapping("foo")
public Mono<String> 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("<ACCEPTANCE_TEST> <TRACE:{}> Hello from consumer", traceId);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 -> {
Expand All @@ -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("<ACCEPTANCE_TEST> <TRACE:{}> Hello from producer", tracer.currentSpan().context().traceId());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -27,14 +28,16 @@ class WebFluxController {

private final Tracer tracer;

private final ContextSnapshotFactory contextSnapshotFactory = ContextSnapshotFactory.builder().build();

WebFluxController(Tracer tracer) {
this.tracer = tracer;
}

@RequestMapping("/")
public Mono<String> 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("<ACCEPTANCE_TEST> <TRACE:{}> Hello from producer", traceId);
Expand Down