From 79e28759f053a730ff2091403e50dd130e3edb55 Mon Sep 17 00:00:00 2001 From: Michele Azzolari Date: Tue, 10 Dec 2024 16:52:18 +0100 Subject: [PATCH] feat(sdk-metrics): PeriodicExportingMetricReader now flushes pending tasks at shutdown (#5242) --- CHANGELOG.md | 2 ++ .../test/node/OTLPMetricExporter.test.ts | 5 +++-- packages/sdk-metrics/src/MeterProvider.ts | 2 +- .../sdk-metrics/src/export/PeriodicExportingMetricReader.ts | 2 +- packages/sdk-metrics/test/MeterProvider.test.ts | 4 ++-- .../test/export/PeriodicExportingMetricReader.test.ts | 2 -- 6 files changed, 9 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f378c59c1..81905d18fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ For semantic convention package changes, see the [semconv CHANGELOG](packages/se ### :rocket: (Enhancement) +* feat(sdk-metrics): PeriodicExportingMetricReader now flushes pending tasks at shutdown [#5242](https://github.com/open-telemetry/opentelemetry-js/pull/5242) + ### :bug: (Bug Fix) * fix(sdk-trace-base): do not load OTEL_ env vars on module load, but when needed [#5224](https://github.com/open-telemetry/opentelemetry-js/pull/5224) diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/node/OTLPMetricExporter.test.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/node/OTLPMetricExporter.test.ts index 87569f327a..04d3228dee 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/node/OTLPMetricExporter.test.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/node/OTLPMetricExporter.test.ts @@ -214,8 +214,9 @@ describe('OTLPMetricExporter', () => { meterProvider.getMeter('test-meter').createCounter('test-counter').add(1); // act - meterProvider.forceFlush(); - meterProvider.shutdown(); + meterProvider.forceFlush().then(() => { + return meterProvider.shutdown(); + }); }); }); }); diff --git a/packages/sdk-metrics/src/MeterProvider.ts b/packages/sdk-metrics/src/MeterProvider.ts index 962d59b429..6d5cd56fd0 100644 --- a/packages/sdk-metrics/src/MeterProvider.ts +++ b/packages/sdk-metrics/src/MeterProvider.ts @@ -122,7 +122,7 @@ export class MeterProvider implements IMeterProvider { } /** - * Flush all buffered data and shut down the MeterProvider and all registered + * Shut down the MeterProvider and all registered * MetricReaders. * * Returns a promise which is resolved when all flushes are complete. diff --git a/packages/sdk-metrics/src/export/PeriodicExportingMetricReader.ts b/packages/sdk-metrics/src/export/PeriodicExportingMetricReader.ts index 646c832aa4..6a9096652d 100644 --- a/packages/sdk-metrics/src/export/PeriodicExportingMetricReader.ts +++ b/packages/sdk-metrics/src/export/PeriodicExportingMetricReader.ts @@ -161,7 +161,7 @@ export class PeriodicExportingMetricReader extends MetricReader { if (this._interval) { clearInterval(this._interval); } - + await this.onForceFlush(); await this._exporter.shutdown(); } } diff --git a/packages/sdk-metrics/test/MeterProvider.test.ts b/packages/sdk-metrics/test/MeterProvider.test.ts index cc534adcb1..453945200d 100644 --- a/packages/sdk-metrics/test/MeterProvider.test.ts +++ b/packages/sdk-metrics/test/MeterProvider.test.ts @@ -126,9 +126,9 @@ describe('MeterProvider', () => { assert.strictEqual(meter1, meter2); }); - it('get a noop meter on shutdown', () => { + it('get a noop meter on shutdown', async () => { const meterProvider = new MeterProvider(); - meterProvider.shutdown(); + await meterProvider.shutdown(); const meter = meterProvider.getMeter('meter1', '1.0.0'); // returned tracer should be no-op, not instance of Meter (from SDK) assert.ok(!(meter instanceof Meter)); diff --git a/packages/sdk-metrics/test/export/PeriodicExportingMetricReader.test.ts b/packages/sdk-metrics/test/export/PeriodicExportingMetricReader.test.ts index 9210f4622f..1fe248cab8 100644 --- a/packages/sdk-metrics/test/export/PeriodicExportingMetricReader.test.ts +++ b/packages/sdk-metrics/test/export/PeriodicExportingMetricReader.test.ts @@ -77,9 +77,7 @@ class TestMetricExporter implements PushMetricExporter { async shutdown(): Promise { if (this._shutdown) return; - const flushPromise = this.forceFlush(); this._shutdown = true; - await flushPromise; } async forceFlush(): Promise {