-
Notifications
You must be signed in to change notification settings - Fork 3
fix(auto-instrumentation): Fully preserve returned promise in tracing channel hook #1617
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
Draft
Luca Forstner (lforst)
wants to merge
1
commit into
main
Choose a base branch
from
lforst/fix-issue-1592
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+91
−16
Draft
Changes from all commits
Commits
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 |
|---|---|---|
|
|
@@ -65,19 +65,17 @@ if (TracingChannel && TracingChannel.prototype.tracePromise) { | |
| ) { | ||
| const { start, end, asyncStart, asyncEnd, error } = this; | ||
|
|
||
| function reject(err: any) { | ||
| function publishRejected(err: any) { | ||
| context.error = err; | ||
| error?.publish(context); | ||
| asyncStart?.publish(context); | ||
| asyncEnd?.publish(context); | ||
| return Promise.reject(err); | ||
| } | ||
|
|
||
| function resolve(result: any) { | ||
| function publishResolved(result: any) { | ||
| context.result = result; | ||
| asyncStart?.publish(context); | ||
| asyncEnd?.publish(context); | ||
| return result; | ||
| } | ||
|
|
||
| start?.publish(context); | ||
|
|
@@ -93,16 +91,31 @@ if (TracingChannel && TracingChannel.prototype.tracePromise) { | |
| (typeof result === "object" || typeof result === "function") && | ||
| typeof result.then === "function" | ||
| ) { | ||
| return result.then(resolve, reject); | ||
| // Preserve the original promise-like object so SDK helper methods | ||
| // like Anthropic APIPromise.withResponse() remain available. | ||
| void result.then( | ||
| (resolved: any) => { | ||
| try { | ||
| publishResolved(resolved); | ||
| } catch { | ||
| // Preserve wrapped promise semantics even if instrumentation fails. | ||
| } | ||
| }, | ||
| (err: any) => { | ||
| try { | ||
| publishRejected(err); | ||
| } catch { | ||
| // Preserve wrapped promise semantics even if instrumentation fails. | ||
| } | ||
| }, | ||
| ); | ||
| return result; | ||
| } | ||
|
|
||
| context.result = result; | ||
| asyncStart?.publish(context); | ||
| asyncEnd?.publish(context); | ||
| publishResolved(result); | ||
| return result; | ||
| } catch (err) { | ||
| context.error = err; | ||
| error?.publish(context); | ||
| publishRejected(err); | ||
|
Contributor
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. This is incorrect as if it reaches here it did not run the async portion and so should not produce asyncStart and asyncEnd events. |
||
| throw err; | ||
| } finally { | ||
| end?.publish(context); | ||
|
|
||
30 changes: 30 additions & 0 deletions
30
js/tests/auto-instrumentations/fixtures/test-api-promise-preservation.mjs
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 |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { tracingChannel } from "node:diagnostics_channel"; | ||
| import { parentPort } from "node:worker_threads"; | ||
|
|
||
| class HelperPromise extends Promise { | ||
| async withResponse() { | ||
| return { | ||
| data: await this, | ||
| response: { ok: true }, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| const channel = tracingChannel("braintrust:test:helper-promise"); | ||
| const traced = channel.tracePromise( | ||
| () => new HelperPromise((resolve) => resolve("ok")), | ||
| {}, | ||
| ); | ||
|
|
||
| const withResponse = await traced.withResponse(); | ||
|
|
||
| parentPort?.postMessage({ | ||
| type: "helper-result", | ||
| result: { | ||
| awaitedValue: await traced, | ||
| constructorName: traced.constructor.name, | ||
| hasWithResponse: typeof traced.withResponse === "function", | ||
| withResponseData: withResponse.data, | ||
| withResponseOk: withResponse.response.ok, | ||
| }, | ||
| }); |
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
Oops, something went wrong.
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.
The problem with doing this though is that we'd be silencing
unhandledRejectionevents due to branching and ignoring failures rather than passing through and re-raising the error for the downstream consumer.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.
I had the same thought actually. I am wondering though, is there any way to preserve exact Node.js behavior while also preserving the "extended" promise?
Can we throw inside the error callback?
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.
It's a bug on their part for not preserving the promise species correctly. Unsure if there's a way we can hack around it as correct species use also requires retaining the constructor signature for it to be able to correctly construct descendants, but Anthropic violated that contract and gave it a different constructor signature which makes species construction crash. 😐