Skip to content

Commit 1eca70a

Browse files
committed
Restore Graphite tags behaviour unless configured explicitly
This commit harmonizes how Graphite is configured. If tagsAsPrefix is set, it is used transparently. Otherwise, the new native tagging system is used. See micrometer-metrics/micrometer#2007 Closes gh-20884
1 parent e533ce3 commit 1eca70a

File tree

4 files changed

+29
-13
lines changed

4 files changed

+29
-13
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/graphite/GraphiteProperties.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.micrometer.graphite.GraphiteProtocol;
2323

2424
import org.springframework.boot.context.properties.ConfigurationProperties;
25+
import org.springframework.util.ObjectUtils;
2526

2627
/**
2728
* {@link ConfigurationProperties @ConfigurationProperties} for configuring Graphite
@@ -71,9 +72,9 @@ public class GraphiteProperties {
7172

7273
/**
7374
* Whether Graphite tags should be used, as opposed to a hierarchical naming
74-
* convention.
75+
* convention. Enabled by default unless "tagsAsPrefix" is set.
7576
*/
76-
private boolean graphiteTagsEnabled = true;
77+
private Boolean graphiteTagsEnabled;
7778

7879
/**
7980
* For the hierarchical naming convention, turn the specified tag keys into part of
@@ -137,11 +138,11 @@ public void setProtocol(GraphiteProtocol protocol) {
137138
this.protocol = protocol;
138139
}
139140

140-
public boolean isGraphiteTagsEnabled() {
141-
return this.graphiteTagsEnabled;
141+
public Boolean getGraphiteTagsEnabled() {
142+
return (this.graphiteTagsEnabled != null) ? this.graphiteTagsEnabled : ObjectUtils.isEmpty(this.tagsAsPrefix);
142143
}
143144

144-
public void setGraphiteTagsEnabled(boolean graphiteTagsEnabled) {
145+
public void setGraphiteTagsEnabled(Boolean graphiteTagsEnabled) {
145146
this.graphiteTagsEnabled = graphiteTagsEnabled;
146147
}
147148

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/graphite/GraphitePropertiesConfigAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public GraphiteProtocol protocol() {
7878

7979
@Override
8080
public boolean graphiteTagsEnabled() {
81-
return get(GraphiteProperties::isGraphiteTagsEnabled, GraphiteConfig.super::graphiteTagsEnabled);
81+
return get(GraphiteProperties::getGraphiteTagsEnabled, GraphiteConfig.super::graphiteTagsEnabled);
8282
}
8383

8484
@Override

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/graphite/GraphiteMetricsExportAutoConfigurationTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,26 @@ void backsOffWithoutAClock() {
4747
}
4848

4949
@Test
50-
void autoConfiguresUseTagsAsPrefixIsIgnoredByDefault() {
50+
void autoConfiguresUseTagsAsPrefix() {
5151
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
52-
.withPropertyValues("management.metrics.export.graphite.tags-as-prefix=ignored").run((context) -> {
52+
.withPropertyValues("management.metrics.export.graphite.tags-as-prefix=app").run((context) -> {
5353
assertThat(context).hasSingleBean(GraphiteMeterRegistry.class);
5454
GraphiteMeterRegistry registry = context.getBean(GraphiteMeterRegistry.class);
5555
registry.counter("test.count", Tags.of("app", "myapp"));
56-
assertThat(registry.getDropwizardRegistry().getMeters()).containsOnlyKeys("test.count;app=myapp");
56+
assertThat(registry.getDropwizardRegistry().getMeters()).containsOnlyKeys("myapp.testCount");
5757
});
5858
}
5959

6060
@Test
61-
void autoConfiguresUseTagsAsPrefix() {
61+
void autoConfiguresWithTagsAsPrefixCanBeDisabled() {
6262
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
6363
.withPropertyValues("management.metrics.export.graphite.tags-as-prefix=app",
64-
"management.metrics.export.graphite.graphite-tags-enabled=false")
64+
"management.metrics.export.graphite.graphite-tags-enabled=true")
6565
.run((context) -> {
6666
assertThat(context).hasSingleBean(GraphiteMeterRegistry.class);
6767
GraphiteMeterRegistry registry = context.getBean(GraphiteMeterRegistry.class);
6868
registry.counter("test.count", Tags.of("app", "myapp"));
69-
assertThat(registry.getDropwizardRegistry().getMeters()).containsOnlyKeys("myapp.testCount");
69+
assertThat(registry.getDropwizardRegistry().getMeters()).containsOnlyKeys("test.count;app=myapp");
7070
});
7171
}
7272

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/graphite/GraphitePropertiesTests.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,23 @@ void defaultValuesAreConsistent() {
3939
assertThat(properties.getHost()).isEqualTo(config.host());
4040
assertThat(properties.getPort()).isEqualTo(config.port());
4141
assertThat(properties.getProtocol()).isEqualTo(config.protocol());
42-
assertThat(properties.isGraphiteTagsEnabled()).isEqualTo(config.graphiteTagsEnabled());
42+
assertThat(properties.getGraphiteTagsEnabled()).isEqualTo(config.graphiteTagsEnabled());
4343
assertThat(properties.getTagsAsPrefix()).isEqualTo(config.tagsAsPrefix());
4444
}
4545

46+
@Test
47+
void graphiteTagsAreDisabledIfTagsAsPrefixIsSet() {
48+
GraphiteProperties properties = new GraphiteProperties();
49+
properties.setTagsAsPrefix(new String[] { "app" });
50+
assertThat(properties.getGraphiteTagsEnabled()).isFalse();
51+
}
52+
53+
@Test
54+
void graphiteTagsCanBeEnabledEvenIfTagsAsPrefixIsSet() {
55+
GraphiteProperties properties = new GraphiteProperties();
56+
properties.setGraphiteTagsEnabled(true);
57+
properties.setTagsAsPrefix(new String[] { "app" });
58+
assertThat(properties.getGraphiteTagsEnabled()).isTrue();
59+
}
60+
4661
}

0 commit comments

Comments
 (0)