Skip to content

Commit

Permalink
Drop summary and timer lines with a count of 0 (#3030)
Browse files Browse the repository at this point in the history
Co-authored-by: Georg Pirklbauer <[email protected]>
  • Loading branch information
jonatan-ivanov and pirgeo authored Feb 15, 2022
1 parent fba079f commit eb7ba78
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ Stream<String> toTimerLine(Timer meter) {

private Stream<String> toSummaryLine(Meter meter, HistogramSnapshot histogramSnapshot, TimeUnit timeUnit) {
long count = histogramSnapshot.count();
if (count < 1) {
logger.debug("Summary with 0 count dropped: %s", meter.getId().getName());
return Stream.empty();
}
double total = (timeUnit != null) ? histogramSnapshot.total(timeUnit) : histogramSnapshot.total();
double max = (timeUnit != null) ? histogramSnapshot.max(timeUnit) : histogramSnapshot.max();
double min = (count == 1) ? max : minFromHistogramSnapshot(histogramSnapshot, timeUnit);
Expand Down Expand Up @@ -218,9 +222,13 @@ Stream<String> toFunctionCounterLine(FunctionCounter meter) {
}

Stream<String> toFunctionTimerLine(FunctionTimer meter) {
long count = (long) meter.count();
if (count < 1) {
logger.debug("Timer with 0 count dropped: %s", meter.getId().getName());
return Stream.empty();
}
double total = meter.totalTime(getBaseTimeUnit());
double average = meter.mean(getBaseTimeUnit());
long count = Double.valueOf(meter.count()).longValue();

return createSummaryLine(meter, average, average, total, count);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,22 @@ void toTimerLine() {
assertThat(lines.get(0)).isEqualTo("my.timer,dt.metrics.source=micrometer gauge,min=0.0,max=60.0,sum=90.0,count=3 " + clock.wallTime());
}

@Test
void toTimerLine_DropIfCountIsZero() {
Timer timer = meterRegistry.timer("my.timer");
timer.record(Duration.ofMillis(60));
clock.add(config.step());

List<String> lines = exporter.toTimerLine(timer).collect(Collectors.toList());
assertThat(lines).hasSize(1);
assertThat(lines.get(0)).isEqualTo("my.timer,dt.metrics.source=micrometer gauge,min=60.0,max=60.0,sum=60.0,count=1 " + clock.wallTime());

clock.add(config.step());
// Before the update to drop zero count lines, this would contain 1 line (with count=0), which is not desired.
List<String> zeroCountLines = exporter.toTimerLine(timer).collect(Collectors.toList());
assertThat(zeroCountLines).isEmpty();
}

@Test
void toFunctionTimerLineShouldDropNanMean() {
FunctionTimer functionTimer = new FunctionTimer() {
Expand Down Expand Up @@ -329,6 +345,22 @@ void testToDistributionSummaryLine() {
assertThat(lines.get(0)).isEqualTo("my.summary,dt.metrics.source=micrometer gauge,min=0.0,max=5.4,sum=10.9,count=4 " + clock.wallTime());
}

@Test
void testToDistributionSummaryLine_DropsLineIfCountIsZero() {
DistributionSummary summary = DistributionSummary.builder("my.summary").register(meterRegistry);
summary.record(3.1);
clock.add(config.step());

List<String> nonEmptyLines = exporter.toDistributionSummaryLine(summary).collect(Collectors.toList());
assertThat(nonEmptyLines).hasSize(1);
assertThat(nonEmptyLines.get(0)).isEqualTo("my.summary,dt.metrics.source=micrometer gauge,min=3.1,max=3.1,sum=3.1,count=1 " + clock.wallTime());

clock.add(config.step());
// Before the update to drop zero count lines, this would contain 1 line (with count=0), which is not desired.
List<String> zeroCountLines = exporter.toDistributionSummaryLine(summary).collect(Collectors.toList());
assertThat(zeroCountLines).isEmpty();
}

@Test
void toMeterLine() {
Measurement m1 = new Measurement(() -> 23d, Statistic.VALUE);
Expand Down

0 comments on commit eb7ba78

Please sign in to comment.