1

I'm trying to create an extension for opentelemetry javaagent. I need some static variables outside the methods to handle spans. But I get the error java.lang.ClassNotFoundException for a class where static variables are defined. The simplest code to reproduce:

public static class MethodAdvice {

        public static Integer CONST = 123;

        @Advice.OnMethodEnter(suppress = Throwable.class)
        public static void onEnter() {
            try {
                System.out.println(CONST.toString());
//                Class.forName(MethodAdvice.class.getName());
            } catch (Throwable e) {
                System.out.println("ERROR: " + e.getCause().toString());
                throw new RuntimeException(e);
            }
        }
    }

I get error when using CONST static variable for the first time. The same if I try to load the class. How can I use a static variable defined outside of the @Advice method? As I can see the opentelemetry library uses static variables outside the methods and it works, but I can't figure out how it is handled there.

3 Answers 3

1

I assume that these variables are on the boot loader, or they are final and primitive such that they are inlined during compilation.

Advice code is copied to the targeted method. It is at it was written there. As a result, it will have a different scope of visibility and access.

Sign up to request clarification or add additional context in comments.

Comments

0

The OpenTelemetry Java agent loads Advice classes inside of its own isolated AgentClassLoader.

But the Advice method bytecode is inlined into the instrumented application classes.

And application classes do not have access to the AgentClassLoader.

You'll want to move your constant to a "helper" class, which this OTel Java agent will inject into the application class loader, and so will be accessible from your advice.

This documentation should help: https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/docs/contributing/writing-instrumentation-module.md#write-an-instrumentationmodule-step-by-step

1 Comment

Yes, you're right. I solved this issue here - stackoverflow.com/questions/75508576/…
0

I think this is how extensions are implemented in opentelemetry agent. I checked the current thread classloader, it's my application class loader. So application classloader tries to load MethodAdvice class but it is not present in the application classpath. So it is required to add the extension jar to the classpath. I've just added the extension as a dependency in my multimodule project and the issue was resolved.

UPD: But now I have another issue( - java.lang.NoSuchFieldError if CONST field more complicated and requires initialization in static block.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.