From 80c20e14608103c1dcfd65175d56b067445d1bd3 Mon Sep 17 00:00:00 2001 From: Johnny Lim Date: Thu, 21 Sep 2023 07:17:48 +0900 Subject: [PATCH] Polish gh-4006 (#4086) Co-authored-by: Jonatan Ivanov --- .../dynatrace/v2/DynatraceExporterV2.java | 21 +--- .../dynatrace/DynatraceMeterRegistryTest.java | 12 +- .../dynatrace/v2/DynatraceExporterV2Test.java | 108 ++++++++---------- 3 files changed, 55 insertions(+), 86 deletions(-) diff --git a/implementations/micrometer-registry-dynatrace/src/main/java/io/micrometer/dynatrace/v2/DynatraceExporterV2.java b/implementations/micrometer-registry-dynatrace/src/main/java/io/micrometer/dynatrace/v2/DynatraceExporterV2.java index 771b63ce09..fdfaa6f2d9 100644 --- a/implementations/micrometer-registry-dynatrace/src/main/java/io/micrometer/dynatrace/v2/DynatraceExporterV2.java +++ b/implementations/micrometer-registry-dynatrace/src/main/java/io/micrometer/dynatrace/v2/DynatraceExporterV2.java @@ -195,8 +195,8 @@ private void sendBatchIfFull(List batch, int partitionSize) { private Stream toMetricLines(Meter meter, Map seenMetadata) { return meter.match(m -> toGaugeLine(m, seenMetadata), m -> toCounterLine(m, seenMetadata), m -> toTimerLine(m, seenMetadata), m -> toDistributionSummaryLine(m, seenMetadata), - m -> toLongTaskTimerLine(m, seenMetadata), m -> toTimeGaugeLine(m, seenMetadata), - m -> toFunctionCounterLine(m, seenMetadata), m -> toFunctionTimerLine(m, seenMetadata), + m -> toLongTaskTimerLine(m, seenMetadata), m -> toGaugeLine(m, seenMetadata), + m -> toCounterLine(m, seenMetadata), m -> toFunctionTimerLine(m, seenMetadata), m -> toGaugeLine(m, seenMetadata)); } @@ -235,9 +235,8 @@ private String createGaugeLine(Meter meter, Map seenMetadata, Me return null; } - Stream toCounterLine(Counter counter, Map seenMetadata) { - return toMeterLine(counter, - (Meter meter, Measurement measurement) -> this.createCounterLine(meter, seenMetadata, measurement)); + Stream toCounterLine(Meter counter, Map seenMetadata) { + return toMeterLine(counter, (meter, measurement) -> createCounterLine(meter, seenMetadata, measurement)); } private String createCounterLine(Meter meter, Map seenMetadata, Measurement measurement) { @@ -344,14 +343,6 @@ else if (count == 1) { return toSummaryLine(meter, seenMetadata, snapshot, getBaseTimeUnit()); } - Stream toTimeGaugeLine(TimeGauge meter, Map seenMetadata) { - return toMeterLine(meter, (theMeter, measurement) -> createGaugeLine(theMeter, seenMetadata, measurement)); - } - - Stream toFunctionCounterLine(FunctionCounter meter, Map seenMetadata) { - return toMeterLine(meter, (theMeter, measurement) -> createCounterLine(theMeter, seenMetadata, measurement)); - } - Stream toFunctionTimerLine(FunctionTimer meter, Map seenMetadata) { long count = (long) meter.count(); if (count == 0) { @@ -376,10 +367,6 @@ Stream toFunctionTimerLine(FunctionTimer meter, Map seen return createSummaryLine(meter, seenMetadata, average, average, total, count); } - Stream toMeterLine(Meter meter, Map seenMetadata) { - return toMeterLine(meter, (theMeter, measurement) -> createGaugeLine(theMeter, seenMetadata, measurement)); - } - private Stream toMeterLine(Meter meter, BiFunction measurementConverter) { return streamOf(meter.measure()).map(measurement -> measurementConverter.apply(meter, measurement)) .filter(Objects::nonNull); diff --git a/implementations/micrometer-registry-dynatrace/src/test/java/io/micrometer/dynatrace/DynatraceMeterRegistryTest.java b/implementations/micrometer-registry-dynatrace/src/test/java/io/micrometer/dynatrace/DynatraceMeterRegistryTest.java index d570762fc5..adcf64db1d 100644 --- a/implementations/micrometer-registry-dynatrace/src/test/java/io/micrometer/dynatrace/DynatraceMeterRegistryTest.java +++ b/implementations/micrometer-registry-dynatrace/src/test/java/io/micrometer/dynatrace/DynatraceMeterRegistryTest.java @@ -130,8 +130,7 @@ void shouldResetBetweenRequests() throws Throwable { assertThat(request2.getEntity()).asString() .hasLineCount(2) - .containsIgnoringNewLines( - "my.timer,dt.metrics.source=micrometer gauge,min=33,max=44,sum=77,count=2 " + clock.wallTime(), + .contains("my.timer,dt.metrics.source=micrometer gauge,min=33,max=44,sum=77,count=2 " + clock.wallTime(), "#my.timer gauge dt.meta.unit=milliseconds"); } @@ -148,8 +147,7 @@ void shouldNotTrackPercentilesWithDynatraceSummary() throws Throwable { verify(httpClient).send(assertArg((request -> assertThat(request.getEntity()).asString() .hasLineCount(2) - .containsIgnoringNewLines( - "my.timer,dt.metrics.source=micrometer gauge,min=22,max=55,sum=77,count=2 " + clock.wallTime(), + .contains("my.timer,dt.metrics.source=micrometer gauge,min=22,max=55,sum=77,count=2 " + clock.wallTime(), "#my.timer gauge dt.meta.unit=milliseconds")))); } @@ -166,8 +164,7 @@ void shouldNotExportLinesWithZeroCount() throws Throwable { verify(httpClient).send(assertArg(request -> assertThat(request.getEntity()).asString() .hasLineCount(2) - .containsIgnoringNewLines( - "my.timer,dt.metrics.source=micrometer gauge,min=44,max=44,sum=44,count=1 " + clock.wallTime(), + .contains("my.timer,dt.metrics.source=micrometer gauge,min=44,max=44,sum=44,count=1 " + clock.wallTime(), "#my.timer gauge dt.meta.unit=milliseconds"))); // reset for next export interval @@ -192,8 +189,7 @@ void shouldNotExportLinesWithZeroCount() throws Throwable { verify(httpClient).send(assertArg(request -> assertThat(request.getEntity()).asString() .hasLineCount(2) - .containsIgnoringNewLines( - "my.timer,dt.metrics.source=micrometer gauge,min=33,max=33,sum=33,count=1 " + clock.wallTime(), + .contains("my.timer,dt.metrics.source=micrometer gauge,min=33,max=33,sum=33,count=1 " + clock.wallTime(), "#my.timer gauge dt.meta.unit=milliseconds"))); } diff --git a/implementations/micrometer-registry-dynatrace/src/test/java/io/micrometer/dynatrace/v2/DynatraceExporterV2Test.java b/implementations/micrometer-registry-dynatrace/src/test/java/io/micrometer/dynatrace/v2/DynatraceExporterV2Test.java index c6ce4b75b7..a7448f337a 100644 --- a/implementations/micrometer-registry-dynatrace/src/test/java/io/micrometer/dynatrace/v2/DynatraceExporterV2Test.java +++ b/implementations/micrometer-registry-dynatrace/src/test/java/io/micrometer/dynatrace/v2/DynatraceExporterV2Test.java @@ -32,7 +32,6 @@ import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; -import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.time.Duration; @@ -132,35 +131,35 @@ void toGaugeLineShouldDropInfiniteValues() { } @Test - void toTimeGaugeLine() { + void toGaugeLineWithTimeGauge() { AtomicReference obj = new AtomicReference<>(2.3d); meterRegistry.more().timeGauge("my.timeGauge", Tags.empty(), obj, MILLISECONDS, AtomicReference::get); TimeGauge timeGauge = meterRegistry.find("my.timeGauge").timeGauge(); - List lines = exporter.toTimeGaugeLine(timeGauge, SEEN_METADATA).collect(Collectors.toList()); + List lines = exporter.toGaugeLine(timeGauge, SEEN_METADATA).collect(Collectors.toList()); assertThat(lines).hasSize(1); assertThat(lines.get(0)).isEqualTo("my.timeGauge,dt.metrics.source=micrometer gauge,2.3 " + clock.wallTime()); } @Test - void toTimeGaugeLineShouldDropNanValue() { + void toGaugeLineWithTimeGaugeShouldDropNanValue() { AtomicReference obj = new AtomicReference<>(NaN); meterRegistry.more().timeGauge("my.timeGauge", Tags.empty(), obj, MILLISECONDS, AtomicReference::get); TimeGauge timeGauge = meterRegistry.find("my.timeGauge").timeGauge(); - assertThat(exporter.toTimeGaugeLine(timeGauge, SEEN_METADATA)).isEmpty(); + assertThat(exporter.toGaugeLine(timeGauge, SEEN_METADATA)).isEmpty(); } @Test - void toTimeGaugeLineShouldDropInfiniteValues() { + void toGaugeLineWithTimeGaugeShouldDropInfiniteValues() { AtomicReference obj = new AtomicReference<>(POSITIVE_INFINITY); meterRegistry.more().timeGauge("my.timeGauge", Tags.empty(), obj, MILLISECONDS, AtomicReference::get); TimeGauge timeGauge = meterRegistry.find("my.timeGauge").timeGauge(); - assertThat(exporter.toTimeGaugeLine(timeGauge, SEEN_METADATA)).isEmpty(); + assertThat(exporter.toGaugeLine(timeGauge, SEEN_METADATA)).isEmpty(); obj = new AtomicReference<>(NEGATIVE_INFINITY); meterRegistry.more().timeGauge("my.timeGauge", Tags.empty(), obj, MILLISECONDS, AtomicReference::get); timeGauge = meterRegistry.find("my.timeGauge").timeGauge(); - assertThat(exporter.toTimeGaugeLine(timeGauge, SEEN_METADATA)).isEmpty(); + assertThat(exporter.toGaugeLine(timeGauge, SEEN_METADATA)).isEmpty(); } @Test @@ -195,7 +194,7 @@ void toCounterLineShouldDropInfiniteValue() { } @Test - void toFunctionCounterLine() { + void toCounterLineWithFunctionCounter() { AtomicReference obj = new AtomicReference<>(0.0d); FunctionCounter.builder("my.functionCounter", obj, AtomicReference::get).register(meterRegistry); FunctionCounter functionCounter = meterRegistry.find("my.functionCounter").functionCounter(); @@ -204,15 +203,14 @@ void toFunctionCounterLine() { obj.set(2.3d); clock.add(config.step()); - List lines = exporter.toFunctionCounterLine(functionCounter, SEEN_METADATA) - .collect(Collectors.toList()); + List lines = exporter.toCounterLine(functionCounter, SEEN_METADATA).collect(Collectors.toList()); assertThat(lines).hasSize(1); assertThat(lines.get(0)) .isEqualTo("my.functionCounter,dt.metrics.source=micrometer count,delta=2.3 " + clock.wallTime()); } @Test - void toFunctionCounterLineShouldDropNanValue() { + void toCounterLineWithFunctionCounterShouldDropNanValue() { AtomicReference obj = new AtomicReference<>(0.0d); FunctionCounter.builder("my.functionCounter", obj, AtomicReference::get).register(meterRegistry); FunctionCounter functionCounter = meterRegistry.find("my.functionCounter").functionCounter(); @@ -221,11 +219,11 @@ void toFunctionCounterLineShouldDropNanValue() { obj.set(NaN); clock.add(config.step()); - assertThat(exporter.toFunctionCounterLine(functionCounter, SEEN_METADATA)).isEmpty(); + assertThat(exporter.toCounterLine(functionCounter, SEEN_METADATA)).isEmpty(); } @Test - void toFunctionCounterLineShouldDropInfiniteValue() { + void toCounterLineWithFunctionCounterShouldDropInfiniteValue() { AtomicReference obj = new AtomicReference<>(0.0d); FunctionCounter.builder("my.functionCounter", obj, AtomicReference::get).register(meterRegistry); FunctionCounter functionCounter = meterRegistry.find("my.functionCounter").functionCounter(); @@ -234,7 +232,7 @@ void toFunctionCounterLineShouldDropInfiniteValue() { obj.set(POSITIVE_INFINITY); clock.add(config.step()); - assertThat(exporter.toFunctionCounterLine(functionCounter, SEEN_METADATA)).isEmpty(); + assertThat(exporter.toCounterLine(functionCounter, SEEN_METADATA)).isEmpty(); } @Test @@ -509,13 +507,13 @@ void testToDistributionSummaryLine_DropsLineIfCountIsZero() { } @Test - void toMeterLine() { + void toGaugeLineWithMeter() { Measurement m1 = new Measurement(() -> 23d, Statistic.VALUE); Measurement m2 = new Measurement(() -> 42d, Statistic.VALUE); Measurement m3 = new Measurement(() -> 5d, Statistic.VALUE); Meter meter = Meter.builder("my.custom", Meter.Type.OTHER, Arrays.asList(m1, m2, m3)).register(meterRegistry); - List lines = exporter.toMeterLine(meter, SEEN_METADATA).collect(Collectors.toList()); + List lines = exporter.toGaugeLine(meter, SEEN_METADATA).collect(Collectors.toList()); assertThat(lines).hasSize(3); assertThat(lines.get(0)).isEqualTo("my.custom,dt.metrics.source=micrometer gauge,23 " + clock.wallTime()); assertThat(lines.get(1)).isEqualTo("my.custom,dt.metrics.source=micrometer gauge,42 " + clock.wallTime()); @@ -716,8 +714,8 @@ public String get(String key) { assertThat(firstRequest.getUrl()).hasToString(firstUri); assertThat(firstRequest.getRequestHeaders()).containsOnly(entry("Content-Type", "text/plain"), entry("User-Agent", "micrometer"), entry("Authorization", "Api-Token YOUR.DYNATRACE.TOKEN.FIRST")); - String firstReqBody = new String(firstRequest.getEntity(), StandardCharsets.UTF_8); - assertThat(firstReqBody).isEqualTo("test.counter,dt.metrics.source=micrometer count,delta=10"); + assertThat(firstRequest.getEntity()).asString() + .isEqualTo("test.counter,dt.metrics.source=micrometer count,delta=10"); counter.increment(30); clock.add(config.step()); @@ -737,8 +735,8 @@ public String get(String key) { assertThat(secondRequest.getUrl()).hasToString(secondUri); assertThat(secondRequest.getRequestHeaders()).containsOnly(entry("Content-Type", "text/plain"), entry("User-Agent", "micrometer"), entry("Authorization", "Api-Token YOUR.DYNATRACE.TOKEN.SECOND")); - String secondReqBody = new String(secondRequest.getEntity(), StandardCharsets.UTF_8); - assertThat(secondReqBody).isEqualTo("test.counter,dt.metrics.source=micrometer count,delta=30"); + assertThat(secondRequest.getEntity()).asString() + .isEqualTo("test.counter,dt.metrics.source=micrometer count,delta=30"); } @Test @@ -749,14 +747,13 @@ void gaugeMetadataIsSerialized() { Gauge.builder("my.gauge", () -> 1.23).description("my.description").baseUnit("Liters").register(meterRegistry); exporter.export(meterRegistry.getMeters()); - ArgumentCaptor stringArgumentCaptor = ArgumentCaptor.forClass(String.class); - verify(builder).withPlainText(stringArgumentCaptor.capture()); - // get the data set to the request and split it into lines on the newline char. - List lines = Arrays.asList(stringArgumentCaptor.getValue().split("\n")); - - assertThat(lines).hasSize(2) - .containsExactly("my.gauge,dt.metrics.source=micrometer gauge,1.23 " + clock.wallTime(), + verify(builder).withPlainText(assertArg(body -> { + // get the data set to the request and split it into lines on the newline + // char. + assertThat(body.split("\n")).containsExactly( + "my.gauge,dt.metrics.source=micrometer gauge,1.23 " + clock.wallTime(), "#my.gauge gauge dt.meta.description=my.description,dt.meta.unit=Liters"); + })); } @Test @@ -772,13 +769,11 @@ void counterMetadataIsSerialized() { clock.add(config.step()); exporter.export(meterRegistry.getMeters()); - ArgumentCaptor stringArgumentCaptor = ArgumentCaptor.forClass(String.class); - verify(builder).withPlainText(stringArgumentCaptor.capture()); - List lines = Arrays.asList(stringArgumentCaptor.getValue().split("\n")); - - assertThat(lines).hasSize(2) - .containsExactly("my.count,dt.metrics.source=micrometer count,delta=5.234 " + clock.wallTime(), + verify(builder).withPlainText(assertArg(body -> { + assertThat(body.split("\n")).containsExactly( + "my.count,dt.metrics.source=micrometer count,delta=5.234 " + clock.wallTime(), "#my.count count dt.meta.description=count\\ description,dt.meta.unit=Bytes"); + })); } @Test @@ -788,7 +783,7 @@ void sendsTwoRequestsWhenSizeLimitIsReachedWithMetadata() { when(httpClient.post(anyString())).thenReturn(firstReq).thenReturn(secondReq); // create a dynatrace config (same as the one returned by - // createDefaultDynatraceConfig) but with a batch size of 3. + // createDefaultDynatraceConfig() but with a batch size of 3). DynatraceConfig config = new DynatraceConfig() { @Override public String get(String key) { @@ -839,17 +834,17 @@ public int batchSize() { verify(firstReq).withPlainText(firstReqCap.capture()); verify(secondReq).withPlainText(secondReqCap.capture()); - List firstReqLines = Arrays.asList(firstReqCap.getValue().split("\n")); - List secondReqLines = Arrays.asList(secondReqCap.getValue().split("\n")); + String[] firstReqLines = firstReqCap.getValue().split("\n"); + String[] secondReqLines = secondReqCap.getValue().split("\n"); // the first request will contain the metric lines - assertThat(firstReqLines).hasSize(3) - .containsExactly("my.count,dt.metrics.source=micrometer count,delta=5.234 " + clock.wallTime(), - "my.gauge,dt.metrics.source=micrometer gauge,1.23 " + clock.wallTime(), - "#my.count count dt.meta.description=count\\ description,dt.meta.unit=Bytes"); + assertThat(firstReqLines).containsExactly( + "my.count,dt.metrics.source=micrometer count,delta=5.234 " + clock.wallTime(), + "my.gauge,dt.metrics.source=micrometer gauge,1.23 " + clock.wallTime(), + "#my.count count dt.meta.description=count\\ description,dt.meta.unit=Bytes"); - // the second request will the leftover metadata line - assertThat(secondReqLines).hasSize(1) + // the second request will contain the leftover metadata line + assertThat(secondReqLines) .containsExactly("#my.gauge gauge dt.meta.description=my.description,dt.meta.unit=Liters"); } @@ -875,23 +870,14 @@ void metadataIsSerializedOnceWhenSetTwice() { clock.add(config.step()); exporter.export(meterRegistry.getMeters()); - ArgumentCaptor stringArgumentCaptor = ArgumentCaptor.forClass(String.class); - verify(builder).withPlainText(stringArgumentCaptor.capture()); - List lines = Arrays.asList(stringArgumentCaptor.getValue().split("\n")); - - List> expectedDimes = Arrays.asList( - Arrays.asList("counter-number=counter1", "dt.metrics.source=micrometer"), - Arrays.asList("counter-number=counter2", "dt.metrics.source=micrometer"), - Arrays.asList("dt.meta.description=count\\ description", "dt.meta.unit=Bytes")); - List expectedBases = Arrays.asList("my.count count,delta=5.234 " + clock.wallTime(), - "my.count count,delta=2.345 " + clock.wallTime(), "#my.count count"); - - assertThat(lines).hasSize(3) - .extracting(line -> extractBase(line)) - .containsExactlyInAnyOrderElementsOf(expectedBases); - for (int i = 0; i < lines.size(); i++) { - assertThat(extractDims(lines.get(i))).containsExactlyInAnyOrderElementsOf(expectedDimes.get(i)); - } + verify(builder).withPlainText(assertArg(body -> { + assertThat(body.split("\n")).containsExactly( + "my.count,dt.metrics.source=micrometer,counter-number=counter1 count,delta=5.234 " + + clock.wallTime(), + "my.count,dt.metrics.source=micrometer,counter-number=counter2 count,delta=2.345 " + + clock.wallTime(), + "#my.count count dt.meta.description=count\\ description,dt.meta.unit=Bytes"); + })); } @Test @@ -941,7 +927,7 @@ void metadataIsNotExportedWhenTurnedOff() { when(httpClient.post(anyString())).thenReturn(builder); // create a dynatrace config (same as the one returned by - // createDefaultDynatraceConfig() but with metadata turned off + // createDefaultDynatraceConfig() but with metadata turned off). DynatraceConfig config = new DynatraceConfig() { @Override public String get(String key) {