Skip to content

Commit

Permalink
fix(metric-reader): do not throw on multiple shutdown calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
pichlermarc committed Jan 5, 2022
1 parent 97b6b50 commit 8021fb3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import * as api from '@opentelemetry/api';
import { AggregationTemporality } from './AggregationTemporality';
import { MetricProducer } from './MetricProducer';
import { MetricData } from './MetricData';
Expand Down Expand Up @@ -151,7 +152,8 @@ export abstract class MetricReader {
async shutdown(options: ReaderForceFlushOptions): Promise<void> {
// Do not call shutdown again if it has already been called.
if (this._shutdown) {
throw new Error('Cannot call shutdown twice.');
api.diag.error('Cannot call shutdown twice.');
return;
}

await callWithTimeout(this.onShutdown(), options.timeoutMillis ?? 10000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@
export { MeterProvider, MeterProviderOptions } from './MeterProvider';
export * from './export/MetricExporter';
export * from './export/MetricReader';
export * from './export/ReaderResult';
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ describe('PeriodicExportingMetricReader', () => {

reader.setMetricProducer(new TestMetricProducer());
await reader.shutdown({});
await assert.rejects(() => reader.shutdown({}));
await assert.rejects(() => reader.forceFlush({}));
});
});

Expand Down Expand Up @@ -270,8 +270,10 @@ describe('PeriodicExportingMetricReader', () => {
thrown => thrown instanceof ReaderTimeoutError);
});

it('should throw when called twice', async () => {
it('called twice should call export shutdown only once', async () => {
const exporter = new TestMetricExporter();
const exporterMock = sinon.mock(exporter);
exporterMock.expects('shutdown').calledOnceWithExactly();
const reader = new PeriodicExportingMetricReader({
exporter: exporter,
exportIntervalMillis: MAX_32_BIT_INT,
Expand All @@ -280,9 +282,11 @@ describe('PeriodicExportingMetricReader', () => {

reader.setMetricProducer(new TestMetricProducer());

// first call should succeed.
// call twice, the exporter's shutdown must only be called once.
await reader.shutdown({});
await assert.rejects(() => reader.shutdown({}));
await reader.shutdown({});

exporterMock.verify();
});

it('should throw on non-initialized instance.', async () => {
Expand Down

0 comments on commit 8021fb3

Please sign in to comment.