Commit 33f06e1c authored by Dave Syer's avatar Dave Syer

Ensure redis export properties are actually set using a @Value()

Previously the @Value annotation was not on a top level @Bean field
(it was nested inside). Manually constructing the bean in a separate
configuration class seems like the best way to get it to actually bind
at runtime.
parent eef027a4
...@@ -22,6 +22,7 @@ import java.util.List; ...@@ -22,6 +22,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.endpoint.MetricsEndpointMetricReader; import org.springframework.boot.actuate.endpoint.MetricsEndpointMetricReader;
import org.springframework.boot.actuate.metrics.export.MetricExportProperties; import org.springframework.boot.actuate.metrics.export.MetricExportProperties;
import org.springframework.boot.actuate.metrics.export.MetricExporters; import org.springframework.boot.actuate.metrics.export.MetricExporters;
...@@ -43,7 +44,7 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar; ...@@ -43,7 +44,7 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
@Configuration @Configuration
@EnableScheduling @EnableScheduling
@ConditionalOnProperty(value = "spring.metrics.export.enabled", matchIfMissing = true) @ConditionalOnProperty(value = "spring.metrics.export.enabled", matchIfMissing = true)
@EnableConfigurationProperties(MetricExportProperties.class) @EnableConfigurationProperties
public class MetricExportAutoConfiguration { public class MetricExportAutoConfiguration {
@Autowired(required = false) @Autowired(required = false)
...@@ -60,6 +61,20 @@ public class MetricExportAutoConfiguration { ...@@ -60,6 +61,20 @@ public class MetricExportAutoConfiguration {
@Autowired(required = false) @Autowired(required = false)
private MetricsEndpointMetricReader endpointReader; private MetricsEndpointMetricReader endpointReader;
@Configuration
protected static class MetricExportPropertiesConfiguration {
@Value("spring.metrics.${random.value:0000}.${spring.application.name:application}")
private String prefix = "spring.metrics";
@Bean(name = "spring.metrics.export.CONFIGURATION_PROPERTIES")
@ConditionalOnMissingBean
public MetricExportProperties metricExportProperties() {
MetricExportProperties export = new MetricExportProperties();
export.getRedis().setPrefix(prefix);
return export;
}
}
@Bean @Bean
@ConditionalOnMissingBean(name = "metricWritersMetricExporter") @ConditionalOnMissingBean(name = "metricWritersMetricExporter")
public SchedulingConfigurer metricWritersMetricExporter() { public SchedulingConfigurer metricWritersMetricExporter() {
......
...@@ -26,7 +26,6 @@ import org.springframework.boot.actuate.metrics.buffer.CounterBuffers; ...@@ -26,7 +26,6 @@ import org.springframework.boot.actuate.metrics.buffer.CounterBuffers;
import org.springframework.boot.actuate.metrics.buffer.GaugeBuffers; import org.springframework.boot.actuate.metrics.buffer.GaugeBuffers;
import org.springframework.boot.actuate.metrics.export.Exporter; import org.springframework.boot.actuate.metrics.export.Exporter;
import org.springframework.boot.actuate.metrics.export.MetricCopyExporter; import org.springframework.boot.actuate.metrics.export.MetricCopyExporter;
import org.springframework.boot.actuate.metrics.export.MetricExportProperties;
import org.springframework.boot.actuate.metrics.repository.InMemoryMetricRepository; import org.springframework.boot.actuate.metrics.repository.InMemoryMetricRepository;
import org.springframework.boot.actuate.metrics.repository.MetricRepository; import org.springframework.boot.actuate.metrics.repository.MetricRepository;
import org.springframework.boot.actuate.metrics.writer.DefaultCounterService; import org.springframework.boot.actuate.metrics.writer.DefaultCounterService;
...@@ -37,7 +36,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnJava; ...@@ -37,7 +36,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnJava;
import org.springframework.boot.autoconfigure.condition.ConditionalOnJava.JavaVersion; import org.springframework.boot.autoconfigure.condition.ConditionalOnJava.JavaVersion;
import org.springframework.boot.autoconfigure.condition.ConditionalOnJava.Range; import org.springframework.boot.autoconfigure.condition.ConditionalOnJava.Range;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.MessageChannel; import org.springframework.messaging.MessageChannel;
...@@ -81,7 +79,6 @@ import com.codahale.metrics.MetricRegistry; ...@@ -81,7 +79,6 @@ import com.codahale.metrics.MetricRegistry;
* @author Dave Syer * @author Dave Syer
*/ */
@Configuration @Configuration
@EnableConfigurationProperties(MetricExportProperties.class)
public class MetricRepositoryAutoConfiguration { public class MetricRepositoryAutoConfiguration {
@Configuration @Configuration
......
...@@ -22,7 +22,6 @@ import java.util.Map.Entry; ...@@ -22,7 +22,6 @@ import java.util.Map.Entry;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.PatternMatchUtils; import org.springframework.util.PatternMatchUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
...@@ -149,7 +148,6 @@ public class MetricExportProperties extends Trigger { ...@@ -149,7 +148,6 @@ public class MetricExportProperties extends Trigger {
* set spring.application.name elsewhere, then the default will be in the right * set spring.application.name elsewhere, then the default will be in the right
* form. * form.
*/ */
@Value("spring.metrics.${random.value:0000}.${spring.application.name:application}")
private String prefix = "spring.metrics"; private String prefix = "spring.metrics";
/** /**
...@@ -179,6 +177,7 @@ public class MetricExportProperties extends Trigger { ...@@ -179,6 +177,7 @@ public class MetricExportProperties extends Trigger {
if (tokens.length > 1) { if (tokens.length > 1) {
if (StringUtils.hasText(tokens[1])) { if (StringUtils.hasText(tokens[1])) {
// If the prefix has 2 or more non-trivial parts, use the first 1 // If the prefix has 2 or more non-trivial parts, use the first 1
// (the aggregator strips a further 2 by default).
return tokens[0]; return tokens[0];
} }
} }
......
...@@ -100,11 +100,6 @@ public class RedisMetricRepository implements MetricRepository { ...@@ -100,11 +100,6 @@ public class RedisMetricRepository implements MetricRepository {
prefix = prefix + "."; prefix = prefix + ".";
} }
this.prefix = prefix; this.prefix = prefix;
if (!DEFAULT_METRICS_PREFIX.equals(this.prefix)) {
if (DEFAULT_KEY.equals(key)) {
key = "keys." + prefix;
}
}
if (key.endsWith(".")) { if (key.endsWith(".")) {
key = key.substring(0, key.length() - 1); key = key.substring(0, key.length() - 1);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment