Skip to main content

How to send custom metrics with OpenTelemetry in java

 OpenTelemetry is an observability framework that allows you to collect, process, and export telemetry data such as traces and metrics from your applications. In Java, sending custom metrics with OpenTelemetry involves a few key steps. The following steps assume that you have already set up OpenTelemetry in your Java project.

1. Dependency Setup: Make sure you have the necessary dependencies in your project. Add the OpenTelemetry API, SDK, and the metrics module to your pom.xml (if you are using Maven):

<dependencies> <!-- OpenTelemetry API and SDK --> <dependency> <groupId>io.opentelemetry</groupId> <artifactId>opentelemetry-api</artifactId> <version>1.7.0</version> <!-- Use the latest version --> </dependency> <dependency> <groupId>io.opentelemetry</groupId> <artifactId>opentelemetry-sdk</artifactId> <version>1.7.0</version> </dependency> <!-- OpenTelemetry Metrics --> <dependency> <groupId>io.opentelemetry</groupId> <artifactId>opentelemetry-extension-metrics</artifactId> <version>1.7.0</version> </dependency> </dependencies>

2. Initialize OpenTelemetry: Initialize the OpenTelemetry SDK in your application. This typically involves setting up a TracerProvider and optionally configuring exporters. This initialization is necessary for both traces and metrics.

import io.opentelemetry.OpenTelemetry; import io.opentelemetry.metrics.Meter; import io.opentelemetry.metrics.MeterProvider; import io.opentelemetry.metrics.SdkMeterProvider; import io.opentelemetry.metrics.MeterRegistry; public class OpenTelemetryInitializer { public static void initialize() { SdkMeterProvider meterProvider = SdkMeterProvider.builder().buildAndRegisterGlobal(); OpenTelemetry.setTracerProvider(meterProvider); // Other OpenTelemetry initialization code (traces, exporters, etc.) goes here } }

3. Create and Record Custom Metrics: Create a Meter instance from the MeterProvider and use it to create custom metrics. Record values for these metrics as needed in your application.

import io.opentelemetry.metrics.DoubleCounter; import io.opentelemetry.metrics.DoubleValueObserver; import io.opentelemetry.metrics.LongUpDownCounter; import io.opentelemetry.metrics.LongValueRecorder; public class CustomMetricsExample { private static final Meter meter = OpenTelemetry.getGlobalMeter("example-meter"); private static final LongValueRecorder longValueRecorder = meter.longValueRecorderBuilder("custom_long_metric") .setDescription("A custom long metric") .setUnit("1") .build(); private static final DoubleValueObserver doubleValueObserver = meter.doubleValueObserverBuilder("custom_double_metric") .setDescription("A custom double metric") .setUnit("1") .setUpdater(result -> result.observe(Math.random() * 100)) .build(); public static void recordMetrics() { // Record values for custom metrics longValueRecorder.record(42); } public static void main(String[] args) { OpenTelemetryInitializer.initialize(); recordMetrics(); } }

In this example, longValueRecorder is a custom long metric, and doubleValueObserver is a custom double metric. You can create different types of metrics based on your use case.

4. Export Metrics: Configure an exporter to send the recorded metrics to your chosen backend (e.g., Prometheus, Jaeger, or another supported backend). The configuration depends on the exporter you're using.

For example, if you're using the OpenTelemetry Prometheus Exporter, you might need to add the following dependency:

<dependency> <groupId>io.opentelemetry</groupId> <artifactId>opentelemetry-exporters-prometheus</artifactId> <version>1.7.0</version> </dependency>

And configure the Prometheus exporter in your initialization code:

import io.opentelemetry.exporter.prometheus.PrometheusCollector; import io.opentelemetry.exporter.prometheus.PrometheusCollectorBuilder; public class OpenTelemetryInitializer { public static void initialize() { SdkMeterProvider meterProvider = SdkMeterProvider.builder().buildAndRegisterGlobal(); PrometheusCollector.builder().setMetricProducer(meterProvider).buildAndRegister(); OpenTelemetry.setTracerProvider(meterProvider); // Other OpenTelemetry initialization code (traces, etc.) goes here } }

By following these steps, you can integrate OpenTelemetry into your Java application, create custom metrics, and export them to the monitoring backend of your choice.

Comments