Skip to content

Commit 11d1039

Browse files
author
Ruslan Gainutdinov
committed
v. 0.4. Reimplemented using serialization. Reduced number of trips to Redis server. Updating human readable parameters as keys available in Redis on each save.
1 parent 9d785a9 commit 11d1039

16 files changed

+291
-1334
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
Drop-in enhancement for [Dropwizard Metrics](http://metrics.dropwizard.io/) which provide metric persistence using Redis DB via [Redisson](https://github.com/redisson/redisson) library.
44

5+
Uses [XStream](http://x-stream.github.io/) library for serialization.
6+
57
## Limitations
68

79
__ALPHA QUALITY__ Use only if you intend to help improve it.
@@ -42,7 +44,7 @@ Maven repository is created using [jitpack.io](https://jitpack.io/) [![](https:/
4244
<dependency>
4345
<groupId>com.wizecore</groupId>
4446
<artifactId>persistent-metrics</artifactId>
45-
<version>0.3</version>
47+
<version>0.4</version>
4648
</dependency>
4749
```
4850

pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>com.wizecore</groupId>
55
<artifactId>persistent-metrics</artifactId>
66
<packaging>jar</packaging>
7-
<version>0.3</version>
7+
<version>0.4</version>
88
<name>persistent-metrics</name>
99
<url>http://github.com/wizecore/persistent-metrics</url>
1010

@@ -17,10 +17,16 @@
1717
</properties>
1818

1919
<dependencies>
20+
<dependency>
21+
<groupId>com.thoughtworks.xstream</groupId>
22+
<artifactId>xstream</artifactId>
23+
<version>1.4.9</version>
24+
</dependency>
2025
<dependency>
2126
<groupId>org.slf4j</groupId>
2227
<artifactId>slf4j-jdk14</artifactId>
2328
<version>1.7.24</version>
29+
<scope>test</scope>
2430
</dependency>
2531
<dependency>
2632
<groupId>org.slf4j</groupId>

src/main/java/com/wizecore/metrics/PersistenceUtil.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
import org.redisson.Redisson;
88
import org.redisson.api.RAtomicDouble;
99
import org.redisson.api.RAtomicLong;
10+
import org.redisson.api.RBucket;
1011
import org.redisson.api.RedissonClient;
1112
import org.redisson.config.Config;
13+
import org.redisson.config.SingleServerConfig;
1214
import org.slf4j.Logger;
1315
import org.slf4j.LoggerFactory;
1416

@@ -34,10 +36,15 @@ public class PersistenceUtil {
3436
private static String redisConfig = null;
3537

3638
/**
37-
* Optional redis address. Use REDIS_ADDR environment variable to set.
39+
* Optional redis address. Use REDIS_ADDR environment variable to set. Have no effect if REDIS_CONF is set.
3840
*/
3941
private static String redisAddr = null;
4042

43+
/**
44+
* Optional redis password. Have no effect if REDIS_CONF is set.
45+
*/
46+
private static String redisPassword = null;
47+
4148
/**
4249
* Common prefix for all values stored. By default <code>metrics.</code>
4350
* Can be specified in environment variable METRICS_PREFIX.
@@ -76,7 +83,12 @@ protected static void init() {
7683

7784
if (redisConf == null && redisAddr != null && !redisAddr.equals("")) {
7885
redisConf = new Config();
79-
redisConf.useSingleServer().setAddress(redisAddr);
86+
SingleServerConfig ss = redisConf.useSingleServer();
87+
ss.setAddress(redisAddr);
88+
89+
if (redisPassword != null && !redisPassword.equals("")) {
90+
ss.setPassword(redisPassword);
91+
}
8092
}
8193

8294
log.info("Initializing persistent metrics via Redis with " + (redisConf != null ? redisConf.toJSON() : "defaults"));
@@ -151,6 +163,24 @@ public static RAtomicDouble createAtomicDouble(String name) {
151163
}
152164
return v;
153165
}
166+
167+
public static String getValue(String name) {
168+
init();
169+
RBucket<String> b = redis.getBucket(name);
170+
return b.isExists() ? b.get() : null;
171+
}
172+
173+
public static void setValue(String name, String value) {
174+
init();
175+
RBucket<String> b = redis.getBucket(name);
176+
b.set(value);
177+
}
178+
179+
180+
public static RBucket<String> getBucket(String name) {
181+
init();
182+
return redis.getBucket(name);
183+
}
154184

155185
public static String getRedisConfig() {
156186
return redisConfig;
@@ -175,4 +205,12 @@ public static String getMetricPrefix() {
175205
public static void setMetricPrefix(String metricPrefix) {
176206
PersistenceUtil.metricPrefix = metricPrefix;
177207
}
208+
209+
public static String getRedisPassword() {
210+
return redisPassword;
211+
}
212+
213+
public static void setRedisPassword(String redisPassword) {
214+
PersistenceUtil.redisPassword = redisPassword;
215+
}
178216
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.wizecore.metrics;
2+
3+
public interface Persistent {
4+
5+
void save();
6+
}

src/main/java/com/wizecore/metrics/PersistentCounter.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
11
package com.wizecore.metrics;
22

3+
import org.redisson.api.RAtomicLong;
4+
35
import com.codahale.metrics.Counter;
6+
import com.thoughtworks.xstream.XStream;
47

58
/**
69
* An incrementing and decrementing counter metric.
710
*/
8-
public class PersistentCounter extends Counter {
9-
private final LongAdderAdapter count;
11+
public class PersistentCounter extends Counter implements Persistent {
12+
private Counter value;
13+
private RAtomicLong counter;
14+
private String key;
1015

1116
public PersistentCounter(String name) {
12-
this.count = PersistenceUtil.createLongAdderAdapter(name);
17+
XStream x = new XStream();
18+
key = name + ".xml";
19+
String xml = PersistenceUtil.getValue(key);
20+
counter = PersistenceUtil.createAtomicLong(name);
21+
if (xml != null) {
22+
value = (Counter) x.fromXML(xml);
23+
} else {
24+
value = new Counter();
25+
save();
26+
}
27+
}
28+
29+
public void save() {
30+
XStream x = new XStream();
31+
String xml = x.toXML(value);
32+
PersistenceUtil.setValue(key, xml);
33+
counter.set(getCount());
1334
}
1435

1536
/**
@@ -25,14 +46,16 @@ public void inc() {
2546
* @param n the amount by which the counter will be increased
2647
*/
2748
public void inc(long n) {
28-
count.add(n);
49+
value.inc(n);
50+
save();
2951
}
3052

3153
/**
3254
* Decrement the counter by one.
3355
*/
3456
public void dec() {
3557
dec(1);
58+
save();
3659
}
3760

3861
/**
@@ -41,7 +64,8 @@ public void dec() {
4164
* @param n the amount by which the counter will be decreased
4265
*/
4366
public void dec(long n) {
44-
count.add(-n);
67+
value.dec(n);
68+
save();
4569
}
4670

4771
/**
@@ -51,6 +75,6 @@ public void dec(long n) {
5175
*/
5276
@Override
5377
public long getCount() {
54-
return count.sum();
78+
return value.getCount();
5579
}
5680
}

src/main/java/com/wizecore/metrics/PersistentEWMA.java

Lines changed: 0 additions & 115 deletions
This file was deleted.

0 commit comments

Comments
 (0)