From aa01a5c91fae329026c463a1a4ad4083ee45edf6 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Mon, 18 Feb 2019 15:00:57 +0100 Subject: [PATCH] Refactored the way we decorate reactor scheduler --- .../TraceReactorAutoConfiguration.java | 24 +++++++------------ .../async/TraceAsyncIntegrationTests.java | 20 ++++++++-------- src/checkstyle/checkstyle-suppressions.xml | 1 + 3 files changed, 20 insertions(+), 25 deletions(-) diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/reactor/TraceReactorAutoConfiguration.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/reactor/TraceReactorAutoConfiguration.java index 035dbe3eb..21c36a3e7 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/reactor/TraceReactorAutoConfiguration.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/reactor/TraceReactorAutoConfiguration.java @@ -16,9 +16,6 @@ package org.springframework.cloud.sleuth.instrument.reactor; -import java.util.concurrent.ScheduledExecutorService; -import java.util.function.Supplier; - import javax.annotation.PreDestroy; import brave.Tracing; @@ -58,6 +55,8 @@ import org.springframework.context.annotation.Configuration; @AutoConfigureAfter(TraceWebFluxAutoConfiguration.class) public class TraceReactorAutoConfiguration { + static final String SLEUTH_REACTOR_EXECUTOR_SERVICE_KEY = "sleuth"; + @Configuration @ConditionalOnBean(Tracing.class) static class TraceReactorConfiguration { @@ -86,7 +85,8 @@ public class TraceReactorAutoConfiguration { log.trace("Cleaning up hooks"); } Hooks.resetOnLastOperator(SLEUTH_TRACE_REACTOR_KEY); - Schedulers.resetFactory(); + Schedulers + .removeExecutorServiceDecorator(SLEUTH_REACTOR_EXECUTOR_SERVICE_KEY); } } @@ -118,17 +118,11 @@ class HookRegisteringBeanDefinitionRegistryPostProcessor Hooks.onLastOperator( TraceReactorAutoConfiguration.TraceReactorConfiguration.SLEUTH_TRACE_REACTOR_KEY, ReactorSleuth.scopePassingSpanOperator(this.context)); - Schedulers.setFactory(factoryInstance(beanFactory)); - } - - private Schedulers.Factory factoryInstance(final BeanFactory beanFactory) { - return new Schedulers.Factory() { - @Override - public ScheduledExecutorService decorateExecutorService(String schedulerType, - Supplier actual) { - return new TraceableScheduledExecutorService(beanFactory, actual.get()); - } - }; + Schedulers.setExecutorServiceDecorator( + TraceReactorAutoConfiguration.SLEUTH_REACTOR_EXECUTOR_SERVICE_KEY, + (scheduler, + scheduledExecutorService) -> new TraceableScheduledExecutorService( + beanFactory, scheduledExecutorService)); } } diff --git a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/async/TraceAsyncIntegrationTests.java b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/async/TraceAsyncIntegrationTests.java index faa057428..145801cd3 100644 --- a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/async/TraceAsyncIntegrationTests.java +++ b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/async/TraceAsyncIntegrationTests.java @@ -127,8 +127,8 @@ public class TraceAsyncIntegrationTests { .getSpans(); zipkin2.Span reportedAsyncSpan = spans.stream() .filter(span2 -> span2.name().equals("invoke-asynchronous-logic")) - .findFirst() - .orElseThrow(() -> new AssertionError("Should have a span with custom name")); + .findFirst().orElseThrow(() -> new AssertionError( + "Should have a span with custom name")); then(reportedAsyncSpan.traceId()).isEqualTo(span.context().traceIdString()); then(reportedAsyncSpan.name()).isEqualTo("invoke-asynchronous-logic"); then(reportedAsyncSpan.tags()) @@ -146,8 +146,8 @@ public class TraceAsyncIntegrationTests { then(spans).hasSize(2); zipkin2.Span reportedAsyncSpan = spans.stream() .filter(span -> span.name().equals("invoke-asynchronous-logic")) - .findFirst() - .orElseThrow(() -> new AssertionError("Should have a span with custom name")); + .findFirst().orElseThrow(() -> new AssertionError( + "Should have a span with custom name")); then(reportedAsyncSpan.tags()) .contains(new AbstractMap.SimpleEntry<>("class", "ClassPerformingAsyncLogic")) @@ -166,9 +166,9 @@ public class TraceAsyncIntegrationTests { .getSpans(); then(spans).hasSize(2); zipkin2.Span reportedAsyncSpan = spans.stream() - .filter(span2 -> span2.name().equals("foo")) - .findFirst() - .orElseThrow(() -> new AssertionError("Should have a span with custom name")); + .filter(span2 -> span2.name().equals("foo")).findFirst() + .orElseThrow(() -> new AssertionError( + "Should have a span with custom name")); then(reportedAsyncSpan.traceId()).isEqualTo(span.context().traceIdString()); then(reportedAsyncSpan.name()).isEqualTo("foo"); then(reportedAsyncSpan.tags()) @@ -184,9 +184,9 @@ public class TraceAsyncIntegrationTests { List spans = TraceAsyncIntegrationTests.this.reporter .getSpans(); zipkin2.Span reportedAsyncSpan = spans.stream() - .filter(span2 -> span2.name().equals("foo")) - .findFirst() - .orElseThrow(() -> new AssertionError("Should have a span with custom name")); + .filter(span2 -> span2.name().equals("foo")).findFirst() + .orElseThrow(() -> new AssertionError( + "Should have a span with custom name")); then(reportedAsyncSpan.name()).isEqualTo("foo"); then(reportedAsyncSpan.tags()) .contains(new AbstractMap.SimpleEntry<>("class", diff --git a/src/checkstyle/checkstyle-suppressions.xml b/src/checkstyle/checkstyle-suppressions.xml index 16eb7bc54..c526753f2 100644 --- a/src/checkstyle/checkstyle-suppressions.xml +++ b/src/checkstyle/checkstyle-suppressions.xml @@ -28,5 +28,6 @@ +