Adding tags for spans created via annotations (#691)

without this change we don't know which class or method (if the SpanCreator got overridden) was annotated
with this change we're adding the tags with this info

fixes #690
This commit is contained in:
Marcin Grzejszczak
2017-09-12 13:37:01 +02:00
committed by GitHub
parent cdace454c8
commit 5bb444f042
2 changed files with 13 additions and 3 deletions

View File

@@ -20,14 +20,12 @@ import java.lang.annotation.Annotation;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Method;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.annotation.PostConstruct;
import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.IntroductionInterceptor;
import org.springframework.aop.Pointcut;
@@ -173,6 +171,8 @@ class SleuthAdvisorConfig extends AbstractPointcutAdvisor implements BeanFactor
class SleuthInterceptor implements IntroductionInterceptor, BeanFactoryAware {
private static final Log logger = LogFactory.getLog(MethodHandles.lookup().lookupClass());
private static final String CLASS_KEY = "class";
private static final String METHOD_KEY = "method";
private BeanFactory beanFactory;
private SpanCreator spanCreator;
@@ -204,6 +204,7 @@ class SleuthInterceptor implements IntroductionInterceptor, BeanFactoryAware {
logEvent(span, log + ".before");
}
spanTagAnnotationHandler().addAnnotatedParameters(invocation);
addTags(invocation, span);
return invocation.proceed();
} catch (Exception e) {
if (logger.isDebugEnabled()) {
@@ -226,6 +227,11 @@ class SleuthInterceptor implements IntroductionInterceptor, BeanFactoryAware {
}
}
private void addTags(MethodInvocation invocation, Span span) {
tracer().addTag(CLASS_KEY, invocation.getThis().getClass().getSimpleName());
tracer().addTag(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 "

View File

@@ -129,7 +129,9 @@ public class SleuthSpanCreatorAspectTests {
List<Span> spans = new ArrayList<>(this.accumulator.getSpans());
then(new ListOfSpans(spans)).hasSize(1)
.hasASpanWithName("custom-name-on-test-method9");
.hasASpanWithName("custom-name-on-test-method9")
.hasASpanWithTagEqualTo("class", "TestBean")
.hasASpanWithTagEqualTo("method", "testMethod9");
then(ExceptionUtils.getLastException()).isNull();
}
@@ -162,6 +164,8 @@ public class SleuthSpanCreatorAspectTests {
then(new ListOfSpans(spans)).hasSize(1)
.hasASpanWithName("foo")
.hasASpanWithTagEqualTo("customTestTag11", "test")
.hasASpanWithTagEqualTo("class", "TestBean")
.hasASpanWithTagEqualTo("method", "testMethod11")
.hasASpanWithLogEqualTo("customTest.before")
.hasASpanWithLogEqualTo("customTest.after");
then(ExceptionUtils.getLastException()).isNull();