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 27, 2024
1 parent 89bc9a9 commit e1a0889
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 88 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<version.mp.rest.client>3.0.1</version.mp.rest.client>
<version.microprofile-config-api>3.1</version.microprofile-config-api>
<version.awaitility>4.2.0</version.awaitility>
<version.otel.semconv>1.30.1-alpha</version.otel.semconv>
<version.otel.semconv>1.23.1-alpha</version.otel.semconv>
</properties>

<issueManagement>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.eclipse.microprofile.telemetry.metrics.tck;

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

import io.opentelemetry.api.common.AttributeKey;

public class TestUtils {

public static 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 @@ -24,9 +24,9 @@
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.TestUtils;
import org.eclipse.microprofile.telemetry.metrics.tck.exporter.InMemoryMetricExporter;
import org.eclipse.microprofile.telemetry.metrics.tck.exporter.InMemoryMetricExporterProvider;
import org.jboss.arquillian.container.test.api.Deployment;
Expand All @@ -39,7 +39,6 @@
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.DoubleCounter;
import io.opentelemetry.api.metrics.Meter;
Expand Down Expand Up @@ -114,15 +113,9 @@ void testDoubleCounter() throws InterruptedException {
Assert.assertTrue(point.getAttributes().equals(expectedResults.get(point.getValue())),
"Attributes were not equal."
+ System.lineSeparator() + "Actual values: "
+ mapToString(point.getAttributes().asMap())
+ TestUtils.mapToString(point.getAttributes().asMap())
+ System.lineSeparator() + "Expected values: "
+ mapToString(expectedResults.get(point.getValue()).asMap()));
+ TestUtils.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,7 +21,12 @@
**********************************************************************/
package org.eclipse.microprofile.telemetry.metrics.tck.cdi;

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

import org.eclipse.microprofile.telemetry.metrics.tck.TestLibraries;
import org.eclipse.microprofile.telemetry.metrics.tck.TestUtils;
import org.eclipse.microprofile.telemetry.metrics.tck.exporter.InMemoryMetricExporter;
import org.eclipse.microprofile.telemetry.metrics.tck.exporter.InMemoryMetricExporterProvider;
import org.jboss.arquillian.container.test.api.Deployment;
Expand All @@ -34,6 +39,7 @@
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

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

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 @@ -82,17 +91,30 @@ void testDoubleHistogram() throws InterruptedException {
.setUnit(histogramUnit)
.build();
Assert.assertNotNull(doubleHistogram);
doubleHistogram.record(10);
MetricData metric = metricExporter.getMetricData((MetricDataType.HISTOGRAM)).get(0);
Assert.assertEquals(metric.getName(), histogramName);
Assert.assertEquals(metric.getDescription(), histogramDescription);
Assert.assertEquals(metric.getUnit(), histogramUnit);

Assert.assertEquals(metric.getHistogramData()
.getPoints()
.stream()
.findFirst()
.get()
.getSum(), 10);
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());

expectedResults.keySet().stream().forEach(key -> doubleHistogram.record(key, expectedResults.get(key)));

List<MetricData> metrics = metricExporter.getMetricData((MetricDataType.HISTOGRAM));
metrics.stream()
.peek(metricData -> {
Assert.assertEquals(metricData.getName(), histogramName);
Assert.assertEquals(metricData.getDescription(), histogramDescription);
Assert.assertEquals(metricData.getUnit(), histogramUnit);
})
.flatMap(metricData -> metricData.getHistogramData().getPoints().stream())
.forEach(point -> {
Assert.assertTrue(expectedResults.containsKey(point.getSum()),
"Double " + point.getSum() + " was not an expected result");
Assert.assertTrue(point.getAttributes().equals(expectedResults.get(point.getSum())),
"Attributes were not equal."
+ System.lineSeparator() + "Actual values: "
+ TestUtils.mapToString(point.getAttributes().asMap())
+ System.lineSeparator() + "Expected values: "
+ TestUtils.mapToString(expectedResults.get(point.getSum()).asMap()));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@
**********************************************************************/
package org.eclipse.microprofile.telemetry.metrics.tck.cdi;

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

import org.eclipse.microprofile.telemetry.metrics.tck.TestLibraries;
import org.eclipse.microprofile.telemetry.metrics.tck.TestUtils;
import org.eclipse.microprofile.telemetry.metrics.tck.exporter.InMemoryMetricExporter;
import org.eclipse.microprofile.telemetry.metrics.tck.exporter.InMemoryMetricExporterProvider;
import org.jboss.arquillian.container.test.api.Deployment;
Expand All @@ -34,6 +39,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 +53,9 @@ 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_WITH_ATTRIBUTES = -20;
private static final double DOUBLE_WITHOUT_ATTRIBUTES = -10;

@Deployment
public static WebArchive createTestArchive() {

Expand Down Expand Up @@ -83,19 +92,30 @@ 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);
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());

Assert.assertEquals(metric.getDoubleSumData()
.getPoints()
.stream()
.findFirst()
.get()
.getValue(), -10);
}
expectedResults.keySet().stream().forEach(key -> doubleUpDownCounter.add(key, expectedResults.get(key)));

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: "
+ TestUtils.mapToString(point.getAttributes().asMap())
+ System.lineSeparator() + "Expected values: "
+ TestUtils.mapToString(expectedResults.get(point.getValue()).asMap()));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
**********************************************************************/
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 org.eclipse.microprofile.telemetry.metrics.tck.TestLibraries;
import org.eclipse.microprofile.telemetry.metrics.tck.TestUtils;
import org.eclipse.microprofile.telemetry.metrics.tck.exporter.InMemoryMetricExporter;
import org.eclipse.microprofile.telemetry.metrics.tck.exporter.InMemoryMetricExporterProvider;
import org.jboss.arquillian.container.test.api.Deployment;
Expand All @@ -42,7 +43,6 @@
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 +53,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 +85,38 @@ 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: "
+ TestUtils.mapToString(point.getAttributes().asMap())
+ System.lineSeparator() + "Expected values: "
+ TestUtils.mapToString(expectedResults.get(point.getValue()).asMap()));
});
}

}
Loading

0 comments on commit e1a0889

Please sign in to comment.