From 1043fd83c158e4817241b6cc981e4ed8b00c0ad7 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Mon, 7 Mar 2016 09:48:58 +0100 Subject: [PATCH] Added missing Span tags for @Scheduled methods --- .../scheduling/TraceSchedulingAspect.java | 13 ++++++-- .../TraceSchedulingAutoConfiguration.java | 7 +++-- .../scheduling/TracingOnScheduledTests.java | 30 +++++++++++++++++-- 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/scheduling/TraceSchedulingAspect.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/scheduling/TraceSchedulingAspect.java index 399cae7cf..08f5ad422 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/scheduling/TraceSchedulingAspect.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/scheduling/TraceSchedulingAspect.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2015 the original author or authors. + * Copyright 2013-2016 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. @@ -20,6 +20,7 @@ import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.cloud.sleuth.Span; +import org.springframework.cloud.sleuth.TraceKeys; import org.springframework.cloud.sleuth.Tracer; /** @@ -43,16 +44,22 @@ public class TraceSchedulingAspect { private static final String SCHEDULED_COMPONENT = "scheduled"; private final Tracer tracer; + private final TraceKeys traceKeys; - public TraceSchedulingAspect(Tracer tracer) { + public TraceSchedulingAspect(Tracer tracer, TraceKeys traceKeys) { this.tracer = tracer; + this.traceKeys = traceKeys; } @Around("execution (@org.springframework.scheduling.annotation.Scheduled * *.*(..))") public Object traceBackgroundThread(final ProceedingJoinPoint pjp) throws Throwable { - String spanName = pjp.getTarget().getClass().getSimpleName(); + String spanName = pjp.getSignature().getName(); Span span = this.tracer.createSpan(spanName); this.tracer.addTag(Span.SPAN_LOCAL_COMPONENT_TAG_NAME, SCHEDULED_COMPONENT); + this.tracer.addTag(this.traceKeys.getAsync().getPrefix() + + this.traceKeys.getAsync().getClassNameKey(), pjp.getTarget().getClass().getSimpleName()); + this.tracer.addTag(this.traceKeys.getAsync().getPrefix() + + this.traceKeys.getAsync().getMethodNameKey(), pjp.getSignature().getName()); try { return pjp.proceed(); } diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/scheduling/TraceSchedulingAutoConfiguration.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/scheduling/TraceSchedulingAutoConfiguration.java index b72e4dd44..f2c5e7272 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/scheduling/TraceSchedulingAutoConfiguration.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/scheduling/TraceSchedulingAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2015 the original author or authors. + * Copyright 2013-2016 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. @@ -25,6 +25,7 @@ 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.ConditionalOnProperty; +import org.springframework.cloud.sleuth.TraceKeys; import org.springframework.cloud.sleuth.Tracer; import org.springframework.cloud.sleuth.autoconfig.TraceAutoConfiguration; import org.springframework.context.annotation.Bean; @@ -49,8 +50,8 @@ public class TraceSchedulingAutoConfiguration { @ConditionalOnClass(ProceedingJoinPoint.class) @Bean - public TraceSchedulingAspect traceSchedulingAspect(Tracer tracer) { - return new TraceSchedulingAspect(tracer); + public TraceSchedulingAspect traceSchedulingAspect(Tracer tracer, TraceKeys traceKeys) { + return new TraceSchedulingAspect(tracer, traceKeys); } } diff --git a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/scheduling/TracingOnScheduledTests.java b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/scheduling/TracingOnScheduledTests.java index 8e49bc084..de7606bad 100644 --- a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/scheduling/TracingOnScheduledTests.java +++ b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/scheduling/TracingOnScheduledTests.java @@ -1,7 +1,20 @@ -package org.springframework.cloud.sleuth.instrument.scheduling; +/* + * Copyright 2013-2016 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. + */ -import static com.jayway.awaitility.Awaitility.await; -import static org.assertj.core.api.BDDAssertions.then; +package org.springframework.cloud.sleuth.instrument.scheduling; import org.junit.Test; import org.junit.runner.RunWith; @@ -9,12 +22,16 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.cloud.sleuth.Span; import org.springframework.cloud.sleuth.instrument.DefaultTestAutoConfiguration; +import org.springframework.cloud.sleuth.sampler.AlwaysSampler; import org.springframework.cloud.sleuth.trace.TestSpanContextHolder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import static com.jayway.awaitility.Awaitility.await; +import static org.springframework.cloud.sleuth.assertions.SleuthAssertions.then; + @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = {ScheduledTestConfiguration.class}) public class TracingOnScheduledTests { @@ -39,6 +56,8 @@ public class TracingOnScheduledTests { Span storedSpan = TracingOnScheduledTests.this.beanWithScheduledMethod.getSpan(); then(storedSpan).isNotNull(); then(storedSpan.getTraceId()).isNotNull(); + then(storedSpan).hasATag("class", "TestBeanWithScheduledMethod"); + then(storedSpan).hasATag("method", "scheduledMethod"); } }; } @@ -62,6 +81,11 @@ class ScheduledTestConfiguration { return new TestBeanWithScheduledMethod(); } + @Bean + AlwaysSampler alwaysSampler() { + return new AlwaysSampler(); + } + } class TestBeanWithScheduledMethod {