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.
This commit is contained in:
Dave Syer 2015-06-01 11:30:00 +01:00
parent eef027a4f0
commit 33f06e1cb0
4 changed files with 18 additions and 12 deletions

View File

@ -22,6 +22,7 @@ import java.util.List;
import java.util.Map;
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.metrics.export.MetricExportProperties;
import org.springframework.boot.actuate.metrics.export.MetricExporters;
@ -43,7 +44,7 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
@Configuration
@EnableScheduling
@ConditionalOnProperty(value = "spring.metrics.export.enabled", matchIfMissing = true)
@EnableConfigurationProperties(MetricExportProperties.class)
@EnableConfigurationProperties
public class MetricExportAutoConfiguration {
@Autowired(required = false)
@ -60,6 +61,20 @@ public class MetricExportAutoConfiguration {
@Autowired(required = false)
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
@ConditionalOnMissingBean(name = "metricWritersMetricExporter")
public SchedulingConfigurer metricWritersMetricExporter() {

View File

@ -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.export.Exporter;
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.MetricRepository;
import org.springframework.boot.actuate.metrics.writer.DefaultCounterService;
@ -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.Range;
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.Configuration;
import org.springframework.messaging.MessageChannel;
@ -81,7 +79,6 @@ import com.codahale.metrics.MetricRegistry;
* @author Dave Syer
*/
@Configuration
@EnableConfigurationProperties(MetricExportProperties.class)
public class MetricRepositoryAutoConfiguration {
@Configuration

View File

@ -22,7 +22,6 @@ import java.util.Map.Entry;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.PatternMatchUtils;
import org.springframework.util.StringUtils;
@ -149,7 +148,6 @@ public class MetricExportProperties extends Trigger {
* set spring.application.name elsewhere, then the default will be in the right
* form.
*/
@Value("spring.metrics.${random.value:0000}.${spring.application.name:application}")
private String prefix = "spring.metrics";
/**
@ -157,7 +155,7 @@ public class MetricExportProperties extends Trigger {
* system sharing a redis repository.
*/
private String key = "keys.spring.metrics";
public String getPrefix() {
return prefix;
}
@ -179,6 +177,7 @@ public class MetricExportProperties extends Trigger {
if (tokens.length > 1) {
if (StringUtils.hasText(tokens[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];
}
}

View File

@ -100,11 +100,6 @@ public class RedisMetricRepository implements MetricRepository {
prefix = prefix + ".";
}
this.prefix = prefix;
if (!DEFAULT_METRICS_PREFIX.equals(this.prefix)) {
if (DEFAULT_KEY.equals(key)) {
key = "keys." + prefix;
}
}
if (key.endsWith(".")) {
key = key.substring(0, key.length() - 1);
}