- Micrometer Metrics
Helidon MP simplifies how you can use Micrometer for application-specific metrics:
The endpoint
/micrometer: A configurable endpoint that exposes metrics according to which Micrometer meter registry responds to the HTTP request.The Micrometer annotations
@Timedand@Counted.Configuration to tailor the Prometheus and other Micrometer meter registries.
In Helidon 2.6.14, Micrometer support is separate from the Helidon MP metrics API and the built-in Helidon metrics.
Prerequisites
Declare the following dependency in your project:
<dependency>
<groupId>io.helidon.integrations.micrometer</groupId>
<artifactId>helidon-integrations-micrometer-cdi</artifactId>
</dependency>Micrometer supports different types of meter registries which have different output styles and formats. Helidon provides built-in support for the Prometheus meter registry. To use other meter registry types, you will need to add dependencies for them to your pom.xml and, optionally, add configuration to set them up as you wish.
Using Micrometer in Your Application
Add the Micrometer @Timed and @Counted annotations to methods in your application.
The examples below enhance the Helidon MP QuickStart application to track (by time and invocation count) all GET methods and to count all requests for a personalized greeting.
Add Micrometer annotations
GET methodsimport io.micrometer.core.annotation.Counted;
import io.micrometer.core.annotation.Timed;
private static final String PERSONALIZED_GETS_COUNTER_NAME = "personalizedGets";
private static final String PERSONALIZED_GETS_COUNTER_DESCRIPTION = "Counts personalized GET operations";
private static final String GETS_TIMER_NAME = "allGets";
private static final String GETS_TIMER_DESCRIPTION = "Tracks all GET operations";
@GET
@Produces(MediaType.APPLICATION_JSON)
@Timed(value = GETS_TIMER_NAME, description = GETS_TIMER_DESCRIPTION, histogram = true)
public JsonObject getDefaultMessage() {
return createResponse("World");
}
@Path("/{name}")
@GET
@Produces(MediaType.APPLICATION_JSON)
@Counted(value = PERSONALIZED_GETS_COUNTER_NAME, description = PERSONALIZED_GETS_COUNTER_DESCRIPTION)
@Timed(value = GETS_TIMER_NAME, description = GETS_TIMER_DESCRIPTION, histogram = true)
public JsonObject getMessage(@PathParam("name") String name) {
return createResponse(name);
}- Declare constants used in annotating multiple methods.
- Use
@Timedto time and count bothGETmethods. - Use
@Countedto count the accesses to theGETmethod that returns a personalized greeting.
Using the Helidon-provided Micrometer MeterRegistry from Code
In addition to annotating your methods, you can create, look up, and update metrics explicitly in your code.
Add the following injection to a bean:
MeterRegistry@Inject
private MeterRegistry registry;Helidon automatically injects a reference to the MeterRegistry it manages into your code. Your code can use the normal Micrometer API with this registry to create, find, update, and even delete meters.
Overriding Defaults for Built-in Meter Registry Types
Unless you specify otherwise, Helidon uses defaults for any built-in Micrometer meter registry. For example, Helidon configures the built-in Prometheus registry using PrometheusConfig.DEFAULT.
To use configuration to control the selection and behavior of Helidon’s built-in Micrometer meter registries, include in your configuration (such as application.yaml) a micrometer.builtin-registries section.
micrometer:
builtin-registries:
- type: prometheusmicrometer:
builtin-registries:
- type: prometheus
prefix: myPrefixNote that the first config example is equivalent to the default Helidon Micrometer behavior; Helidon by default supports the Prometheus meter registry.
The configuration keys that are valid for the builtin-registries child entries depend on the type of Micrometer meter registry. For example, the Prometheus meter registry supports the prefix configuration setting but other meter registries might not and might support other settings. Refer to the documentation for the meter registry you want to configure to find out what items apply to that registry type.
Helidon does not validate the configuration keys you specify for meter registries.
Accessing the Helidon Micrometer Endpoint
By default, Helidon Micrometer integration exposes the /micrometer endpoint. You can override this using the micrometer.web-context configuration key.
Within Helidon, each type of meter registry is paired with code that examines the incoming HTTP request and decides whether the request matches up with the associated meter registry. The first pairing that accepts the request returns the response.