Skip to content

Commit

Permalink
Test metrc attributes in tck
Browse files Browse the repository at this point in the history
  • Loading branch information
yasmin-aumeeruddy committed Feb 26, 2024
1 parent 89bc9a9 commit 009f1db
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
**********************************************************************/
package org.eclipse.microprofile.telemetry.metrics.tck.cdi;

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

import java.util.List;

import org.eclipse.microprofile.telemetry.metrics.tck.TestLibraries;
import org.eclipse.microprofile.telemetry.metrics.tck.exporter.InMemoryMetricExporter;
import org.eclipse.microprofile.telemetry.metrics.tck.exporter.InMemoryMetricExporterProvider;
Expand All @@ -34,6 +38,7 @@
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.metrics.DoubleUpDownCounter;
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.sdk.autoconfigure.spi.metrics.ConfigurableMetricExporterProvider;
Expand All @@ -47,6 +52,10 @@ public class DoubleUpDownCounterTest extends Arquillian {
private static final String counterDescription = "Testing double up down counter";
private static final String counterUnit = "Metric Tonnes";

private static final double DOUBLE_VALUE = -10;
private static final double DOUBLE_WITH_ATTRIBUTES = -20;
private static final double DOUBLE_WITHOUT_ATTRIBUTES = -10;

@Deployment
public static WebArchive createTestArchive() {

Expand Down Expand Up @@ -83,19 +92,23 @@ void testDoubleUpDownCounter() throws InterruptedException {
.setUnit(counterUnit)
.build();
Assert.assertNotNull(doubleUpDownCounter);
doubleUpDownCounter.add(-10);

MetricData metric = metricExporter.getMetricData((MetricDataType.DOUBLE_SUM)).get(0);
Assert.assertEquals(metric.getName(), counterName);
Assert.assertEquals(metric.getDescription(), counterDescription);
Assert.assertEquals(metric.getUnit(), counterUnit);
doubleUpDownCounter.add(DOUBLE_VALUE, Attributes.builder().put("K", "V").build());
doubleUpDownCounter.add(DOUBLE_VALUE, Attributes.builder().put("K", "V").build());

Assert.assertEquals(metric.getDoubleSumData()
.getPoints()
doubleUpDownCounter.add(DOUBLE_VALUE, Attributes.empty());

List<MetricData> metrics = metricExporter.getMetricData((MetricDataType.DOUBLE_SUM));

boolean result = metrics
.stream()
.findFirst()
.get()
.getValue(), -10);
.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")));

Assert.assertTrue(result);
}

}
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 All @@ -38,11 +39,11 @@
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.metrics.LongCounter;
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.sdk.autoconfigure.spi.metrics.ConfigurableMetricExporterProvider;
import io.opentelemetry.sdk.metrics.data.LongPointData;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.metrics.data.MetricDataType;
import jakarta.inject.Inject;
Expand All @@ -53,6 +54,10 @@ public class LongCounterTest extends Arquillian {
private static final String counterDescription = "Testing long counter";
private static final String counterUnit = "Metric Tonnes";

private static final long LONG_VALUE = 12;
private static final long LONG_WITH_ATTRIBUTES = 24;
private static final long LONG_WITHOUT_ATTRIBUTES = 12;

@Deployment
public static WebArchive createTestArchive() {

Expand Down Expand Up @@ -81,42 +86,44 @@ void setUp() {

@Test
void testLongCounter() throws InterruptedException {

LongCounter longCounter =
sdkMeter
.counterBuilder(counterName)
.setDescription(counterDescription)
.setUnit(counterUnit)
.build();
Assert.assertNotNull(longCounter);
longCounter.add(12, Attributes.builder().put("K", "V").build());
longCounter.add(12, Attributes.builder().put("K", "V").build());

longCounter.add(12, Attributes.empty());

List<MetricData> metrics = metricExporter.getMetricData((MetricDataType.LONG_SUM));

long value = 24;
long valueWithoutAttributes = 12;

for (MetricData metric : metrics) {
Map<Long, Attributes> expectedResults = new HashMap<Long, Attributes>();
expectedResults.put(LONG_WITH_ATTRIBUTES, Attributes.builder().put("K", "V").build());
expectedResults.put(LONG_WITHOUT_ATTRIBUTES, Attributes.empty());

valueWithoutAttributes = metric.getLongSumData().getPoints().stream()
.filter(point -> point.getAttributes() == Attributes.empty())
.mapToLong(LongPointData::getValue)
.findFirst()
.getAsLong();
expectedResults.keySet().stream().forEach(key -> longCounter.add(key, expectedResults.get(key)));

value = metric.getLongSumData().getPoints().stream()
.filter(point -> ("V").equals(point.getAttributes().get(stringKey("K"))))
.mapToLong(LongPointData::getValue)
.findFirst()
.getAsLong();

}

Assert.assertEquals(value, 24);

Assert.assertEquals(valueWithoutAttributes, 12);
List<MetricData> metrics = metricExporter.getMetricData((MetricDataType.LONG_SUM));
metrics.stream()
.peek(metricData -> {
Assert.assertEquals(metricData.getName(), counterName);
Assert.assertEquals(metricData.getDescription(), counterDescription);
Assert.assertEquals(metricData.getUnit(), counterUnit);
})
.flatMap(metricData -> metricData.getLongSumData().getPoints().stream())
.forEach(point -> {
Assert.assertTrue(expectedResults.containsKey(point.getValue()),
"Long" + 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<AttributeKey<?>, ?> map) {
return (String) map.keySet().stream()
.map(key -> "" + key.getKey() + "=" + map.get(key))
.collect(Collectors.joining(", ", "{", "}"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
**********************************************************************/
package org.eclipse.microprofile.telemetry.metrics.tck.cdi;

import java.util.List;

import org.eclipse.microprofile.telemetry.metrics.tck.TestLibraries;
import org.eclipse.microprofile.telemetry.metrics.tck.exporter.InMemoryMetricExporter;
import org.eclipse.microprofile.telemetry.metrics.tck.exporter.InMemoryMetricExporterProvider;
Expand All @@ -34,6 +36,7 @@
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.metrics.LongHistogram;
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.sdk.autoconfigure.spi.metrics.ConfigurableMetricExporterProvider;
Expand All @@ -47,6 +50,9 @@ public class LongHistogramTest extends Arquillian {
private static final String histogramDescription = "Testing long histogram";
private static final String histogramUnit = "Metric Tonnes";

private static final long LONG_WITH_ATTRIBUTES = 20;
private static final long LONG_WITHOUT_ATTRIBUTES = 10;

@Deployment
public static WebArchive createTestArchive() {

Expand Down Expand Up @@ -83,7 +89,11 @@ void testLongHistogram() throws InterruptedException {
.setUnit(histogramUnit)
.build();
Assert.assertNotNull(longHistogram);
longHistogram.record(10);
longHistogram.record(LONG_WITH_ATTRIBUTES, Attributes.empty());
longHistogram.record(LONG_WITHOUT_ATTRIBUTES, Attributes.builder().put("K", "V").build());

List<MetricData> metrics = metricExporter.getMetricData((MetricDataType.DOUBLE_SUM));

MetricData metric = metricExporter.getMetricData((MetricDataType.HISTOGRAM)).get(0);
Assert.assertEquals(metric.getName(), histogramName);
Assert.assertEquals(metric.getDescription(), histogramDescription);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
**********************************************************************/
package org.eclipse.microprofile.telemetry.metrics.tck.cdi;

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

import java.util.List;

import org.eclipse.microprofile.telemetry.metrics.tck.TestLibraries;
import org.eclipse.microprofile.telemetry.metrics.tck.exporter.InMemoryMetricExporter;
import org.eclipse.microprofile.telemetry.metrics.tck.exporter.InMemoryMetricExporterProvider;
Expand All @@ -34,6 +38,7 @@
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.metrics.LongUpDownCounter;
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.sdk.autoconfigure.spi.metrics.ConfigurableMetricExporterProvider;
Expand All @@ -47,6 +52,10 @@ public class LongUpDownCounterTest extends Arquillian {
private static final String counterDescription = "Testing long up down counter";
private static final String counterUnit = "Metric Tonnes";

private static final long LONG_VALUE = -10;
private static final long LONG_WITH_ATTRIBUTES = -20;
private static final long LONG_WITHOUT_ATTRIBUTES = -10;

@Deployment
public static WebArchive createTestArchive() {

Expand Down Expand Up @@ -74,27 +83,31 @@ void setUp() {
}

@Test
void testLongUpDownCounter() throws InterruptedException {
void testlongUpDownCounter() throws InterruptedException {
LongUpDownCounter longUpDownCounter =
sdkMeter
.upDownCounterBuilder(counterName)
.setDescription(counterDescription)
.setUnit(counterUnit)
.build();
Assert.assertNotNull(longUpDownCounter);
longUpDownCounter.add(-10);

MetricData metric = metricExporter.getMetricData((MetricDataType.LONG_SUM)).get(0);
Assert.assertEquals(metric.getName(), counterName);
Assert.assertEquals(metric.getDescription(), counterDescription);
Assert.assertEquals(metric.getUnit(), counterUnit);
longUpDownCounter.add(LONG_VALUE, Attributes.builder().put("K", "V").build());
longUpDownCounter.add(LONG_VALUE, Attributes.builder().put("K", "V").build());

Assert.assertEquals(metric.getLongSumData()
.getPoints()
longUpDownCounter.add(LONG_VALUE, Attributes.empty());

List<MetricData> metrics = metricExporter.getMetricData((MetricDataType.LONG_SUM));

boolean result = metrics
.stream()
.findFirst()
.get()
.getValue(), -10);
.flatMap(metricData -> metricData.getLongSumData().getPoints().stream())
.allMatch(point -> (point.getValue() == LONG_WITHOUT_ATTRIBUTES
&& point.getAttributes() == Attributes.empty())
|| (point.getValue() == LONG_WITH_ATTRIBUTES
&& point.getAttributes().get(stringKey("K")).equals("V")));

Assert.assertTrue(result);
}

}

0 comments on commit 009f1db

Please sign in to comment.