Class InMemoryMetricExporter
java.lang.Object
io.opentelemetry.sdk.metrics.testing.InMemoryMetricExporter
- All Implemented Interfaces:
MetricExporter
A
MetricExporter 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
Modifier and TypeMethodDescriptionstatic InMemoryMetricExportercreate()Returns a new instance of theInMemoryMetricExporter.io.opentelemetry.sdk.common.CompletableResultCodeexport(Collection<MetricData> metrics) Exports the collection ofMetrics into the inmemory queue.io.opentelemetry.sdk.common.CompletableResultCodeflush()The InMemory exporter does not batch metrics, so this method will immediately return with success.Returns aListof the finishedMetrics, represented byMetricData.Returns the preferred temporality for metrics.Returns the set of all supported temporalities for this exporter.voidreset()Clears the internalListof finishedMetrics.io.opentelemetry.sdk.common.CompletableResultCodeshutdown()Clears the internalListof finishedMetrics.
-
Method Details
-
create
Returns a new instance of theInMemoryMetricExporter.- Returns:
- a new instance of the
InMemoryMetricExporter.
-
getFinishedMetricItems
Returns aListof the finishedMetrics, represented byMetricData.- Returns:
- a
Listof the finishedMetrics.
-
reset
public void reset()Clears the internalListof finishedMetrics.Does not reset the state of this exporter if already shutdown.
-
getSupportedTemporality
Description copied from interface:MetricExporterReturns the set of all supported temporalities for this exporter.- Specified by:
getSupportedTemporalityin interfaceMetricExporter
-
getPreferredTemporality
Description copied from interface:MetricExporterReturns the preferred temporality for metrics.- Specified by:
getPreferredTemporalityin interfaceMetricExporter
-
export
Exports the collection ofMetrics into the inmemory queue.If this is called after
shutdown, this will returnResultCode.FAILURE.- Specified by:
exportin interfaceMetricExporter- Parameters:
metrics- the collection ofMetricDatato be exported.- Returns:
- the result of the export, which is often an asynchronous operation.
-
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:
flushin interfaceMetricExporter- Returns:
- always Success
-
shutdown
public io.opentelemetry.sdk.common.CompletableResultCode shutdown()Clears the internalListof finishedMetrics.Any subsequent call to export() function on this MetricExporter, will return
CompletableResultCode.ofFailure()- Specified by:
shutdownin interfaceMetricExporter- Returns:
- a
CompletableResultCodewhich is completed when shutdown completes.
-