-
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.
- Loading branch information
1 parent
b55bcd0
commit f096c29
Showing
6 changed files
with
313 additions
and
1 deletion.
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
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() { | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...trics/src/main/java/org/eclipse/microprofile/telemetry/metrics/tck/cdi/MeterBeanTest.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,71 @@ | ||
/* | ||
********************************************************************** | ||
* 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.ConfigAsset; | ||
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.spec.WebArchive; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
import io.opentelemetry.api.metrics.LongCounter; | ||
import io.opentelemetry.api.metrics.Meter; | ||
import jakarta.inject.Inject; | ||
|
||
public class MeterBeanTest extends Arquillian { | ||
|
||
@Deployment | ||
public static WebArchive createTestArchive() { | ||
ConfigAsset config = new ConfigAsset().add(ConfigAsset.SDK_DISABLED, "false"); | ||
|
||
return ShrinkWrap.create(WebArchive.class) | ||
.addAsResource(EmptyAsset.INSTANCE, "META-INF/beans.xml") | ||
.addAsResource(config, "META-INF/microprofile-config.properties"); | ||
} | ||
|
||
@Inject | ||
private Meter sdkMeter; | ||
|
||
@Test | ||
void testLongCounter() { | ||
LongCounter longCounter = | ||
sdkMeter | ||
.counterBuilder("testLongCounter") | ||
.setDescription("My very own counter") | ||
.setUnit("metric tonnes") | ||
.build(); | ||
Assert.assertNotNull(longCounter); | ||
|
||
// Note: We no longer get the same instrument instance as these instances are lightweight | ||
/* | ||
* objects backed by storage now. Here we just make sure it doesn't log a warning. sdkMeter | ||
* .counterBuilder("testLongCounter") .setDescription("My very own counter") .setUnit("metric tonnes") .build(); | ||
* assertThat(metricStorageLogs.getEvents()).isEmpty(); | ||
* | ||
* sdkMeter.counterBuilder("testLongCounter").build(); | ||
* metricStorageLogs.assertContains("Found duplicate metric definition"); | ||
*/ | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
.../java/org/eclipse/microprofile/telemetry/metrics/tck/exporter/InMemoryMetricExporter.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,113 @@ | ||
/* | ||
* 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.exporter; | ||
|
||
import static java.util.Comparator.comparingLong; | ||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
import static java.util.function.Predicate.not; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
import java.util.stream.Collectors; | ||
|
||
import org.awaitility.Awaitility; | ||
import org.testng.Assert; | ||
|
||
import io.opentelemetry.sdk.common.CompletableResultCode; | ||
import io.opentelemetry.sdk.metrics.export.MetricExporter; | ||
import io.opentelemetry.semconv.metrics.attributes.SemanticAttributes; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
|
||
@ApplicationScoped | ||
public class InMemoryMetricExporter implements MetricExporter { | ||
private boolean isStopped = false; | ||
private final List<SpanData> finishedMetricItems = new CopyOnWriteArrayList<>(); | ||
|
||
/** | ||
* Return the default aggregation for the {@link InstrumentType}. | ||
* | ||
* @see DefaultAggregationSelector#getDefaultAggregation(InstrumentType) | ||
* @since 1.16.0 | ||
*/ | ||
@Override | ||
default Aggregation getDefaultAggregation(InstrumentType instrumentType) { | ||
return Aggregation.defaultAggregation(); | ||
} | ||
|
||
/** | ||
* Returns the memory mode used by this exporter's associated reader. | ||
* | ||
* @return The {@link MemoryMode} used by this exporter's associated reader | ||
* @since 1.31.0 | ||
*/ | ||
default MemoryMode getMemoryMode() { | ||
return IMMUTABLE_DATA; | ||
} | ||
|
||
/** | ||
* Exports the {@code metrics}. The caller (i.e. {@link PeriodicMetricReader} will not call export until the | ||
* previous call completes. | ||
* | ||
* @param metrics | ||
* the metrics to export. | ||
* @return the result of the export, which is often an asynchronous operation. | ||
*/ | ||
@Override | ||
public CompletableResultCode export(Collection<MetricData> metrics) { | ||
if (isStopped) { | ||
return CompletableResultCode.ofFailure(); | ||
} | ||
metrics.stream() | ||
.filter(not(InMemoryMetricExporter::isArquillianMetric)) | ||
.forEach(finishedMetricItems::add); | ||
return CompletableResultCode.ofSuccess(); | ||
} | ||
/** | ||
* A hint that any metrics previously {@link #export(Collection)}ed should be completed. | ||
* | ||
* @return the result of the flush, which is often an asynchronous operation. | ||
*/ | ||
@Override | ||
public CompletableResultCode flush() { | ||
return CompletableResultCode.ofSuccess(); | ||
} | ||
/** | ||
* Shuts down the exporter. | ||
* | ||
* <p> | ||
* Called when {@link PeriodicMetricReader#shutdown()} of the associated reader is called. | ||
* | ||
* @return a {@link CompletableResultCode} which is completed when shutdown completes. | ||
*/ | ||
@Override | ||
public CompletableResultCode shutdown() { | ||
finishedMetricItems.clear(); | ||
isStopped = true; | ||
return CompletableResultCode.ofSuccess(); | ||
} | ||
|
||
/** Closes this {@link MetricExporter}, releasing any resources. */ | ||
@Override | ||
default void close() { | ||
shutdown().join(10, TimeUnit.SECONDS); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...g/eclipse/microprofile/telemetry/metrics/tck/exporter/InMemoryMetricExporterProvider.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,37 @@ | ||
/* | ||
* 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.exporter; | ||
|
||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
import io.opentelemetry.sdk.autoconfigure.spi.metrics.ConfigurableMetricExporterProvider; | ||
import io.opentelemetry.sdk.metrics.export.MetricExporter; | ||
import jakarta.enterprise.inject.spi.CDI; | ||
|
||
public class InMemoryMetricExporterProvider implements ConfigurableMetricExporterProvider { | ||
@Override | ||
public MetricExporter createExporter(final ConfigProperties config) { | ||
return CDI.current().select(InMemoryMetricExporter.class).get(); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "in-memory"; | ||
} | ||
} |