io.opentelemetry.javaagent.instrumentation.opentelemetryapi.OpenTelemetryInstrumentation Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opentelemetry-javaagent-opentelemetry-api-1.0 Show documentation
Show all versions of opentelemetry-javaagent-opentelemetry-api-1.0 Show documentation
Instrumentation of Java libraries using OpenTelemetry.
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.javaagent.instrumentation.opentelemetryapi;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.isStatic;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
// Our convention for accessing agent package
@SuppressWarnings("UnnecessarilyFullyQualified")
public class OpenTelemetryInstrumentation implements TypeInstrumentation {
@Override
public ElementMatcher typeMatcher() {
return named("application.io.opentelemetry.api.GlobalOpenTelemetry");
}
@Override
public void transform(TypeTransformer transformer) {
transformer.applyAdviceToMethod(
isMethod().and(isStatic()).and(named("get")).and(takesArguments(0)),
OpenTelemetryInstrumentation.class.getName() + "$GetGlobalOpenTelemetryAdvice");
}
@SuppressWarnings("unused")
public static class GetGlobalOpenTelemetryAdvice {
@Advice.OnMethodEnter(skipOn = Advice.OnDefaultValue.class)
public static Object onEnter() {
return null;
}
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void methodExit(
@Advice.Return(readOnly = false)
application.io.opentelemetry.api.OpenTelemetry openTelemetry) {
openTelemetry = ApplicationOpenTelemetry.INSTANCE;
}
}
}