Skip to main content
Sumo Logic

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 7+ JVMs.

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 (OPENTELEMETRY_COLLECTOR_HOSTNAME) and the desired name of the service (SERVICE_NAME). 

Instruction below applies to OpenTelemetry Java Auto Instrumentation in version 0.13.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.

Step 2 - Update the JVM configuration (valid for version 0.13.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.

  • OPENTELEMETRY_COLLECTOR_HOSTNAME must be provided with the location of the OpenTelemetry Collector/Agent or centralized collector (depending on which one is used). Refer to the following setup instructions if you don't have yet collector installed:

  • SERVICE_NAME needs to be replaced with the name used for the identification of the service.

Option 1 - leveraging environment variables (recommended)

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

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

OTEL_EXPORTER=otlp_span
OTEL_EXPORTER_OTLP_ENDPOINT=OPENTELEMETRY_COLLECTOR_HOSTNAME:55680
OTEL_RESOURCE_ATTRIBUTES="service.name=SERVICE_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-all.jar \
    -Dotel.exporter=otlp_span \
    -Dotel.exporter.otlp.endpoint=OPENTELEMETRY_COLLECTOR_HOSTNAME:55680 \
    -Dotel.resource.attributes="service.name=SERVICE_NAME" \
    ...

Troubleshooting

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

[opentelemetry.auto.trace 2021-01-12 11:39:59:838 +0000] [main] INFO io.opentelemetry.javaagent.tooling.OpenTelemetryInstaller - Installed span exporter: io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter

When errors are present in the console, describing that some system libraries are missing or that connection cannot be established, a Zipkin exporter can be used instead of OTLP, for example:

OTEL_EXPORTER=zipkin
OTEL_EXPORTER_ZIPKIN_ENDPOINT=http://OPENTELEMETRY_COLLECTOR_HOSTNAME:9411/api/v2/spans
OTEL_EXPORTER_ZIPKIN_SERVICE_NAME=SERVICE_NAME