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
# 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=trueDocker 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.
Related reading
- 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.