Metrics

Helidon SE provides the following to support metrics:

  1. The endpoint /metrics: A configurable endpoint that exposes metrics information in JSON format (as specified by the MicroProfile Metrics specification) or in plain text (for Prometheus metrics).
  2. A base set of metrics, available at /metrics/base, as specified by the MicroProfile Metrics specification.
  3. A set of Helidon-specific metrics, available at /metrics/vendor

Prerequisites

Declare the following dependency in your project:

    <dependency>
        <groupId>io.helidon.metrics</groupId>
        <artifactId>helidon-metrics</artifactId>
    </dependency>
Copied

Using Metrics in Your Application

To enable Metrics, register it with the WebServer.

import io.helidon.metrics.MetricsSupport;
//...

Routing.builder()
                .register(MetricsSupport.create())
                .register("/myapp", new MyService())
                .build();
Copied

Then you can use metrics in your service.

Define and use a Metrics Counter
import io.helidon.metrics.RegistryFactory;
import org.eclipse.microprofile.metrics.Counter;
import org.eclipse.microprofile.metrics.MetricRegistry;
//...

public class MyService implements Service {

    private final MetricRegistry registry = RegistryFactory.getRegistryFactory().get()
        .getRegistry(MetricRegistry.Type.APPLICATION); 
    private final Counter accessCtr = registry.counter("accessctr"); 

    @Override
    public void update(Routing.Rules rules) {
        rules
             .any(this::countAccess)
             .get("/", this::myGet);
    }

    private void countAccess(ServerRequest request, ServerResponse response) {
            accessCtr.inc(); 
            request.next();
    }
}
Copied
  • Get the application metrics registry
  • Create a counter in that registry
  • Increment the counter for every request

Accessing Metrics Endpoint

Access metrics data via the /metrics endpoint. Two reporting formats are supported. The HTTP Accept header sent by the client determines the reporting format:

  1. JSON format - used when the HTTP Accept header matches application/json
  2. Prometheus text format - used when the HTTP Accept header is text/plain or otherwise does not match application/json
Example Reporting: Prometheus format
curl -s -H 'Accept: text/plain' -X GET http://localhost:8080/metrics/
# TYPE base:classloader_total_loaded_class_count counter
# HELP base:classloader_total_loaded_class_count Displays the total number of classes that have been loaded since the Java virtual machine has started execution.
base:classloader_total_loaded_class_count 3157
#...
Copied
Example Reporting: JSON format
curl -s -H 'Accept: application/json' -X GET http://localhost:8080/metrics/ | json_pp
{
   "base" : {
      "memory.maxHeap" : 3817865216,
      "memory.committedHeap" : 335544320,
#...
Copied

In addition to your application metrics the reports contain other metrics of interest such as system and VM information.

For full details see the MicroProfile Metrics specification.

The Metrics component in Helidon SE is the core for the Helidon MP implementation of the MicroProfile Metrics specification.