Skip to content

Commit

Permalink
emit useful errors on failure
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamin-confino committed Feb 23, 2024
1 parent c374642 commit 136531e
Showing 1 changed file with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
**********************************************************************/
package org.eclipse.microprofile.telemetry.metrics.tck.cdi;

import static io.opentelemetry.api.common.AttributeKey.stringKey;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.eclipse.microprofile.telemetry.metrics.tck.TestLibraries;
import org.eclipse.microprofile.telemetry.metrics.tck.exporter.InMemoryMetricExporter;
Expand Down Expand Up @@ -82,6 +83,7 @@ void setUp() {

@Test
void testDoubleCounter() throws InterruptedException {

DoubleCounter doubleCounter =
sdkMeter
.counterBuilder(counterName)
Expand All @@ -91,19 +93,30 @@ void testDoubleCounter() throws InterruptedException {
.build();
Assert.assertNotNull(doubleCounter);

doubleCounter.add(DOUBLE_WITH_ATTRIBUTES, Attributes.builder().put("K", "V").build());
doubleCounter.add(DOUBLE_WITHOUT_ATTRIBUTES, Attributes.empty());
List<MetricData> metrics = metricExporter.getMetricData((MetricDataType.DOUBLE_SUM));
Map<Double, Attributes> expectedResults = new HashMap<Double, Attributes>();
expectedResults.put(DOUBLE_WITH_ATTRIBUTES, Attributes.builder().put("K", "V").build());
expectedResults.put(DOUBLE_WITHOUT_ATTRIBUTES, Attributes.empty());

boolean result = metrics
.stream()
.flatMap(metricData -> metricData.getDoubleSumData().getPoints().stream())
.allMatch(point -> (point.getValue() == DOUBLE_WITHOUT_ATTRIBUTES
&& point.getAttributes() == Attributes.empty())
|| (point.getValue() == DOUBLE_WITH_ATTRIBUTES
&& point.getAttributes().get(stringKey("K")).equals("V")));
expectedResults.keySet().stream().forEach(key -> doubleCounter.add(key, expectedResults.get(key)));

Assert.assertTrue(result);
List<MetricData> metrics = metricExporter.getMetricData((MetricDataType.DOUBLE_SUM));
metrics.stream()
.flatMap(metricData -> metricData.getDoubleSumData().getPoints().stream())
.forEach(point -> {
Assert.assertTrue(expectedResults.containsKey(point.getValue()),
"Double" + point.getValue() + " was not an expected result");
Assert.assertTrue(point.getAttributes().equals(expectedResults.get(point.getValue())),
"Attributes were not equal."
+ System.lineSeparator() + "Actual values: "
+ mapToString(point.getAttributes().asMap())
+ System.lineSeparator() + "Expected values: "
+ mapToString(expectedResults.get(point.getValue()).asMap()));
});
}

private String mapToString(Map<?, ?> map) {
return (String) map.keySet().stream()
.map(key -> "" + key + "=" + map.get(key))
.collect(Collectors.joining(", ", "{", "}"));
}
}

0 comments on commit 136531e

Please sign in to comment.