OpenTelemetry guide
OpenTelemetry for Java and Spring Boot
The OpenTelemetry Java agent instruments Spring MVC, WebFlux, JDBC, Hibernate, Kafka and common HTTP clients at startup, with no code changes.
3 min read11 sectionsUpdated
On this page
curl -L -o opentelemetry-javaagent.jar \
https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/latest/download/opentelemetry-javaagent.jar
export OTEL_SERVICE_NAME=orders-service
export OTEL_RESOURCE_ATTRIBUTES=service.version=5.2.0,deployment.environment.name=production
export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
export OTEL_LOGS_EXPORTER=none
java -javaagent:./opentelemetry-javaagent.jar -jar target/orders-service.jarFROM eclipse-temurin:21-jre
WORKDIR /app
COPY target/orders-service.jar app.jar
ADD https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/latest/download/opentelemetry-javaagent.jar /otel/agent.jar
ENV JAVA_TOOL_OPTIONS="-javaagent:/otel/agent.jar -XX:MaxRAMPercentage=75"
ENTRYPOINT ["java", "-jar", "app.jar"]Pin a specific agent version in production rather than "latest", so builds are reproducible.
import io.opentelemetry.instrumentation.annotations.SpanAttribute;
import io.opentelemetry.instrumentation.annotations.WithSpan;
@Service
public class InvoiceService {
@WithSpan("render invoice")
public byte[] render(@SpanAttribute("invoice.id") String invoiceId) {
// ...
}
}The annotations come from the io.opentelemetry.instrumentation:opentelemetry-instrumentation-annotations dependency; the agent picks them up at runtime.
Send it to VitalRix
Directly. VitalRix accepts OTLP over HTTP with protobuf or JSON, which is what the Java exporter sends. Instead of a Collector at localhost:4318, point the exporter at VitalRix with the per-signal endpoints and your API key. Create the key in VitalRix under Settings, API keys, with trace, metric and log ingestion.
export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=https://api.vitalrix.com/api/v1/traces/otlp/v1/traces
export OTEL_EXPORTER_OTLP_METRICS_ENDPOINT=https://api.vitalrix.com/api/v1/metrics/otlp/v1/metrics
export OTEL_EXPORTER_OTLP_LOGS_ENDPOINT=https://api.vitalrix.com/api/v1/logs/otlp/v1/logs
export OTEL_EXPORTER_OTLP_HEADERS=x-api-key=your_api_keyThrough a Collector. Run an OpenTelemetry Collector next to your app, or as a shared service, when the SDK exports over gRPC or you want one place for batching, sampling and retries. It receives from the SDK and forwards to VitalRix with your key.
receivers:
otlp:
protocols:
grpc: { endpoint: 0.0.0.0:4317 }
http: { endpoint: 0.0.0.0:4318 }
processors:
memory_limiter: { check_interval: 1s, limit_mib: 512 }
batch: {}
exporters:
otlphttp/vitalrix:
traces_endpoint: ${env:VITALRIX_API}/api/v1/traces/otlp/v1/traces
metrics_endpoint: ${env:VITALRIX_API}/api/v1/metrics/otlp/v1/metrics
headers:
x-api-key: ${env:VITALRIX_KEY}
service:
pipelines:
traces: { receivers: [otlp], processors: [memory_limiter, batch], exporters: [otlphttp/vitalrix] }
metrics: { receivers: [otlp], processors: [memory_limiter, batch], exporters: [otlphttp/vitalrix] }docker run -d --name otel-collector -p 4317:4317 -p 4318:4318 \
-e VITALRIX_API=https://api.vitalrix.com -e VITALRIX_KEY=your_api_key \
-v "$PWD/otel-collector.yaml:/etc/otelcol-contrib/config.yaml" \
otel/opentelemetry-collector-contrib:latestTuning
- Startup time grows slightly with the agent; startup probes should allow for it.
- Disable instrumentations you do not need with
OTEL_INSTRUMENTATION_<NAME>_ENABLED=false. - Sampling:
OTEL_TRACES_SAMPLER=parentbased_traceidratioandOTEL_TRACES_SAMPLER_ARG=0.25on very busy services; see trace sampling. - Memory: size the heap from the container limit with
-XX:MaxRAMPercentage; see OOMKilled.
Check that it works
- Send a few requests to the app.
- In VitalRix, open APM: orders-service appears in the service catalog within a minute, with request rate, error rate and p95.
- Open an endpoint and then one request to see its trace waterfall, including database spans.
- If nothing arrives, check the exporter's (or the Collector's) logs for export errors: 401 means the key or header is wrong, 402 that a new host is over the plan's APM host allowance. The packages have a
checkcommand that sends one test log.
Add service.version so new releases appear as deploy markers, and put the trace ID in your logs so each trace shows its log lines. See APM and logs, metrics and traces.
Agent or Spring Boot starter?
The agent needs no code changes and covers the most libraries. The OpenTelemetry Spring Boot starter suits native images or setups where a javaagent is not allowed.
Does it work with Kotlin and other JVM languages?
Yes. The agent instruments bytecode, so Kotlin, Scala and Groovy services on supported frameworks are traced the same way.
Related reading
- OpenTelemetry for Java: the agent, step by step: Instrument any Java application with the OpenTelemetry Java agent: no code changes, environment configuration, Docker and Kubernetes setup and troubleshooting.
- Java performance monitoring: Monitor Java and Spring applications: JVM heap and GC, thread pools, JDBC connection pools, request latency from traces.
- 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.