Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Co-authored-by: Jonatan Ivanov <[email protected]>
  • Loading branch information
izeye and jonatan-ivanov authored Sep 20, 2023
1 parent f267eeb commit 80c20e1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ private void sendBatchIfFull(List<String> batch, int partitionSize) {
private Stream<String> toMetricLines(Meter meter, Map<String, String> 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));
}

Expand Down Expand Up @@ -235,9 +235,8 @@ private String createGaugeLine(Meter meter, Map<String, String> seenMetadata, Me
return null;
}

Stream<String> toCounterLine(Counter counter, Map<String, String> seenMetadata) {
return toMeterLine(counter,
(Meter meter, Measurement measurement) -> this.createCounterLine(meter, seenMetadata, measurement));
Stream<String> toCounterLine(Meter counter, Map<String, String> seenMetadata) {
return toMeterLine(counter, (meter, measurement) -> createCounterLine(meter, seenMetadata, measurement));
}

private String createCounterLine(Meter meter, Map<String, String> seenMetadata, Measurement measurement) {
Expand Down Expand Up @@ -344,14 +343,6 @@ else if (count == 1) {
return toSummaryLine(meter, seenMetadata, snapshot, getBaseTimeUnit());
}

Stream<String> toTimeGaugeLine(TimeGauge meter, Map<String, String> seenMetadata) {
return toMeterLine(meter, (theMeter, measurement) -> createGaugeLine(theMeter, seenMetadata, measurement));
}

Stream<String> toFunctionCounterLine(FunctionCounter meter, Map<String, String> seenMetadata) {
return toMeterLine(meter, (theMeter, measurement) -> createCounterLine(theMeter, seenMetadata, measurement));
}

Stream<String> toFunctionTimerLine(FunctionTimer meter, Map<String, String> seenMetadata) {
long count = (long) meter.count();
if (count == 0) {
Expand All @@ -376,10 +367,6 @@ Stream<String> toFunctionTimerLine(FunctionTimer meter, Map<String, String> seen
return createSummaryLine(meter, seenMetadata, average, average, total, count);
}

Stream<String> toMeterLine(Meter meter, Map<String, String> seenMetadata) {
return toMeterLine(meter, (theMeter, measurement) -> createGaugeLine(theMeter, seenMetadata, measurement));
}

private Stream<String> toMeterLine(Meter meter, BiFunction<Meter, Measurement, String> measurementConverter) {
return streamOf(meter.measure()).map(measurement -> measurementConverter.apply(meter, measurement))
.filter(Objects::nonNull);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand All @@ -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"))));
}

Expand All @@ -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
Expand All @@ -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")));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -132,35 +131,35 @@ void toGaugeLineShouldDropInfiniteValues() {
}

@Test
void toTimeGaugeLine() {
void toGaugeLineWithTimeGauge() {
AtomicReference<Double> obj = new AtomicReference<>(2.3d);
meterRegistry.more().timeGauge("my.timeGauge", Tags.empty(), obj, MILLISECONDS, AtomicReference::get);
TimeGauge timeGauge = meterRegistry.find("my.timeGauge").timeGauge();
List<String> lines = exporter.toTimeGaugeLine(timeGauge, SEEN_METADATA).collect(Collectors.toList());
List<String> 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<Double> 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<Double> 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
Expand Down Expand Up @@ -195,7 +194,7 @@ void toCounterLineShouldDropInfiniteValue() {
}

@Test
void toFunctionCounterLine() {
void toCounterLineWithFunctionCounter() {
AtomicReference<Double> obj = new AtomicReference<>(0.0d);
FunctionCounter.builder("my.functionCounter", obj, AtomicReference::get).register(meterRegistry);
FunctionCounter functionCounter = meterRegistry.find("my.functionCounter").functionCounter();
Expand All @@ -204,15 +203,14 @@ void toFunctionCounterLine() {
obj.set(2.3d);
clock.add(config.step());

List<String> lines = exporter.toFunctionCounterLine(functionCounter, SEEN_METADATA)
.collect(Collectors.toList());
List<String> 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<Double> obj = new AtomicReference<>(0.0d);
FunctionCounter.builder("my.functionCounter", obj, AtomicReference::get).register(meterRegistry);
FunctionCounter functionCounter = meterRegistry.find("my.functionCounter").functionCounter();
Expand All @@ -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<Double> obj = new AtomicReference<>(0.0d);
FunctionCounter.builder("my.functionCounter", obj, AtomicReference::get).register(meterRegistry);
FunctionCounter functionCounter = meterRegistry.find("my.functionCounter").functionCounter();
Expand All @@ -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
Expand Down Expand Up @@ -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<String> lines = exporter.toMeterLine(meter, SEEN_METADATA).collect(Collectors.toList());
List<String> 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());
Expand Down Expand Up @@ -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());
Expand All @@ -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
Expand All @@ -749,14 +747,13 @@ void gaugeMetadataIsSerialized() {
Gauge.builder("my.gauge", () -> 1.23).description("my.description").baseUnit("Liters").register(meterRegistry);
exporter.export(meterRegistry.getMeters());

ArgumentCaptor<String> 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<String> 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
Expand All @@ -772,13 +769,11 @@ void counterMetadataIsSerialized() {
clock.add(config.step());
exporter.export(meterRegistry.getMeters());

ArgumentCaptor<String> stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
verify(builder).withPlainText(stringArgumentCaptor.capture());
List<String> 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
Expand All @@ -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) {
Expand Down Expand Up @@ -839,17 +834,17 @@ public int batchSize() {
verify(firstReq).withPlainText(firstReqCap.capture());
verify(secondReq).withPlainText(secondReqCap.capture());

List<String> firstReqLines = Arrays.asList(firstReqCap.getValue().split("\n"));
List<String> 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");
}

Expand All @@ -875,23 +870,14 @@ void metadataIsSerializedOnceWhenSetTwice() {
clock.add(config.step());
exporter.export(meterRegistry.getMeters());

ArgumentCaptor<String> stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
verify(builder).withPlainText(stringArgumentCaptor.capture());
List<String> lines = Arrays.asList(stringArgumentCaptor.getValue().split("\n"));

List<List<String>> 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<String> 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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 80c20e1

Please sign in to comment.