|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.quartz.v2_0; |
| 7 | + |
| 8 | +import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed; |
| 9 | +import static net.bytebuddy.matcher.ElementMatchers.hasSuperType; |
| 10 | +import static net.bytebuddy.matcher.ElementMatchers.isConstructor; |
| 11 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 12 | + |
| 13 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
| 14 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 15 | +import io.opentelemetry.javaagent.instrumentation.api.CallDepth; |
| 16 | +import net.bytebuddy.asm.Advice; |
| 17 | +import net.bytebuddy.description.type.TypeDescription; |
| 18 | +import net.bytebuddy.matcher.ElementMatcher; |
| 19 | +import org.quartz.Scheduler; |
| 20 | + |
| 21 | +public class QuartzInstrumentation implements TypeInstrumentation { |
| 22 | + @Override |
| 23 | + public ElementMatcher<ClassLoader> classLoaderOptimization() { |
| 24 | + return hasClassesNamed("org.quartz.Scheduler"); |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public ElementMatcher<TypeDescription> typeMatcher() { |
| 29 | + return hasSuperType(named("org.quartz.Scheduler")); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public void transform(TypeTransformer transformer) { |
| 34 | + transformer.applyAdviceToMethod( |
| 35 | + isConstructor(), this.getClass().getName() + "$ConstructorAdvice"); |
| 36 | + } |
| 37 | + |
| 38 | + @SuppressWarnings("unused") |
| 39 | + public static class ConstructorAdvice { |
| 40 | + |
| 41 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 42 | + public static void trackCallDepth(@Advice.Local("otelCallDepth") CallDepth callDepth) { |
| 43 | + callDepth = CallDepth.forClass(Scheduler.class); |
| 44 | + callDepth.getAndIncrement(); |
| 45 | + } |
| 46 | + |
| 47 | + @Advice.OnMethodExit(suppress = Throwable.class) |
| 48 | + public static void addTracingInterceptor( |
| 49 | + @Advice.This Scheduler scheduler, @Advice.Local("otelCallDepth") CallDepth callDepth) { |
| 50 | + // No-args constructor is automatically called by constructors with args, but we only want to |
| 51 | + // run once from the constructor with args because that is where the dedupe needs to happen. |
| 52 | + if (callDepth.decrementAndGet() > 0) { |
| 53 | + return; |
| 54 | + } |
| 55 | + QuartzSingletons.TRACING.configure(scheduler); |
| 56 | + } |
| 57 | + } |
| 58 | +} |
0 commit comments