From 7c36c003313133478b1c0c644a74996c61137323 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 28 Mar 2018 14:47:55 +0200 Subject: [PATCH] Fix default HierarchicalNameMapper implementation for Graphite Closes gh-12557 --- .../GraphiteMetricsExportAutoConfiguration.java | 5 +++-- ...phiteMetricsExportAutoConfigurationTests.java | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/graphite/GraphiteMetricsExportAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/graphite/GraphiteMetricsExportAutoConfiguration.java index 0a30deb4185d..67b4710585b8 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/graphite/GraphiteMetricsExportAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/graphite/GraphiteMetricsExportAutoConfiguration.java @@ -19,6 +19,7 @@ import io.micrometer.core.instrument.Clock; import io.micrometer.core.instrument.util.HierarchicalNameMapper; import io.micrometer.graphite.GraphiteConfig; +import io.micrometer.graphite.GraphiteHierarchicalNameMapper; import io.micrometer.graphite.GraphiteMeterRegistry; import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration; @@ -66,8 +67,8 @@ public GraphiteMeterRegistry graphiteMeterRegistry(GraphiteConfig graphiteConfig @Bean @ConditionalOnMissingBean - public HierarchicalNameMapper hierarchicalNameMapper() { - return HierarchicalNameMapper.DEFAULT; + public HierarchicalNameMapper hierarchicalNameMapper(GraphiteConfig graphiteConfig) { + return new GraphiteHierarchicalNameMapper(graphiteConfig.tagsAsPrefix()); } } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/graphite/GraphiteMetricsExportAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/graphite/GraphiteMetricsExportAutoConfigurationTests.java index 07aa3785e3a2..b75118f90e79 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/graphite/GraphiteMetricsExportAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/graphite/GraphiteMetricsExportAutoConfigurationTests.java @@ -19,6 +19,7 @@ import java.util.Map; import io.micrometer.core.instrument.Clock; +import io.micrometer.core.instrument.Tags; import io.micrometer.graphite.GraphiteConfig; import io.micrometer.graphite.GraphiteMeterRegistry; import org.junit.Test; @@ -39,6 +40,7 @@ * Tests for {@link GraphiteMetricsExportAutoConfiguration}. * * @author Andy Wilkinson + * @author Stephane Nicoll */ public class GraphiteMetricsExportAutoConfigurationTests { @@ -52,6 +54,20 @@ public void backsOffWithoutAClock() { .doesNotHaveBean(GraphiteMeterRegistry.class)); } + @Test + public void autoConfiguresUseTagsAsPrefix() { + this.contextRunner.withUserConfiguration(BaseConfiguration.class) + .withPropertyValues("management.metrics.export.graphite.tags-as-prefix=app") + .run((context) -> { + assertThat(context).hasSingleBean(GraphiteMeterRegistry.class); + GraphiteMeterRegistry registry = context.getBean( + GraphiteMeterRegistry.class); + registry.counter("test.count", Tags.of("app", "myapp")); + assertThat(registry.getDropwizardRegistry().getMeters()) + .containsOnlyKeys("myapp.testCount"); + }); + } + @Test public void autoConfiguresItsConfigAndMeterRegistry() { this.contextRunner.withUserConfiguration(BaseConfiguration.class)