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).
This commit is contained in:
Dave Syer 2014-10-02 17:05:46 +01:00
parent c18b4248e5
commit d63bbdfe44
2 changed files with 49 additions and 37 deletions

View File

@ -22,7 +22,6 @@ 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;
@ -41,11 +40,11 @@ import org.springframework.util.Assert;
*
* @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;
@ -55,44 +54,59 @@ public class RedisMetricRepository implements MetricRepository, InitializingBean
private final RedisOperations<String, String> redisOperations;
public RedisMetricRepository(RedisConnectionFactory redisConnectionFactory) {
Assert.notNull(redisConnectionFactory, "RedisConnectionFactory must not be null");
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 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) {
this(redisConnectionFactory, DEFAULT_METRICS_PREFIX);
}
/**
* 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
*/
public void setPrefix(String prefix) {
public RedisMetricRepository(RedisConnectionFactory redisConnectionFactory,
String prefix) {
this(redisConnectionFactory, prefix, DEFAULT_KEY);
}
/**
* Allows user to set the prefix and key to use to store the index of other keys. The
* redis store will hold a zset under the key just so the metric names can be
* enumerated. Read operations, especially {@link #findAll()} and {@link #count()},
* 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
*/
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;
}
/**
* The redis key to use to store the index of other keys. The redis store will hold a
* zset under this key. Defaults to "keys.spring.metrics". Read operations, especially
* {@link #findAll()} and {@link #count()}, will be much more efficient if the key is
* unique to the {@link #setPrefix(String) prefix} of this repository.
* @param key the key to set
*/
public void setKey(String key) {
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.zSetOperations = this.redisOperations.boundZSetOps(this.key);
}
@Override

View File

@ -41,10 +41,8 @@ public class RedisMetricRepositoryTests {
@Before
public void init() {
this.repository = new RedisMetricRepository(this.redis.getResource());
this.prefix = "spring.test." + System.currentTimeMillis();
this.repository.setPrefix(this.prefix);
this.repository.afterPropertiesSet();
this.repository = new RedisMetricRepository(this.redis.getResource(), this.prefix);
}
@After