Skip to content

Commit

Permalink
v. 0.4. Reimplemented using serialization. Reduced number of trips to…
Browse files Browse the repository at this point in the history
… Redis server. Updating human readable parameters as keys available in Redis on each save.
  • Loading branch information
Ruslan Gainutdinov committed Aug 30, 2017
1 parent 9d785a9 commit 11d1039
Show file tree
Hide file tree
Showing 16 changed files with 291 additions and 1,334 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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.

Uses [XStream](http://x-stream.github.io/) library for serialization.

## Limitations

__ALPHA QUALITY__ Use only if you intend to help improve it.
Expand Down Expand Up @@ -42,7 +44,7 @@ Maven repository is created using [jitpack.io](https://jitpack.io/) [![](https:/
<dependency>
<groupId>com.wizecore</groupId>
<artifactId>persistent-metrics</artifactId>
<version>0.3</version>
<version>0.4</version>
</dependency>
```

Expand Down
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.wizecore</groupId>
<artifactId>persistent-metrics</artifactId>
<packaging>jar</packaging>
<version>0.3</version>
<version>0.4</version>
<name>persistent-metrics</name>
<url>http://github.com/wizecore/persistent-metrics</url>

Expand All @@ -17,10 +17,16 @@
</properties>

<dependencies>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.9</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.24</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
Expand Down
42 changes: 40 additions & 2 deletions src/main/java/com/wizecore/metrics/PersistenceUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import org.redisson.Redisson;
import org.redisson.api.RAtomicDouble;
import org.redisson.api.RAtomicLong;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
* Optional redis address. Use REDIS_ADDR environment variable to set.
* Optional redis address. Use REDIS_ADDR environment variable to set. Have no effect if REDIS_CONF is set.
*/
private static String redisAddr = null;

/**
* Optional redis password. Have no effect if REDIS_CONF is set.
*/
private static String redisPassword = null;

/**
* Common prefix for all values stored. By default <code>metrics.</code>
* Can be specified in environment variable METRICS_PREFIX.
Expand Down Expand Up @@ -76,7 +83,12 @@ protected static void init() {

if (redisConf == null && redisAddr != null && !redisAddr.equals("")) {
redisConf = new Config();
redisConf.useSingleServer().setAddress(redisAddr);
SingleServerConfig ss = redisConf.useSingleServer();
ss.setAddress(redisAddr);

if (redisPassword != null && !redisPassword.equals("")) {
ss.setPassword(redisPassword);
}
}

log.info("Initializing persistent metrics via Redis with " + (redisConf != null ? redisConf.toJSON() : "defaults"));
Expand Down Expand Up @@ -151,6 +163,24 @@ public static RAtomicDouble createAtomicDouble(String name) {
}
return v;
}

public static String getValue(String name) {
init();
RBucket<String> b = redis.getBucket(name);
return b.isExists() ? b.get() : null;
}

public static void setValue(String name, String value) {
init();
RBucket<String> b = redis.getBucket(name);
b.set(value);
}


public static RBucket<String> getBucket(String name) {
init();
return redis.getBucket(name);
}

public static String getRedisConfig() {
return redisConfig;
Expand All @@ -175,4 +205,12 @@ public static String getMetricPrefix() {
public static void setMetricPrefix(String metricPrefix) {
PersistenceUtil.metricPrefix = metricPrefix;
}

public static String getRedisPassword() {
return redisPassword;
}

public static void setRedisPassword(String redisPassword) {
PersistenceUtil.redisPassword = redisPassword;
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/wizecore/metrics/Persistent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.wizecore.metrics;

public interface Persistent {

void save();
}
36 changes: 30 additions & 6 deletions src/main/java/com/wizecore/metrics/PersistentCounter.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
package com.wizecore.metrics;

import org.redisson.api.RAtomicLong;

import com.codahale.metrics.Counter;
import com.thoughtworks.xstream.XStream;

/**
* An incrementing and decrementing counter metric.
*/
public class PersistentCounter extends Counter {
private final LongAdderAdapter count;
public class PersistentCounter extends Counter implements Persistent {
private Counter value;
private RAtomicLong counter;
private String key;

public PersistentCounter(String name) {
this.count = PersistenceUtil.createLongAdderAdapter(name);
XStream x = new XStream();
key = name + ".xml";
String xml = PersistenceUtil.getValue(key);
counter = PersistenceUtil.createAtomicLong(name);
if (xml != null) {
value = (Counter) x.fromXML(xml);
} else {
value = new Counter();
save();
}
}

public void save() {
XStream x = new XStream();
String xml = x.toXML(value);
PersistenceUtil.setValue(key, xml);
counter.set(getCount());
}

/**
Expand All @@ -25,14 +46,16 @@ public void inc() {
* @param n the amount by which the counter will be increased
*/
public void inc(long n) {
count.add(n);
value.inc(n);
save();
}

/**
* Decrement the counter by one.
*/
public void dec() {
dec(1);
save();
}

/**
Expand All @@ -41,7 +64,8 @@ public void dec() {
* @param n the amount by which the counter will be decreased
*/
public void dec(long n) {
count.add(-n);
value.dec(n);
save();
}

/**
Expand All @@ -51,6 +75,6 @@ public void dec(long n) {
*/
@Override
public long getCount() {
return count.sum();
return value.getCount();
}
}
115 changes: 0 additions & 115 deletions src/main/java/com/wizecore/metrics/PersistentEWMA.java

This file was deleted.

Loading

0 comments on commit 11d1039

Please sign in to comment.