Instrument a Java App
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:
- Java JDK
- Gradle
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.
2. Create another file called DiceApplication.java in the same directory and add the following content to it:
3. Create another file called RollController.java in the same directory and add the following content to it:
4. Run the application using the following command then open http://localhost:8080/rolldice in your web browser to ensure it is working.
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:
2. Set and export variables that specify the Java agent JAR and a console exporter.
Replace PATH/TO above, with your path to the JAR file.
3. Run your application using the following command:
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.
5. Stop the server process to see an output of all the metrics collected.
Java Metrics
Section titled “Java Metrics”| ### Name | ### Description |
|---|---|
| jvm_classes_loaded | The number of loaded classes |
| jvm_gc_collections_count | The total number of garbage collections that have occurred |
| jvm_gc_collections_elapsed | The approximate accumulated collection elapsed time |
| jvm_memory_heap_init | The initial amount of memory that the JVM requests from the operating system for the heap |
| jvm_memory_heap_max | The maximum amount of memory can be used for the heap |
| jvm_memory_heap_used | The current heap memory usage |
| jvm_memory_heap_committed | The amount of memory that is guaranteed to be available for the heap |
| jvm_memory_nonheap_init | The initial amount of memory that the JVM requests from the operating system for non-heap purposes |
| jvm_memory_nonheap_max | The maximum amount of memory can be used for non-heap purposes |
| jvm_memory_nonheap_used | The current non-heap memory usage |
| jvm_memory_nonheap_committed | The amount of memory that is guaranteed to be available for non-heap purposes |
| jvm_memory_pool_init | The initial amount of memory that the JVM requests from the operating system for the memory pool |
| jvm_memory_pool_max | The maximum amount of memory can be used for the memory pool |
| jvm_memory_pool_used | The current memory pool memory usage |
| jvm_memory_pool_committed | The amount of memory that is guaranteed to be available for the memory pool |
| jvm_threads_count | The current number of threads |
Source URL for the example application: Getting Started with OpenTelemetry Java