Skip to content

Commit d724c90

Browse files
author
Dave Syer
committed
Change key value if prefix changes
Some weird looking test failures led me to track this down. If the user changes the prefix for metric names, he probably wanted to change the keys as welll (otherwise 2 repositories can use the same key, which is why the test was failing for me). We can do that in an afterPropertiesSet().
1 parent a702ff5 commit d724c90

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

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

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

25+
import org.springframework.beans.factory.InitializingBean;
2526
import org.springframework.boot.actuate.metrics.Metric;
2627
import org.springframework.boot.actuate.metrics.repository.MetricRepository;
2728
import org.springframework.boot.actuate.metrics.writer.Delta;
@@ -40,13 +41,15 @@
4041
*
4142
* @author Dave Syer
4243
*/
43-
public class RedisMetricRepository implements MetricRepository {
44+
public class RedisMetricRepository implements MetricRepository, InitializingBean {
4445

4546
private static final String DEFAULT_METRICS_PREFIX = "spring.metrics.";
4647

48+
private static final String DEFAULT_KEY = "keys." + DEFAULT_METRICS_PREFIX;
49+
4750
private String prefix = DEFAULT_METRICS_PREFIX;
4851

49-
private String key = "keys." + DEFAULT_METRICS_PREFIX;
52+
private String key = DEFAULT_KEY;
5053

5154
private BoundZSetOperations<String, String> zSetOperations;
5255

@@ -57,7 +60,19 @@ public RedisMetricRepository(RedisConnectionFactory redisConnectionFactory) {
5760
this.redisOperations = RedisUtils.stringTemplate(redisConnectionFactory);
5861
this.zSetOperations = this.redisOperations.boundZSetOps(this.key);
5962
}
60-
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+
}
74+
}
75+
6176
/**
6277
* The prefix for all metrics keys.
6378
* @param prefix the prefix to set for all metrics keys
@@ -78,7 +93,6 @@ public void setPrefix(String prefix) {
7893
*/
7994
public void setKey(String key) {
8095
this.key = key;
81-
this.zSetOperations = this.redisOperations.boundZSetOps(this.key);
8296
}
8397

8498
@Override

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public void init() {
4444
this.repository = new RedisMetricRepository(this.redis.getResource());
4545
this.prefix = "spring.test." + System.currentTimeMillis();
4646
this.repository.setPrefix(this.prefix);
47+
this.repository.afterPropertiesSet();
4748
}
4849

4950
@After

0 commit comments

Comments
 (0)