Skip to content

Commit

Permalink
Add metrics TCK
Browse files Browse the repository at this point in the history
  • Loading branch information
yasmin-aumeeruddy committed Jan 15, 2024
1 parent b55bcd0 commit f096c29
Show file tree
Hide file tree
Showing 6 changed files with 313 additions and 1 deletion.
8 changes: 7 additions & 1 deletion tck/metrics/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-semconv</artifactId>
<version>1.30.1-alpha</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
Expand All @@ -88,7 +89,12 @@
<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,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;
}

}
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() {
}
}
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");
*/
}
}
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);
}
}
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";
}
}

0 comments on commit f096c29

Please sign in to comment.