Introduction to Custom Metrics in Java with Logz.io RemoteWrite SDK

RemoteWrite SDK for Prometheus by Logz.io

We just announced the creation of a new RemoteWrite SDK to support custom metrics from applications using several different languages. This tutorial will give a quick rundown of how to use the Java SDK. This SDK – like the others – is completely free and open source, and is meant to apply to any output destination, not just Logz.io.

As we explained in our Python tutorial, Prometheus users can send metrics directly to Logz.io using the RemoteWrite protocol without sending them to Prometheus first. Each SDK can work with different frameworks like Thanos, Cortex, and M3DB. 

What is Micrometer?

We use OpenTelemetry for other custom metrics implementations, but the Java implementation with OTEL is still in the alpha stage. Because of this, we have gone with an older but equally reliable instrumentation tool: micrometer. Micrometer is an instrumentation library for apps based in Java Virtual Machines (JVM) that lets other libraries be included in metrics-shipping apps. 

It maintains pre-instrumented caches, garbage collectors, and other bindings to interface directly with counters, gauges, timers and other metrics. It has support for Influx and Telegraf, Graphite, Azure, Stackdriver, Prometheus and other apps.

The new RemoteWrite SDK is an open source tool from our team here at Logz.io. We wanted to make it easier for our customers to send metrics straight from code, and decided to move toward something that would benefit the entire Prometheus community. 

Configure Java for Shipping Custom Metrics 

Prerequisites are Java 8 or higher. You can add the Micrometer registry in one of three ways: 1) Maven, 2) Gradle, or 3) direct import.

brew install java

brew install maven

Add the following to your pom.xml in your dependencies section.

<dependency>
    <groupId>io.logz.micrometer</groupId>
    <artifactId>micrometer-registry-logzio</artifactId>
    <version>1.0</version>
</dependency>

Use this within your main application .java file. Be sure to fill in your API key and listener hosts.

package your_package;
import io.micrometer.core.instrument.*;
import io.micrometer.core.instrument.Timer;
import io.micrometer.logzio.LogzioConfig;
import io.micrometer.logzio.LogzioMeterRegistry;

class MicrometerLogzio {

   public static void main(String[] args) {
       // initialize config
      LogzioConfig logzioConfig = new LogzioConfig() {
         @Override
         public String get(String key) {
            return null;
         }
         @Override
         public String uri() {
            return "<<LISTENER-HOST>>";
         }

         @Override
         public String token() {
            return "<<PROMETHEUS-METRICS-SHIPPING-TOKEN>>";
         }

         @Override
         public Duration step() {
            return Duration.ofSeconds(<<interval>>);
         }
      };
      // Initialize registry
      LogzioMeterRegistry registry = new LogzioMeterRegistry(logzioConfig, Clock.SYSTEM);
      // Define tags (labels)
      ArrayList<Tag> tags = new ArrayList<>();
      tags.add(Tag.of("env","dev"));

   }
}

Individual Custom Metric Options

Including counter, you can create one of four types of metric instruments with Micrometer: gauge, distributionsummary, and timer.

For instance, let’s look at valuerecorder. Use the same code snippet above, with the section configuring the counter replaced with the following:

Counter counter = Counter
        .builder("counter_example")
        .description("a description of what this counter does") // optional
        .tags(tags) // optional
        .register(registry);
// Increment your counter
counter.increment(); 
counter.increment(2); 
// The following metric will be created and sent to Logz.io: counter_example_total{env="dev"} 3

Better Metrics

This integration shows Logz.io’s commitment to and enthusiasm for OpenTelemetry, and also accommodates developers working in and on original projects. Direct-from-code metrics – plus other forms of telemetry – will see further support from us with new SDK options in other languages in the very near future.

Subscribe to the Logz.io blog for more updates!

Try Prometheus-as-a-Service and our Advanced Metrics UI!

Internal

Organize Your Kubernetes Logs On One Unified SaaS Platform

Learn More