Skip to main content

Java OpenTelemetry Auto-Instrumentation

Perhaps the most convenient way to start capturing telemetry from Java (or, generally speaking, JVM) is to incorporate OpenTelemetry Instrumentation for Java. It automatically detects when one of the popular libraries is being used in the service and injects the instrumentation without writing any code. It’s also possible to mix automatic instrumentation with manual instrumentation if needed. This method works for all Java 8+ JVMs.

Micro Lesson

Tutorial: Auto-instrumentation of a Java app by OpenTelemetry for K8s Environment.

Installation

The Java agent and configuration needs to be provided for each of the monitored service instances. The address of the OpenTelemetry Collector (or Collector/Agent) needs to be prepared first (OTLP_HTTP_ENDPOINT) and the desired name of the service (SERVICE_NAME) and application (APPLICATION_NAME).

Instruction below applies to OpenTelemetry Java Auto Instrumentation in version 2.6.0.

Step 1: Download and distribute the agent JAR

The agent should be downloaded and distributed to each of the service hosts or containers, as the JVM will need access to it.

note

Ensure that the agent has root permissions for the Java jar files.

Step 2: Update the JVM configuration (valid for version 2.6.0)

Either of the following options could be used as the template, with the following changes:

  • The path to the javaagent JAR file needs to replaced with the location of the file downloaded and distributed in step 1.
  • OTLP_HTTP_ENDPOINT must be provided with the location of the OpenTelemetry Collector/Agent (recommended for production) or OTLP/HTTP source. Refer to the following setup instructions if you do not yet have the collector installed:
  • SERVICE_NAME needs to be replaced with the name used for the identification of the service.
  • APPLICATION_NAME needs to be replaced with the name used for the identification of the application.

The following environment variables need to be made accessible by JVM:

JAVA_TOOL_OPTIONS="-javaagent:path/to/opentelemetry-javaagent.jar"

OTEL_TRACES_EXPORTER=otlp
OTEL_METRICS_EXPORTER=none
OTEL_LOGS_EXPORTER=none
OTEL_EXPORTER_OTLP_ENDPOINT=http://OTLP_HTTP_ENDPOINT
OTEL_SERVICE_NAME=SERVICE_NAME
OTEL_RESOURCE_ATTRIBUTES=application=APPLICATION_NAME

Option 2: Changing the java command line

The command line of the service needs to be appended with the following attributes:

java -javaagent:path/to/opentelemetry-javaagent.jar \
-Dotel.traces.exporter=otlp \
-Dotel.metrics.exporter=none \
-Dotel.logs.exporter=none \
-Dotel.exporter.otlp.endpoint=http://OTLP_HTTP_ENDPOINT \
-Dotel.service.name=SERVICE_NAME \
-Dotel.resource.attributes=application=APPLICATION_NAME \
...
note

When setting up OTLP Endpoint for OpenTelemetry Collector/Agent add port number (4318) e.g. http://OTLP_HTTP_ENDPOINT:4318.

Troubleshooting

To confirm the instrumentation was installed, after starting the service, the following log lines are to be expected in the console:

[otel.javaagent 2024-02-28 10:15:24:219 +0000] [main] INFO io.opentelemetry.javaagent.tooling.VersionLogger - opentelemetry-javaagent - version: 2.6.0
Status
Legal
Privacy Statement
Terms of Use

Copyright © 2024 by Sumo Logic, Inc.