Transition spring-scheduling-3.1 to instrumenter API (#3993)

This commit is contained in:
jack-berg 2021-08-30 11:01:43 -05:00 committed by GitHub
parent 0c5a67167a
commit 789c92d79e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 42 deletions

View File

@ -5,7 +5,8 @@
package io.opentelemetry.javaagent.instrumentation.spring.scheduling;
import static io.opentelemetry.javaagent.instrumentation.spring.scheduling.SpringSchedulingTracer.tracer;
import static io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge.currentContext;
import static io.opentelemetry.javaagent.instrumentation.spring.scheduling.SpringSchedulingSingletons.instrumenter;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
@ -23,12 +24,18 @@ public class SpringSchedulingRunnableWrapper implements Runnable {
return;
}
Context context = tracer().startSpan(runnable);
Context parentContext = currentContext();
if (!instrumenter().shouldStart(parentContext, runnable)) {
runnable.run();
return;
}
Context context = instrumenter().start(parentContext, runnable);
try (Scope ignored = context.makeCurrent()) {
runnable.run();
tracer().end(context);
instrumenter().end(context, runnable, null, null);
} catch (Throwable throwable) {
tracer().endExceptionally(context, throwable);
instrumenter().end(context, runnable, null, throwable);
throw throwable;
}
}

View File

@ -0,0 +1,36 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.javaagent.instrumentation.spring.scheduling;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.tracer.SpanNames;
import org.springframework.scheduling.support.ScheduledMethodRunnable;
public final class SpringSchedulingSingletons {
private static final Instrumenter<Runnable, Void> INSTRUMENTER =
Instrumenter.<Runnable, Void>newBuilder(
GlobalOpenTelemetry.get(),
"io.opentelemetry.spring-scheduling-3.1",
SpringSchedulingSingletons::extractSpanName)
.newInstrumenter();
private static String extractSpanName(Runnable runnable) {
if (runnable instanceof ScheduledMethodRunnable) {
ScheduledMethodRunnable scheduledMethodRunnable = (ScheduledMethodRunnable) runnable;
return SpanNames.fromMethod(scheduledMethodRunnable.getMethod());
} else {
return SpanNames.fromMethod(runnable.getClass(), "run");
}
}
public static Instrumenter<Runnable, Void> instrumenter() {
return INSTRUMENTER;
}
private SpringSchedulingSingletons() {}
}

View File

@ -1,38 +0,0 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.javaagent.instrumentation.spring.scheduling;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.tracer.BaseTracer;
import io.opentelemetry.instrumentation.api.tracer.SpanNames;
import org.springframework.scheduling.support.ScheduledMethodRunnable;
public class SpringSchedulingTracer extends BaseTracer {
private static final SpringSchedulingTracer TRACER = new SpringSchedulingTracer();
public static SpringSchedulingTracer tracer() {
return TRACER;
}
@Override
protected String getInstrumentationName() {
return "io.opentelemetry.spring-scheduling-3.1";
}
public Context startSpan(Runnable runnable) {
return startSpan(spanNameOnRun(runnable), SpanKind.INTERNAL);
}
private static String spanNameOnRun(Runnable runnable) {
if (runnable instanceof ScheduledMethodRunnable) {
ScheduledMethodRunnable scheduledMethodRunnable = (ScheduledMethodRunnable) runnable;
return SpanNames.fromMethod(scheduledMethodRunnable.getMethod());
} else {
return SpanNames.fromMethod(runnable.getClass(), "run");
}
}
}