Added condition on presence of WebFlux

without this change we have a ClassNotFoundException when reactor dependency is not passed
with this change, we don't require anyone to actually have that dependency set. What we do is we conditionally create a bean of either reactor or nonreactor type, depending on the presence of the Flux class on the classpath

fixes gh-1074
This commit is contained in:
Marcin Grzejszczak
2018-08-31 13:15:12 +02:00
parent fd038320e1
commit b13f15f9d7
7 changed files with 351 additions and 195 deletions

View File

@@ -0,0 +1,117 @@
/*
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.sleuth.annotation;
import brave.Span;
import brave.Tracer;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
/**
* @author Marcin Grzejszczak
*/
abstract class AbstractSleuthMethodInvocationProcessor
implements SleuthMethodInvocationProcessor, BeanFactoryAware {
private static final Log logger = LogFactory
.getLog(AbstractSleuthMethodInvocationProcessor.class);
private static final String CLASS_KEY = "class";
private static final String METHOD_KEY = "method";
BeanFactory beanFactory;
private NewSpanParser newSpanParser;
private Tracer tracer;
private SpanTagAnnotationHandler spanTagAnnotationHandler;
void before(MethodInvocation invocation, Span span, String log, boolean hasLog) {
if (hasLog) {
logEvent(span, log + ".before");
}
spanTagAnnotationHandler().addAnnotatedParameters(invocation);
addTags(invocation, span);
}
void after(Span span, boolean isNewSpan, String log, boolean hasLog) {
if (hasLog) {
logEvent(span, log + ".after");
}
if (isNewSpan) {
span.finish();
}
}
void onFailure(Span span, String log, boolean hasLog, Throwable e) {
if (logger.isDebugEnabled()) {
logger.debug("Exception occurred while trying to continue the pointcut", e);
}
if (hasLog) {
logEvent(span, log + ".afterFailure");
}
span.error(e);
}
void addTags(MethodInvocation invocation, Span span) {
span.tag(CLASS_KEY, invocation.getThis().getClass().getSimpleName());
span.tag(METHOD_KEY, invocation.getMethod().getName());
}
void logEvent(Span span, String name) {
if (span == null) {
logger.warn("You were trying to continue a span which was null. Please "
+ "remember that if two proxied methods are calling each other from "
+ "the same class then the aspect will not be properly resolved");
return;
}
span.annotate(name);
}
String log(ContinueSpan continueSpan) {
if (continueSpan != null) {
return continueSpan.log();
}
return "";
}
Tracer tracer() {
if (this.tracer == null) {
this.tracer = this.beanFactory.getBean(Tracer.class);
}
return this.tracer;
}
NewSpanParser newSpanParser() {
if (this.newSpanParser == null) {
this.newSpanParser = this.beanFactory.getBean(NewSpanParser.class);
}
return this.newSpanParser;
}
SpanTagAnnotationHandler spanTagAnnotationHandler() {
if (this.spanTagAnnotationHandler == null) {
this.spanTagAnnotationHandler = new SpanTagAnnotationHandler(
this.beanFactory);
}
return this.spanTagAnnotationHandler;
}
@Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.sleuth.annotation;
import brave.Span;
import brave.Tracer;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.util.StringUtils;
/**
* @author Marcin Grzejszczak
* @since 2.1.0
*/
class NonReactorSleuthMethodInvocationProcessor extends AbstractSleuthMethodInvocationProcessor {
@Override public Object process(MethodInvocation invocation, NewSpan newSpan,
ContinueSpan continueSpan) throws Throwable {
return proceedUnderSynchronousSpan(invocation, newSpan, continueSpan);
}
private Object proceedUnderSynchronousSpan(
MethodInvocation invocation, NewSpan newSpan, ContinueSpan continueSpan) throws Throwable {
Span span = tracer().currentSpan();
//in case of @ContinueSpan and no span in tracer we start new span and should close it on completion
boolean startNewSpan = newSpan != null || span == null;
if (startNewSpan) {
span = tracer().nextSpan();
newSpanParser().parse(invocation, newSpan, span);
span.start();
}
String log = log(continueSpan);
boolean hasLog = StringUtils.hasText(log);
try (Tracer.SpanInScope ws = tracer().withSpanInScope(span)) {
before(invocation, span, log, hasLog);
return invocation.proceed();
} catch (Exception e) {
onFailure(span, log, hasLog, e);
throw e;
} finally {
after(span, startNewSpan, log, hasLog);
}
}
}

View File

@@ -0,0 +1,127 @@
/*
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.sleuth.annotation;
import java.lang.reflect.Method;
import java.util.function.Consumer;
import java.util.function.Supplier;
import brave.Span;
import brave.Tracer;
import org.aopalliance.intercept.MethodInvocation;
import org.reactivestreams.Publisher;
import org.springframework.util.StringUtils;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.publisher.SignalType;
/**
* @author Marcin Grzejszczak
* @since 2.1.0
*/
class ReactorSleuthMethodInvocationProcessor extends AbstractSleuthMethodInvocationProcessor {
private NonReactorSleuthMethodInvocationProcessor nonReactorSleuthMethodInvocationProcessor;
@Override public Object process(MethodInvocation invocation, NewSpan newSpan,
ContinueSpan continueSpan) throws Throwable {
Method method = invocation.getMethod();
if(isReactorReturnType(method.getReturnType())){
return proceedUnderReactorSpan(invocation, newSpan, continueSpan);
} else {
return nonReactorSleuthMethodInvocationProcessor()
.process(invocation, newSpan, continueSpan);
}
}
private Object proceedUnderReactorSpan(
MethodInvocation invocation, NewSpan newSpan, ContinueSpan continueSpan) throws Throwable{
Span spanPrevious = tracer().currentSpan();
//in case of @ContinueSpan and no span in tracer we start new span and should close it on completion
boolean startNewSpan = newSpan != null || spanPrevious == null;
Span span;
if (startNewSpan) {
span = tracer().nextSpan();
newSpanParser().parse(invocation, newSpan, span);
} else {
span = spanPrevious;
}
String log = log(continueSpan);
boolean hasLog = StringUtils.hasText(log);
try(Tracer.SpanInScope ws = tracer().withSpanInScope(span)) {
Publisher<?> publisher = (Publisher) invocation.proceed();
Mono<Span> startSpan = Mono.defer(() -> withSpanInScope(span, () -> {
if (startNewSpan) {
span.start();
}
before(invocation, span, log, hasLog);
return Mono.just(span);
}));
if(publisher instanceof Mono){
return startSpan.flatMap(spanStarted -> ((Mono<?>)publisher)
.doOnError(onFailureReactor(log, hasLog, spanStarted))
.doFinally(afterReactor(startNewSpan, log, hasLog, spanStarted)))
//put span in context so it can be used by ScopePassingSpanSubscriber
.subscriberContext(context -> context.put(Span.class, span));
}
else if(publisher instanceof Flux){
return startSpan.flatMapMany(spanStarted -> ((Flux<?>)publisher)
.doOnError(onFailureReactor(log, hasLog, spanStarted))
.doFinally(afterReactor(startNewSpan, log, hasLog, spanStarted)))
//put span in context so it can be used by ScopePassingSpanSubscriber
.subscriberContext(context -> context.put(Span.class, span));
}
else {
throw new IllegalArgumentException("Unexpected type of publisher: "+publisher.getClass());
}
}
}
private <T> T withSpanInScope(Span span, Supplier<T> supplier) {
try(Tracer.SpanInScope ws1 = tracer().withSpanInScope(span)) {
return supplier.get();
}
}
private Consumer<SignalType> afterReactor(boolean isNewSpan, String log, boolean hasLog, Span span) {
return signalType -> {
try(Tracer.SpanInScope ws = tracer().withSpanInScope(span)) {
after(span, isNewSpan, log, hasLog);
}
};
}
private Consumer<Throwable> onFailureReactor(String log, boolean hasLog, Span span) {
return throwable -> {
try(Tracer.SpanInScope ws = tracer().withSpanInScope(span)) {
onFailure(span, log, hasLog, throwable);
}
};
}
private boolean isReactorReturnType(Class<?> returnType) {
return Flux.class.equals(returnType) || Mono.class.equals(returnType);
}
private NonReactorSleuthMethodInvocationProcessor nonReactorSleuthMethodInvocationProcessor() {
if (this.nonReactorSleuthMethodInvocationProcessor == null) {
this.nonReactorSleuthMethodInvocationProcessor = new NonReactorSleuthMethodInvocationProcessor();
this.nonReactorSleuthMethodInvocationProcessor.setBeanFactory(this.beanFactory);
}
return this.nonReactorSleuthMethodInvocationProcessor;
}
}

View File

@@ -19,17 +19,10 @@ package org.springframework.cloud.sleuth.annotation;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.function.Supplier;
import javax.annotation.PostConstruct;
import brave.Span;
import brave.Tracer;
import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.reactivestreams.Publisher;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.IntroductionInterceptor;
import org.springframework.aop.Pointcut;
@@ -42,10 +35,6 @@ import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.publisher.SignalType;
/**
* Custom pointcut advisor that picks all classes / interfaces that
@@ -170,14 +159,8 @@ class SleuthAdvisorConfig extends AbstractPointcutAdvisor implements BeanFactory
*/
class SleuthInterceptor implements IntroductionInterceptor, BeanFactoryAware {
private static final Log logger = LogFactory.getLog(SleuthInterceptor.class);
private static final String CLASS_KEY = "class";
private static final String METHOD_KEY = "method";
private BeanFactory beanFactory;
private NewSpanParser newSpanParser;
private Tracer tracer;
private SpanTagAnnotationHandler spanTagAnnotationHandler;
private SleuthMethodInvocationProcessor methodInvocationProcessor;
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
@@ -185,7 +168,6 @@ class SleuthInterceptor implements IntroductionInterceptor, BeanFactoryAware {
if (method == null) {
return invocation.proceed();
}
Method mostSpecificMethod = AopUtils
.getMostSpecificMethod(method, invocation.getThis().getClass());
NewSpan newSpan = SleuthAnnotationUtils.findAnnotation(mostSpecificMethod, NewSpan.class);
@@ -193,180 +175,14 @@ class SleuthInterceptor implements IntroductionInterceptor, BeanFactoryAware {
if (newSpan == null && continueSpan == null) {
return invocation.proceed();
}
if(isReactorReturnType(method.getReturnType())){
return proceedUnderReactorSpan(invocation, newSpan, continueSpan);
} else {
return proceedUnderSynchronousSpan(invocation, newSpan, continueSpan);
}
return methodInvocationProcessor().process(invocation, newSpan, continueSpan);
}
private boolean isReactorReturnType(Class<?> returnType) {
return Flux.class.equals(returnType) || Mono.class.equals(returnType);
}
private Object proceedUnderSynchronousSpan(
MethodInvocation invocation, NewSpan newSpan, ContinueSpan continueSpan) throws Throwable {
Span span = tracer().currentSpan();
//in case of @ContinueSpan and no span in tracer we start new span and should close it on completion
boolean startNewSpan = newSpan != null || span == null;
if (startNewSpan) {
span = tracer().nextSpan();
newSpanParser().parse(invocation, newSpan, span);
span.start();
private SleuthMethodInvocationProcessor methodInvocationProcessor() {
if (this.methodInvocationProcessor == null) {
this.methodInvocationProcessor = this.beanFactory.getBean(SleuthMethodInvocationProcessor.class);
}
String log = log(continueSpan);
boolean hasLog = StringUtils.hasText(log);
try (Tracer.SpanInScope ws = tracer().withSpanInScope(span)) {
before(invocation, span, log, hasLog);
return invocation.proceed();
} catch (Exception e) {
onFailure(span, log, hasLog, e);
throw e;
} finally {
after(span, startNewSpan, log, hasLog);
}
}
private Object proceedUnderReactorSpan(
MethodInvocation invocation, NewSpan newSpan, ContinueSpan continueSpan) throws Throwable{
Span spanPrevious = tracer().currentSpan();
//in case of @ContinueSpan and no span in tracer we start new span and should close it on completion
boolean startNewSpan = newSpan != null || spanPrevious == null;
Span span;
if (startNewSpan) {
span = tracer().nextSpan();
newSpanParser().parse(invocation, newSpan, span);
} else {
span = spanPrevious;
}
String log = log(continueSpan);
boolean hasLog = StringUtils.hasText(log);
try(Tracer.SpanInScope ws = tracer().withSpanInScope(span)) {
Publisher<?> publisher = (Publisher) invocation.proceed();
Mono<Span> startSpan = Mono.defer(() -> withSpanInScope(span, () -> {
if (startNewSpan) {
span.start();
}
before(invocation, span, log, hasLog);
return Mono.just(span);
}));
if(publisher instanceof Mono){
return startSpan.flatMap(spanStarted -> ((Mono<?>)publisher)
.doOnError(onFailureReactor(log, hasLog, spanStarted))
.doFinally(afterReactor(startNewSpan, log, hasLog, spanStarted)))
//put span in context so it can be used by ScopePassingSpanSubscriber
.subscriberContext(context -> context.put(Span.class, span));
}
else if(publisher instanceof Flux){
return startSpan.flatMapMany(spanStarted -> ((Flux<?>)publisher)
.doOnError(onFailureReactor(log, hasLog, spanStarted))
.doFinally(afterReactor(startNewSpan, log, hasLog, spanStarted)))
//put span in context so it can be used by ScopePassingSpanSubscriber
.subscriberContext(context -> context.put(Span.class, span));
}
else {
throw new IllegalArgumentException("Unexpected type of publisher: "+publisher.getClass());
}
}
}
private <T> T withSpanInScope(Span span, Supplier<T> supplier) {
try(Tracer.SpanInScope ws1 = tracer().withSpanInScope(span)) {
return supplier.get();
}
}
private Consumer<SignalType> afterReactor(boolean isNewSpan, String log, boolean hasLog, Span span) {
return signalType -> {
try(Tracer.SpanInScope ws = tracer().withSpanInScope(span)) {
after(span, isNewSpan, log, hasLog);
}
};
}
private Consumer<Throwable> onFailureReactor(String log, boolean hasLog, Span span) {
return throwable -> {
try(Tracer.SpanInScope ws = tracer().withSpanInScope(span)) {
onFailure(span, log, hasLog, throwable);
}
};
}
private void before(MethodInvocation invocation, Span span, String log, boolean hasLog) {
if (hasLog) {
logEvent(span, log + ".before");
}
spanTagAnnotationHandler().addAnnotatedParameters(invocation);
addTags(invocation, span);
}
private void after(Span span, boolean isNewSpan, String log, boolean hasLog) {
if (hasLog) {
logEvent(span, log + ".after");
}
if (isNewSpan) {
span.finish();
}
}
private void onFailure(Span span, String log, boolean hasLog, Throwable e) {
if (logger.isDebugEnabled()) {
logger.debug("Exception occurred while trying to continue the pointcut", e);
}
if (hasLog) {
logEvent(span, log + ".afterFailure");
}
span.error(e);
}
private void addTags(MethodInvocation invocation, Span span) {
span.tag(CLASS_KEY, invocation.getThis().getClass().getSimpleName());
span.tag(METHOD_KEY, invocation.getMethod().getName());
}
private void logEvent(Span span, String name) {
if (span == null) {
logger.warn("You were trying to continue a span which was null. Please "
+ "remember that if two proxied methods are calling each other from "
+ "the same class then the aspect will not be properly resolved");
return;
}
span.annotate(name);
}
private String log(ContinueSpan continueSpan) {
if (continueSpan != null) {
return continueSpan.log();
}
return "";
}
private Tracer tracer() {
if (this.tracer == null) {
this.tracer = this.beanFactory.getBean(Tracer.class);
}
return this.tracer;
}
private NewSpanParser newSpanParser() {
if (this.newSpanParser == null) {
this.newSpanParser = this.beanFactory.getBean(NewSpanParser.class);
}
return this.newSpanParser;
}
private SpanTagAnnotationHandler spanTagAnnotationHandler() {
if (this.spanTagAnnotationHandler == null) {
this.spanTagAnnotationHandler = new SpanTagAnnotationHandler(this.beanFactory);
}
return this.spanTagAnnotationHandler;
return this.methodInvocationProcessor;
}
@Override public boolean implementsInterface(Class<?> intf) {

View File

@@ -19,7 +19,9 @@ import brave.Tracing;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.sleuth.autoconfig.TraceAutoConfiguration;
@@ -65,4 +67,18 @@ public class SleuthAnnotationAutoConfiguration {
return new SleuthAdvisorConfig();
}
@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@ConditionalOnClass(name = "reactor.core.publisher.Flux")
SleuthMethodInvocationProcessor reactorSleuthMethodInvocationProcessor() {
return new ReactorSleuthMethodInvocationProcessor();
}
@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@ConditionalOnMissingClass("reactor.core.publisher.Flux")
SleuthMethodInvocationProcessor nonReactorSleuthMethodInvocationProcessor() {
return new NonReactorSleuthMethodInvocationProcessor();
}
}

View File

@@ -0,0 +1,28 @@
/*
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.sleuth.annotation;
import org.aopalliance.intercept.MethodInvocation;
/**
* @author Marcin Grzejszczak
* @since 2.1.0
*/
interface SleuthMethodInvocationProcessor {
Object process(MethodInvocation invocation, NewSpan newSpan, ContinueSpan continueSpan) throws Throwable;
}

View File

@@ -161,11 +161,6 @@
<artifactId>reactor-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.reactivestreams</groupId>
<artifactId>reactive-streams</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>