Skip to content

Commit

Permalink
Test meter injection and required metrics in tck
Browse files Browse the repository at this point in the history
  • Loading branch information
yasmin-aumeeruddy committed Feb 15, 2024
1 parent f096c29 commit a2e0502
Show file tree
Hide file tree
Showing 26 changed files with 874 additions and 127 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

<properties>
<inceptionYear>2022</inceptionYear>
<opentelemetry.java.version>1.29.0</opentelemetry.java.version>
<opentelemetry.java.version>1.32.0</opentelemetry.java.version>
<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>
Expand Down
2 changes: 1 addition & 1 deletion spec/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<parent>
<groupId>org.eclipse.microprofile.telemetry</groupId>
<artifactId>microprofile-telemetry-parent</artifactId>
<version>1.2-SNAPSHOT</version>
<version>2.0-SNAPSHOT</version>
</parent>

<artifactId>microprofile-telemetry-spec</artifactId>
Expand Down
10 changes: 4 additions & 6 deletions tck/metrics/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-common</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
Expand Down Expand Up @@ -89,12 +93,6 @@
<artifactId>awaitility</artifactId>
<version>${version.awaitility}</version>
</dependency>

<dependency>
<groupId>org.eclipse.microprofile.telemetry</groupId>
<artifactId>microprofile-telemetry-tracing-tck</artifactId>
<version>1.2-SNAPSHOT</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
**********************************************************************
* 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.DoubleCounter;
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 DoubleCounterTest extends Arquillian {
private static final String counterName = "testDoubleCounter";
private static final String counterDescription = "Testing double counter";
private static final String counterUnit = "Metric Tonnes";

@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 testDoubleCounter() throws InterruptedException {
DoubleCounter doubleCounter =
sdkMeter
.counterBuilder(counterName)
.ofDoubles()
.setDescription(counterDescription)
.setUnit(counterUnit)
.build();
Assert.assertNotNull(doubleCounter);
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()
.findFirst()
.get()
.getValue(), 10.1);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
**********************************************************************
* 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 DoubleGaugeTest extends Arquillian {
private static final String gaugeName = "testDoubleGauge";
private static final String gaugeDescription = "Testing double gauge";
private static final String gaugeUnit = "ms";

@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 testDoubleGauge() throws InterruptedException {
Assert.assertNotNull(
sdkMeter
.gaugeBuilder(gaugeName)
.setDescription(gaugeDescription)
.setUnit("ms")
.buildWithCallback(measurement -> {
measurement.record(1, Attributes.empty());
}));

MetricData metric = metricExporter.getMetricData((MetricDataType.DOUBLE_GAUGE));
Assert.assertEquals(metric.getName(), gaugeName);
Assert.assertEquals(metric.getDescription(), gaugeDescription);
Assert.assertEquals(metric.getUnit(), gaugeUnit);

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
**********************************************************************
* 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.metrics.DoubleHistogram;
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 DoubleHistogramTest extends Arquillian {

private static final String histogramName = "testDoubleHistogram";
private static final String histogramDescription = "Testing double histogram";
private static final String histogramUnit = "Metric Tonnes";

@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 testDoubleHistogram() throws InterruptedException {
DoubleHistogram doubleHistogram =
sdkMeter
.histogramBuilder(histogramName)
.setDescription(histogramDescription)
.setUnit(histogramUnit)
.build();
Assert.assertNotNull(doubleHistogram);
doubleHistogram.record(10);
MetricData metric = metricExporter.getMetricData((MetricDataType.HISTOGRAM));
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);
}
}
Loading

0 comments on commit a2e0502

Please sign in to comment.