diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/AbstractMetricExporter.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/AbstractMetricExporter.java index 2a265ae511..ebabd48425 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/AbstractMetricExporter.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/AbstractMetricExporter.java @@ -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> values = new ArrayList>(); 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) { diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/MetricCopyExporter.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/MetricCopyExporter.java index 5bbccc8021..69dbbe706d 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/MetricCopyExporter.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/MetricCopyExporter.java @@ -65,8 +65,8 @@ public class MetricCopyExporter extends AbstractMetricExporter { @Override protected Iterable> 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>() {