22
22
import java .util .List ;
23
23
import java .util .Set ;
24
24
25
- import org .springframework .beans .factory .InitializingBean ;
26
25
import org .springframework .boot .actuate .metrics .Metric ;
27
26
import org .springframework .boot .actuate .metrics .repository .MetricRepository ;
28
27
import org .springframework .boot .actuate .metrics .writer .Delta ;
41
40
*
42
41
* @author Dave Syer
43
42
*/
44
- public class RedisMetricRepository implements MetricRepository , InitializingBean {
43
+ public class RedisMetricRepository implements MetricRepository {
45
44
46
- private static final String DEFAULT_METRICS_PREFIX = "spring.metrics" ;
45
+ private static final String DEFAULT_METRICS_PREFIX = "spring.metrics. " ;
47
46
48
- private static final String DEFAULT_KEY = "keys." + DEFAULT_METRICS_PREFIX ;
47
+ private static final String DEFAULT_KEY = "keys.spring.metrics" ;
49
48
50
49
private String prefix = DEFAULT_METRICS_PREFIX ;
51
50
@@ -55,44 +54,59 @@ public class RedisMetricRepository implements MetricRepository, InitializingBean
55
54
56
55
private final RedisOperations <String , String > redisOperations ;
57
56
57
+ /**
58
+ * Create a RedisMetricRepository with a default prefix to apply to all metric names.
59
+ * If multiple repositories share a redis instance they will feed into the same global
60
+ * metrics.
61
+ *
62
+ * @param redisConnectionFactory the redis connection factory
63
+ */
58
64
public RedisMetricRepository (RedisConnectionFactory redisConnectionFactory ) {
59
- Assert .notNull (redisConnectionFactory , "RedisConnectionFactory must not be null" );
60
- this .redisOperations = RedisUtils .stringTemplate (redisConnectionFactory );
61
- this .zSetOperations = this .redisOperations .boundZSetOps (this .key );
62
- }
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
- }
65
+ this (redisConnectionFactory , DEFAULT_METRICS_PREFIX );
74
66
}
75
-
67
+
76
68
/**
77
- * The prefix for all metrics keys.
69
+ * Create a RedisMetricRepository with a prefix to apply to all metric names (ideally
70
+ * unique to this repository or to a logical repository contributed to by multiple
71
+ * instances, where they all see the same values). Recommended constructor for general
72
+ * purpose use.
73
+ *
74
+ * @param redisConnectionFactory the redis connection factory
78
75
* @param prefix the prefix to set for all metrics keys
79
76
*/
80
- public void setPrefix (String prefix ) {
81
- if (!prefix .endsWith ("." )) {
82
- prefix = prefix + "." ;
83
- }
84
- this .prefix = prefix ;
77
+ public RedisMetricRepository (RedisConnectionFactory redisConnectionFactory ,
78
+ String prefix ) {
79
+ this (redisConnectionFactory , prefix , DEFAULT_KEY );
85
80
}
86
81
87
82
/**
88
- * The redis key to use to store the index of other keys. The redis store will hold a
89
- * zset under this key. Defaults to "keys.spring.metrics". Read operations, especially
90
- * {@link #findAll()} and {@link #count()}, will be much more efficient if the key is
91
- * unique to the {@link #setPrefix(String) prefix} of this repository.
83
+ * Allows user to set the prefix and key to use to store the index of other keys. The
84
+ * redis store will hold a zset under the key just so the metric names can be
85
+ * enumerated. Read operations, especially {@link #findAll()} and {@link #count()},
86
+ * will only be accurate if the key is unique to the prefix of this repository.
87
+ *
88
+ * @param redisConnectionFactory the redis connection factory
89
+ * @param prefix the prefix to set for all metrics keys
92
90
* @param key the key to set
93
91
*/
94
- public void setKey (String key ) {
92
+ public RedisMetricRepository (RedisConnectionFactory redisConnectionFactory ,
93
+ String prefix , String key ) {
94
+ Assert .notNull (redisConnectionFactory , "RedisConnectionFactory must not be null" );
95
+ this .redisOperations = RedisUtils .stringTemplate (redisConnectionFactory );
96
+ if (!prefix .endsWith ("." )) {
97
+ prefix = prefix + "." ;
98
+ }
99
+ this .prefix = prefix ;
100
+ if (!DEFAULT_METRICS_PREFIX .equals (this .prefix )) {
101
+ if (DEFAULT_KEY .equals (key )) {
102
+ key = "keys." + prefix ;
103
+ }
104
+ }
105
+ if (key .endsWith ("." )) {
106
+ key = key .substring (0 , key .length () - 1 );
107
+ }
95
108
this .key = key ;
109
+ this .zSetOperations = this .redisOperations .boundZSetOps (this .key );
96
110
}
97
111
98
112
@ Override
0 commit comments