Integrate OpenTelemetry with Java applications

OpenTelemetry is a set of tools, APIs, and SDKs that make it possible to generate, collect, and export information about running applications including metrics, logs, and traces. This information can then be imported into other tools to visualize, helping us to promptly grasp the problems our application is facing. In this tutorial, I will show you how to integrate OpenTelemetry with Java applications, specifically Spring Boot applications, to understand how to configure and how OpenTelemetry works!

First, I will create a new Spring Boot project with a Web dependency to expose an API as follows:

The content of this API is as simple as this:

Run this application, then request to the address http://localhost:8080/hello, you will see the following results:

Before talking about OpenTelemtry, I would like to mention a little about the concepts of metrics, logs, or traces first. In short, metrics are parameters, used to measure a certain task of the application. Logs are log messages that are printed when we use the application. Traces are tracking metrics about the application’s request and response. Using OpenTelemetry makes it possible to collect this information of the application.

To use OpenTelemetry, by default, you do not need to modify any code. The first thing you need to do is download an agent file of OpenTelemetry, then configure the -javaagent parameter to point to this agent file when running the application.

You download the latest version of the opentelemetry-javaagent.jar file here: https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases
If you want to run OpenTelemetry with the Spring Boot application in the IDE, you can configure it as follows:

If running with .jar file, the command to run is “java -javaagent:/Users/khanh/Downloads/opentelemetry-javaagent.jar -jar <spring-boot>.jar”, guys!

Another configuration that you need to do is configure the Exporter. After OpenTelemetry collects information, it will export this information using these Exporters. OpenTelemetry supports many different types of Exporters, including:

To use which exporter, you can configure using system properties or environment variables respectively. See details for each exporter from the links above to see what system properties or environment variables each exporter type supports!

I will use the Logger exporter for my example application to print the information to the console by configuring more environment variables into the Run Configuration window of the application as follows:

If you run with the .jar file, please configure the environment variable!

Run the application again and request the address http://localhost:8080/hello again, you will see that the Console prints a lot of information as follows:

A lot of information, isn’t it? For the request to the context path “/hello”, you can see the following information about the request and response:

If you look closely, you will see that the HTTP status code for the response is 200 and some other information from the above log lines.

So in this article, I showed you how to integrate OpenTelemetry with Java applications to observability them with default-supported information.

Add Comment