diff --git a/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/DoubleCounterTest.java b/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/DoubleCounterTest.java index b7339cfb..2b1b17b8 100644 --- a/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/DoubleCounterTest.java +++ b/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/DoubleCounterTest.java @@ -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; @@ -38,6 +42,7 @@ import io.opentelemetry.api.metrics.DoubleCounter; import io.opentelemetry.api.metrics.Meter; import io.opentelemetry.sdk.autoconfigure.spi.metrics.ConfigurableMetricExporterProvider; +import io.opentelemetry.sdk.metrics.data.DoublePointData; import io.opentelemetry.sdk.metrics.data.MetricData; import io.opentelemetry.sdk.metrics.data.MetricDataType; import jakarta.inject.Inject; @@ -83,18 +88,29 @@ void testDoubleCounter() throws InterruptedException { .setUnit(counterUnit) .build(); Assert.assertNotNull(doubleCounter); + + doubleCounter.add(20.2, Attributes.builder().put("K", "V").build()); doubleCounter.add(10.1, Attributes.empty()); - MetricData metric = metricExporter.getMetricData((MetricDataType.DOUBLE_SUM)); - Assert.assertEquals(metric.getName(), counterName); - Assert.assertEquals(metric.getDescription(), counterDescription); - Assert.assertEquals(metric.getUnit(), counterUnit); - - Assert.assertEquals(metric.getDoubleSumData() - .getPoints() - .stream() + List metrics = metricExporter.getMetricData((MetricDataType.DOUBLE_SUM)); + Double value = 0.0; + Double valueWithoutAttributes = 0.0; + MetricData metric = metrics.get(0); + + valueWithoutAttributes = metric.getDoubleSumData().getPoints().stream() + .filter(point -> point.getAttributes() == Attributes.empty()) + .mapToDouble(DoublePointData::getValue) .findFirst() - .get() - .getValue(), 10.1); + .getAsDouble(); + + value = metric.getDoubleSumData().getPoints().stream() + .filter(point -> ("V").equals(point.getAttributes().get(stringKey("K")))) + .mapToDouble(DoublePointData::getValue) + .findFirst() + .getAsDouble(); + + Assert.assertEquals(value, 20.2); + + Assert.assertEquals(valueWithoutAttributes, 10.1); } } diff --git a/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/DoubleGaugeTest.java b/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/DoubleGaugeTest.java index 67d934d8..037b722d 100644 --- a/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/DoubleGaugeTest.java +++ b/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/DoubleGaugeTest.java @@ -83,7 +83,7 @@ void testDoubleGauge() throws InterruptedException { measurement.record(1, Attributes.empty()); })); - MetricData metric = metricExporter.getMetricData((MetricDataType.DOUBLE_GAUGE)); + MetricData metric = metricExporter.getMetricData(MetricDataType.DOUBLE_GAUGE).get(0); Assert.assertEquals(metric.getName(), gaugeName); Assert.assertEquals(metric.getDescription(), gaugeDescription); Assert.assertEquals(metric.getUnit(), gaugeUnit); diff --git a/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/DoubleHistogramTest.java b/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/DoubleHistogramTest.java index 575105bb..8dc27d5b 100644 --- a/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/DoubleHistogramTest.java +++ b/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/DoubleHistogramTest.java @@ -83,7 +83,7 @@ void testDoubleHistogram() throws InterruptedException { .build(); Assert.assertNotNull(doubleHistogram); doubleHistogram.record(10); - MetricData metric = metricExporter.getMetricData((MetricDataType.HISTOGRAM)); + MetricData metric = metricExporter.getMetricData((MetricDataType.HISTOGRAM)).get(0); Assert.assertEquals(metric.getName(), histogramName); Assert.assertEquals(metric.getDescription(), histogramDescription); Assert.assertEquals(metric.getUnit(), histogramUnit); diff --git a/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/DoubleUpDownCounterTest.java b/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/DoubleUpDownCounterTest.java index f882c07d..bfe9ef51 100644 --- a/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/DoubleUpDownCounterTest.java +++ b/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/DoubleUpDownCounterTest.java @@ -85,7 +85,7 @@ void testDoubleUpDownCounter() throws InterruptedException { Assert.assertNotNull(doubleUpDownCounter); doubleUpDownCounter.add(-10); - MetricData metric = metricExporter.getMetricData((MetricDataType.DOUBLE_SUM)); + MetricData metric = metricExporter.getMetricData((MetricDataType.DOUBLE_SUM)).get(0); Assert.assertEquals(metric.getName(), counterName); Assert.assertEquals(metric.getDescription(), counterDescription); Assert.assertEquals(metric.getUnit(), counterUnit); diff --git a/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/LongCounterTest.java b/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/LongCounterTest.java index be56f8c9..4d7d49bc 100644 --- a/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/LongCounterTest.java +++ b/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/LongCounterTest.java @@ -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; @@ -34,9 +38,11 @@ import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; +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; @@ -82,20 +88,35 @@ void testLongCounter() throws InterruptedException { .setUnit(counterUnit) .build(); Assert.assertNotNull(longCounter); - longCounter.add(12); - longCounter.add(12); - - MetricData metric = metricExporter.getMetricData((MetricDataType.LONG_SUM)); - Assert.assertEquals(metric.getName(), counterName); - Assert.assertEquals(metric.getDescription(), counterDescription); - Assert.assertEquals(metric.getUnit(), counterUnit); - - Assert.assertEquals(metric.getLongSumData() - .getPoints() - .stream() - .findFirst() - .get() - .getValue(), 24); + longCounter.add(12, Attributes.builder().put("K", "V").build()); + longCounter.add(12, Attributes.builder().put("K", "V").build()); + + longCounter.add(12, Attributes.empty()); + + List metrics = metricExporter.getMetricData((MetricDataType.LONG_SUM)); + + long value = 24; + long valueWithoutAttributes = 12; + + for (MetricData metric : metrics) { + + valueWithoutAttributes = metric.getLongSumData().getPoints().stream() + .filter(point -> point.getAttributes() == Attributes.empty()) + .mapToLong(LongPointData::getValue) + .findFirst() + .getAsLong(); + + 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); } } diff --git a/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/LongGaugeTest.java b/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/LongGaugeTest.java index bc9fa309..f6f85380 100644 --- a/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/LongGaugeTest.java +++ b/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/LongGaugeTest.java @@ -84,7 +84,7 @@ void testLongGauge() throws InterruptedException { measurement.record(1, Attributes.empty()); })); - MetricData metric = metricExporter.getMetricData((MetricDataType.LONG_GAUGE)); + MetricData metric = metricExporter.getMetricData((MetricDataType.LONG_GAUGE)).get(0); Assert.assertEquals(metric.getName(), gaugeName); Assert.assertEquals(metric.getDescription(), gaugeDescription); Assert.assertEquals(metric.getUnit(), gaugeUnit); diff --git a/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/LongHistogramTest.java b/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/LongHistogramTest.java index d42313a4..1ffd362c 100644 --- a/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/LongHistogramTest.java +++ b/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/LongHistogramTest.java @@ -84,7 +84,7 @@ void testLongHistogram() throws InterruptedException { .build(); Assert.assertNotNull(longHistogram); longHistogram.record(10); - MetricData metric = metricExporter.getMetricData((MetricDataType.HISTOGRAM)); + MetricData metric = metricExporter.getMetricData((MetricDataType.HISTOGRAM)).get(0); Assert.assertEquals(metric.getName(), histogramName); Assert.assertEquals(metric.getDescription(), histogramDescription); Assert.assertEquals(metric.getUnit(), histogramUnit); diff --git a/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/LongUpDownCounterTest.java b/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/LongUpDownCounterTest.java index b3cdd0f9..e3660d99 100644 --- a/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/LongUpDownCounterTest.java +++ b/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/LongUpDownCounterTest.java @@ -84,7 +84,7 @@ void testLongUpDownCounter() throws InterruptedException { Assert.assertNotNull(longUpDownCounter); longUpDownCounter.add(-10); - MetricData metric = metricExporter.getMetricData((MetricDataType.LONG_SUM)); + MetricData metric = metricExporter.getMetricData((MetricDataType.LONG_SUM)).get(0); Assert.assertEquals(metric.getName(), counterName); Assert.assertEquals(metric.getDescription(), counterDescription); Assert.assertEquals(metric.getUnit(), counterUnit); diff --git a/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/exporter/InMemoryMetricExporter.java b/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/exporter/InMemoryMetricExporter.java index bb3bb14d..8c98741e 100644 --- a/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/exporter/InMemoryMetricExporter.java +++ b/tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/exporter/InMemoryMetricExporter.java @@ -66,9 +66,10 @@ public void assertItemCount(int itemCount) { .untilAsserted(() -> Assert.assertEquals(finishedMetricItems.size(), itemCount)); } - public MetricData getMetricData(MetricDataType dataType) { - return getFinishedMetricItems(1).stream().filter(metric -> metric.getType() == dataType).findFirst() - .orElseThrow(() -> new IllegalStateException("No metric found with type " + dataType)); + public List getMetricData(MetricDataType dataType) { + return getFinishedMetricItems(1).stream().filter(metric -> metric.getType() == dataType) + .collect(Collectors.toList()); + // .orElseThrow(() -> new IllegalStateException("No metric found with type " + dataType)); } /**