Better span names for spring-scheduling (#244)

This commit is contained in:
Trask Stalnaker 2020-03-18 15:17:33 -07:00 committed by GitHub
parent d759f846bb
commit f88dabedee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 29 deletions

View File

@ -17,8 +17,6 @@ package io.opentelemetry.auto.instrumentation.springscheduling;
import io.opentelemetry.OpenTelemetry; import io.opentelemetry.OpenTelemetry;
import io.opentelemetry.auto.bootstrap.instrumentation.decorator.BaseDecorator; 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 io.opentelemetry.trace.Tracer;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.support.ScheduledMethodRunnable; import org.springframework.scheduling.support.ScheduledMethodRunnable;
@ -42,19 +40,12 @@ public class SpringSchedulingDecorator extends BaseDecorator {
return "spring-scheduling"; return "spring-scheduling";
} }
public Span onRun(final Span span, final Runnable runnable) { public String spanNameOnRun(final Runnable runnable) {
if (runnable != null) { if (runnable instanceof ScheduledMethodRunnable) {
String resourceName = ""; final ScheduledMethodRunnable scheduledMethodRunnable = (ScheduledMethodRunnable) runnable;
if (runnable instanceof ScheduledMethodRunnable) { return spanNameForMethod(scheduledMethodRunnable.getMethod());
final ScheduledMethodRunnable scheduledMethodRunnable = (ScheduledMethodRunnable) runnable; } else {
resourceName = spanNameForMethod(scheduledMethodRunnable.getMethod()); return spanNameForClass(runnable.getClass()) + "/run";
} else {
final String className = spanNameForClass(runnable.getClass());
final String methodName = "run";
resourceName = className + "." + methodName;
}
span.setAttribute(MoreTags.RESOURCE_NAME, resourceName);
} }
return span;
} }
} }

View File

@ -75,18 +75,17 @@ public final class SpringSchedulingInstrumentation extends Instrumenter.Default
@Override @Override
public void run() { 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); DECORATE.afterStart(span);
try (final Scope scope = TRACER.withSpan(span)) { try (final Scope scope = TRACER.withSpan(span)) {
DECORATE.onRun(span, runnable); runnable.run();
} catch (final Throwable throwable) {
try { DECORATE.onError(span, throwable);
runnable.run(); throw throwable;
} catch (final Throwable throwable) {
DECORATE.onError(span, throwable);
throw throwable;
}
} finally { } finally {
DECORATE.beforeFinish(span); DECORATE.beforeFinish(span);
span.end(); span.end();

View File

@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import io.opentelemetry.auto.instrumentation.api.MoreTags
import io.opentelemetry.auto.instrumentation.api.Tags import io.opentelemetry.auto.instrumentation.api.Tags
import io.opentelemetry.auto.test.AgentTestRunner import io.opentelemetry.auto.test.AgentTestRunner
import org.springframework.context.annotation.AnnotationConfigApplicationContext import org.springframework.context.annotation.AnnotationConfigApplicationContext
@ -32,11 +31,10 @@ class SpringSchedulingTest extends AgentTestRunner {
assertTraces(1) { assertTraces(1) {
trace(0, 1) { trace(0, 1) {
span(0) { span(0) {
operationName "scheduled.call" operationName "TriggerTask.run"
parent() parent()
errored false errored false
tags { tags {
"$MoreTags.RESOURCE_NAME" "TriggerTask.run"
"$Tags.COMPONENT" "spring-scheduling" "$Tags.COMPONENT" "spring-scheduling"
} }
} }
@ -56,11 +54,10 @@ class SpringSchedulingTest extends AgentTestRunner {
assertTraces(1) { assertTraces(1) {
trace(0, 1) { trace(0, 1) {
span(0) { span(0) {
operationName "scheduled.call" operationName "IntervalTask.run"
parent() parent()
errored false errored false
tags { tags {
"$MoreTags.RESOURCE_NAME" "IntervalTask.run"
"$Tags.COMPONENT" "spring-scheduling" "$Tags.COMPONENT" "spring-scheduling"
} }
} }