Move and deprecate InMemoryMetricExporter (#3987)
This commit is contained in:
parent
3940dd254e
commit
cbecf1f706
|
|
@ -23,7 +23,7 @@ import io.opencensus.tags.TagValue;
|
|||
import io.opencensus.tags.Tagger;
|
||||
import io.opencensus.tags.Tags;
|
||||
import io.opentelemetry.sdk.metrics.data.MetricData;
|
||||
import io.opentelemetry.sdk.metrics.testing.InMemoryMetricExporter;
|
||||
import io.opentelemetry.sdk.testing.exporter.InMemoryMetricExporter;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ dependencies {
|
|||
implementation(project(":api:all"))
|
||||
implementation(project(":sdk:all"))
|
||||
implementation(project(":sdk:testing"))
|
||||
implementation(project(":sdk:metrics-testing"))
|
||||
implementation(project(":exporters:otlp:trace"))
|
||||
implementation(project(":exporters:logging"))
|
||||
implementation(project(":semconv"))
|
||||
|
|
|
|||
|
|
@ -22,8 +22,8 @@ import io.opentelemetry.sdk.metrics.data.LongPointData;
|
|||
import io.opentelemetry.sdk.metrics.data.MetricData;
|
||||
import io.opentelemetry.sdk.metrics.data.PointData;
|
||||
import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader;
|
||||
import io.opentelemetry.sdk.metrics.testing.InMemoryMetricExporter;
|
||||
import io.opentelemetry.sdk.resources.Resource;
|
||||
import io.opentelemetry.sdk.testing.exporter.InMemoryMetricExporter;
|
||||
import io.opentelemetry.sdk.trace.SdkTracerProvider;
|
||||
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
|
||||
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,9 @@
|
|||
/*
|
||||
* Copyright The OpenTelemetry Authors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
@ParametersAreNonnullByDefault
|
||||
package io.opentelemetry.sdk.testing.assertj.metrics;
|
||||
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
|
|
@ -0,0 +1,138 @@
|
|||
/*
|
||||
* Copyright The OpenTelemetry Authors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package io.opentelemetry.sdk.testing.exporter;
|
||||
|
||||
import io.opentelemetry.sdk.common.CompletableResultCode;
|
||||
import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
|
||||
import io.opentelemetry.sdk.metrics.data.MetricData;
|
||||
import io.opentelemetry.sdk.metrics.export.MetricExporter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
|
||||
/**
|
||||
* A {@link MetricExporter} implementation that can be used to test OpenTelemetry integration.
|
||||
*
|
||||
* <p>Can be created using {@code InMemoryMetricExporter.create()}
|
||||
*
|
||||
* <p>Example usage:
|
||||
*
|
||||
* <pre><code>
|
||||
* public class InMemoryMetricExporterExample {
|
||||
*
|
||||
* // creating InMemoryMetricExporter
|
||||
* private final InMemoryMetricExporter exporter = InMemoryMetricExporter.create();
|
||||
* private final MeterSdkProvider meterSdkProvider = OpenTelemetrySdk.getMeterProvider();
|
||||
* private final Meter meter = meterSdkProvider.get("InMemoryMetricExporterExample");
|
||||
* private IntervalMetricReader intervalMetricReader;
|
||||
*
|
||||
* void setup() {
|
||||
* intervalMetricReader =
|
||||
* IntervalMetricReader.builder()
|
||||
* .setMetricExporter(exporter)
|
||||
* .setMetricProducers(Collections.singletonList(meterSdkProvider.getMetricProducer()))
|
||||
* .setExportIntervalMillis(1000)
|
||||
* .build();
|
||||
* }
|
||||
*
|
||||
* LongCounter generateLongCounterMeter(String name) {
|
||||
* return meter.longCounterBuilder(name).setDescription("Sample LongCounter").build();
|
||||
* }
|
||||
*
|
||||
* public static void main(String[] args) throws InterruptedException {
|
||||
* InMemoryMetricExporterExample example = new InMemoryMetricExporterExample();
|
||||
* example.setup();
|
||||
* example.generateLongCounterMeter("counter-1");
|
||||
* }
|
||||
* }
|
||||
* </code></pre>
|
||||
*/
|
||||
public final class InMemoryMetricExporter implements MetricExporter {
|
||||
private final Queue<MetricData> finishedMetricItems = new ConcurrentLinkedQueue<>();
|
||||
private final AggregationTemporality preferredTemporality;
|
||||
private boolean isStopped = false;
|
||||
|
||||
private InMemoryMetricExporter(AggregationTemporality preferredTemporality) {
|
||||
this.preferredTemporality = preferredTemporality;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new {@link InMemoryMetricExporter} with a preferred temporality of {@link
|
||||
* AggregationTemporality#CUMULATIVE}.
|
||||
*/
|
||||
public static InMemoryMetricExporter create() {
|
||||
return create(AggregationTemporality.CUMULATIVE);
|
||||
}
|
||||
|
||||
/** Returns a new {@link InMemoryMetricExporter} with the given {@code preferredTemporality}. */
|
||||
public static InMemoryMetricExporter create(AggregationTemporality preferredTemporality) {
|
||||
return new InMemoryMetricExporter(preferredTemporality);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@code List} of the finished {@code Metric}s, represented by {@code MetricData}.
|
||||
*
|
||||
* @return a {@code List} of the finished {@code Metric}s.
|
||||
*/
|
||||
public List<MetricData> getFinishedMetricItems() {
|
||||
return Collections.unmodifiableList(new ArrayList<>(finishedMetricItems));
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the internal {@code List} of finished {@code Metric}s.
|
||||
*
|
||||
* <p>Does not reset the state of this exporter if already shutdown.
|
||||
*/
|
||||
public void reset() {
|
||||
finishedMetricItems.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AggregationTemporality getPreferredTemporality() {
|
||||
return preferredTemporality;
|
||||
}
|
||||
|
||||
/**
|
||||
* Exports the collection of {@code Metric}s into the inmemory queue.
|
||||
*
|
||||
* <p>If this is called after {@code shutdown}, this will return {@code ResultCode.FAILURE}.
|
||||
*/
|
||||
@Override
|
||||
public CompletableResultCode export(Collection<MetricData> metrics) {
|
||||
if (isStopped) {
|
||||
return CompletableResultCode.ofFailure();
|
||||
}
|
||||
finishedMetricItems.addAll(metrics);
|
||||
return CompletableResultCode.ofSuccess();
|
||||
}
|
||||
|
||||
/**
|
||||
* The InMemory exporter does not batch metrics, so this method will immediately return with
|
||||
* success.
|
||||
*
|
||||
* @return always Success
|
||||
*/
|
||||
@Override
|
||||
public CompletableResultCode flush() {
|
||||
return CompletableResultCode.ofSuccess();
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the internal {@code List} of finished {@code Metric}s.
|
||||
*
|
||||
* <p>Any subsequent call to export() function on this MetricExporter, will return {@code
|
||||
* CompletableResultCode.ofFailure()}
|
||||
*/
|
||||
@Override
|
||||
public CompletableResultCode shutdown() {
|
||||
isStopped = true;
|
||||
finishedMetricItems.clear();
|
||||
return CompletableResultCode.ofSuccess();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
/*
|
||||
* Copyright The OpenTelemetry Authors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
@ParametersAreNonnullByDefault
|
||||
package io.opentelemetry.sdk.testing.exporter;
|
||||
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
|
|
@ -3,7 +3,7 @@
|
|||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package io.opentelemetry.sdk.metrics.testing;
|
||||
package io.opentelemetry.sdk.testing.exporter;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
|
@ -52,7 +52,10 @@ import java.util.concurrent.ConcurrentLinkedQueue;
|
|||
* }
|
||||
* }
|
||||
* </code></pre>
|
||||
*
|
||||
* @deprecated Moved to {@code io.opentelemetry:opentelemetry-sdk-metrics-testing} module.
|
||||
*/
|
||||
@Deprecated
|
||||
public final class InMemoryMetricExporter implements MetricExporter {
|
||||
private final Queue<MetricData> finishedMetricItems = new ConcurrentLinkedQueue<>();
|
||||
private final AggregationTemporality preferredTemporality;
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ package io.opentelemetry.sdk.metrics.testing;
|
|||
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
|
||||
|
||||
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
|
||||
import io.opentelemetry.sdk.testing.exporter.InMemoryMetricExporter;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue