-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(core): Instrument Anthropic client in place instead of via a deep proxy #22305
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
069d7fb
fix(core): Instrument Anthropic client in place instead of via a deep…
logaretm 76874ee
test(anthropic): Add regression test for outer-wrapper transparency
logaretm 332d9d5
fix(core): Scope Anthropic stream dedup to the synchronous delegation
logaretm c4ffba2
test(anthropic): Assert stream dedup emits no duplicate span
logaretm 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
110 changes: 110 additions & 0 deletions
110
dev-packages/node-integration-tests/suites/tracing/anthropic/scenario-outer-wrapper.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,110 @@ | ||
| import Anthropic from '@anthropic-ai/sdk'; | ||
| import * as Sentry from '@sentry/node'; | ||
| import express from 'express'; | ||
|
|
||
| function startMockAnthropicServer() { | ||
| const app = express(); | ||
| app.use(express.json()); | ||
|
|
||
| app.post('/anthropic/v1/messages', (req, res) => { | ||
| res.writeHead(200, { | ||
| 'Content-Type': 'text/event-stream', | ||
| 'Cache-Control': 'no-cache', | ||
| Connection: 'keep-alive', | ||
| }); | ||
|
|
||
| const model = req.body.model; | ||
| const events = [ | ||
| { | ||
| type: 'message_start', | ||
| message: { | ||
| id: 'msg_stream_1', | ||
| type: 'message', | ||
| role: 'assistant', | ||
| model, | ||
| content: [], | ||
| usage: { input_tokens: 10 }, | ||
| }, | ||
| }, | ||
| { type: 'content_block_start', index: 0, content_block: { type: 'text', text: '' } }, | ||
| { type: 'content_block_delta', index: 0, delta: { type: 'text_delta', text: 'Hello ' } }, | ||
| { type: 'content_block_delta', index: 0, delta: { type: 'text_delta', text: 'from ' } }, | ||
| { type: 'content_block_delta', index: 0, delta: { type: 'text_delta', text: 'stream!' } }, | ||
| { type: 'content_block_stop', index: 0 }, | ||
| { | ||
| type: 'message_delta', | ||
| delta: { stop_reason: 'end_turn', stop_sequence: null }, | ||
| usage: { output_tokens: 15 }, | ||
| }, | ||
| { type: 'message_stop' }, | ||
| ]; | ||
|
|
||
| events.forEach((event, index) => { | ||
| setTimeout(() => { | ||
| res.write(`event: ${event.type}\n`); | ||
| res.write(`data: ${JSON.stringify(event)}\n\n`); | ||
| if (index === events.length - 1) { | ||
| res.end(); | ||
| } | ||
| }, index * 10); | ||
| }); | ||
| }); | ||
|
|
||
| return new Promise(resolve => { | ||
| const server = app.listen(0, () => { | ||
| resolve(server); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| // Minimal stand-in for a third-party library that also instruments the client by wrapping | ||
| // `messages.create` (e.g. Braintrust's `wrapAnthropic`). Our instrumentation must not hide the | ||
| // client's own `create` from it when the SDK's `messages.stream()` helper delegates internally. | ||
| function wrapClient(client) { | ||
| return new Proxy(client, { | ||
| get(target, prop, receiver) { | ||
| if (prop !== 'messages') { | ||
| return Reflect.get(target, prop, receiver); | ||
| } | ||
| return new Proxy(Reflect.get(target, prop, receiver), { | ||
| get(messages, messagesProp, messagesReceiver) { | ||
| if (messagesProp !== 'create') { | ||
| return Reflect.get(messages, messagesProp, messagesReceiver); | ||
| } | ||
| const originalCreate = Reflect.get(messages, messagesProp, messagesReceiver); | ||
| return function (...args) { | ||
| Sentry.captureMessage('third-party wrapper observed messages.create'); | ||
| return originalCreate.apply(this, args); | ||
| }; | ||
| }, | ||
| }); | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| async function run() { | ||
| const server = await startMockAnthropicServer(); | ||
|
|
||
| await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { | ||
| const client = wrapClient( | ||
| new Anthropic({ | ||
| apiKey: 'mock-api-key', | ||
| baseURL: `http://localhost:${server.address().port}/anthropic`, | ||
| }), | ||
| ); | ||
|
|
||
| const stream = client.messages.stream({ | ||
| model: 'claude-3-haiku-20240307', | ||
| messages: [{ role: 'user', content: 'Stream this please' }], | ||
| }); | ||
| for await (const _ of stream) { | ||
| void _; | ||
| } | ||
| }); | ||
|
|
||
| await Sentry.flush(2000); | ||
|
|
||
| server.close(); | ||
| } | ||
|
|
||
| run(); |
110 changes: 110 additions & 0 deletions
110
...ackages/node-integration-tests/suites/tracing/anthropic/scenario-stream-nested-create.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,110 @@ | ||
| import Anthropic from '@anthropic-ai/sdk'; | ||
| import * as Sentry from '@sentry/node'; | ||
| import express from 'express'; | ||
|
|
||
| function startMockAnthropicServer() { | ||
| const app = express(); | ||
| app.use(express.json()); | ||
|
|
||
| app.post('/anthropic/v1/messages', (req, res) => { | ||
| const model = req.body.model; | ||
|
|
||
| // Non-streaming request (the nested call made from the stream event handler). | ||
| if (!req.body.stream) { | ||
| res.send({ | ||
| id: 'msg_nested', | ||
| type: 'message', | ||
| model, | ||
| role: 'assistant', | ||
| content: [{ type: 'text', text: 'nested reply' }], | ||
| stop_reason: 'end_turn', | ||
| usage: { input_tokens: 3, output_tokens: 4 }, | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| res.writeHead(200, { | ||
| 'Content-Type': 'text/event-stream', | ||
| 'Cache-Control': 'no-cache', | ||
| Connection: 'keep-alive', | ||
| }); | ||
| const events = [ | ||
| { | ||
| type: 'message_start', | ||
| message: { | ||
| id: 'msg_stream_1', | ||
| type: 'message', | ||
| role: 'assistant', | ||
| model, | ||
| content: [], | ||
| usage: { input_tokens: 10 }, | ||
| }, | ||
| }, | ||
| { type: 'content_block_start', index: 0, content_block: { type: 'text', text: '' } }, | ||
| { type: 'content_block_delta', index: 0, delta: { type: 'text_delta', text: 'Hello!' } }, | ||
| { type: 'content_block_stop', index: 0 }, | ||
| { type: 'message_delta', delta: { stop_reason: 'end_turn', stop_sequence: null }, usage: { output_tokens: 15 } }, | ||
| { type: 'message_stop' }, | ||
| ]; | ||
| events.forEach((event, index) => { | ||
| setTimeout(() => { | ||
| res.write(`event: ${event.type}\n`); | ||
| res.write(`data: ${JSON.stringify(event)}\n\n`); | ||
| if (index === events.length - 1) { | ||
| res.end(); | ||
| } | ||
| }, index * 10); | ||
| }); | ||
| }); | ||
|
|
||
| return new Promise(resolve => { | ||
| const server = app.listen(0, () => { | ||
| resolve(server); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| async function run() { | ||
| const server = await startMockAnthropicServer(); | ||
|
|
||
| await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { | ||
| const client = new Anthropic({ | ||
| apiKey: 'mock-api-key', | ||
| baseURL: `http://localhost:${server.address().port}/anthropic`, | ||
| }); | ||
|
|
||
| const stream = client.messages.stream({ | ||
| model: 'claude-3-haiku-20240307', | ||
| messages: [{ role: 'user', content: 'Stream this please' }], | ||
| }); | ||
|
|
||
| // Fire a separate, non-streaming request from a stream event handler. This runs inside the | ||
| // stream's async continuation, so the streaming-helper span is still the active span here. | ||
| // It must still be traced (the dedup must only suppress the helper's own internal `create`). | ||
| let resolveNested; | ||
| const nested = new Promise(resolve => (resolveNested = resolve)); | ||
| let fired = false; | ||
| stream.on('streamEvent', () => { | ||
| if (fired) return; | ||
| fired = true; | ||
| client.messages | ||
| .create({ | ||
| model: 'claude-3-haiku-20240307', | ||
| messages: [{ role: 'user', content: 'Nested call from handler' }], | ||
| max_tokens: 10, | ||
| }) | ||
| .then(resolveNested, resolveNested); | ||
| }); | ||
|
|
||
| for await (const _ of stream) { | ||
| void _; | ||
| } | ||
| await nested; | ||
| }); | ||
|
|
||
| await Sentry.flush(2000); | ||
|
|
||
| server.close(); | ||
| } | ||
|
|
||
| run(); |
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.
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.
Uh oh!
There was an error while loading. Please reload this page.