diff --git a/instrumentation/spring/spring-scheduling-3.1/src/main/java/io/opentelemetry/instrumentation/auto/spring/scheduling/SpringSchedulingInstrumentation.java b/instrumentation/spring/spring-scheduling-3.1/src/main/java/io/opentelemetry/instrumentation/auto/spring/scheduling/SpringSchedulingInstrumentation.java index 2e31f6dc7a..ef26e2a676 100644 --- a/instrumentation/spring/spring-scheduling-3.1/src/main/java/io/opentelemetry/instrumentation/auto/spring/scheduling/SpringSchedulingInstrumentation.java +++ b/instrumentation/spring/spring-scheduling-3.1/src/main/java/io/opentelemetry/instrumentation/auto/spring/scheduling/SpringSchedulingInstrumentation.java @@ -16,18 +16,13 @@ package io.opentelemetry.instrumentation.auto.spring.scheduling; -import static io.opentelemetry.instrumentation.auto.spring.scheduling.SpringSchedulingDecorator.DECORATE; -import static io.opentelemetry.instrumentation.auto.spring.scheduling.SpringSchedulingDecorator.TRACER; -import static io.opentelemetry.trace.TracingContextUtils.currentContextWith; import static java.util.Collections.singletonMap; import static net.bytebuddy.matcher.ElementMatchers.isConstructor; import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.matcher.ElementMatchers.takesArgument; import com.google.auto.service.AutoService; -import io.opentelemetry.context.Scope; import io.opentelemetry.javaagent.tooling.Instrumenter; -import io.opentelemetry.trace.Span; import java.util.Map; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.method.MethodDescription; @@ -49,7 +44,7 @@ public final class SpringSchedulingInstrumentation extends Instrumenter.Default @Override public String[] helperClassNames() { return new String[] { - packageName + ".SpringSchedulingDecorator", getClass().getName() + "$RunnableWrapper", + packageName + ".SpringSchedulingDecorator", packageName + ".SpringSchedulingRunnableWrapper", }; } @@ -64,43 +59,7 @@ public final class SpringSchedulingInstrumentation extends Instrumenter.Default @Advice.OnMethodEnter(suppress = Throwable.class) public static void onConstruction( @Advice.Argument(value = 0, readOnly = false) Runnable runnable) { - runnable = RunnableWrapper.wrapIfNeeded(runnable); - } - } - - public static class RunnableWrapper implements Runnable { - private final Runnable runnable; - - private RunnableWrapper(Runnable runnable) { - this.runnable = runnable; - } - - @Override - public void run() { - if (runnable == null) { - return; - } - Span span = TRACER.spanBuilder(DECORATE.spanNameOnRun(runnable)).startSpan(); - DECORATE.afterStart(span); - - try (Scope scope = currentContextWith(span)) { - runnable.run(); - } catch (Throwable throwable) { - DECORATE.onError(span, throwable); - throw throwable; - } finally { - DECORATE.beforeFinish(span); - span.end(); - } - } - - public static Runnable wrapIfNeeded(Runnable task) { - // We wrap only lambdas' anonymous classes and if given object has not already been wrapped. - // Anonymous classes have '/' in class name which is not allowed in 'normal' classes. - if (task instanceof RunnableWrapper) { - return task; - } - return new RunnableWrapper(task); + runnable = SpringSchedulingRunnableWrapper.wrapIfNeeded(runnable); } } } diff --git a/instrumentation/spring/spring-scheduling-3.1/src/main/java/io/opentelemetry/instrumentation/auto/spring/scheduling/SpringSchedulingRunnableWrapper.java b/instrumentation/spring/spring-scheduling-3.1/src/main/java/io/opentelemetry/instrumentation/auto/spring/scheduling/SpringSchedulingRunnableWrapper.java new file mode 100644 index 0000000000..6b29a204dc --- /dev/null +++ b/instrumentation/spring/spring-scheduling-3.1/src/main/java/io/opentelemetry/instrumentation/auto/spring/scheduling/SpringSchedulingRunnableWrapper.java @@ -0,0 +1,60 @@ +/* + * Copyright The OpenTelemetry 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. + */ + +package io.opentelemetry.instrumentation.auto.spring.scheduling; + +import static io.opentelemetry.instrumentation.auto.spring.scheduling.SpringSchedulingDecorator.TRACER; +import static io.opentelemetry.trace.TracingContextUtils.currentContextWith; + +import io.opentelemetry.context.Scope; +import io.opentelemetry.trace.Span; + +public class SpringSchedulingRunnableWrapper implements Runnable { + private final Runnable runnable; + + private SpringSchedulingRunnableWrapper(Runnable runnable) { + this.runnable = runnable; + } + + @Override + public void run() { + if (runnable == null) { + return; + } + Span span = + TRACER.spanBuilder(SpringSchedulingDecorator.DECORATE.spanNameOnRun(runnable)).startSpan(); + SpringSchedulingDecorator.DECORATE.afterStart(span); + + try (Scope scope = currentContextWith(span)) { + runnable.run(); + } catch (Throwable throwable) { + SpringSchedulingDecorator.DECORATE.onError(span, throwable); + throw throwable; + } finally { + SpringSchedulingDecorator.DECORATE.beforeFinish(span); + span.end(); + } + } + + public static Runnable wrapIfNeeded(Runnable task) { + // We wrap only lambdas' anonymous classes and if given object has not already been wrapped. + // Anonymous classes have '/' in class name which is not allowed in 'normal' classes. + if (task instanceof SpringSchedulingRunnableWrapper) { + return task; + } + return new SpringSchedulingRunnableWrapper(task); + } +}