Four ways to run OpenTelemetry in Java without changing a line of code
Pick the right zero-code OpenTelemetry setup for Java by answering one question: what are you allowed to change? Startup command, host, pod spec or nothing at all.
Trace every request end-to-end with Elastic. Get hands-on with application performance monitoring, explore the APM docs, or start a free cloud trial today.
You want to monitor a Java application without changing its source code. Which OpenTelemetry installation method should you use?
There are four zero-code ways to add OpenTelemetry to a Java service, and the choice comes down to one question: what can you change? If you can alter the startup command or environment, you can add -javaagent or set JAVA_TOOL_OPTIONS. On Kubernetes, the Operator injects the agent pod by pod. For large numbers of Linux hosts where the launch scripts are all different and you can’t control them, the Injector hooks into the dynamic loader so any newly launched JVMs pick up the agent. If you can't touch the process at all and can't restart it, OBI will observe it from the outside using eBPF (trading framework-level detail for zero intrusion).
| Environment | Recommended approach |
|---|---|
| A few Java processes | Add -javaagent directly, or use JAVA_TOOL_OPTIONS to add it indirectly |
| Many processes on Linux hosts | Use the OpenTelemetry Injector |
| Many pods in Kubernetes | Use the OpenTelemetry Kubernetes Operator |
| Processes that cannot easily be changed or restarted | Use OpenTelemetry eBPF Instrumentation (OBI) |
This article goes into more detail about each choice.
Scope, versions tested, and production readiness
- Much of the deployment guidance in this article also applies to supported .NET, Python, and Node.js instrumentation, but this article covers only Java startup configuration.
- The versions used to test were: OpenTelemetry Java agent 2.32.0; OBI 1.0.0; Injector 0.11.0; Operator 0.162.0; EDOT Java agent 1.14.0; Elastic stack 9.5.4.
- Neither Injector nor Operator have yet released a 1.0.0+ version, so may not be suitable for production. While the Operator has been more extensively used in many production systems, the Injector is only half a year old at the time of writing this article, so it should be considered less stable.
OpenTelemetry Java agent or eBPF: which gives you more visibility?
There are two kinds of zero-code instrumentation available to you: a Java application agent running inside the application and/or an external process monitoring the application using OBI.
The first, a Java agent running inside the JVM, instruments supported frameworks and libraries. You get most common framework details, JDBC calls, messaging operations, exceptions, and more. This is the best option for detailed application traces.
The second, OBI, runs outside the JVM and observes supported protocols and runtime behavior using eBPF. It is useful when the application cannot be changed, when it is already running and can't be restarted, or when protocol-level visibility (e.g., HTTP) is sufficient.
The Injector, the Operator, and setting JAVA_TOOL_OPTIONS all cause a Java agent to load at JVM startup; they differ in how that configuration is applied.
All arrange for the Java agent to load into the JVM by adding -javaagent to JAVA_TOOL_OPTIONS, but each applies JAVA_TOOL_OPTIONS in a different way.
How to add the OpenTelemetry Java agent with -javaagent
When you control the startup of a small number of services or have a common deployment mechanism for your services, directly configuring the JVM is the simplest and most explicit option. In this case, add the agent to the Java command. For example:
export OTEL_SERVICE_NAME=my-service export OTEL_EXPORTER_OTLP_ENDPOINT=https://<your-otlp-endpoint> export OTEL_EXPORTER_OTLP_HEADERS="Authorization=ApiKey <your-api-key>" java -javaagent:/opt/elastic/elastic-otel-javaagent.jar -jar myapp.jar
The -javaagent argument makes the scope clear: this JVM loads this agent. It also works well in startup scripts, systemd service definitions,
container entry points, and application-server configuration. Use this approach when:
- Only a few services need instrumentation, or the application deployment has a common deployment configuration that can be changed.
- You can change each service's startup command.
- You want to control agent versions and rollout per service.
- Restarting each service is part of the normal deployment process.
Using JAVA_TOOL_OPTIONS when you cannot change the Java command
The JVM reads the JAVA_TOOL_OPTIONS environment variable at startup. You can use it to supply the agent when the final Java command is hidden behind a script or managed launcher:
export JAVA_TOOL_OPTIONS="-javaagent:/opt/elastic/elastic-otel-javaagent.jar" export OTEL_SERVICE_NAME=my-service export OTEL_EXPORTER_OTLP_ENDPOINT=https://<your-otlp-endpoint> export OTEL_EXPORTER_OTLP_HEADERS="Authorization=ApiKey <your-api-key>" java -jar myapp.jar
This is still startup-time instrumentation and still requires a restart. The difference is in how you configure it.
Best practice is to be deliberate about the scope of the JAVA_TOOL_OPTIONS variable. Every JVM started in the same environment inherits JAVA_TOOL_OPTIONS,
potentially including helper processes, build tools, and other applications. The latest Java agent doesn't activate if it detects that the
JVM is running as a known tool like jcmd or jps, but it's still advisable to avoid having the agent try to start if it's not wanted. You should prefer an explicit
-javaagent argument when you can edit the intended service command without difficulty. Use JAVA_TOOL_OPTIONS when environment configuration is the cleaner or only practical control point.
Instrumenting many processes on one or more Linux hosts with the OpenTelemetry Injector
On a Linux host, Java services might be launched through different systemd units, vendor scripts, cron jobs, or custom wrappers. Updating every launch path creates unnecessary work.
The OpenTelemetry Injector moves the setup to the host. It uses LD_PRELOAD for selected processes or /etc/ld.so.preload system-wide.
When a supported process starts, the Injector identifies its runtime and supplies the environment needed to load the bundled auto-instrumentation. For Java, the result is a JVM running with the OpenTelemetry Java agent.
Use the Injector when:
- You need to instrument many services on Linux hosts.
- Those services have inconsistent or inconvenient startup mechanisms.
- Host administrators can install and configure the Injector.
- Services can be restarted through their normal operational procedures.
The Injector does not attach an agent to a JVM that is already running. It affects processes started after Injector installation, so you need to plan a staged service restart. It's also worth noting that the Injector can't inject instrumentation into containerized processes.
Host-wide instrumentation may need careful selection rules. The Injector supports configuring the inclusion and exclusion of processes based on wildcard matching. This lets you expand observability cautiously, or at least exclude obviously inappropriate processes.
Injector limitations: Windows, macOS, and statically linked binaries
Because the mechanism relies on the Linux dynamic loader, it applies to dynamically linked ELF processes. It does not work on Windows or macOS, and it cannot preload into statically linked or libc-free executables. System-wide configuration normally requires root to install the Injector, though after installation root is no longer necessary (except perhaps for reconfiguring).
Instrumenting Kubernetes pods with the OpenTelemetry Operator
The OpenTelemetry Operator gives Kubernetes deployments a declarative way to describe which workloads should be instrumented. The Operator uses an admission webhook to apply that configuration when pods are created.
An Instrumentation resource defines the agent image and OpenTelemetry settings. An annotation on a workload's pod template tells the Operator to inject the Java agent:
apiVersion: apps/v1 kind: Deployment metadata: name: checkout spec: template: metadata: annotations: instrumentation.opentelemetry.io/inject-java: opentelemetry-operator-system/elastic-instrumentation spec: containers: - name: checkout image: example/checkout:1.0
The annotation belongs under spec.template.metadata.annotations, not on the Deployment's own metadata. You can annotate individual workloads for precise control or annotate a namespace when all eligible workloads in that namespace should use the same instrumentation.
Use the Operator when:
- You have many Java workloads in Kubernetes.
- Instrumentation should be managed through Kubernetes resources and GitOps.
- Platform teams want to provide shared agent and exporter configuration
- Application teams should opt in by workload or namespace.
The Operator adds an init container that supplies the agent JAR and configures the application container to load it. It only affects newly created pods. After adding an annotation to a deployment or namespace, you would need to perform a normal rollout to replace existing pods.
For Elastic Observability, follow the EDOT Java Kubernetes guide. It provides an Instrumentation resource that injects the EDOT Java image and sends data through the configured OpenTelemetry Collector.
OpenTelemetry eBPF Instrumentation (OBI) for processes you cannot restart
Sometimes you cannot modify the Java command, set an environment variable, mutate the pod, or schedule a restart. This is common with third-party containers, appliances, sensitive production services, and workloads owned by another team.
OpenTelemetry eBPF Instrumentation (OBI) runs outside the application. It discovers running processes and uses eBPF to observe supported network protocols and runtime behavior. It can begin monitoring a process that is already running, without loading a Java agent into its JVM.
Use OBI when:
- You cannot change the application command or environment.
- Restarting or replacing the workload is not practical.
- You need initial RED metrics, service relationships, or supported protocol traces.
- Out-of-process isolation matters more than detailed framework visibility.
OBI is useful for instrumenting containers, but it is not limited to containers. The deciding factor is whether an in-process agent can be deployed. In Kubernetes, OBI can run as a sidecar for one pod or as a DaemonSet with host process access. It can also monitor non-containerized Linux processes.
What OBI isn't good at
The easier deployment of OBI comes with a visibility trade-off. OBI observes activity from outside the JVM and cannot generally provide the same framework detail or context propagation coverage as the Java agent. OBI documents limitations for Java virtual threads, reactive frameworks, and complex thread pools.
OBI also requires Linux, an eBPF-capable environment, and elevated capabilities. Cluster or host security policy may rule it out. By default, OBI detects services already publishing OpenTelemetry data and excludes them from its own instrumentation to reduce duplicate telemetry.
OBI can be a useful first step for an unknown estate. It shows which services communicate and which ones need attention. You can later deploy Java agents to selected services when deeper diagnostics justify a controlled restart.
What EDOT Java adds to the OpenTelemetry Java agent
EDOT Java is Elastic's distribution of the upstream OpenTelemetry Java agent. It uses OpenTelemetry APIs, OTEL_* configuration, semantic conventions, and OTLP.
EDOT adds defaults and features intended for Elastic Observability, including Elastic-specific processors, inferred spans, span stack traces, Universal Profiling correlation, and compatibility with APM Agent Central Configuration.
For new Java services sending telemetry to Elastic, EDOT is the recommended agent.
Which OpenTelemetry Java option should you choose?
For most environments, use the least complicated mechanism that reaches the intended applications:
- Add EDOT with
-javaagentwhen you control a few Java service commands. - Use
JAVA_TOOL_OPTIONSwhen environment configuration is easier than changing those commands. - Use the Injector for a large fleet of Linux host processes.
- Use the Operator to inject EDOT across Kubernetes workloads.
- Use OBI where an agent cannot be deployed or a restart cannot be scheduled, accepting its different level of detail.
Start with one service or a small workload group. Confirm the service name, resource attributes, data flow, trace quality, and overhead before expanding the rollout. The best zero-code option is the one that fits the environment without hiding what it changes.
How helpful was this content?


_(1).png)

