Micrometer - Add default prefix in metric name

Viewed 2937

In micrometer, we can create a new gauge doing something like

myMeterRegistry.gauge("my_metric", 69);

See the code here https://github.com/micrometer-metrics/micrometer/blob/master/micrometer-core/src/main/java/io/micrometer/core/instrument/MeterRegistry.java#L468

Would be possible to include a "prefix" name by default for my myMeterRegistry object?

Manually, it should like:

myeterRegistry.gauge("myprefix_my_metric", 69);

My goal is that every developer that creates a gauge metric in my application does not have to take care of adding the "myprefix_" at the beginning of the metric name

1 Answers

A MeterFilter would let you do that (but don't!):

new MeterFilter() {
    @Override
    public Meter.Id map(Meter.Id id) {
      return id.withName("myprefix." + id.getName());
    }
}

However a common prefix is typically a smell of an incorrect dimensionality. Usually users try to add a region, host, or the application's name as a prefix. Those are better provided as tags since then you can aggregate across systems and use common dashboards.

The commonTags approach is recommended:

registry.config().commonTags("team", "myteam", "region", "us-east-1");

For hierarchical meter registries, tags will be included in the name as a prefix.

Related