Skip to content

Commit

Permalink
fix(sdk-metrics): InMemoryMetricExporter clear metrics after shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
paper2 committed Nov 29, 2024
1 parent fd7f2d9 commit b3c14d5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ For semantic convention package changes, see the [semconv CHANGELOG](packages/se

### :rocket: (Enhancement)

* fix(sdk-metrics): InMemoryMetricExporter clear metrics after shutdown to align with other exporters [#5131](https://github.com/open-telemetry/opentelemetry-js/issues/5131) @paper2

### :bug: (Bug Fix)

### :books: (Refine Doc)
Expand Down
1 change: 1 addition & 0 deletions packages/sdk-metrics/src/export/InMemoryMetricExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export class InMemoryMetricExporter implements PushMetricExporter {

shutdown(): Promise<void> {
this._shutdown = true;
this._metrics = [];
return Promise.resolve();
}
}
18 changes: 18 additions & 0 deletions packages/sdk-metrics/test/export/InMemoryMetricExporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit b3c14d5

Please sign in to comment.