Skip to content

Troubleshooting guide

OpenTelemetry for Java: the agent, step by step

The OpenTelemetry Java agent adds traces and metrics to a JVM application without code changes. You attach one JAR and set a few environment variables.

3 min read10 sectionsUpdated

On this page

Key points

  • No source changes needed.
  • Works by bytecode instrumentation at class load.
  • It covers common frameworks and clients.
  • Startup gets a little slower.

How it works

The agent runs with -javaagent and modifies classes as they load, wrapping supported libraries such as Servlet, Spring, JDBC, Kafka and HTTP clients. It propagates trace context across threads and processes, exports via OTLP and needs no change to your code.

Before you start

  • Java 8 or later is required.
  • An OTLP endpoint to send data to.
  • A service name for each application, which is the most important setting.
  • Other Java agents can conflict, so test them together.

Install and run

Download the JAR and attach it to your start command.

bash
# Download the latest agent release
curl -sLO https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/latest/download/opentelemetry-javaagent.jar

# Configure and run
export OTEL_SERVICE_NAME=checkout
export OTEL_EXPORTER_OTLP_ENDPOINT=http://collector:4318
export OTEL_RESOURCE_ATTRIBUTES=deployment.environment=production
java -javaagent:./opentelemetry-javaagent.jar -jar app.jar

# Confirm by logging what it does
export OTEL_JAVAAGENT_DEBUG=true

Docker and Kubernetes

In an image, copy the JAR in and use JAVA_TOOL_OPTIONS=-javaagent:/otel/opentelemetry-javaagent.jar, so the start command does not need to change. In Kubernetes, the OpenTelemetry Operator can inject it from an annotation. Set the service name per deployment. To add business detail, add the opentelemetry-api dependency and enrich the current span: the agent provides the implementation at runtime. Set a separate deployment.environment for staging and production, so their data never mixes. Pin the agent to a specific release in production builds, so that an update cannot change behaviour unexpectedly, and upgrade it deliberately.

Controlling the overhead

The agent adds startup time because classes are transformed as they load, typically seconds for a large application. Runtime overhead is usually small but real. Disable instrumentation that you do not need, using OTEL_INSTRUMENTATION_COMMON_DEFAULT_ENABLED=false along with selectively enabling individual libraries. Limit which HTTP headers get captured, and avoid recording query parameters that could contain personal data: the JDBC instrumentation sanitises statements by default, and you should leave that on. Compare p99 and CPU before and after.

Verify it works

Generate some traffic and look for the service in your APM within a minute. If nothing appears, turn on the debug flag and check the Collector. For Spring, see OpenTelemetry for Spring Boot.

Frequently asked questions

Do I have to change my code?

No. Attach the agent and set environment variables.

What is the overhead?

Usually small, but measure with your own workload.

Does it work with virtual threads?

Recent agent versions support them.

  • OpenTelemetry: Send traces and metrics with standard OpenTelemetry SDKs or a Collector using one endpoint and an API key. No proprietary agent and no lock-in.
  • What is a span in tracing?: What a span is in distributed tracing: its fields, parent and child spans, span kinds, attributes and status, with an annotated example you can read.
  • OpenTelemetry for FastAPI: Trace a FastAPI service with OpenTelemetry: instrument the app, async database drivers and httpx, run uvicorn workers correctly and export.
  • OpenTelemetry for Go: Instrument a Go service with OpenTelemetry: set up the tracer provider, wrap net/http with otelhttp, pass context everywhere and export the spans.
  • OpenTelemetry for PHP and Laravel: Trace a Laravel app with OpenTelemetry: install the PHP extension and auto-instrumentation packages, configure PHP-FPM and trace queries.
  • OpenTelemetry for NestJS: Trace a NestJS service with OpenTelemetry: load the SDK before Nest boots, instrument HTTP, Express and NestJS core, and export JSON to VitalRix.

Watch your own site and services with VitalRix

Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.