-
Notifications
You must be signed in to change notification settings - Fork 992
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
Merged
pichlermarc
merged 12 commits into
open-telemetry:main
from
anuraaga:simplespanprocessor-waitalways
Feb 12, 2025
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8ae7697
fix(opentelemetry-sdk-trace-base): always wait on pending export in S…
anuraaga 8c4c802
changelog
anuraaga 229d810
Fix changelog
anuraaga a14fdb9
Use async function for clarity
anuraaga b5065db
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
anuraaga c233b8f
Fix
anuraaga 2be6d4e
Cleanup
anuraaga 5241daa
Cleanup
anuraaga 6be30b4
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
anuraaga 18483aa
Merge
anuraaga 54f0fbf
Fix changelog
anuraaga 9ae1616
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
anuraaga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,6 @@ | |
| ExportResultCode, | ||
| globalErrorHandler, | ||
| BindOnceFuture, | ||
| ExportResult, | ||
| } from '@opentelemetry/core'; | ||
| import { Span } from '../Span'; | ||
| import { SpanProcessor } from '../SpanProcessor'; | ||
|
|
@@ -38,16 +37,15 @@ | |
| */ | ||
| export class SimpleSpanProcessor implements SpanProcessor { | ||
| private _shutdownOnce: BindOnceFuture<void>; | ||
| private _unresolvedExports: Set<Promise<void>>; | ||
| private _pendingExports: Set<Promise<void>>; | ||
|
|
||
| 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.
Show resolved
Hide 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 @@ | |
| 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})`) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
unresolved seemed to be referring specifically to the resource so I renamed to pending to be more generic