Skip to content

Commit eb7ba78

Browse files
Drop summary and timer lines with a count of 0 (#3030)
Co-authored-by: Georg Pirklbauer <[email protected]>
1 parent fba079f commit eb7ba78

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

implementations/micrometer-registry-dynatrace/src/main/java/io/micrometer/dynatrace/v2/DynatraceExporterV2.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ Stream<String> toTimerLine(Timer meter) {
174174

175175
private Stream<String> toSummaryLine(Meter meter, HistogramSnapshot histogramSnapshot, TimeUnit timeUnit) {
176176
long count = histogramSnapshot.count();
177+
if (count < 1) {
178+
logger.debug("Summary with 0 count dropped: %s", meter.getId().getName());
179+
return Stream.empty();
180+
}
177181
double total = (timeUnit != null) ? histogramSnapshot.total(timeUnit) : histogramSnapshot.total();
178182
double max = (timeUnit != null) ? histogramSnapshot.max(timeUnit) : histogramSnapshot.max();
179183
double min = (count == 1) ? max : minFromHistogramSnapshot(histogramSnapshot, timeUnit);
@@ -218,9 +222,13 @@ Stream<String> toFunctionCounterLine(FunctionCounter meter) {
218222
}
219223

220224
Stream<String> toFunctionTimerLine(FunctionTimer meter) {
225+
long count = (long) meter.count();
226+
if (count < 1) {
227+
logger.debug("Timer with 0 count dropped: %s", meter.getId().getName());
228+
return Stream.empty();
229+
}
221230
double total = meter.totalTime(getBaseTimeUnit());
222231
double average = meter.mean(getBaseTimeUnit());
223-
long count = Double.valueOf(meter.count()).longValue();
224232

225233
return createSummaryLine(meter, average, average, total, count);
226234
}

implementations/micrometer-registry-dynatrace/src/test/java/io/micrometer/dynatrace/v2/DynatraceExporterV2Test.java

+32
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,22 @@ void toTimerLine() {
230230
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());
231231
}
232232

233+
@Test
234+
void toTimerLine_DropIfCountIsZero() {
235+
Timer timer = meterRegistry.timer("my.timer");
236+
timer.record(Duration.ofMillis(60));
237+
clock.add(config.step());
238+
239+
List<String> lines = exporter.toTimerLine(timer).collect(Collectors.toList());
240+
assertThat(lines).hasSize(1);
241+
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());
242+
243+
clock.add(config.step());
244+
// Before the update to drop zero count lines, this would contain 1 line (with count=0), which is not desired.
245+
List<String> zeroCountLines = exporter.toTimerLine(timer).collect(Collectors.toList());
246+
assertThat(zeroCountLines).isEmpty();
247+
}
248+
233249
@Test
234250
void toFunctionTimerLineShouldDropNanMean() {
235251
FunctionTimer functionTimer = new FunctionTimer() {
@@ -329,6 +345,22 @@ void testToDistributionSummaryLine() {
329345
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());
330346
}
331347

348+
@Test
349+
void testToDistributionSummaryLine_DropsLineIfCountIsZero() {
350+
DistributionSummary summary = DistributionSummary.builder("my.summary").register(meterRegistry);
351+
summary.record(3.1);
352+
clock.add(config.step());
353+
354+
List<String> nonEmptyLines = exporter.toDistributionSummaryLine(summary).collect(Collectors.toList());
355+
assertThat(nonEmptyLines).hasSize(1);
356+
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());
357+
358+
clock.add(config.step());
359+
// Before the update to drop zero count lines, this would contain 1 line (with count=0), which is not desired.
360+
List<String> zeroCountLines = exporter.toDistributionSummaryLine(summary).collect(Collectors.toList());
361+
assertThat(zeroCountLines).isEmpty();
362+
}
363+
332364
@Test
333365
void toMeterLine() {
334366
Measurement m1 = new Measurement(() -> 23d, Statistic.VALUE);

0 commit comments

Comments
 (0)