Video in progress

What risks do you assess before adding an APM or OpenTelemetry Java agent to the whole fleet?

The agent changes the bytecode of every loaded class: this raises startup time, metaspace and code cache, CPU and allocation, and sometimes breaks JIT optimizations. I measure the overhead, limit the scope of instrumentation and turn it on step by step.

📖 Answer

-javaagent changes classes' bytecode as they load, through the Instrumentation API (usually with ByteBuddy). That is powerful, but it touches everything inside the JVM.

Common problems:

  • Startup time. Every class goes through the transformer. In a big Spring application startup can go up 30-100%, so account for that in readiness and autoscaling calculations.
  • Metaspace and code cache. Instrumented methods are bigger, and extra helper classes are loaded.
  • Hot path overhead. Every span means objects, context propagation, ThreadLocals. In a service with tens of thousands of internal calls per second this is noticeable CPU and allocation.
  • Broken inlining. A small method grows with advice and goes over the inline limit; escape analysis may stop working too.
  • Compatibility. If the agent doesn't support your JDK version or library version: VerifyError, NoSuchMethodError or silent non-functioning. If two different agents change one class at the same time, the results are unpredictable.

My rollout plan:

  1. The same load test without the agent and with it: p99, CPU, allocation rate, startup time, RSS.
  2. Enable only the instrumentations you need (otel.instrumentation.<name>.enabled in OpenTelemetry) and limit sampling from the start.
  3. A canary, then waves. Pin the agent version in the base image.
  4. A way to switch it off: the ability to start without the agent with a single environment variable.

As an alternative, in very hot services, manual instrumentation in code (the OpenTelemetry API, Micrometer Observation) is cheaper and more controlled than an agent.