Class InMemoryMetricExporter

java.lang.Object
io.opentelemetry.sdk.testing.exporter.InMemoryMetricExporter
All Implemented Interfaces:
io.opentelemetry.sdk.metrics.export.AggregationTemporalitySelector, io.opentelemetry.sdk.metrics.export.DefaultAggregationSelector, io.opentelemetry.sdk.metrics.export.MetricExporter, Closeable, AutoCloseable

public final class InMemoryMetricExporter extends Object implements io.opentelemetry.sdk.metrics.export.MetricExporter
A MetricExporter implementation that can be used to test OpenTelemetry integration.

Can be created using create() or configured using builder().

Example usage:

// class MyClassTest {
//   private final InMemoryMetricExporter exporter = InMemoryMetricExporter.create();
//   private final SdkMeterProvider meterProvider =
//       SdkMeterProvider.builder()
//           .registerMetricReader(PeriodicMetricReader.builder(exporter).build())
//           .build();
//
//   @Test
//   public void getFinishedMetricItems() {
//     LongCounter counter = meterProvider.get("test-scope").counterBuilder("counter").build();
//     counter.add(1);
//     meterProvider.forceFlush().join(10, TimeUnit.SECONDS);
//
//     assertThat(exporter.getFinishedMetricItems()).hasSize(1);
//   }
// }
Since:
1.14.0
  • Method Summary

    Modifier and Type
    Method
    Description
    Creates an InMemoryMetricExporterBuilder with defaults.
    Returns a new InMemoryMetricExporter with cumulative aggregation temporality for all instrument types.
    create(io.opentelemetry.sdk.metrics.data.AggregationTemporality aggregationTemporality)
    Returns a new InMemoryMetricExporter with the given aggregationTemporality.
    io.opentelemetry.sdk.common.CompletableResultCode
    export(Collection<io.opentelemetry.sdk.metrics.data.MetricData> metrics)
    Exports the collection of Metrics into the inmemory queue.
    io.opentelemetry.sdk.common.CompletableResultCode
    The InMemory exporter does not batch metrics, so this method will immediately return with success.
    io.opentelemetry.sdk.metrics.data.AggregationTemporality
    getAggregationTemporality(io.opentelemetry.sdk.metrics.InstrumentType instrumentType)
     
    List<io.opentelemetry.sdk.metrics.data.MetricData>
    Returns a List of the finished Metrics, represented by MetricData.
    void
    Clears the internal List of finished Metrics.
    io.opentelemetry.sdk.common.CompletableResultCode
    Clears the internal List of finished Metrics.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface io.opentelemetry.sdk.metrics.export.DefaultAggregationSelector

    with

    Methods inherited from interface io.opentelemetry.sdk.metrics.export.MetricExporter

    close, getDefaultAggregation, getMemoryMode
  • Method Details

    • builder

      public static InMemoryMetricExporterBuilder builder()
      Creates an InMemoryMetricExporterBuilder with defaults.
      Returns:
      a builder with always-cumulative AggregationTemporalitySelector
    • create

      public static InMemoryMetricExporter create()
      Returns a new InMemoryMetricExporter with cumulative aggregation temporality for all instrument types.
    • create

      public static InMemoryMetricExporter create(io.opentelemetry.sdk.metrics.data.AggregationTemporality aggregationTemporality)
      Returns a new InMemoryMetricExporter with the given aggregationTemporality.
    • getFinishedMetricItems

      public List<io.opentelemetry.sdk.metrics.data.MetricData> getFinishedMetricItems()
      Returns a List of the finished Metrics, represented by MetricData.
      Returns:
      a List of the finished Metrics.
    • reset

      public void reset()
      Clears the internal List of finished Metrics.

      Does not reset the state of this exporter if already shutdown.

    • getAggregationTemporality

      public io.opentelemetry.sdk.metrics.data.AggregationTemporality getAggregationTemporality(io.opentelemetry.sdk.metrics.InstrumentType instrumentType)
      Specified by:
      getAggregationTemporality in interface io.opentelemetry.sdk.metrics.export.AggregationTemporalitySelector
    • export

      public io.opentelemetry.sdk.common.CompletableResultCode export(Collection<io.opentelemetry.sdk.metrics.data.MetricData> metrics)
      Exports the collection of Metrics into the inmemory queue.

      If this is called after shutdown, this will return ResultCode.FAILURE.

      Specified by:
      export in interface io.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 interface io.opentelemetry.sdk.metrics.export.MetricExporter
      Returns:
      always Success
    • shutdown

      public io.opentelemetry.sdk.common.CompletableResultCode shutdown()
      Clears the internal List of finished Metrics.

      Any subsequent call to export() function on this MetricExporter, will return CompletableResultCode.ofFailure()

      Specified by:
      shutdown in interface io.opentelemetry.sdk.metrics.export.MetricExporter