-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test meter injection and required metrics in tck
- Loading branch information
1 parent
a6010c1
commit fc1144d
Showing
26 changed files
with
1,070 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/ConfigAsset.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright (c) 2022-2023 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.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Properties; | ||
|
||
import org.jboss.shrinkwrap.api.asset.Asset; | ||
|
||
public class ConfigAsset implements Asset { | ||
|
||
public static final String SDK_DISABLED = "otel.sdk.disabled"; | ||
|
||
private Properties properties = new Properties(); | ||
|
||
@Override | ||
public InputStream openStream() { | ||
try { | ||
ByteArrayOutputStream os = new ByteArrayOutputStream(); | ||
properties.store(os, null); | ||
return new ByteArrayInputStream(os.toByteArray()); | ||
} catch (IOException e) { | ||
// Shouldn't happen since we're only using in memory streams | ||
throw new RuntimeException("Unexpected error saving properties", e); | ||
} | ||
} | ||
|
||
public ConfigAsset add(String key, String value) { | ||
properties.put(key, value); | ||
return this; | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
tck/metrics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/TestLibraries.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* 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 org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
|
||
public class TestLibraries { | ||
|
||
public static final JavaArchive AWAITILITY_LIB = ShrinkWrap.create(JavaArchive.class, "awaitility.jar") | ||
.addPackages(true, "org.awaitility", "org.hamcrest"); | ||
|
||
private TestLibraries() { | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
...s/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/DoubleCounterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
99 changes: 99 additions & 0 deletions
99
...ics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/DoubleGaugeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
Oops, something went wrong.