Skip to content

Commit

Permalink
Ensure redis export properties are actually set using a @value()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dsyer committed Jun 1, 2015
1 parent eef027a commit 33f06e1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;
Expand All @@ -43,7 +44,7 @@
@Configuration
@EnableScheduling
@ConditionalOnProperty(value = "spring.metrics.export.enabled", matchIfMissing = true)
@EnableConfigurationProperties(MetricExportProperties.class)
@EnableConfigurationProperties
public class MetricExportAutoConfiguration {

@Autowired(required = false)
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
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;
Expand All @@ -37,7 +36,6 @@
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;
Expand Down Expand Up @@ -81,7 +79,6 @@
* @author Dave Syer
*/
@Configuration
@EnableConfigurationProperties(MetricExportProperties.class)
public class MetricRepositoryAutoConfiguration {

@Configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

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;
Expand Down Expand Up @@ -149,15 +148,14 @@ public static class Redis {
* 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";

/**
* Key for redis repository export (if active). Should be globally unique for a
* system sharing a redis repository.
*/
private String key = "keys.spring.metrics";

public String getPrefix() {
return prefix;
}
Expand All @@ -179,6 +177,7 @@ public String getAggregatePrefix() {
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];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,6 @@ public RedisMetricRepository(RedisConnectionFactory redisConnectionFactory,
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);
}
Expand Down

0 comments on commit 33f06e1

Please sign in to comment.