Commit d63bbdfe authored by Dave Syer's avatar Dave Syer

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).
parent c18b4248
...@@ -22,7 +22,6 @@ import java.util.Iterator; ...@@ -22,7 +22,6 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.actuate.metrics.Metric; import org.springframework.boot.actuate.metrics.Metric;
import org.springframework.boot.actuate.metrics.repository.MetricRepository; import org.springframework.boot.actuate.metrics.repository.MetricRepository;
import org.springframework.boot.actuate.metrics.writer.Delta; import org.springframework.boot.actuate.metrics.writer.Delta;
...@@ -41,11 +40,11 @@ import org.springframework.util.Assert; ...@@ -41,11 +40,11 @@ import org.springframework.util.Assert;
* *
* @author Dave Syer * @author Dave Syer
*/ */
public class RedisMetricRepository implements MetricRepository, InitializingBean { public class RedisMetricRepository implements MetricRepository {
private static final String DEFAULT_METRICS_PREFIX = "spring.metrics"; private static final String DEFAULT_METRICS_PREFIX = "spring.metrics.";
private static final String DEFAULT_KEY = "keys." + DEFAULT_METRICS_PREFIX; private static final String DEFAULT_KEY = "keys.spring.metrics";
private String prefix = DEFAULT_METRICS_PREFIX; private String prefix = DEFAULT_METRICS_PREFIX;
...@@ -55,44 +54,59 @@ public class RedisMetricRepository implements MetricRepository, InitializingBean ...@@ -55,44 +54,59 @@ public class RedisMetricRepository implements MetricRepository, InitializingBean
private final RedisOperations<String, String> redisOperations; private final RedisOperations<String, String> redisOperations;
/**
* Create a RedisMetricRepository with a default prefix to apply to all metric names.
* If multiple repositories share a redis instance they will feed into the same global
* metrics.
*
* @param redisConnectionFactory the redis connection factory
*/
public RedisMetricRepository(RedisConnectionFactory redisConnectionFactory) { public RedisMetricRepository(RedisConnectionFactory redisConnectionFactory) {
Assert.notNull(redisConnectionFactory, "RedisConnectionFactory must not be null"); this(redisConnectionFactory, DEFAULT_METRICS_PREFIX);
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. * Create a RedisMetricRepository with a prefix to apply to all metric names (ideally
* unique to this repository or to a logical repository contributed to by multiple
* instances, where they all see the same values). Recommended constructor for general
* purpose use.
*
* @param redisConnectionFactory the redis connection factory
* @param prefix the prefix to set for all metrics keys * @param prefix the prefix to set for all metrics keys
*/ */
public void setPrefix(String prefix) { public RedisMetricRepository(RedisConnectionFactory redisConnectionFactory,
if (!prefix.endsWith(".")) { String prefix) {
prefix = prefix + "."; this(redisConnectionFactory, prefix, DEFAULT_KEY);
}
this.prefix = prefix;
} }
/** /**
* The redis key to use to store the index of other keys. The redis store will hold a * Allows user to set the prefix and key to use to store the index of other keys. The
* zset under this key. Defaults to "keys.spring.metrics". Read operations, especially * redis store will hold a zset under the key just so the metric names can be
* {@link #findAll()} and {@link #count()}, will be much more efficient if the key is * enumerated. Read operations, especially {@link #findAll()} and {@link #count()},
* unique to the {@link #setPrefix(String) prefix} of this repository. * will only be accurate if the key is unique to the prefix of this repository.
*
* @param redisConnectionFactory the redis connection factory
* @param prefix the prefix to set for all metrics keys
* @param key the key to set * @param key the key to set
*/ */
public void setKey(String key) { public RedisMetricRepository(RedisConnectionFactory redisConnectionFactory,
String prefix, String key) {
Assert.notNull(redisConnectionFactory, "RedisConnectionFactory must not be null");
this.redisOperations = RedisUtils.stringTemplate(redisConnectionFactory);
if (!prefix.endsWith(".")) {
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);
}
this.key = key; this.key = key;
this.zSetOperations = this.redisOperations.boundZSetOps(this.key);
} }
@Override @Override
......
...@@ -41,10 +41,8 @@ public class RedisMetricRepositoryTests { ...@@ -41,10 +41,8 @@ public class RedisMetricRepositoryTests {
@Before @Before
public void init() { public void init() {
this.repository = new RedisMetricRepository(this.redis.getResource());
this.prefix = "spring.test." + System.currentTimeMillis(); this.prefix = "spring.test." + System.currentTimeMillis();
this.repository.setPrefix(this.prefix); this.repository = new RedisMetricRepository(this.redis.getResource(), this.prefix);
this.repository.afterPropertiesSet();
} }
@After @After
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment