Add support for sendLatest=true/false in exporter

Also fix bug in includes/excludes
This commit is contained in:
Dave Syer 2015-05-08 16:11:00 -04:00 committed by Andy Wilkinson
parent 3fda744522
commit 089b1d05dc
2 changed files with 22 additions and 2 deletions

View File

@ -46,6 +46,10 @@ public abstract class AbstractMetricExporter implements Exporter {
private final String prefix;
private Date latestTimestamp = new Date(0L);
private boolean sendLatest = true;
public AbstractMetricExporter(String prefix) {
this.prefix = !StringUtils.hasText(prefix) ? "" : (prefix.endsWith(".") ? prefix
: prefix + ".");
@ -67,13 +71,24 @@ public abstract class AbstractMetricExporter implements Exporter {
this.ignoreTimestamps = ignoreTimestamps;
}
/**
* Send only the data that changed since the last export.
*
* @param sendLatest the flag to set
*/
public void setSendLatest(boolean sendLatest) {
this.sendLatest = sendLatest;
}
@Override
public void export() {
if (!this.processing.compareAndSet(false, true)) {
// skip a tick
return;
}
long latestTimestamp = 0;
try {
latestTimestamp = System.currentTimeMillis();
for (String group : groups()) {
Collection<Metric<?>> values = new ArrayList<Metric<?>>();
for (Metric<?> metric : next(group)) {
@ -83,6 +98,10 @@ public abstract class AbstractMetricExporter implements Exporter {
if (!this.ignoreTimestamps && this.earliestTimestamp.after(timestamp)) {
continue;
}
if (!this.ignoreTimestamps && this.sendLatest
&& this.latestTimestamp.after(timestamp)) {
continue;
}
values.add(value);
}
if (!values.isEmpty()) {
@ -96,6 +115,7 @@ public abstract class AbstractMetricExporter implements Exporter {
}
finally {
try {
this.latestTimestamp = new Date(latestTimestamp);
flush();
}
catch (Exception e) {

View File

@ -65,8 +65,8 @@ public class MetricCopyExporter extends AbstractMetricExporter {
@Override
protected Iterable<Metric<?>> next(String group) {
if ((this.includes != null || this.includes.length == 0)
&& (this.excludes != null || this.excludes.length == 0)) {
if ((this.includes == null || this.includes.length == 0)
&& (this.excludes == null || this.excludes.length == 0)) {
return this.reader.findAll();
}
return new Iterable<Metric<?>>() {