From be236d71752fa864086be89394394c3edf8868fb Mon Sep 17 00:00:00 2001 From: Yohei Kamitsukasa <35333687+paper2@users.noreply.github.com> Date: Thu, 28 Nov 2024 07:01:01 +0900 Subject: [PATCH] fix(sdk-metrics): InMemoryMetricExporter clear metrics after shutdown --- CHANGELOG.md | 2 ++ .../src/export/InMemoryMetricExporter.ts | 1 + .../test/export/InMemoryMetricExporter.test.ts | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index adbcd497163..c949f24c512 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,9 +12,11 @@ For semantic convention package changes, see the [semconv CHANGELOG](packages/se ### :boom: Breaking Change * feat(sdk-metrics): Add support for aggregation cardinality limit with a default limit of 2000. This limit can be customized via views [#5182](https://github.com/open-telemetry/opentelemetry-js/pull/5128) +* fix(sdk-metrics): InMemoryMetricExporter clear metrics after shutdown to align with other exporters [#5131](https://github.com/open-telemetry/opentelemetry-js/issues/5131) @paper2 ### :rocket: (Enhancement) + ### :bug: (Bug Fix) ### :books: (Refine Doc) diff --git a/packages/sdk-metrics/src/export/InMemoryMetricExporter.ts b/packages/sdk-metrics/src/export/InMemoryMetricExporter.ts index 2a80973bb95..fd1bcf78e12 100644 --- a/packages/sdk-metrics/src/export/InMemoryMetricExporter.ts +++ b/packages/sdk-metrics/src/export/InMemoryMetricExporter.ts @@ -76,6 +76,7 @@ export class InMemoryMetricExporter implements PushMetricExporter { shutdown(): Promise { this._shutdown = true; + this._metrics = []; return Promise.resolve(); } } diff --git a/packages/sdk-metrics/test/export/InMemoryMetricExporter.test.ts b/packages/sdk-metrics/test/export/InMemoryMetricExporter.test.ts index 1e14a8ed5e1..93c9cfdf432 100644 --- a/packages/sdk-metrics/test/export/InMemoryMetricExporter.test.ts +++ b/packages/sdk-metrics/test/export/InMemoryMetricExporter.test.ts @@ -113,6 +113,24 @@ describe('InMemoryMetricExporter', () => { await metricReader.shutdown(); }); + it('should get no metrics after shutdown', async () => { + const counter = meter.createCounter('counter_total', { + description: 'a test description', + }); + const counterAttribute = { key1: 'attributeValue1' }; + counter.add(10, counterAttribute); + + const exportedMetrics = await waitForNumberOfExports(exporter, 1); + assert.ok(exportedMetrics.length > 0); + + await exporter.shutdown(); + + const otherMetrics = exporter.getMetrics(); + assert.ok(otherMetrics.length === 0); + + await metricReader.shutdown(); + }); + it('should be able to access metric', async () => { const counter = meter.createCounter('counter_total', { description: 'a test description',