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

View File

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