From f88dabedeeea7d96d8affe5277a679f7260f7210 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Wed, 18 Mar 2020 15:17:33 -0700 Subject: [PATCH] Better span names for spring-scheduling (#244) --- .../SpringSchedulingDecorator.java | 21 ++++++------------- .../SpringSchedulingInstrumentation.java | 17 +++++++-------- .../test/groovy/SpringSchedulingTest.groovy | 7 ++----- 3 files changed, 16 insertions(+), 29 deletions(-) diff --git a/instrumentation/spring-scheduling-3.1/src/main/java/io/opentelemetry/auto/instrumentation/springscheduling/SpringSchedulingDecorator.java b/instrumentation/spring-scheduling-3.1/src/main/java/io/opentelemetry/auto/instrumentation/springscheduling/SpringSchedulingDecorator.java index fe6b57d94b..f4d17f52ea 100644 --- a/instrumentation/spring-scheduling-3.1/src/main/java/io/opentelemetry/auto/instrumentation/springscheduling/SpringSchedulingDecorator.java +++ b/instrumentation/spring-scheduling-3.1/src/main/java/io/opentelemetry/auto/instrumentation/springscheduling/SpringSchedulingDecorator.java @@ -17,8 +17,6 @@ package io.opentelemetry.auto.instrumentation.springscheduling; import io.opentelemetry.OpenTelemetry; import io.opentelemetry.auto.bootstrap.instrumentation.decorator.BaseDecorator; -import io.opentelemetry.auto.instrumentation.api.MoreTags; -import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Tracer; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.support.ScheduledMethodRunnable; @@ -42,19 +40,12 @@ public class SpringSchedulingDecorator extends BaseDecorator { return "spring-scheduling"; } - public Span onRun(final Span span, final Runnable runnable) { - if (runnable != null) { - String resourceName = ""; - if (runnable instanceof ScheduledMethodRunnable) { - final ScheduledMethodRunnable scheduledMethodRunnable = (ScheduledMethodRunnable) runnable; - resourceName = spanNameForMethod(scheduledMethodRunnable.getMethod()); - } else { - final String className = spanNameForClass(runnable.getClass()); - final String methodName = "run"; - resourceName = className + "." + methodName; - } - span.setAttribute(MoreTags.RESOURCE_NAME, resourceName); + public String spanNameOnRun(final Runnable runnable) { + if (runnable instanceof ScheduledMethodRunnable) { + final ScheduledMethodRunnable scheduledMethodRunnable = (ScheduledMethodRunnable) runnable; + return spanNameForMethod(scheduledMethodRunnable.getMethod()); + } else { + return spanNameForClass(runnable.getClass()) + "/run"; } - return span; } } diff --git a/instrumentation/spring-scheduling-3.1/src/main/java/io/opentelemetry/auto/instrumentation/springscheduling/SpringSchedulingInstrumentation.java b/instrumentation/spring-scheduling-3.1/src/main/java/io/opentelemetry/auto/instrumentation/springscheduling/SpringSchedulingInstrumentation.java index 630c55abf3..c1d6d6cda5 100644 --- a/instrumentation/spring-scheduling-3.1/src/main/java/io/opentelemetry/auto/instrumentation/springscheduling/SpringSchedulingInstrumentation.java +++ b/instrumentation/spring-scheduling-3.1/src/main/java/io/opentelemetry/auto/instrumentation/springscheduling/SpringSchedulingInstrumentation.java @@ -75,18 +75,17 @@ public final class SpringSchedulingInstrumentation extends Instrumenter.Default @Override public void run() { - final Span span = TRACER.spanBuilder("scheduled.call").startSpan(); + if (runnable == null) { + return; + } + final Span span = TRACER.spanBuilder(DECORATE.spanNameOnRun(runnable)).startSpan(); DECORATE.afterStart(span); try (final Scope scope = TRACER.withSpan(span)) { - DECORATE.onRun(span, runnable); - - try { - runnable.run(); - } catch (final Throwable throwable) { - DECORATE.onError(span, throwable); - throw throwable; - } + runnable.run(); + } catch (final Throwable throwable) { + DECORATE.onError(span, throwable); + throw throwable; } finally { DECORATE.beforeFinish(span); span.end(); diff --git a/instrumentation/spring-scheduling-3.1/src/test/groovy/SpringSchedulingTest.groovy b/instrumentation/spring-scheduling-3.1/src/test/groovy/SpringSchedulingTest.groovy index 2c185545fd..7c92d24c4a 100644 --- a/instrumentation/spring-scheduling-3.1/src/test/groovy/SpringSchedulingTest.groovy +++ b/instrumentation/spring-scheduling-3.1/src/test/groovy/SpringSchedulingTest.groovy @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import io.opentelemetry.auto.instrumentation.api.MoreTags import io.opentelemetry.auto.instrumentation.api.Tags import io.opentelemetry.auto.test.AgentTestRunner import org.springframework.context.annotation.AnnotationConfigApplicationContext @@ -32,11 +31,10 @@ class SpringSchedulingTest extends AgentTestRunner { assertTraces(1) { trace(0, 1) { span(0) { - operationName "scheduled.call" + operationName "TriggerTask.run" parent() errored false tags { - "$MoreTags.RESOURCE_NAME" "TriggerTask.run" "$Tags.COMPONENT" "spring-scheduling" } } @@ -56,11 +54,10 @@ class SpringSchedulingTest extends AgentTestRunner { assertTraces(1) { trace(0, 1) { span(0) { - operationName "scheduled.call" + operationName "IntervalTask.run" parent() errored false tags { - "$MoreTags.RESOURCE_NAME" "IntervalTask.run" "$Tags.COMPONENT" "spring-scheduling" } }