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

Maven Coordinates

To enable Metrics add the following dependency to your project’s pom.xml (see Managing Dependencies).

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

Using Metrics in Your Application

To enable Metrics, register it with the WebServer.

import io.helidon.metrics.serviceapi.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.api.RegistryFactory;
import org.eclipse.microprofile.metrics.Counter;
import org.eclipse.microprofile.metrics.MetricRegistry;
//...

public class MyService implements Service {

    private final MetricRegistry registry = RegistryFactory.getInstance()
        .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

Helidon-provided endpoints for /metrics do their work synchronously, using the same thread on which the request arrived via Netty. To prevent performance degradation, avoid including long-running code that can be invoked by these handlers while Helidon is responding to the metric.
For example, if you implement your own application-specific metric types, you will write logic to format the JSON and OpenMetrics output for those metric types. Helidon invokes this formatting logic whenever a client accesses the /metrics endpoints, so make that formatting code as efficient as possible.

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.