Search code examples
javaspring-bootmetricsgraphitemicrometer

Add prefix to graphite micrometer event in spring-boot


I am using spring-boot 2 with graphite. I want to add a prefix to all my metrics.

@Bean
public MeterRegistry graphiteRegistsry() {
    return new GraphiteMeterRegistry(
           GraphiteConfig.DEFAULT, Clock.SYSTEM,
           (id, convention) -> "prefix." + 
           HierarchicalNameMapper.DEFAULT.toHierarchicalName(id,convention));
}

If I use this code this is actually add prefix, But also create some metrics without the prefix. It seems that their is almost all the metrics is duplicate.

How can I add this prefix? and that all the metrics that goes to graphite from this app will contains the prefix?

Thanks.


Solution

  • First, you can add a common tag using MeterRegistryCustomizer:

    @Configuration
    public class MetricsConfig {
      @Bean
      public MeterRegistryCustomizer<MeterRegistry> commonTags() {
        return r -> r.config().commonTags("commonTag", "somePrefix");
      }
    }
    

    Second, extend your application properties converting this tag to prefix:

    management.metrics.export.graphite.tags-as-prefix=commonTag
    

    Please have a look at the detailed explanation here