Skip to content

Commit

Permalink
Merge pull request #161 from benjamin-confino/tck-with-attributes
Browse files Browse the repository at this point in the history
emit useful errors on failure
  • Loading branch information
yasmin-aumeeruddy authored Feb 26, 2024
2 parents 1845879 + a8015b9 commit 0f00aec
Showing 1 changed file with 31 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,35 @@ 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()
.peek(metricData -> {
Assert.assertEquals(metricData.getName(), counterName);
Assert.assertEquals(metricData.getDescription(), counterDescription);
Assert.assertEquals(metricData.getUnit(), counterUnit);
})
.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 0f00aec

Please sign in to comment.