The javaagent jar can logically be divided into 3 parts:
- Modules that live in the system class loader
- Modules that live in the bootstrap class loader
- Modules that live in the agent class loader
This module consists of single class
io.opentelemetry.javaagent.OpenTelemetryAgent
which implements Java
instrumentation
agent.
This class is loaded during application startup by application classloader.
Its sole responsibility is to push agent's classes into JVM's bootstrap
classloader and immediately delegate to
io.opentelemetry.javaagent.bootstrap.AgentInitializer
(now in the bootstrap class loader)
class from there.
io.opentelemetry.javaagent.bootstrap.AgentInitializer
and a few other classes that live in the bootstrap class
loader but are not used directly by auto-instrumentation
These modules contain support classes for actual instrumentations to be loaded
later and separately. These classes should be available from all possible
classloaders in the running application. For this reason the javaagent
module puts
all these classes into JVM's bootstrap classloader. For the same reason this
module should be as small as possible and have as few dependencies as
possible. Otherwise, there is a risk of accidentally exposing these classes to
the actual application.
instrumentation-api
contains classes that are needed for both library and auto-instrumentation,
while javaagent-instrumentation-api
contains classes that are only needed for auto-instrumentation.
Contains everything necessary to make instrumentation machinery work, including integration with ByteBuddy and actual library-specific instrumentations. As these classes depend on many classes from different libraries, it is paramount to hide all these classes from the host application. This is achieved in the following way:
- When
javaagent
module builds the final agent, it moves all classes frominstrumentation
submodules,javaagent-tooling
andjavaagent-extension-api
modules into a separate folder inside final jar file, calledinst
. In addition, the extension of all class files is changed fromclass
toclassdata
. This ensures that general classloaders cannot find nor load these classes. - When
io.opentelemetry.javaagent.bootstrap.AgentInitializer
is invoked, it creates an instance ofio.opentelemetry.javaagent.bootstrap.AgentClassLoader
, loads anio.opentelemetry.javaagent.tooling.AgentInstaller
from thatAgentClassLoader
and then passes control on to theAgentInstaller
(now in theAgentClassLoader
). TheAgentInstaller
then installs all of the instrumentations with the help of ByteBuddy. Instead of using agent classloader all agent classes could be shaded and used from the bootstrap classloader. However, this opens de-serialization security vulnerability and in addition to that the shaded classes are harder to debug.
The complicated process above ensures that the majority of auto-instrumentation agent's classes are totally isolated from application classes, and an instrumented class from arbitrary classloader in JVM can still access helper classes from bootstrap classloader.
If you now look inside
javaagent/build/libs/opentelemetry-javaagent-<version>.jar
, you will see the
following "clusters" of classes:
Available in the system class loader:
io/opentelemetry/javaagent/bootstrap/AgentBootstrap
- the one class fromjavaagent
module
Available in the bootstrap class loader:
io/opentelemetry/javaagent/bootstrap/
- contains thejavaagent-bootstrap
moduleio/opentelemetry/javaagent/instrumentation/api/
- contains thejavaagent-instrumentation-api
moduleio/opentelemetry/javaagent/shaded/instrumentation/api/
- contains theinstrumentation-api
module, shaded during creation ofjavaagent
jar file by Shadow Gradle pluginio/opentelemetry/javaagent/shaded/io/
- contains the OpenTelemetry API and its dependency gRPC Context, both shaded during creation ofjavaagent
jar file by Shadow Gradle pluginio/opentelemetry/javaagent/slf4j/
- contains SLF4J and its simple logger implementation, shaded during creation ofjavaagent
jar file by Shadow Gradle plugin
Available in the agent class loader:
inst/
- containsjavaagent-tooling
andjavaagent-extension-api
modules andinstrumentation
submodules, loaded and isolated insideAgentClassLoader
. Includes the OpenTelemetry SDK.