Skip to content

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

Key points

  • Add one JVM flag: -javaagent pointing at opentelemetry-javaagent.jar.
  • Configure with OTEL_ environment variables or system properties.
  • JDBC, HTTP clients and messaging are traced automatically.
  • Use @WithSpan for your own methods.

Add the agent

bash
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.jar

In a container

Dockerfiledockerfile
FROM 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.

Custom spans

InvoiceService.javajava
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.

Log correlation

With the agent attached, Logback and Log4j MDC gain trace_id and span_id. Add them to your log pattern, for example %X{trace_id}, and ship logs to VitalRix so each trace shows its log lines.

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.

bash
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_key

Through 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.

otel-collector.yamlyaml
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] }
bash
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:latest

Tuning

  • 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_traceidratio and OTEL_TRACES_SAMPLER_ARG=0.25 on very busy services; see trace sampling.
  • Memory: size the heap from the container limit with -XX:MaxRAMPercentage; see OOMKilled.

Check that it works

  1. Send a few requests to the app.
  2. In VitalRix, open APM: orders-service appears in the service catalog within a minute, with request rate, error rate and p95.
  3. Open an endpoint and then one request to see its trace waterfall, including database spans.
  4. 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 check command 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.

Frequently asked questions

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.

  • 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.