public class InMemoryMetricExporter extends Object implements io.opentelemetry.sdk.metrics.export.MetricExporter
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");
}
}
| Modifier and Type | Method and Description |
|---|---|
static InMemoryMetricExporter |
create()
Returns a new instance of the
InMemoryMetricExporter. |
io.opentelemetry.sdk.metrics.export.MetricExporter.ResultCode |
export(Collection<io.opentelemetry.sdk.metrics.data.MetricData> metrics)
Exports the collection of
Metrics into the inmemory queue. |
io.opentelemetry.sdk.metrics.export.MetricExporter.ResultCode |
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 a
List of the finished Metrics, represented by MetricData. |
void |
reset()
Clears the internal
List of finished Metrics. |
void |
shutdown()
Clears the internal
List of finished Metrics. |
public static InMemoryMetricExporter create()
InMemoryMetricExporter.InMemoryMetricExporter.public List<io.opentelemetry.sdk.metrics.data.MetricData> getFinishedMetricItems()
List of the finished Metrics, represented by MetricData.List of the finished Metrics.public void reset()
List of finished Metrics.
Does not reset the state of this exporter if already shutdown.
public io.opentelemetry.sdk.metrics.export.MetricExporter.ResultCode export(Collection<io.opentelemetry.sdk.metrics.data.MetricData> metrics)
Metrics into the inmemory queue.
If this is called after shutdown, this will return ResultCode.FAILURE.
export in interface io.opentelemetry.sdk.metrics.export.MetricExporterpublic io.opentelemetry.sdk.metrics.export.MetricExporter.ResultCode flush()
flush in interface io.opentelemetry.sdk.metrics.export.MetricExporterpublic void shutdown()
List of finished Metrics.
Any subsequent call to export() function on this MetricExporter, will return ResultCode.FAILURE
shutdown in interface io.opentelemetry.sdk.metrics.export.MetricExporter