From d724c9062bf4fcb69ad7d3eba8cafaa10b6c28cd Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Thu, 2 Oct 2014 16:36:07 +0100 Subject: [PATCH] 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(). --- .../redis/RedisMetricRepository.java | 22 +++++++++++++++---- .../redis/RedisMetricRepositoryTests.java | 1 + 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/repository/redis/RedisMetricRepository.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/repository/redis/RedisMetricRepository.java index a3956898f1..b36c6f4880 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/repository/redis/RedisMetricRepository.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/repository/redis/RedisMetricRepository.java @@ -22,6 +22,7 @@ import java.util.Iterator; import java.util.List; import java.util.Set; +import org.springframework.beans.factory.InitializingBean; import org.springframework.boot.actuate.metrics.Metric; import org.springframework.boot.actuate.metrics.repository.MetricRepository; import org.springframework.boot.actuate.metrics.writer.Delta; @@ -40,13 +41,15 @@ import org.springframework.util.Assert; * * @author Dave Syer */ -public class RedisMetricRepository implements MetricRepository { +public class RedisMetricRepository implements MetricRepository, InitializingBean { private static final String DEFAULT_METRICS_PREFIX = "spring.metrics."; + private static final String DEFAULT_KEY = "keys." + DEFAULT_METRICS_PREFIX; + private String prefix = DEFAULT_METRICS_PREFIX; - private String key = "keys." + DEFAULT_METRICS_PREFIX; + private String key = DEFAULT_KEY; private BoundZSetOperations zSetOperations; @@ -57,7 +60,19 @@ public class RedisMetricRepository implements MetricRepository { this.redisOperations = RedisUtils.stringTemplate(redisConnectionFactory); this.zSetOperations = this.redisOperations.boundZSetOps(this.key); } - + + @Override + public void afterPropertiesSet() { + if (!DEFAULT_METRICS_PREFIX.equals(this.prefix)) { + if (DEFAULT_KEY.equals(this.key)) { + this.key = "keys." + this.prefix; + } + } + if (!DEFAULT_KEY.equals(this.key)) { + this.zSetOperations = this.redisOperations.boundZSetOps(this.key); + } + } + /** * The prefix for all metrics keys. * @param prefix the prefix to set for all metrics keys @@ -78,7 +93,6 @@ public class RedisMetricRepository implements MetricRepository { */ public void setKey(String key) { this.key = key; - this.zSetOperations = this.redisOperations.boundZSetOps(this.key); } @Override diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/repository/redis/RedisMetricRepositoryTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/repository/redis/RedisMetricRepositoryTests.java index 1c7048960a..935b45bc18 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/repository/redis/RedisMetricRepositoryTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/repository/redis/RedisMetricRepositoryTests.java @@ -44,6 +44,7 @@ public class RedisMetricRepositoryTests { this.repository = new RedisMetricRepository(this.redis.getResource()); this.prefix = "spring.test." + System.currentTimeMillis(); this.repository.setPrefix(this.prefix); + this.repository.afterPropertiesSet(); } @After