A Quick Start Guide OpenTelemetry java

openetelemetry quick guide java

OpenTelemetry is an open-source observability framework that provides a standardized way of collecting, processing, and exporting telemetry data. It allows developers to instrument their applications to capture data about performance, errors, and other metrics. In this quick start guide, we will explore how to use OpenTelemetry with Java, along with some sample code to help you get started.

Setting up OpenTelemetry for Java

The first step is to set up OpenTelemetry for Java. The easiest way to get started is to use the OpenTelemetry Java SDK, which provides an API for instrumenting your Java application. You can add the OpenTelemetry Java SDK to your project by adding the following dependency to your project’s build file:

<dependency>
  <groupId>io.opentelemetry</groupId>
  <artifactId>opentelemetry-sdk</artifactId>
  <version>1.5.0</version>
</dependency>

Instrumenting your Java Application

Once you have set up OpenTelemetry for Java, you can start instrumenting your Java application to collect telemetry data. To instrument your application, you need to create a Tracer object using the OpenTelemetry SDK. A Tracer is used to create Spans, which represent a unit of work in your application.

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;

Tracer tracer = OpenTelemetry.getGlobalTracerProvider()
                        .get("my-instrumentation-name", "1.0.0");

Span span = tracer.spanBuilder("my-span").startSpan();

In the above code, we first import the required classes from the OpenTelemetry API. We then create a Tracer object using the OpenTelemetry SDK and use it to create a new Span object. The Span represents a unit of work in our application and can be used to capture telemetry data.

Adding Attributes and Events to your Spans

Once you have created a Span, you can add attributes and events to it. Attributes are key-value pairs that provide additional information about the Span, while events represent a specific point in time during the lifecycle of the Span.

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;

Tracer tracer = OpenTelemetry.getGlobalTracerProvider()
                        .get("my-instrumentation-name", "1.0.0");

Span span = tracer.spanBuilder("my-span").startSpan();
span.setAttribute("my-attribute-key", "my-attribute-value");
span.addEvent("my-event");

In the above code, we add a key-value pair to the Span using the setAttribute() method. We also add an event to the Span using the addEvent() method.

Exporting Telemetry Data

Once you have instrumented your application and captured telemetry data, you need to export that data to an external system. OpenTelemetry provides various exporters that allow you to export data to systems such as Prometheus, Jaeger, and Zipkin.

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.exporter.jaeger.JaegerGrpcSpanExporter;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;

Tracer tracer = OpenTelemetrySdk.getTracerProvider().get("my-instrumentation");

Troubleshooting

In this troubleshooting section, we will explore some common issues that can arise when using OpenTelemetry with Java, along with some sample code to help you diagnose and resolve these issues.

  1. Issue: No telemetry data is being captured

If you are not seeing any telemetry data being captured, there are several possible causes. One common cause is that the OpenTelemetry SDK is not properly configured.

Solution: Verify that the OpenTelemetry SDK is properly configured

You can verify that the OpenTelemetry SDK is properly configured by checking that the Tracer object is being created and used to create Spans. You can also check that the Spans are being properly configured with attributes and events.

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;

Tracer tracer = OpenTelemetry.getGlobalTracerProvider()
                        .get("my-instrumentation-name", "1.0.0");

Span span = tracer.spanBuilder("my-span").startSpan();
span.setAttribute("my-attribute-key", "my-attribute-value");
span.addEvent("my-event");
  1. Issue: Telemetry data is not being exported

If you are seeing telemetry data being captured but it is not being exported to an external system, there are several possible causes. One common cause is that the exporter is not properly configured.

Solution: Verify that the exporter is properly configured

You can verify that the exporter is properly configured by checking that the exporter is being created and that the SimpleSpanProcessor is being used to export Spans.

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.exporter.jaeger.JaegerGrpcSpanExporter;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;

Tracer tracer = OpenTelemetrySdk.getTracerProvider().get("my-instrumentation-name", "1.0.0");

JaegerGrpcSpanExporter exporter = JaegerGrpcSpanExporter.builder()
                                    .setEndpoint("localhost:14250")
                                    .build();

OpenTelemetrySdk.getTracerProvider().addSpanProcessor(SimpleSpanProcessor.builder(exporter).build());
  1. Issue: High CPU usage or memory usage

If you are experiencing high CPU or memory usage when using OpenTelemetry, there are several possible causes. One common cause is that too much data is being collected or exported.

Solution: Reduce the amount of data being collected or exported

You can reduce the amount of data being collected or exported by setting appropriate sampling rates or reducing the number of Spans being created. You can also configure the exporter to batch Spans to reduce the number of network calls.

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.exporter.jaeger.JaegerGrpcSpanExporter;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;

Tracer tracer = OpenTelemetrySdk.getTracerProvider().get("my-instrumentation-name", "1.0.0");

JaegerGrpcSpanExporter exporter = JaegerGrpcSpanExporter.builder()
                                    .setEndpoint("localhost:14250")
                                    .setBatchMaxExportTimeout(Duration.ofSeconds(5))
                                   

Conclusion

In this blog we saw how to setup OpenTelementry with Java based application as well as a troubleshooting guide to solve common problems. Hope this helps! 🙂

Tags:

Leave a Reply