Skip to main content

Java

Auto Instrumentation

  1. Download the OpenTelemetry Java Agent jar.

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

  2. Modify the application run command as follows:

    java -javaagent:</path/opentelemetry-javaagent.jar> \
    -Dotel.metrics.exporter=none \
    -Dotel.logs.exporter=none \
    -Dotel.traces.exporter=otlp \
    -Dotel.exporter.otlp.protocol=http/protobuf \
    -Dotel.exporter.otlp.traces.endpoint=http://<ip_address_of_cubeapm_server>:4318/v1/traces \
    -Dotel.exporter.otlp.compression=gzip \
    -Dotel.service.name=<app_name> \
    -jar <myapp>.jar

    Alternatively, the following environment variables can be set:

    JAVA_TOOL_OPTIONS=-javaagent:</path/opentelemetry-javaagent.jar>
    OTEL_METRICS_EXPORTER=none
    OTEL_LOGS_EXPORTER=none
    OTEL_TRACES_EXPORTER=otlp
    OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
    OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://<ip_address_of_cubeapm_server>:4318/v1/traces
    OTEL_EXPORTER_OTLP_COMPRESSION=gzip
    OTEL_SERVICE_NAME=<app_name>

Troubleshooting

The following can be used for debugging:

-Dotel.javaagent.debug=true
or
OTEL_JAVAAGENT_DEBUG=true

Also, traces exporter can be changed from otlp to logging to output traces on console.

The following command can be tried on the application host server to check connectivity to CubeAPM server(s):

telnet <ip_address_of_cubeapm_server> 4318

Manual Instrumentation

OpenTelemetry captures a lot of information automatically. However, there is sometimes a need to capture additional information that may be specific to your application. Follow the below steps to attach additional information to traces generated by your application.

  1. Add the following dependencies to pom.xml

    <project>
    <dependencyManagement>
    <dependencies>
    <dependency>
    <groupId>io.opentelemetry</groupId>
    <artifactId>opentelemetry-bom</artifactId>
    <version>1.37.0</version>
    <type>pom</type>
    <scope>import</scope>
    </dependency>
    </dependencies>
    </dependencyManagement>
    <dependencies>
    <dependency>
    <groupId>io.opentelemetry</groupId>
    <artifactId>opentelemetry-api</artifactId>
    </dependency>
    </dependencies>
    </project>

    If you use Gradle, add the following:

    dependencies {
    implementation(platform("io.opentelemetry:opentelemetry-bom:1.37.0"));
    implementation("io.opentelemetry:opentelemetry-api");
    }
  2. Go to the place in your code where you want to add additional information, and do the following:

    import io.opentelemetry.api.trace.Span;

    @GetMapping
    public String controllerMethod() {
    Span span = Span.current();
    if (span != null) {
    span.setAttribute("custom.any.name.you.want", "any_value_you_want");
    }
    }

Reference

https://opentelemetry.io/docs/instrumentation/java/manual/