open-telemetry / opentelemetry-java-instrumentation

OpenTelemetry auto-instrumentation and instrumentation libraries for Java
https://opentelemetry.io
Apache License 2.0
1.96k stars 860 forks source link

OpenTelemetry Automatic instrumentation - General usability question #8227

Closed banandh closed 1 year ago

banandh commented 1 year ago

I'm using automatic instrumentation in my springboot app.

  1. Would like to return the traceId being generated from Opentelemetry agents in my application response header so it will be used for request tracing.

  2. Currently adding -javaagent for automatic instrumentation, would like to include this javaagent jar in classpath and enforce this instrumentation via manifest.mf

SpringBoot maven plugin creates a executable jar , in that i wanted to include this Launcher-Agent-Class to point to the opentelemetry agent class so that it can be automatically added whenever the jar is executed

trask commented 1 year ago

Would like to return the traceId being generated from Opentelemetry agents in my application response header so it will be used for request tracing.

this isn't supported out-of-the-box since it's not part of the OpenTelemetry specification (yet?), but you could implement it using a javaagent extension using the new HttpServerResponseCustomizer implemented in #7990 and #8095

SpringBoot maven plugin creates a executable jar , in that i wanted to include this Launcher-Agent-Class to point to the opentelemetry agent class so that it can be automatically added whenever the jar is executed

I'm not sure if anyone has tried this. It may not work since we add the javaagent jar to the bootstrap classpath, and that could be problematic if the javaagent classes are in an uber jar. Let us know how if you have any success / run into any problems.

trask commented 1 year ago

also check out https://github.com/open-telemetry/opentelemetry-java-contrib/tree/main/runtime-attach

banandh commented 1 year ago

also check out https://github.com/open-telemetry/opentelemetry-java-contrib/tree/main/runtime-attach

This really helps. Will try out :)

banandh commented 1 year ago

specification

May be I missed to convey my requirement clearly. What I wanted is "use automatic instrumentation and get the traceId inside the service either by adding opentelemetry api jars as dependency or someother way".

mateuszrzeszutek commented 1 year ago

May be I missed to convey my requirement clearly. What I wanted is "use automatic instrumentation and get the traceId inside the service either by adding opentelemetry api jars as dependency or someother way".

We have that feature already -- you can add the opentelemetry-api dependency to your app, and use the Span.current() to retrieve the current span in your code. (you can also use the OTel API to add any manual instrumentation that you want, and that the agent does not provide out of the box)

laurit commented 1 year ago

I haven't tried Launcher-Agent-Class myself, but I think the easiest way to make it work is probably to package the agent jar inside your app as a jar file and use a custom launcher agent class that extracts the agent jar from your jar, adds it to classpath somehow(Instrumentation.appendToBootstrapClassLoaderSearch or new URLClassLoader), loads the agent main class (io.opentelemetry.javaagent.OpenTelemetryAgent) and calls agentmain or premain

banandh commented 1 year ago

custom launcher agent class

Can you point some examples. Not sure whether , i understood it rightly

banandh commented 1 year ago

also check out https://github.com/open-telemetry/opentelemetry-java-contrib/tree/main/runtime-attach

This really helps. Will try out :)

This runtime attach has a limitation "The application is running on a JRE (a JDK is necessary, see https://github.com/raphw/byte-buddy/issues/374)". Usually runtime contains JRE not the JDK so it can fail in higher environment

laurit commented 1 year ago

custom launcher agent class

Can you point some examples. Not sure whether , i understood it rightly

I don't believe anybody has tried this before, so there is no sample code.

github-actions[bot] commented 1 year ago

This has been automatically marked as stale because it has been marked as needing author feedback and has not had any activity for 7 days. It will be closed automatically if there is no response from the author within 7 additional days from this comment.