Skip to content

Commit d63bbdf

Browse files
author
Dave Syer
committed
Change constructor signature for RedisMetricRepository
Otherwise we had to rely on afterPropertiesSet() being called to validate and compute the prefix and key (which depend on each other).
1 parent c18b424 commit d63bbdf

File tree

2 files changed

+46
-34
lines changed

2 files changed

+46
-34
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/repository/redis/RedisMetricRepository.java

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.List;
2323
import java.util.Set;
2424

25-
import org.springframework.beans.factory.InitializingBean;
2625
import org.springframework.boot.actuate.metrics.Metric;
2726
import org.springframework.boot.actuate.metrics.repository.MetricRepository;
2827
import org.springframework.boot.actuate.metrics.writer.Delta;
@@ -41,11 +40,11 @@
4140
*
4241
* @author Dave Syer
4342
*/
44-
public class RedisMetricRepository implements MetricRepository, InitializingBean {
43+
public class RedisMetricRepository implements MetricRepository {
4544

46-
private static final String DEFAULT_METRICS_PREFIX = "spring.metrics";
45+
private static final String DEFAULT_METRICS_PREFIX = "spring.metrics.";
4746

48-
private static final String DEFAULT_KEY = "keys." + DEFAULT_METRICS_PREFIX;
47+
private static final String DEFAULT_KEY = "keys.spring.metrics";
4948

5049
private String prefix = DEFAULT_METRICS_PREFIX;
5150

@@ -55,44 +54,59 @@ public class RedisMetricRepository implements MetricRepository, InitializingBean
5554

5655
private final RedisOperations<String, String> redisOperations;
5756

57+
/**
58+
* Create a RedisMetricRepository with a default prefix to apply to all metric names.
59+
* If multiple repositories share a redis instance they will feed into the same global
60+
* metrics.
61+
*
62+
* @param redisConnectionFactory the redis connection factory
63+
*/
5864
public RedisMetricRepository(RedisConnectionFactory redisConnectionFactory) {
59-
Assert.notNull(redisConnectionFactory, "RedisConnectionFactory must not be null");
60-
this.redisOperations = RedisUtils.stringTemplate(redisConnectionFactory);
61-
this.zSetOperations = this.redisOperations.boundZSetOps(this.key);
62-
}
63-
64-
@Override
65-
public void afterPropertiesSet() {
66-
if (!DEFAULT_METRICS_PREFIX.equals(this.prefix)) {
67-
if (DEFAULT_KEY.equals(this.key)) {
68-
this.key = "keys." + this.prefix;
69-
}
70-
}
71-
if (!DEFAULT_KEY.equals(this.key)) {
72-
this.zSetOperations = this.redisOperations.boundZSetOps(this.key);
73-
}
65+
this(redisConnectionFactory, DEFAULT_METRICS_PREFIX);
7466
}
75-
67+
7668
/**
77-
* The prefix for all metrics keys.
69+
* Create a RedisMetricRepository with a prefix to apply to all metric names (ideally
70+
* unique to this repository or to a logical repository contributed to by multiple
71+
* instances, where they all see the same values). Recommended constructor for general
72+
* purpose use.
73+
*
74+
* @param redisConnectionFactory the redis connection factory
7875
* @param prefix the prefix to set for all metrics keys
7976
*/
80-
public void setPrefix(String prefix) {
81-
if (!prefix.endsWith(".")) {
82-
prefix = prefix + ".";
83-
}
84-
this.prefix = prefix;
77+
public RedisMetricRepository(RedisConnectionFactory redisConnectionFactory,
78+
String prefix) {
79+
this(redisConnectionFactory, prefix, DEFAULT_KEY);
8580
}
8681

8782
/**
88-
* The redis key to use to store the index of other keys. The redis store will hold a
89-
* zset under this key. Defaults to "keys.spring.metrics". Read operations, especially
90-
* {@link #findAll()} and {@link #count()}, will be much more efficient if the key is
91-
* unique to the {@link #setPrefix(String) prefix} of this repository.
83+
* Allows user to set the prefix and key to use to store the index of other keys. The
84+
* redis store will hold a zset under the key just so the metric names can be
85+
* enumerated. Read operations, especially {@link #findAll()} and {@link #count()},
86+
* will only be accurate if the key is unique to the prefix of this repository.
87+
*
88+
* @param redisConnectionFactory the redis connection factory
89+
* @param prefix the prefix to set for all metrics keys
9290
* @param key the key to set
9391
*/
94-
public void setKey(String key) {
92+
public RedisMetricRepository(RedisConnectionFactory redisConnectionFactory,
93+
String prefix, String key) {
94+
Assert.notNull(redisConnectionFactory, "RedisConnectionFactory must not be null");
95+
this.redisOperations = RedisUtils.stringTemplate(redisConnectionFactory);
96+
if (!prefix.endsWith(".")) {
97+
prefix = prefix + ".";
98+
}
99+
this.prefix = prefix;
100+
if (!DEFAULT_METRICS_PREFIX.equals(this.prefix)) {
101+
if (DEFAULT_KEY.equals(key)) {
102+
key = "keys." + prefix;
103+
}
104+
}
105+
if (key.endsWith(".")) {
106+
key = key.substring(0, key.length() - 1);
107+
}
95108
this.key = key;
109+
this.zSetOperations = this.redisOperations.boundZSetOps(this.key);
96110
}
97111

98112
@Override

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/repository/redis/RedisMetricRepositoryTests.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,8 @@ public class RedisMetricRepositoryTests {
4141

4242
@Before
4343
public void init() {
44-
this.repository = new RedisMetricRepository(this.redis.getResource());
4544
this.prefix = "spring.test." + System.currentTimeMillis();
46-
this.repository.setPrefix(this.prefix);
47-
this.repository.afterPropertiesSet();
45+
this.repository = new RedisMetricRepository(this.redis.getResource(), this.prefix);
4846
}
4947

5048
@After

0 commit comments

Comments
 (0)