88import static net .bytebuddy .matcher .ElementMatchers .isMethod ;
99import static net .bytebuddy .matcher .ElementMatchers .isStatic ;
1010import static net .bytebuddy .matcher .ElementMatchers .named ;
11+ import static net .bytebuddy .matcher .ElementMatchers .returns ;
12+ import static net .bytebuddy .matcher .ElementMatchers .takesArgument ;
1113import static net .bytebuddy .matcher .ElementMatchers .takesArguments ;
1214
1315import io .opentelemetry .javaagent .extension .instrumentation .TypeInstrumentation ;
1416import io .opentelemetry .javaagent .extension .instrumentation .TypeTransformer ;
1517import net .bytebuddy .asm .Advice ;
1618import net .bytebuddy .description .type .TypeDescription ;
1719import net .bytebuddy .matcher .ElementMatcher ;
20+ import org .slf4j .LoggerFactory ;
1821
1922// Our convention for accessing agent package
2023@ SuppressWarnings ("UnnecessarilyFullyQualified" )
@@ -28,12 +31,23 @@ public ElementMatcher<TypeDescription> typeMatcher() {
2831 @ Override
2932 public void transform (TypeTransformer transformer ) {
3033 transformer .applyAdviceToMethod (
31- isMethod ().and (isStatic ()).and (named ("get" )).and (takesArguments (0 )),
32- OpenTelemetryInstrumentation .class .getName () + "$GetGlobalOpenTelemetryAdvice" );
34+ isMethod ()
35+ .and (isStatic ())
36+ .and (named ("get" ))
37+ .and (takesArguments (0 ))
38+ .and (returns (named ("application.io.opentelemetry.api.OpenTelemetry" ))),
39+ OpenTelemetryInstrumentation .class .getName () + "$GetAdvice" );
40+ transformer .applyAdviceToMethod (
41+ isMethod ()
42+ .and (isStatic ())
43+ .and (named ("set" ))
44+ .and (takesArguments (1 ))
45+ .and (takesArgument (0 , named ("application.io.opentelemetry.api.OpenTelemetry" ))),
46+ OpenTelemetryInstrumentation .class .getName () + "$SetAdvice" );
3347 }
3448
3549 @ SuppressWarnings ("unused" )
36- public static class GetGlobalOpenTelemetryAdvice {
50+ public static class GetAdvice {
3751
3852 @ Advice .OnMethodEnter (skipOn = Advice .OnDefaultValue .class )
3953 public static Object onEnter () {
@@ -47,4 +61,18 @@ public static void methodExit(
4761 openTelemetry = ApplicationOpenTelemetry .INSTANCE ;
4862 }
4963 }
64+
65+ @ SuppressWarnings ("unused" )
66+ public static class SetAdvice {
67+
68+ @ Advice .OnMethodEnter (suppress = Throwable .class )
69+ public static void onEnter () {
70+ LoggerFactory .getLogger (application .io .opentelemetry .api .GlobalOpenTelemetry .class )
71+ .warn (
72+ "You are currently using the OpenTelemetry Instrumentation Java Agent;"
73+ + " all GlobalOpenTelemetry.set calls are ignored - the agent provides"
74+ + " the global OpenTelemetry object used by your application." ,
75+ new Throwable ());
76+ }
77+ }
5078}
0 commit comments