Class InMemoryMetricExporter
- java.lang.Object
-
- io.opentelemetry.sdk.testing.exporter.InMemoryMetricExporter
-
- All Implemented Interfaces:
io.opentelemetry.sdk.metrics.export.MetricExporter
public final class InMemoryMetricExporter extends Object implements io.opentelemetry.sdk.metrics.export.MetricExporter
AMetricExporter
implementation that can be used to test OpenTelemetry integration.Can be created using
InMemoryMetricExporter.create()
Example usage:
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"); } }
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static InMemoryMetricExporter
create()
Returns a new instance of theInMemoryMetricExporter
.io.opentelemetry.sdk.common.CompletableResultCode
export(Collection<io.opentelemetry.sdk.metrics.data.MetricData> metrics)
Exports the collection ofMetric
s into the inmemory queue.io.opentelemetry.sdk.common.CompletableResultCode
flush()
The InMemory exporter does not batch metrics, so this method will immediately return with success.List<io.opentelemetry.sdk.metrics.data.MetricData>
getFinishedMetricItems()
Returns aList
of the finishedMetric
s, represented byMetricData
.void
reset()
Clears the internalList
of finishedMetric
s.io.opentelemetry.sdk.common.CompletableResultCode
shutdown()
Clears the internalList
of finishedMetric
s.
-
-
-
Method Detail
-
create
public static InMemoryMetricExporter create()
Returns a new instance of theInMemoryMetricExporter
.- Returns:
- a new instance of the
InMemoryMetricExporter
.
-
getFinishedMetricItems
public List<io.opentelemetry.sdk.metrics.data.MetricData> getFinishedMetricItems()
Returns aList
of the finishedMetric
s, represented byMetricData
.- Returns:
- a
List
of the finishedMetric
s.
-
reset
public void reset()
Clears the internalList
of finishedMetric
s.Does not reset the state of this exporter if already shutdown.
-
export
public io.opentelemetry.sdk.common.CompletableResultCode export(Collection<io.opentelemetry.sdk.metrics.data.MetricData> metrics)
Exports the collection ofMetric
s into the inmemory queue.If this is called after
shutdown
, this will returnResultCode.FAILURE
.- Specified by:
export
in interfaceio.opentelemetry.sdk.metrics.export.MetricExporter
-
flush
public io.opentelemetry.sdk.common.CompletableResultCode flush()
The InMemory exporter does not batch metrics, so this method will immediately return with success.- Specified by:
flush
in interfaceio.opentelemetry.sdk.metrics.export.MetricExporter
- Returns:
- always Success
-
shutdown
public io.opentelemetry.sdk.common.CompletableResultCode shutdown()
Clears the internalList
of finishedMetric
s.Any subsequent call to export() function on this MetricExporter, will return
CompletableResultCode.ofFailure()
- Specified by:
shutdown
in interfaceio.opentelemetry.sdk.metrics.export.MetricExporter
-
-