- Metrics
Helidon SE provides the following to support metrics:
- 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). - A base set of metrics, available at
/metrics/base, as specified by the MicroProfile Metrics specification. - A set of Helidon-specific metrics, available at
/metrics/vendor
- The endpoint
Prerequisites
Declare the following dependency in your project:
<dependency>
<groupId>io.helidon.metrics</groupId>
<artifactId>helidon-metrics</artifactId>
</dependency>content_copy
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();content_copy
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();
}
}content_copy
- 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:
- JSON format - used when the HTTP Accept header matches
application/json - Prometheus text format - used when the HTTP Accept header is
text/plainor otherwise does not matchapplication/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
#...content_copy
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,
#...content_copy
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.