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 https://github.com/micrometer-metrics/micrometer/pull/2007

Closes gh-20884
This commit is contained in:
Stephane Nicoll
2020-04-20 12:20:46 +02:00
parent e533ce3826
commit 1eca70a84b
4 changed files with 37 additions and 21 deletions

View File

@@ -22,6 +22,7 @@ import java.util.concurrent.TimeUnit;
import io.micrometer.graphite.GraphiteProtocol;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.ObjectUtils;
/**
* {@link ConfigurationProperties @ConfigurationProperties} for configuring Graphite
@@ -71,9 +72,9 @@ public class GraphiteProperties {
/**
* Whether Graphite tags should be used, as opposed to a hierarchical naming
* convention.
* convention. Enabled by default unless "tagsAsPrefix" is set.
*/
private boolean graphiteTagsEnabled = true;
private Boolean graphiteTagsEnabled;
/**
* For the hierarchical naming convention, turn the specified tag keys into part of
@@ -137,11 +138,11 @@ public class GraphiteProperties {
this.protocol = protocol;
}
public boolean isGraphiteTagsEnabled() {
return this.graphiteTagsEnabled;
public Boolean getGraphiteTagsEnabled() {
return (this.graphiteTagsEnabled != null) ? this.graphiteTagsEnabled : ObjectUtils.isEmpty(this.tagsAsPrefix);
}
public void setGraphiteTagsEnabled(boolean graphiteTagsEnabled) {
public void setGraphiteTagsEnabled(Boolean graphiteTagsEnabled) {
this.graphiteTagsEnabled = graphiteTagsEnabled;
}

View File

@@ -78,7 +78,7 @@ class GraphitePropertiesConfigAdapter extends PropertiesConfigAdapter<GraphitePr
@Override
public boolean graphiteTagsEnabled() {
return get(GraphiteProperties::isGraphiteTagsEnabled, GraphiteConfig.super::graphiteTagsEnabled);
return get(GraphiteProperties::getGraphiteTagsEnabled, GraphiteConfig.super::graphiteTagsEnabled);
}
@Override

View File

@@ -46,23 +46,10 @@ class GraphiteMetricsExportAutoConfigurationTests {
this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(GraphiteMeterRegistry.class));
}
@Test
void autoConfiguresUseTagsAsPrefixIsIgnoredByDefault() {
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
.withPropertyValues("management.metrics.export.graphite.tags-as-prefix=ignored").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("test.count;app=myapp");
});
}
@Test
void autoConfiguresUseTagsAsPrefix() {
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
.withPropertyValues("management.metrics.export.graphite.tags-as-prefix=app",
"management.metrics.export.graphite.graphite-tags-enabled=false")
.run((context) -> {
.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"));
@@ -70,6 +57,19 @@ class GraphiteMetricsExportAutoConfigurationTests {
});
}
@Test
void autoConfiguresWithTagsAsPrefixCanBeDisabled() {
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
.withPropertyValues("management.metrics.export.graphite.tags-as-prefix=app",
"management.metrics.export.graphite.graphite-tags-enabled=true")
.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("test.count;app=myapp");
});
}
@Test
void autoConfiguresItsConfigAndMeterRegistry() {
this.contextRunner.withUserConfiguration(BaseConfiguration.class).run((context) -> assertThat(context)

View File

@@ -39,8 +39,23 @@ class GraphitePropertiesTests {
assertThat(properties.getHost()).isEqualTo(config.host());
assertThat(properties.getPort()).isEqualTo(config.port());
assertThat(properties.getProtocol()).isEqualTo(config.protocol());
assertThat(properties.isGraphiteTagsEnabled()).isEqualTo(config.graphiteTagsEnabled());
assertThat(properties.getGraphiteTagsEnabled()).isEqualTo(config.graphiteTagsEnabled());
assertThat(properties.getTagsAsPrefix()).isEqualTo(config.tagsAsPrefix());
}
@Test
void graphiteTagsAreDisabledIfTagsAsPrefixIsSet() {
GraphiteProperties properties = new GraphiteProperties();
properties.setTagsAsPrefix(new String[] { "app" });
assertThat(properties.getGraphiteTagsEnabled()).isFalse();
}
@Test
void graphiteTagsCanBeEnabledEvenIfTagsAsPrefixIsSet() {
GraphiteProperties properties = new GraphiteProperties();
properties.setGraphiteTagsEnabled(true);
properties.setTagsAsPrefix(new String[] { "app" });
assertThat(properties.getGraphiteTagsEnabled()).isTrue();
}
}