Merge branch 'optimize-reactor-hooks' of https://github.com/robotmrv/spring-cloud-sleuth into robotmrv-optimize-reactor-hooks

This commit is contained in:
Marcin Grzejszczak
2021-02-19 10:59:17 +01:00
18 changed files with 1288 additions and 55 deletions

View File

@@ -89,9 +89,7 @@ public class SleuthReactorProperties {
/**
* Decorates on each operator, will be less performing, but logging will always
* contain the tracing entries in each operator.
* @deprecated to be removed in Sleuth 4.0.0
*/
@Deprecated
DECORATE_ON_EACH,
/**

View File

@@ -48,7 +48,9 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import static org.springframework.cloud.sleuth.autoconfig.instrument.reactor.TraceReactorAutoConfiguration.SLEUTH_REACTOR_EXECUTOR_SERVICE_KEY;
import static org.springframework.cloud.sleuth.autoconfig.instrument.reactor.TraceReactorAutoConfiguration.TraceReactorConfiguration.SLEUTH_TRACE_REACTOR_KEY;
import static org.springframework.cloud.sleuth.instrument.reactor.ReactorSleuth.onLastOperatorForOnEachInstrumentation;
/**
* {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration
@@ -125,12 +127,18 @@ class HooksRefresher implements ApplicationListener<RefreshScopeRefreshedEvent>
}
Hooks.resetOnEachOperator(SLEUTH_TRACE_REACTOR_KEY);
Hooks.resetOnLastOperator(SLEUTH_TRACE_REACTOR_KEY);
Hooks.resetOnLastOperator(SLEUTH_REACTOR_EXECUTOR_SERVICE_KEY);
switch (this.reactorProperties.getInstrumentationType()) {
case DECORATE_ON_EACH:
if (log.isTraceEnabled()) {
log.trace("Decorating onEach operator instrumentation");
}
Hooks.onEachOperator(SLEUTH_TRACE_REACTOR_KEY, ReactorSleuth.scopePassingSpanOperator(this.context));
Hooks.onEachOperator(SLEUTH_TRACE_REACTOR_KEY,
ReactorSleuth.onEachOperatorForOnEachInstrumentation(this.context));
Hooks.onLastOperator(SLEUTH_TRACE_REACTOR_KEY,
ReactorSleuth.onLastOperatorForOnEachInstrumentation(this.context));
Schedulers.onScheduleHook(TraceReactorAutoConfiguration.SLEUTH_REACTOR_EXECUTOR_SERVICE_KEY,
ReactorSleuth.scopePassingOnScheduleHook(this.context));
break;
case DECORATE_ON_LAST:
if (log.isTraceEnabled()) {
@@ -178,15 +186,23 @@ class HookRegisteringBeanDefinitionRegistryPostProcessor implements BeanDefiniti
}
else if (property == SleuthReactorProperties.InstrumentationType.DECORATE_ON_EACH) {
decorateOnEach(springContext);
decorateOnLast(onLastOperatorForOnEachInstrumentation(springContext));
decorateScheduler(springContext);
}
else if (property == SleuthReactorProperties.InstrumentationType.DECORATE_ON_LAST) {
decorateOnLast(ReactorSleuth.scopePassingSpanOperator(springContext));
decorateScheduler(springContext);
}
else if (property == SleuthReactorProperties.InstrumentationType.MANUAL) {
decorateOnLast(ReactorSleuth.springContextSpanOperator(springContext));
}
}
private static void decorateScheduler(ConfigurableApplicationContext springContext) {
Schedulers.onScheduleHook(TraceReactorAutoConfiguration.SLEUTH_REACTOR_EXECUTOR_SERVICE_KEY,
ReactorSleuth.scopePassingOnScheduleHook(springContext));
}
private static void decorateOnLast(Function<? super Publisher<Object>, ? extends Publisher<Object>> function) {
if (log.isTraceEnabled()) {
log.trace("Decorating onLast operator instrumentation");
@@ -198,7 +214,8 @@ class HookRegisteringBeanDefinitionRegistryPostProcessor implements BeanDefiniti
if (log.isTraceEnabled()) {
log.trace("Decorating onEach operator instrumentation");
}
Hooks.onEachOperator(SLEUTH_TRACE_REACTOR_KEY, ReactorSleuth.scopePassingSpanOperator(springContext));
Hooks.onEachOperator(SLEUTH_TRACE_REACTOR_KEY,
ReactorSleuth.onEachOperatorForOnEachInstrumentation(springContext));
}
@Override
@@ -208,7 +225,7 @@ class HookRegisteringBeanDefinitionRegistryPostProcessor implements BeanDefiniti
}
Hooks.resetOnEachOperator(SLEUTH_TRACE_REACTOR_KEY);
Hooks.resetOnLastOperator(SLEUTH_TRACE_REACTOR_KEY);
Schedulers.removeExecutorServiceDecorator(TraceReactorAutoConfiguration.SLEUTH_REACTOR_EXECUTOR_SERVICE_KEY);
Schedulers.resetOnScheduleHook(TraceReactorAutoConfiguration.SLEUTH_REACTOR_EXECUTOR_SERVICE_KEY);
}
}

View File

@@ -43,7 +43,7 @@ public final class TraceReactorAutoConfigurationAccessorConfiguration {
}
Hooks.resetOnEachOperator(SLEUTH_TRACE_REACTOR_KEY);
Hooks.resetOnLastOperator(SLEUTH_TRACE_REACTOR_KEY);
Schedulers.removeExecutorServiceDecorator(SLEUTH_REACTOR_EXECUTOR_SERVICE_KEY);
Schedulers.resetOnScheduleHook(SLEUTH_REACTOR_EXECUTOR_SERVICE_KEY);
}
public static void setup(ConfigurableApplicationContext context) {

View File

@@ -18,6 +18,7 @@ package org.springframework.cloud.sleuth.autoconfig.instrument.web;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.cloud.sleuth.CurrentTraceContext;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.cloud.sleuth.http.HttpServerHandler;
import org.springframework.cloud.sleuth.instrument.web.TraceWebFilter;
@@ -38,8 +39,8 @@ class TraceWebFluxConfiguration {
@Bean
TraceWebFilter traceFilter(Tracer tracer, HttpServerHandler httpServerHandler,
SleuthWebProperties sleuthWebProperties) {
TraceWebFilter traceWebFilter = new TraceWebFilter(tracer, httpServerHandler);
CurrentTraceContext currentTraceContext, SleuthWebProperties sleuthWebProperties) {
TraceWebFilter traceWebFilter = new TraceWebFilter(tracer, httpServerHandler, currentTraceContext);
traceWebFilter.setOrder(sleuthWebProperties.getFilterOrder());
return traceWebFilter;
}