You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 2集成Graphite:如何为所有指标统一添加前缀?

Fixing Duplicate Metrics When Adding Prefix to Graphite in Spring Boot 2

Hey there, I see what's going on here—those duplicate metrics (some with your prefix, some without) are happening because you've got two GraphiteMeterRegistry instances running at the same time. Let me break this down and show you how to fix it properly.

Why the Duplicates Happen

Spring Boot's auto-configuration automatically creates a default GraphiteMeterRegistry for you if it detects Graphite dependencies in your project. When you add your own @Bean for GraphiteMeterRegistry, you're essentially creating a second registry. Both registries are pushing metrics to Graphite: one uses your custom prefix, the other uses the default naming convention (no prefix). That's why you're seeing duplicates.

Solution 1: Disable Auto-Configured Registry & Use Your Custom Bean

If you want to stick with your custom registry setup, you need to tell Spring Boot to skip creating its default one. You can do this by excluding the auto-configuration class for Graphite metrics:

@SpringBootApplication(exclude = GraphiteMetricsAutoConfiguration.class)
public class YourSpringBootApplication {

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

This way, only your custom registry will be active, and all metrics will get your prefix.

A cleaner approach that aligns with Spring Boot's auto-configuration philosophy is to customize the existing auto-configured registry instead of creating a new one. Use GraphiteMeterRegistryCustomizer to apply your prefix rule to the default registry:

@Bean
public GraphiteMeterRegistryCustomizer graphitePrefixCustomizer() {
    return registry -> {
        registry.config().namingConvention(new NamingConvention() {
            @Override
            public String name(String name, Meter.Type type, String baseUnit) {
                // Add your prefix to every metric name
                return "prefix." + NamingConvention.DEFAULT.name(name, type, baseUnit);
            }

            // Keep default behavior for tag keys/values unless you need to customize those too
            @Override
            public String tagKey(String key) {
                return NamingConvention.DEFAULT.tagKey(key);
            }

            @Override
            public String tagValue(String value) {
                return NamingConvention.DEFAULT.tagValue(value);
            }
        });
    };
}

Why This Is Better

  • No duplicate registries: You're modifying the single auto-configured registry instead of adding a new one.
  • All Spring Boot's default Graphite configuration (from application.properties/application.yml) still applies (like host, port, polling interval).
  • It's more maintainable and follows Spring Boot's best practices.

Final Check

Make sure your application.properties (or YAML) has the necessary Graphite export settings, like:

management.metrics.export.graphite.host=your-graphite-server
management.metrics.export.graphite.port=2003
management.metrics.export.graphite.enabled=true

With either solution, you should stop seeing duplicate metrics, and every single metric will have your desired prefix.

内容的提问来源于stack exchange,提问作者Alon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:13:36