Skip to content

Commit

Permalink
Add support for sendLatest=true/false in exporter
Browse files Browse the repository at this point in the history
Also fix bug in includes/excludes
  • Loading branch information
dsyer authored and wilkinsona committed May 13, 2015
1 parent 3fda744 commit 089b1d0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 + ".");
Expand All @@ -67,13 +71,24 @@ public void setIgnoreTimestamps(boolean 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
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)) {
Expand All @@ -83,6 +98,10 @@ public void export() {
if (!this.ignoreTimestamps && this.earliestTimestamp.after(timestamp)) {
continue;
}
if (!this.ignoreTimestamps && this.sendLatest
&& this.latestTimestamp.after(timestamp)) {
continue;
}
values.add(value);
}
if (!values.isEmpty()) {
Expand All @@ -96,6 +115,7 @@ public void export() {
}
finally {
try {
this.latestTimestamp = new Date(latestTimestamp);
flush();
}
catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public MetricCopyExporter(MetricReader reader, MetricWriter writer, String prefi

@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<?>>() {
Expand Down

0 comments on commit 089b1d0

Please sign in to comment.