Commit d59cbc83 authored by Dave Syer's avatar Dave Syer

Group resolution for RedisMultiMetricRepository with prefix

The prefix needs to be added before looking for keys. In addition
I rationalized the constructor and final fields (it didn't make
any sense for the prefix to be mutable).

Fixes gh-927
parent ed2876e9
...@@ -75,7 +75,7 @@ public class RedisMetricRepository implements MetricRepository { ...@@ -75,7 +75,7 @@ public class RedisMetricRepository implements MetricRepository {
/** /**
* The redis key to use to store the index of other keys. The redis store will hold a * 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 * 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 * {@link #findAll()} and {@link #count()}, will be much more efficient if the key is
* unique to the {@link #setPrefix(String) prefix} of this repository. * unique to the {@link #setPrefix(String) prefix} of this repository.
* @param key the key to set * @param key the key to set
......
...@@ -34,7 +34,7 @@ import org.springframework.util.Assert; ...@@ -34,7 +34,7 @@ import org.springframework.util.Assert;
* {@link MultiMetricRepository} implementation backed by a redis store. Metric values are * {@link MultiMetricRepository} implementation backed by a redis store. Metric values are
* stored as regular values against a key composed of the group name prefixed with a * stored as regular values against a key composed of the group name prefixed with a
* constant prefix (default "spring.groups."). The group names are stored as a zset under * constant prefix (default "spring.groups."). The group names are stored as a zset under
* <code>[prefix]</code> + "keys". * "keys." + <code>[prefix]</code>.
* *
* @author Dave Syer * @author Dave Syer
*/ */
...@@ -42,30 +42,28 @@ public class RedisMultiMetricRepository implements MultiMetricRepository { ...@@ -42,30 +42,28 @@ public class RedisMultiMetricRepository implements MultiMetricRepository {
private static final String DEFAULT_METRICS_PREFIX = "spring.groups."; private static final String DEFAULT_METRICS_PREFIX = "spring.groups.";
private String prefix = DEFAULT_METRICS_PREFIX; private final String prefix;
private String keys = "keys." + this.prefix; private final String keys;
private final BoundZSetOperations<String, String> zSetOperations; private final BoundZSetOperations<String, String> zSetOperations;
private final RedisOperations<String, String> redisOperations; private final RedisOperations<String, String> redisOperations;
public RedisMultiMetricRepository(RedisConnectionFactory redisConnectionFactory) { public RedisMultiMetricRepository(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.keys);
} }
/** public RedisMultiMetricRepository(RedisConnectionFactory redisConnectionFactory,
* The prefix for all metrics keys. String prefix) {
* @param prefix the prefix to set for all metrics keys Assert.notNull(redisConnectionFactory, "RedisConnectionFactory must not be null");
*/ this.redisOperations = RedisUtils.stringTemplate(redisConnectionFactory);
public void setPrefix(String prefix) {
if (!prefix.endsWith(".")) { if (!prefix.endsWith(".")) {
prefix = prefix + "."; prefix = prefix + ".";
} }
this.prefix = prefix; this.prefix = prefix;
this.keys = "keys." + this.prefix; this.keys = "keys." + this.prefix.substring(0, prefix.length() - 1);
this.zSetOperations = this.redisOperations.boundZSetOps(this.keys);
} }
@Override @Override
...@@ -105,7 +103,7 @@ public class RedisMultiMetricRepository implements MultiMetricRepository { ...@@ -105,7 +103,7 @@ public class RedisMultiMetricRepository implements MultiMetricRepository {
Set<String> range = this.zSetOperations.range(0, -1); Set<String> range = this.zSetOperations.range(0, -1);
Collection<String> result = new ArrayList<String>(); Collection<String> result = new ArrayList<String>();
for (String key : range) { for (String key : range) {
result.add(nameFor(key)); result.add(nameFor(this.prefix + key));
} }
return range; return range;
} }
......
...@@ -17,11 +17,16 @@ ...@@ -17,11 +17,16 @@
package org.springframework.boot.actuate.metrics.repository.redis; package org.springframework.boot.actuate.metrics.repository.redis;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import org.springframework.boot.actuate.metrics.Iterables; import org.springframework.boot.actuate.metrics.Iterables;
import org.springframework.boot.actuate.metrics.Metric; import org.springframework.boot.actuate.metrics.Metric;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
...@@ -33,27 +38,42 @@ import static org.junit.Assert.assertTrue; ...@@ -33,27 +38,42 @@ import static org.junit.Assert.assertTrue;
/** /**
* @author Dave Syer * @author Dave Syer
*/ */
@RunWith(Parameterized.class)
public class RedisMultiMetricRepositoryTests { public class RedisMultiMetricRepositoryTests {
@Rule @Rule
public RedisServer redis = RedisServer.running(); public RedisServer redis = RedisServer.running();
private RedisMultiMetricRepository repository; private RedisMultiMetricRepository repository;
@Parameter(0)
public String prefix;
@Parameters
public static List<Object[]> parameters() {
return Arrays.<Object[]> asList(new Object[] { null }, new Object[] { "test" });
}
@Before @Before
public void init() { public void init() {
this.repository = new RedisMultiMetricRepository(this.redis.getResource()); if (this.prefix == null) {
this.prefix = "spring.groups";
this.repository = new RedisMultiMetricRepository(this.redis.getResource());
}
else {
this.repository = new RedisMultiMetricRepository(this.redis.getResource(),
this.prefix);
}
} }
@After @After
public void clear() { public void clear() {
assertTrue(new StringRedisTemplate(this.redis.getResource()).opsForZSet().size( assertTrue(new StringRedisTemplate(this.redis.getResource()).opsForZSet().size(
"spring.groups.foo") > 0); "keys." + this.prefix) > 0);
this.repository.reset("foo"); this.repository.reset("foo");
this.repository.reset("bar"); this.repository.reset("bar");
assertNull(new StringRedisTemplate(this.redis.getResource()).opsForValue().get( assertNull(new StringRedisTemplate(this.redis.getResource()).opsForValue().get(
"spring.groups.foo")); this.prefix + ".foo"));
assertNull(new StringRedisTemplate(this.redis.getResource()).opsForValue().get( assertNull(new StringRedisTemplate(this.redis.getResource()).opsForValue().get(
"spring.groups.bar")); this.prefix + ".bar"));
} }
@Test @Test
......
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