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 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 plugins { id("java") id("org springframework boot") version "3 0 6" id("io spring dependency management") version "1 1 0" } sourcesets { main { java setsrcdirs(setof(" ")) } } repositories { mavencentral() } dependencies { implementation("org springframework boot\ spring boot starter web") } 2\ create another file called diceapplication java in the same directory and add the following content to it package otel; import org springframework boot springapplication; import org springframework boot banner; import org springframework boot autoconfigure springbootapplication; @springbootapplication public class diceapplication { public static void main(string\[] args) { springapplication app = new springapplication(diceapplication class); app setbannermode(banner mode off); app run(args); } } 3\ create another file called rollcontroller java in the same directory and add the following content to it package otel; import java util optional; import java util concurrent threadlocalrandom; import org slf4j logger; import org slf4j loggerfactory; import org springframework web bind annotation getmapping; import org springframework web bind annotation requestparam; import org springframework web bind annotation restcontroller; @restcontroller public class rollcontroller { private static final logger logger = loggerfactory getlogger(rollcontroller class); @getmapping("/rolldice") public string index(@requestparam("player") optional\<string> player) { int result = this getrandomnumber(1, 6); if (player ispresent()) { logger info("{} is rolling the dice {}", player get(), result); } else { logger info("anonymous player is rolling the dice {}", result); } return integer tostring(result); } public int getrandomnumber(int min, int max) { return threadlocalrandom current() nextint(min, max + 1); } } 4\ run the application using the following command then open http //localhost 8080/rolldice in your web browser to ensure it is working gradle assemble java jar /build/libs/java simple jar 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 https //github com/open telemetry/opentelemetry java instrumentation/releases 2\ set and export variables that specify the java agent jar and a console exporter export java tool options=" javaagent\ path/to/opentelemetry javaagent jar" \\ otel traces exporter=otlp \\ otel metrics exporter=otlp \\ otel logs exporter=otlp \\ otel exporter otlp protocol=http/protobuf \\ otel exporter otlp endpoint=https //otel kloudmate com 4318 \\ otel exporter otlp headers="authorization=\<your secret key>" 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 jar /build/libs/java simple jar 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 curl localhost 8080/rolldice 5\ stop the server process to see an output of all the metrics collected source url for the example application https //opentelemetry io/docs/instrumentation/java/getting started/ https //opentelemetry io/docs/instrumentation/java/getting started/