OpenTelemetry provides a unified framework for collecting telemetry data across different technologies, making it easier to troubleshoot issues, optimize performance, and ensure reliability. By centralizing telemetry collection with the OpenTelemetry Collector, you can standardize your observability pipeline and integrate with tools like Grafana, Prometheus, or Jaeger. This guide explains how to easily instrument your application to send traces, metrics and logs to an OpenTelemetry Collector. All examples use no-code auto-instrumentation , for more complex use cases refer to the official OpenTelemetry documentation.

Java Applications

Official documentation: https://opentelemetry.io/docs/zero-code/java/agent/

Agent Download: https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/latest/download/opentelemetry-javaagent.jar

The preferred way to configure a Java application to send telemetry (metrics, traces & logs) to an OpenTelemetry compatible backend (e.g. OpenTelemetry Collector) is to use the official OpenTelemetry Java Agent. It can be run alongside the original app to automatically push metrics/traces/logs with minimal configuration. You can download the agent jar manually, but it is advised to include the download in your build process (see example below).

To use the agent with your application, you can either:

  • set the JAVA_TOOL_OPTIONS environment variable to JAVA_TOOL_OPTIONS="-javaagent:<path to otel agent JAR>"
  • or pass a flag to your java command java -javaagent:<path to otel agent JAR> -jar application.jar.

Example: Spring Boot built via Gradle

Add the following elements to your gradle.build file to automatically download the Java Agent in the build process and save it at ./build/libs/otel-agent.jar:

configurations {
  telemetry {}
}
 
ext {
  set('otelAgentVersion', "2.9.0")
}
 
dependencies {
  telemetry "io.opentelemetry.javaagent:opentelemetry-javaagent:${otelAgentVersion}"
}
 
tasks.register('copyAgent', Copy) {
  from(configurations.telemetry.singleFile)
  into(layout.buildDirectory.dir("libs"))
  rename("opentelemetry-javaagent-.*\\.jar", "otel-agent.jar")
}
 
bootJar {
  dependsOn copyAgent
}

Include the Java Agent JAR file in the Docker image and add the required environment variables in the Dockerfile so that the agent will be used when running the container:

FROM baseimage
...
COPY ./build/libs/otel-agent.jar /application/otel-agent.jar
 
# use the agent when running the 'java' command
ENV JAVA_TOOL_OPTIONS="-javaagent:/application/otel-agent.jar"
 
# OpenTelemetry configuration example
ENV OTEL_SDK_DISABLED=false
ENV OTEL_EXPORTER_OTLP_ENDPOINT="http://opentelemetry-collector:4318"
ENV OTEL_SERVICE_NAME=application-servicename
ENV OTEL_LOGS_EXPORTER=none
...
ENTRYPOINT ["sh", "-c", "java -jar /application/application.jar"]