Skip to content

Commit

Permalink
Add async counter tests to metrics tck
Browse files Browse the repository at this point in the history
  • Loading branch information
yasmin-aumeeruddy committed Feb 28, 2024
1 parent e1a0889 commit d737066
Show file tree
Hide file tree
Showing 9 changed files with 224 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
**********************************************************************
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* See the NOTICES 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.
*
* SPDX-License-Identifier: Apache-2.0
**********************************************************************/
package org.eclipse.microprofile.telemetry.metrics.tck.cdi;

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;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.testng.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.sdk.autoconfigure.spi.metrics.ConfigurableMetricExporterProvider;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.metrics.data.MetricDataType;
import jakarta.inject.Inject;

public class AsyncDoubleCounterTest extends Arquillian {
private static final String counterName = "testDoubleCounter";
private static final String counterDescription = "Testing double counter";
private static final String counterUnit = "Metric Tonnes";

private static final double DOUBLE_WITH_ATTRIBUTES = 20.2;
private static final double DOUBLE_WITHOUT_ATTRIBUTES = 10.1;

@Deployment
public static WebArchive createTestArchive() {

return ShrinkWrap.create(WebArchive.class)
.addClasses(InMemoryMetricExporter.class, InMemoryMetricExporterProvider.class)
.addAsLibrary(TestLibraries.AWAITILITY_LIB)
.addAsServiceProvider(ConfigurableMetricExporterProvider.class, InMemoryMetricExporterProvider.class)
.addAsResource(new StringAsset(
"otel.sdk.disabled=false\notel.metrics.exporter=in-memory\notel.traces.exporter=none\notel.metric.export.interval=3000"),
"META-INF/microprofile-config.properties")
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}

@Inject
private Meter sdkMeter;

@Inject
private InMemoryMetricExporter metricExporter;

@BeforeMethod
void setUp() {
if (metricExporter != null) {
metricExporter.reset();
}
}

@Test
void testAsyncDoubleCounter() throws InterruptedException {
Assert.assertNotNull(
sdkMeter
.counterBuilder(counterName)
.ofDoubles()
.setDescription(counterDescription)
.setUnit(counterUnit)
.buildWithCallback(measurement -> {
measurement.record(1, Attributes.empty());
}));

MetricData metric = metricExporter.getMetricData((MetricDataType.DOUBLE_SUM)).get(0);

Assert.assertEquals(metric.getName(), counterName);
Assert.assertEquals(metric.getDescription(), counterDescription);
Assert.assertEquals(metric.getUnit(), counterUnit);

Assert.assertEquals(metric.getDoubleSumData()
.getPoints()
.stream()
.findFirst()
.get()
.getValue(), 1);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
**********************************************************************
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* See the NOTICES 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.
*
* SPDX-License-Identifier: Apache-2.0
**********************************************************************/
package org.eclipse.microprofile.telemetry.metrics.tck.cdi;

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;
import org.jboss.arquillian.testng.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.sdk.autoconfigure.spi.metrics.ConfigurableMetricExporterProvider;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.metrics.data.MetricDataType;
import jakarta.inject.Inject;

public class AsyncLongCounterTest extends Arquillian {

private static final String counterName = "testLongCounter";
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() {

return ShrinkWrap.create(WebArchive.class)
.addClasses(InMemoryMetricExporter.class, InMemoryMetricExporterProvider.class, TestUtils.class)
.addAsLibrary(TestLibraries.AWAITILITY_LIB)
.addAsServiceProvider(ConfigurableMetricExporterProvider.class, InMemoryMetricExporterProvider.class)
.addAsResource(new StringAsset(
"otel.sdk.disabled=false\notel.metrics.exporter=in-memory\notel.traces.exporter=none\notel.metric.export.interval=3000"),
"META-INF/microprofile-config.properties")
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}

@Inject
private Meter sdkMeter;

@Inject
private InMemoryMetricExporter metricExporter;

@BeforeMethod
void setUp() {
if (metricExporter != null) {
metricExporter.reset();
}
}

@Test
void testAsyncLongCounter() throws InterruptedException {
Assert.assertNotNull(
sdkMeter
.counterBuilder(counterName)
.setDescription(counterDescription)
.setUnit(counterUnit)
.buildWithCallback(measurement -> {
measurement.record(1, Attributes.empty());
}));

MetricData metric = metricExporter.getMetricData((MetricDataType.LONG_SUM)).get(0);

Assert.assertEquals(metric.getName(), counterName);
Assert.assertEquals(metric.getDescription(), counterDescription);
Assert.assertEquals(metric.getUnit(), counterUnit);

Assert.assertEquals(metric.getLongSumData()
.getPoints()
.stream()
.findFirst()
.get()
.getValue(), 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class DoubleCounterTest extends Arquillian {
public static WebArchive createTestArchive() {

return ShrinkWrap.create(WebArchive.class)
.addClasses(InMemoryMetricExporter.class, InMemoryMetricExporterProvider.class)
.addClasses(InMemoryMetricExporter.class, InMemoryMetricExporterProvider.class, TestUtils.class)
.addAsLibrary(TestLibraries.AWAITILITY_LIB)
.addAsServiceProvider(ConfigurableMetricExporterProvider.class, InMemoryMetricExporterProvider.class)
.addAsResource(new StringAsset(
Expand Down Expand Up @@ -118,4 +118,5 @@ void testDoubleCounter() throws InterruptedException {
+ TestUtils.mapToString(expectedResults.get(point.getValue()).asMap()));
});
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class DoubleHistogramTest extends Arquillian {
public static WebArchive createTestArchive() {

return ShrinkWrap.create(WebArchive.class)
.addClasses(InMemoryMetricExporter.class, InMemoryMetricExporterProvider.class)
.addClasses(InMemoryMetricExporter.class, InMemoryMetricExporterProvider.class, TestUtils.class)
.addAsLibrary(TestLibraries.AWAITILITY_LIB)
.addAsServiceProvider(ConfigurableMetricExporterProvider.class, InMemoryMetricExporterProvider.class)
.addAsResource(new StringAsset(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class DoubleUpDownCounterTest extends Arquillian {
public static WebArchive createTestArchive() {

return ShrinkWrap.create(WebArchive.class)
.addClasses(InMemoryMetricExporter.class, InMemoryMetricExporterProvider.class)
.addClasses(InMemoryMetricExporter.class, InMemoryMetricExporterProvider.class, TestUtils.class)
.addAsLibrary(TestLibraries.AWAITILITY_LIB)
.addAsServiceProvider(ConfigurableMetricExporterProvider.class, InMemoryMetricExporterProvider.class)
.addAsResource(new StringAsset(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class LongCounterTest extends Arquillian {
public static WebArchive createTestArchive() {

return ShrinkWrap.create(WebArchive.class)
.addClasses(InMemoryMetricExporter.class, InMemoryMetricExporterProvider.class)
.addClasses(InMemoryMetricExporter.class, InMemoryMetricExporterProvider.class, TestUtils.class)
.addAsLibrary(TestLibraries.AWAITILITY_LIB)
.addAsServiceProvider(ConfigurableMetricExporterProvider.class, InMemoryMetricExporterProvider.class)
.addAsResource(new StringAsset(
Expand Down Expand Up @@ -119,4 +119,4 @@ void testLongCounter() throws InterruptedException {
+ TestUtils.mapToString(expectedResults.get(point.getValue()).asMap()));
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class LongHistogramTest extends Arquillian {
public static WebArchive createTestArchive() {

return ShrinkWrap.create(WebArchive.class)
.addClasses(InMemoryMetricExporter.class, InMemoryMetricExporterProvider.class)
.addClasses(InMemoryMetricExporter.class, InMemoryMetricExporterProvider.class, TestUtils.class)
.addAsLibrary(TestLibraries.AWAITILITY_LIB)
.addAsServiceProvider(ConfigurableMetricExporterProvider.class, InMemoryMetricExporterProvider.class)
.addAsResource(new StringAsset(
Expand Down Expand Up @@ -100,6 +100,7 @@ void testLongHistogram() throws InterruptedException {
expectedResults.keySet().stream().forEach(key -> longHistogram.record(key, expectedResults.get(key)));

List<MetricData> metrics = metricExporter.getMetricData((MetricDataType.HISTOGRAM));
System.out.println("Expected results :" + expectedResults);
metrics.stream()
.peek(metricData -> {
Assert.assertEquals(metricData.getName(), histogramName);
Expand All @@ -108,14 +109,14 @@ void testLongHistogram() throws InterruptedException {
})
.flatMap(metricData -> metricData.getHistogramData().getPoints().stream())
.forEach(point -> {
Assert.assertTrue(expectedResults.containsKey(point.getSum()),
"Long " + point.getSum() + " was not an expected result");
Assert.assertTrue(point.getAttributes().equals(expectedResults.get(point.getSum())),
Assert.assertTrue(expectedResults.containsKey((long) point.getSum()),
"Long " + (long) point.getSum() + " was not an expected result");
Assert.assertTrue(point.getAttributes().equals(expectedResults.get((long) 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()));
+ TestUtils.mapToString(expectedResults.get((long) point.getSum()).asMap()));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class LongUpDownCounterTest extends Arquillian {
public static WebArchive createTestArchive() {

return ShrinkWrap.create(WebArchive.class)
.addClasses(InMemoryMetricExporter.class, InMemoryMetricExporterProvider.class)
.addClasses(InMemoryMetricExporter.class, InMemoryMetricExporterProvider.class, TestUtils.class)
.addAsLibrary(TestLibraries.AWAITILITY_LIB)
.addAsServiceProvider(ConfigurableMetricExporterProvider.class, InMemoryMetricExporterProvider.class)
.addAsResource(new StringAsset(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public CompletableResultCode export(Collection<MetricData> metrics) {
if (isStopped) {
return CompletableResultCode.ofFailure();
}
System.out.println("Exporting metrics :" + metrics);
finishedMetricItems.addAll(metrics);
return CompletableResultCode.ofSuccess();
}
Expand Down

0 comments on commit d737066

Please sign in to comment.