OpenTelemetry Support
Using OpenTelemetry SDKs

OTel For Java

6min

In this guide, we will walk you through the process of setting up and using OpenTelemetry in Java. You will learn how to instrument a simple Java application to produce traces, metrics, and logs.

Step 1: Prerequisites

Before diving into OpenTelemetry, be sure that you have the following installed locally:

Step 2: Example Application

For this tutorial, we will be using a basic Spring Boot application. However, OpenTelemetry Java is compatible with other web frameworks like Apache Wicket and Play as well. Feel free to adapt the instructions to your preferred framework.

Step 3: Installation

1. Set up an environment in a new directory called java-simple. Create a file build.gradle.kts in that directory and add the following content to it.

Java


2. Create another file called DiceApplication.java in the same directory and add the following content to it:

Java


3. Create another file called RollController.java in the same directory and add the following content to it:

Java


4. Run the application using the following command then open http://localhost:8080/rolldice in your web browser to ensure it is working.

Java


Step 4: Instrumentation

Now we are going to use Java agent to automatically instrument the application at launch time. We will configure the Java agent using environment variables. 1. Download the opentelemetry-javaagent.jar from Releases of the opentelemetry-java-instrumentation repository. The JAR file contains the agent and all automatic instrumentation packages:

text


2. Set and export variables that specify the Java agent JAR and a console exporter.

Text


The exporter URL mentioned above, along with the KloudMate API-Key as the Authorization header will send the data to KloudMate.

Replace PATH/TO above, with your path to the JAR file.

3. Run your application using the following command:

Java


Note the output from the otel.javaagent.

4. Send a request using curl from another terminal. You will see trace & log output from the server and client.

Java


5. Stop the server process to see an output of all the metrics collected.