-
Notifications
You must be signed in to change notification settings - Fork 227
Do not crash on metrics errors #6854
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dlm-log-metrics-errors
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import {InstantaneousMetricReader} from './InstantaneousMetricReader.js' | ||
| import {ExportResultCode} from '@opentelemetry/core' | ||
| import type {PushMetricExporter, ResourceMetrics} from '@opentelemetry/sdk-metrics' | ||
| import {MeterProvider} from '@opentelemetry/sdk-metrics' | ||
| import {describe, expect, test, vi} from 'vitest' | ||
|
|
||
| function createMockExporter(resultCode: ExportResultCode, error?: Error): PushMetricExporter { | ||
| return { | ||
| export: vi.fn((_metrics: ResourceMetrics, callback: (result: {code: ExportResultCode; error?: Error}) => void) => { | ||
| callback({code: resultCode, error}) | ||
| }), | ||
| forceFlush: vi.fn().mockResolvedValue(undefined), | ||
| shutdown: vi.fn().mockResolvedValue(undefined), | ||
| } as unknown as PushMetricExporter | ||
| } | ||
|
|
||
| function createReaderWithProvider(exporter: PushMetricExporter): { | ||
| reader: InstantaneousMetricReader | ||
| provider: MeterProvider | ||
| } { | ||
| const reader = new InstantaneousMetricReader({exporter, throttleLimit: 0}) | ||
| const provider = new MeterProvider() | ||
| provider.addMetricReader(reader) | ||
| return {reader, provider} | ||
| } | ||
|
|
||
| describe('InstantaneousMetricReader', () => { | ||
| test('resolves on successful export', async () => { | ||
| const exporter = createMockExporter(ExportResultCode.SUCCESS) | ||
| const {reader, provider} = createReaderWithProvider(exporter) | ||
|
|
||
| await expect(reader.forceFlush()).resolves.toBeUndefined() | ||
| await provider.shutdown() | ||
| }) | ||
|
|
||
| test('resolves without rejecting on export failure', async () => { | ||
| const exporter = createMockExporter(ExportResultCode.FAILED, new Error('Export failed with retryable status')) | ||
| const {reader, provider} = createReaderWithProvider(exporter) | ||
|
|
||
| await expect(reader.forceFlush()).resolves.toBeUndefined() | ||
| await provider.shutdown() | ||
| }) | ||
|
|
||
| test('resolves without rejecting when export error is undefined', async () => { | ||
| const exporter = createMockExporter(ExportResultCode.FAILED) | ||
| const {reader, provider} = createReaderWithProvider(exporter) | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should perhaps test that the |
||
| await expect(reader.forceFlush()).resolves.toBeUndefined() | ||
| await provider.shutdown() | ||
| }) | ||
| }) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,13 +41,12 @@ export class InstantaneousMetricReader extends MetricReader { | |
| diag.error('PeriodicExportingMetricReader: metrics collection errors', ...errors) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: This isn't anything we did in this PR, but this class name isn't correct any more -- perhaps a miss when vendoring? |
||
| } | ||
|
|
||
| return new Promise((resolve, reject) => { | ||
| return new Promise((resolve) => { | ||
| this._exporter.export(resourceMetrics, (result) => { | ||
| if (result.code === ExportResultCode.SUCCESS) { | ||
| resolve() | ||
| } else { | ||
| reject(result.error ?? new Error(`InstantaneousMetricReader: metrics export failed (error ${result.error})`)) | ||
| if (result.code !== ExportResultCode.SUCCESS) { | ||
| diag.error('InstantaneousMetricReader: metrics export failed', result.error) | ||
| } | ||
| resolve() | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also add a test case for
exportreturning synchronous exceptions?