Micrometer provides an abstraction layer for metrics collection.
It defines an API for basic meter types, like counters, gauges, timers, and distribution summaries, along with a MeterRegistry
API that generalizes metrics collection and propagation for different backend monitoring systems.
Important
|
Micrometer is the recommended approach to metrics for Quarkus. |
Note
|
By default, the metrics are exposed on the main HTTP server. If you would like to surface metrics from a separate management port, see the Managed interface section. |
Quarkus Micrometer extensions are structured in the same way as the Micrometer project.
The quarkus-micrometer
extension provides core Micrometer support and runtime integration.
Other Quarkus and Quarkiverse extensions use the Quarkus Micrometer extension to provide support for other monitoring systems.
Quarkus extensions:
-
micrometer
-
micrometer-registry-prometheus
Quarkiverse extensions (may be incomplete):
-
micrometer-registry-azure-monitor
-
micrometer-registry-datadog
-
micrometer-registry-graphite
-
micrometer-registry-influx
-
micrometer-registry-jmx
-
micrometer-registry-newrelic-telemetry
-
micrometer-registry-otlp
-
micrometer-registry-signalfx
-
micrometer-registry-stackdriver
-
micrometer-registry-statsd
To add support for Prometheus metrics to your application, for example, use the micrometer-registry-prometheus
extension.
It will bring in the Quarkus Micrometer extension and Micrometer core libraries as dependencies.
Add the extension to your project using following command (from your project directory):
This will add the following to your build file:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-micrometer-registry-prometheus</artifactId>
</dependency>
implementation("io.quarkus:quarkus-micrometer-registry-prometheus")
And you’re all set!
A similar process applies for other meter registry extensions.
To use the Micrometer StackDriver MeterRegistry, for example, you would use the
quarkus-micrometer-registry-stackdriver
extension from the Quarkiverse:
<dependency>
<groupId>io.quarkiverse.micrometer.registry</groupId>
<artifactId>quarkus-micrometer-registry-stackdriver</artifactId>
</dependency>
implementation("io.quarkiverse.micrometer.registry:quarkus-micrometer-registry-stackdriver")
If the Micrometer registry you would like to use does not yet have an associated extension,
use the quarkus-micrometer
extension and bring in the Micrometer meter registry dependency directly:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-micrometer</artifactId>
</dependency>
<dependency>
<groupId>com.acme</groupId>
<artifactId>custom-micrometer-registry</artifactId>
<version>...</version>
</dependency>
implementation("io.quarkus:quarkus-micrometer")
implementation("com.acme:custom-micrometer-registry")
You will then need to specify your own provider to configure and initialize the MeterRegistry, as discussed in the next section.
Use a custom @Produces
method to create and configure a your own MeterRegistry
if you need to.
The following example customizes the line format used for StatsD:
@Produces
@Singleton // (1)
public StatsdMeterRegistry createStatsdMeterRegistry(StatsdConfig statsdConfig, Clock clock) { // (2)
// define what to do with lines
Consumer<String> lineLogger = line -> logger.info(line);
// inject a configuration object, and then customize the line builder
return StatsdMeterRegistry.builder(statsdConfig)
.clock(clock)
.lineSink(lineLogger)
.build();
}
-
The method returns a
@Singleton
. -
The method returns the specific type of
MeterRegistry
This example corresponds to the following instructions in the Micrometer documentation: Micrometer StatsD: Customizing the Metrics Sink
Use MicroProfile Config to inject any configuration attributes you need to configure the registry.
Most Micrometer registry extensions, like quarkus-micrometer-registry-statsd
, provide registry-specific configuration objects that are integrated with the Quarkus configuration model.
The Quarkiverse GitHub Repository can be a useful implementation reference.
Metrics data is used in the aggregate to observe how data changes over time. This data is used for trend analysis, anomaly detection, and alerting. Data is stored by backend monitoring systems in time series databases, with new values appended to the end of the series.
Note
|
Metrics are constructed lazily. You may not see any data for the metric you’re looking for until you’ve performed an action that will create it, like visiting an endpoint. |
Meter names should use dots to separate segments, a.name.like.this
.
Micrometer applies naming conventions to convert registered meter names to match the expectations of backend monitoring systems.
Given the following declaration of a timer: registry.timer("http.server.requests")
, applied naming conventions will emit the following metrics for different monitoring systems:
-
Prometheus:
http_server_requests_duration_seconds
-
Atlas:
httpServerRequests
-
Graphite:
http.server.requests
-
InfluxDB:
http_server_requests
Metrics, single numerical measurements, often have additional data captured with them. This ancillary data is used to group or aggregate metrics for analysis. The Micrometer API refers to this dimensional data as tags, but you may see it referred to as "labels" or "attributes" in other documentation sources.
Micrometer is built primarily for backend monitoring systems that support dimensional data (metric names that are enriched with key/value pairs). For hierarchical systems that only support a flat metric name, Micrometer will flatten the set of key/value pairs (sorted by key) and add them to the name.
Tags can be specified when a meter is registered with a MeterRegistry
or using a Meter Filter.
See the Micrometer documentation for additional advice on tag naming.
Important
|
Each unique combination of metric name and dimension produces a unique time series. Using an unbounded set of dimensional data can lead to a "cardinality explosion", an exponential increase in the creation of new time series. |
To register meters, you need a reference to a MeterRegistry
, which is configured and maintained by the Micrometer extension.
Use one of the following methods to obtain a reference to a MeterRegistry
:
-
Use CDI Constructor injection:
package org.acme.micrometer; import io.micrometer.core.instrument.MeterRegistry; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; import jakarta.ws.rs.Produces; @Path("/example") @Produces("text/plain") public class ExampleResource { private final MeterRegistry registry; ExampleResource(MeterRegistry registry) { this.registry = registry; } }
-
Use a
MeterRegistry
member variable and use@Inject
:@Inject MeterRegistry registry;
-
Use the global
MeterRegistry
:MeterRegistry registry = Metrics.globalRegistry;
Gauges measure a value that can increase or decrease over time, like the speedometer on a car. Gauges can be useful when monitoring the statistics for a cache or collection.
Gauge values are sampled rather than set; there is no record of how the value associated with a gauge may have changed between measurements.
Micrometer provides a few mechanisms for creating gauges:
-
Wrap construction of a collection to monitor its size:
List<String> list = registry.gaugeCollectionSize("fantastic.list", // (1) Tags.of("key", "value") // optional (2) new ArrayList<>()); // (3)
-
Create a new gauge,
list.size
, using the dot-separated convention. -
Associate tags with the gauge. Gauge tag values are constant, and must be assigned at construction time.
-
Construct the array list whose size should be observed.
-
-
Use a builder to create a Gauge that will call a function:
Gauge.builder("jvm.threads.peak", threadBean, ThreadMXBean::getPeakThreadCount) // (1) .baseUnit(BaseUnits.THREADS) // optional (2) .description("The peak live thread count...") // optional (3) .tags("key", "value") // optional (4) .register(registry); // (5)
-
Create a new gauge called
jvm.threads.peak
that will callgetPeakThreadCount
onthreadBean
, an instance ofThreadMXBean
-
Define the base unit, see BaseUnits.java for predefined values.
-
Provide a description of the Gauge
-
Associate tags with the gauge
-
Register the Gauge with the MeterRegistry
-
See Gauges in the Micrometer documentation for more information and examples.
Of note are two special cases: TimeGauge
for measuring time, and a MultiGauge
for reporting several criteria together.
Note
|
Micrometer does not create strong references to the objects it observes by default.
Depending on the registry, Micrometer either omits gauges that observe
objects that have been garbage-collected entirely or uses NaN (not a number) as the observed value.
|
When should you use a gauge? Only use a gauge when you can’t use something else. Gauges can be less straight-forward to use than other meters. If what you are measuring can be counted (because the value always increments), use a counter instead.
Counters measure values that only increase. Use one of the methods below to create a counter.
-
Use a convenience method on the
MeterRegistry
:registry.counter("example.prime.number", "type", "prime"); // (1) (2)
-
example.prime.number
is the counter name. -
type
is a dimensional tag with valueprime
.
-
-
Use
Counter.builder
to provide a description and units:Counter.builder("count.me") // (1) .baseUnit("beans") // optional (2) .description("a description") // optional (3) .tags("region", "test") // optional (4) .register(registry);
-
Create a new counter called
count.me
-
Define a custom base unit. See BaseUnits.java for predefined values.
-
Provide a description for the counter
-
Associate tags with the counter
-
-
Annotate a method
@Counted(value = "counted.method", extraTags = { "extra", "annotated" }) // (1) (2) void countThisMethod(){ ... }
-
A CDI interceptor will create and register a counter called
counted.method
-
The interceptor-created counter will have the "extra" dimension tag with value "annotated"
-
See Counters in the Micrometer documentation for more information and examples, including the less common FunctionCounter
that can be used to measure the result returned by an always increasing function.
When should you use a counter? Use a counter if you are doing something that can not be either timed or summarized. If you want to understand more about how a value is changing, a timer (when the base unit of measurement is time) or a distribution summary might be more appropriate.
Timers and distribution summaries in Micrometer are very similar. Both meters record data, and can capture additional histogram or percentile data. While distribution summaries can be use for arbitrary types of data, timers are optimized for measuring time and durations.
Timers and distribution summaries store at least three values internally:
-
the aggregation of all recorded values as a sum
-
the number of values that have been recorded (a counter)
-
the highest value seen within a decaying time window (a gauge).
Use a distribution summary to record a value, not time. Use one of the following methods to create a distribution summary.
-
Use a convenience method on the
MeterRegistry
:registry.summary("bytes.written", "protocol", "http"); // (1) (2)
-
bytes.written
is the summary name -
protocol
is a dimensional tag with valuehttp
.
-
-
Use
DistributionSummary.builder
to provide a description and units:DistributionSummary.builder("response.size") // (1) .baseUnit("bytes") // optional (2) .description("a description") // optional (3) .tags("protocol", "http") // optional (4) .register(registry);
-
Create a new distribution summary called
response.size
-
Use
bytes
as a base unit. See BaseUnits.java for predefined values. -
Provide a description for the distribution summary
-
Associate tags with the distribution summary
-
Timers measure short-duration latencies and how often they occur. Negative values are not supported, and longer durations could cause an overflow of the total time (Long.MAX_VALUE nanoseconds (292.3 years)).
Use one of the following methods to construct a timer.
-
Use a convenience method on the
MeterRegistry
:registry.timer("fabric.selection", "primary", "blue"); // (1) (2)
-
fabric.selection
is the summary name -
primary
is a dimensional tag with valueblue
.
-
-
Use
Timer.builder
to provide a description and units:Timer.builder("my.timer") // (1) (2) .description("description ") // optional (3) .tags("region", "test") // optional (4) .register(registry);
-
Create a new timer called
my.timer
-
Timers measure time, and will convert it into the units required by the monitoring backend
-
Provide a description for the distribution summary
-
Associate tags with the timer
-
-
Annotate a method
@Timed(value = "call", extraTags = {"region", "test"}) // (1) (2)
-
A CDI interceptor will create and register a timer called
call
-
The interceptor-created timer will have the "region" dimension tag with value "test"
-
Micrometer provides the following convenience mechanisms for recording durations.
-
Wrap the invocation of a
Runnable
:timer.record(() -> noReturnValue());
-
Wrap the invocation of a
Callable
:timer.recordCallable(() -> returnValue());
-
Create a wrapped
Runnable
for repeated invocation:Runnable r = timer.wrap(() -> noReturnValue());
-
Create a wrapped
Callable
for repeated invocation:Callable c = timer.wrap(() -> returnValue());
-
Use a
Sample
for more complex code paths:Sample sample = Timer.start(registry); // (1) doStuff; // (2) sample.stop(registry.timer("my.timer", "response", response.status())); // (3)
-
We create a sample, which records the start of the timer.
-
The sample can be passed along as context
-
We can choose the timer when the sample is stopped. This example uses a response status as a tag identifying the timer, which won’t be known until processing is complete.
-
Both timers and distribution summaries can be configured to emit additional statistics, like histogram data, precomputed percentiles, or service level objective (SLO) boundaries. See Timers and Distribution Summaries in the Micrometer documentation for more information and examples, including memory footprint estimation for both types.
Important
|
The count, sum, and histogram data associated with timers and distribution summaries can be re-aggregated across dimensions (or across a series of instances). Precomputed percentile values can not. Percentiles are unique to each dataset (the 90th percentile of this collection of measurements). |
The Micrometer extension automatically times HTTP server requests. Following Prometheus naming conventions for
timers, look for http_server_requests_seconds_count
, http_server_requests_seconds_sum
, and
http_server_requests_seconds_max
. Dimensional labels have been added for the requested uri, the HTTP method
(GET, POST, etc.), the status code (200, 302, 404, etc.), and a more general outcome field.
You can disable measurement of HTTP endpoints using the quarkus.micrometer.binder.http-server.ignore-patterns
property.
This property accepts a comma-separated list of simple regex match patterns identifying URI paths that should
be ignored.
For example, setting quarkus.micrometer.binder.http-server.ignore-patterns=/example/prime/[0-9]+
will
ignore a request to http://localhost:8080/example/prime/7919
.
A request to http://localhost:8080/example/gauge/7919
would still be measured.
The micrometer extension will make a best effort at representing URIs containing path parameters in templated form.
Using examples from above, a request to http://localhost:8080/example/prime/7919
should appear as an attribute of
http_server_requests_seconds_*
metrics with a value of uri=/example/prime/{number}
.
Use the quarkus.micrometer.binder.http-server.match-patterns
property if the correct URL can not be determined.
This property accepts a comma-separated list defining an association between a simple regex match pattern and a replacement
string.
For example, setting
quarkus.micrometer.binder.http-server.match-patterns=/example/prime/=/example/{jellybeans}` would use the value
`/example/{jellybeans}` for the uri attribute any time the requested uri matches `/example/prime/[0-9]
.
By default, the metrics are exported using the Prometheus format application/openmetrics-text
,
you can revert to the former format by specifying the Accept
request header to text/plain
(curl -H "Accept: text/plain" localhost:8080/q/metrics/
).
Quarkus provides a variety of way to customize Micrometer.
Micrometer uses MeterFilter
instances to customize the metrics emitted by MeterRegistry
instances.
The Micrometer extension will detect MeterFilter
CDI beans and use them when initializing MeterRegistry
instances.
@Singleton
public class CustomConfiguration {
@ConfigProperty(name = "deployment.env")
String deploymentEnv;
/** Define common tags that apply only to a Prometheus Registry */
@Produces
@Singleton
@MeterFilterConstraint(applyTo = PrometheusMeterRegistry.class)
public MeterFilter configurePrometheusRegistries() {
return MeterFilter.commonTags(Arrays.asList(
Tag.of("registry", "prometheus")));
}
/** Define common tags that apply globally */
@Produces
@Singleton
public MeterFilter configureAllRegistries() {
return MeterFilter.commonTags(Arrays.asList(
Tag.of("env", deploymentEnv)));
}
/** Enable histogram buckets for a specific timer */
@Produces
@Singleton
public MeterFilter enableHistogram() {
return new MeterFilter() {
@Override
public DistributionStatisticConfig configure(Meter.Id id, DistributionStatisticConfig config) {
if(id.getName().startsWith("myservice")) {
return DistributionStatisticConfig.builder()
.percentiles(0.5, 0.95) // median and 95th percentile, not aggregable
.percentilesHistogram(true) // histogram buckets (e.g. prometheus histogram_quantile)
.build()
.merge(config);
}
return config;
}
};
}
}
In this example, a singleton CDI bean will produce two different MeterFilter
beans.
One will be applied only to
Prometheus MeterRegistry
instances (using the @MeterFilterConstraint
qualifier), and another will be applied
to all MeterRegistry
instances.
An application configuration property is also injected and used as a tag value.
Additional examples of MeterFilters can be found in the
official documentation.
By providing CDI beans that implement io.quarkus.micrometer.runtime.HttpServerMetricsTagsContributor
, user code can contribute arbitrary tags based on the details of HTTP request
By providing CDI beans that implement io.quarkus.micrometer.runtime.HttpClientMetricsTagsContributor
, user code can contribute arbitrary tags based on the details of HTTP request
By providing CDI beans that implement io.quarkus.micrometer.runtime.MeterRegistryCustomizer
user code has the change to change the configuration of any MeterRegistry
that has been activated.
Unless an implementation is annotated with @io.quarkus.micrometer.runtime.MeterRegistryCustomizerConstraint
, the customization applies to all MeterRegistry
instances.
Micrometer does define two annotations, @Counted
and @Timed
, that can be added to methods.
The @Timed
annotation will wrap the execution of a method and will emit the following tags
in addition to any tags defined on the annotation itself:
class, method, and exception (either "none" or the simple class name of a detected exception).
Parameters to @Counted
and @Timed
can be annotated with @MeterTag
to dynamically assign meaningful tag values.
MeterTag.resolver
can be used to extract a tag from a method parameter, by creating a bean
implementing io.micrometer.common.annotation.ValueResolver
and referring to this class: @MeterTag(resolver=CustomResolver.class)
MeterTag.expression
is also supported, but you have to implement the evaluation of the expression
by creating a bean implementing io.micrometer.common.annotation.ValueExpressionResolver
that can evaluate expressions.
enum Currency { USD, EUR }
@Singleton
class EnumOrdinalResolver implements ValueResolver {
@Override
public String resolve(Object parameter) {
if(parameter instanceof Enum) {
return String.valueOf(((Enum<?>) parameter).ordinal());
}
return null;
}
}
@Singleton
public class MyExpressionResolver implements ValueExpressionResolver {
@Override
public String resolve(String expression, Object parameter) {
return someParser.parse(expression).evaluate(parameter);
}
}
// tags = type=with_enum, currency=${currency.toString()}
@Timed(value="time_something", extraTags = {"type", "with_enum"})
public Something calculateSomething(@MeterTag Currency currency) { ... }
// tags = type=with_enum, the_currency=${currency.toString()}
@Timed(value="time_something", extraTags = {"type", "with_enum"})
public Something calculateSomething(@MeterTag(key="the_currency") Currency currency) { ... }
// tags = type=with_enum, currency=${currency.ordinal()}
@Timed(value="time_something", extraTags = {"type", "with_enum"})
public Something calculateSomething(@MeterTag(resolver=EnumOrdinalResolver.class) Currency currency) { ... }
// tags = type=with_enum, currency=${currency.ordinal()}
@Timed(value="time_something", extraTags = {"type", "with_enum"})
public Something calculateSomething(@MeterTag(expression="currency.ordinal()") Currency currency) { ... }
Important
|
Provided tag values MUST BE of LOW-CARDINALITY. High-cardinality values can lead to performance and storage issues in your metrics backend (a "cardinality explosion"). Tag values should not use end-user data, since those could be high-cardinality. |
Many methods, like REST endpoint methods or Vert.x Routes, are counted and timed by the micrometer extension out of the box.
If you use the MicroProfile Metrics API in your application, the Micrometer extension will create an adaptive layer to map those metrics into the Micrometer registry. Note that naming conventions between the two systems is different, so the metrics that are emitted when using MP Metrics with Micrometer will change.
Use a MeterFilter
to remap names or tags according to your conventions.
@Produces
@Singleton
public MeterFilter renameApplicationMeters() {
final String targetMetric = MPResourceClass.class.getName() + ".mpAnnotatedMethodName";
return MeterFilter() {
@Override
public Meter.Id map(Meter.Id id) {
if (id.getName().equals(targetMetric)) {
// Drop the scope tag (MP Registry type: application, vendor, base)
List<Tag> tags = id.getTags().stream().filter(x -> !"scope".equals(x.getKey()))
.collect(Collectors.toList());
// rename the metric
return id.withName("my.metric.name").replaceTags(tags);
}
return id;
}
};
}
Ensure the following dependency is present if you require the MicroProfile Metrics API:
<dependency>
<groupId>org.eclipse.microprofile.metrics</groupId>
<artifactId>microprofile-metrics-api</artifactId>
</dependency>
implementation("org.eclipse.microprofile.metrics:microprofile-metrics-api")
Note
|
The MP Metrics API compatibility layer may be moved to a different extension in the future. |
By default, the metrics are exposed on the main HTTP server.
You can expose them on a separate network interface and port by setting quarkus.management.enabled=true
in your application configuration.
Note that this property is a build-time property.
The value cannot be overridden at runtime.
If you enable the management interface without customizing the management network interface and port, the metrics are exposed under: http://0.0.0.0:9000/q/metrics
.
You can configure the path of each exposed format using:
quarkus.micrometer.export.json.enabled=true # Enable json metrics
quarkus.micrometer.export.json.path=metrics/json
quarkus.micrometer.export.prometheus.path=metrics/prometheus
With such a configuration, the json metrics will be available from http://0.0.0.0:9000/q/metrics/json
.
The prometheus metrics will be available from http://0.0.0.0:9000/q/metrics/prometheus
.
Refer to the management interface reference for more information.
-
jboss-logging
(logging API in Quarkus) -
-
AMQP 1.0
-
RabbitMQ
-
Kafka
-
Pulsar
-
JMS
-
MQTT
-
Camel Messaging
-
-
quarkus-vertx
(http requests)