Automatically instrument your Java application with OpenTelemetry

This walkthrough shows how to add observability to your Java application using the automatic instrumentation agent for OpenTelemetry Java.

Enrichment with OneAgent

It is currently not possible to enrich automatically instrumented services with host-relevant information. To achieve this, you'd need to switch to manual instrumentation.

Step 1 Get the Dynatrace access details

Determine the API base URL

For details on how to assemble the base OTLP endpoint URL, see Export with OTLP.

The URL should end in /api/v2/otlp.

Get API access token

The access token for ingesting traces, logs, and metrics can be generated under Access Tokens.

Export with OTLP has more details on the format and the necessary access scopes.

Step 2 Instrument your application

  1. Download the latest opentelemetry-javaagent.jar agent file and save it to a directory accessible to your application (for example, libs).

  2. Configure the following environment variables to set the service and protocol details. If you export using OTLP, also set the URL and token variables to the respective values.

    OTEL_EXPORTER_OTLP_ENDPOINT=[URL]
    OTEL_EXPORTER_OTLP_HEADERS="Authorization=Api-Token [TOKEN]"
    OTEL_RESOURCE_ATTRIBUTES="service.name=java-quickstart,service.version=1.0.1"
    OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE=delta
  3. Include the -javaagent parameter in your Java invocation command and specify the path to the agent file. For example, if you started your application from the command line:

    java -javaagent:/PATH/TO/opentelemetry-javaagent.jar -jar myapplication.jar

Step 3 Ensure context propagation

Context propagation is particularly important when network calls (for example, REST) are involved.

If you are using automatic instrumentation and your networking libraries are covered by automatic instrumentation, this will be automatically taken care of by the instrumentation libraries. Otherwise, your code needs to take this into account.

Step 4 optional Configure data capture to meet privacy requirements optional

While Dynatrace automatically captures all OpenTelemetry attributes, only attribute values specified in the allowlist are stored and displayed in the Dynatrace web UI. This prevents accidental storage of personal data, so you can meet your privacy requirements and control the amount of monitoring data stored.

To view your custom attributes, you need to allow them in the Dynatrace web UI first. To learn how to configure attribute storage and masking, see Attribute redaction.

Step 5 Verify data ingestion into Dynatrace

Once you have finished the instrumentation of your application, perform a couple of test actions to create and send demo traces, metrics, and logs and verify that they were correctly ingested into Dynatrace.

To do that for traces, go to Distributed Traces or Distributed Traces Classic (latest Dynatrace) and select the Ingested traces tab. If you use OneAgent, select PurePaths instead.

For metrics and logs, go to Metrics or Logs or Logs & Events (latest Dynatrace).