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.
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).
Step 1 - Download and distribute the agent JAR
The most recent version of 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 versions 0.7.0 - 0.8.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).
SERVICE_NAME needs to be replaced with the name used for the identification of the service.
Option 1 - 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 \
Option 2 - leveraging environment variables
The following environment variables need to be made accessible by JVM:
Step 3 (optional) - confirming that the instrumentation was installed
After starting the service with instrumentation enabled, the following log lines are to be expected in the console:
[opentelemetry.auto.trace 2020-08-26 12:15:10:343 +0200] [main] INFO io.opentelemetry.auto.tooling.TracerInstaller - Installed span exporter: io.opentelemetry.exporters.otlp.OtlpGrpcSpanExporter
[opentelemetry.auto.trace 2020-08-26 12:15:10:348 +0200] [main] INFO io.opentelemetry.auto.tooling.VersionLogger - opentelemetry-javaagent - version: 0.7.0~227bde87c