-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(sdk-trace-base): always wait on pending export in SimpleSpanProcessor #5303
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
Changes from 8 commits
8ae7697
8c4c802
229d810
a14fdb9
b5065db
c233b8f
2be6d4e
5241daa
6be30b4
18483aa
54f0fbf
9ae1616
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 |
|---|---|---|
|
|
@@ -20,7 +20,6 @@ import { | |
| ExportResultCode, | ||
| globalErrorHandler, | ||
| BindOnceFuture, | ||
| ExportResult, | ||
| } from '@opentelemetry/core'; | ||
| import { Span } from '../Span'; | ||
| import { SpanProcessor } from '../SpanProcessor'; | ||
|
|
@@ -38,16 +37,15 @@ import { Resource } from '@opentelemetry/resources'; | |
| */ | ||
| export class SimpleSpanProcessor implements SpanProcessor { | ||
| private _shutdownOnce: BindOnceFuture<void>; | ||
| private _unresolvedExports: Set<Promise<void>>; | ||
| private _pendingExports: Set<Promise<void>>; | ||
|
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. unresolved seemed to be referring specifically to the resource so I renamed to pending to be more generic |
||
|
|
||
| constructor(private readonly _exporter: SpanExporter) { | ||
| this._shutdownOnce = new BindOnceFuture(this._shutdown, this); | ||
| this._unresolvedExports = new Set<Promise<void>>(); | ||
| this._pendingExports = new Set<Promise<void>>(); | ||
|
trentm marked this conversation as resolved.
|
||
| } | ||
|
|
||
| async forceFlush(): Promise<void> { | ||
| // await unresolved resources before resolving | ||
| await Promise.all(Array.from(this._unresolvedExports)); | ||
| await Promise.all(Array.from(this._pendingExports)); | ||
| if (this._exporter.forceFlush) { | ||
| await this._exporter.forceFlush(); | ||
| } | ||
|
|
@@ -64,43 +62,26 @@ export class SimpleSpanProcessor implements SpanProcessor { | |
| return; | ||
| } | ||
|
|
||
| const doExport = () => | ||
| internal | ||
| ._export(this._exporter, [span]) | ||
| .then((result: ExportResult) => { | ||
| if (result.code !== ExportResultCode.SUCCESS) { | ||
| globalErrorHandler( | ||
| result.error ?? | ||
| new Error( | ||
| `SimpleSpanProcessor: span export failed (status ${result})` | ||
| ) | ||
| ); | ||
| } | ||
| }) | ||
| .catch(error => { | ||
| globalErrorHandler(error); | ||
| }); | ||
| const pendingExport = this._doExport(span).catch(err => | ||
| globalErrorHandler(err) | ||
| ); | ||
| // Enqueue this export to the pending list so it can be flushed by the user. | ||
| this._pendingExports.add(pendingExport); | ||
| pendingExport.finally(() => this._pendingExports.delete(pendingExport)); | ||
| } | ||
|
|
||
| // Avoid scheduling a promise to make the behavior more predictable and easier to test | ||
|
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. Removed this since it seemed to be misleading - a promise is scheduled by |
||
| private async _doExport(span: ReadableSpan): Promise<void> { | ||
| if (span.resource.asyncAttributesPending) { | ||
| const exportPromise = (span.resource as Resource) | ||
| .waitForAsyncAttributes?.() | ||
| .then( | ||
| () => { | ||
| if (exportPromise != null) { | ||
| this._unresolvedExports.delete(exportPromise); | ||
| } | ||
| return doExport(); | ||
| }, | ||
| err => globalErrorHandler(err) | ||
| ); | ||
| // Ensure resource is fully resolved before exporting. | ||
| await (span.resource as Resource).waitForAsyncAttributes?.(); | ||
| } | ||
|
|
||
| // store the unresolved exports | ||
| if (exportPromise != null) { | ||
| this._unresolvedExports.add(exportPromise); | ||
| } | ||
| } else { | ||
| void doExport(); | ||
| const result = await internal._export(this._exporter, [span]); | ||
| if (result.code !== ExportResultCode.SUCCESS) { | ||
| throw ( | ||
| result.error ?? | ||
| new Error(`SimpleSpanProcessor: span export failed (status ${result})`) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.