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 22, 2024
1 parent ba993d1 commit 3d3ecf9
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 33 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 @@ -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;
Expand Down Expand Up @@ -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<MetricData> 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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,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;
Expand Down Expand Up @@ -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<MetricData> 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<MetricData> getMetricData(MetricDataType dataType) {
return getFinishedMetricItems(1).stream().filter(metric -> metric.getType() == dataType)
.collect(Collectors.toList());
// .orElseThrow(() -> new IllegalStateException("No metric found with type " + dataType));
}

/**
Expand Down

0 comments on commit 3d3ecf9

Please sign in to comment.