diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/src/export/InMemoryMetricExporter.ts b/experimental/packages/opentelemetry-sdk-metrics-base/src/export/InMemoryMetricExporter.ts new file mode 100644 index 00000000000..f6beecc0e3b --- /dev/null +++ b/experimental/packages/opentelemetry-sdk-metrics-base/src/export/InMemoryMetricExporter.ts @@ -0,0 +1,78 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { ExportResultCode } from "@opentelemetry/core" +import { ExportResult } from "@opentelemetry/core" +import { InstrumentType } from "../InstrumentDescriptor" +import { AggregationTemporality } from "./AggregationTemporality" +import { ResourceMetrics } from "./MetricData" +import { PushMetricExporter } from "./MetricExporter" + +/** + * TODO + */ +export class InMemoryMetricExporter implements PushMetricExporter { + protected _shutdown = false + protected _aggregationTemporality: AggregationTemporality + private _metrics: ResourceMetrics[] = [] + + constructor(aggregationTemporality: AggregationTemporality) { + this._aggregationTemporality = aggregationTemporality + } + + /** + * @inheritedDoc + */ + export(metrics: ResourceMetrics, resultCallback: (result: ExportResult) => void): void { + // Avoid storing metrics when exporter is shutdown + if (this. _shutdown) { + process.nextTick(() => resultCallback({ code: ExportResultCode.FAILED })); + return + } + + this._metrics.push(metrics) + process.nextTick(() => resultCallback({ code: ExportResultCode.SUCCESS })); + } + + /** + * Returns all the collected scope metrics + * @returns ScopeMetric[] + */ + public getMetrics(): ResourceMetrics[] { + return this._metrics + } + + /** + * Clear the collected metrics + */ + public clear(): void { + this._metrics = [] + } + + async forceFlush() { + this._metrics = [] + return Promise.resolve() + } + + selectAggregationTemporality(_instrumentType: InstrumentType): AggregationTemporality { + return this._aggregationTemporality + } + + shutdown(): Promise { + this._shutdown = true + return Promise.resolve() + } +} diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/src/index.ts b/experimental/packages/opentelemetry-sdk-metrics-base/src/index.ts index a43ec3bcbca..439f76571ab 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/src/index.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/src/index.ts @@ -21,6 +21,7 @@ export * from './export/MetricExporter'; export * from './export/MetricProducer'; export * from './export/MetricReader'; export * from './export/PeriodicExportingMetricReader'; +export * from './export/InMemoryMetricExporter'; export { InstrumentDescriptor, InstrumentType } from './InstrumentDescriptor'; export * from './Meter'; export * from './MeterProvider'; diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/test/export/InMemoryMetricExporter.test.ts b/experimental/packages/opentelemetry-sdk-metrics-base/test/export/InMemoryMetricExporter.test.ts new file mode 100644 index 00000000000..79f74f39c3e --- /dev/null +++ b/experimental/packages/opentelemetry-sdk-metrics-base/test/export/InMemoryMetricExporter.test.ts @@ -0,0 +1,25 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import * as assert from 'assert'; +import { AggregationTemporality } from "../../src/export/AggregationTemporality" +import { InMemoryMetricExporter } from "../../src/export/InMemoryMetricExporter" + +describe('InMemoryMetricExporter', () => { + describe('clear metrics on request', () => { + const exporter = new InMemoryMetricExporter(AggregationTemporality.CUMULATIVE) + assert.ok(exporter, 'exporter exists') + }) +})