-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(server-utils): Migrate Anthropic integration to orchestrion #21902
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
5 commits
Select commit
Hold shift + click to select a range
e56fd89
feat(server-utils): Migrate Anthropic integration to orchestrion
nicohrubec 5b6fc6d
Merge remote-tracking branch 'origin/develop' into nh/anthropic-orche…
nicohrubec a2b6aa4
test(langchain): Branch v1 anthropic origin assertion under orchestrion
nicohrubec d58409e
fix(server-utils): Don't capture handled errors in orchestrion Anthro…
nicohrubec c7f35f2
Merge remote-tracking branch 'origin/develop' into nh/anthropic-orche…
nicohrubec 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
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
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
183 changes: 183 additions & 0 deletions
183
packages/server-utils/src/integrations/tracing-channel/anthropic.ts
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,183 @@ | ||
| import * as diagnosticsChannel from 'node:diagnostics_channel'; | ||
| import type { AnthropicAiOptions, AnthropicAiResponse, IntegrationFn, Span, SpanAttributeValue } from '@sentry/core'; | ||
| import { | ||
| _INTERNAL_shouldSkipAiProviderWrapping, | ||
| addAnthropicRequestAttributes, | ||
| addAnthropicResponseAttributes, | ||
| debug, | ||
| defineIntegration, | ||
| extractAnthropicRequestAttributes, | ||
| GEN_AI_REQUEST_MODEL_ATTRIBUTE, | ||
| instrumentAsyncIterableStream, | ||
| instrumentMessageStream, | ||
| resolveAIRecordingOptions, | ||
| SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, | ||
| shouldEnableTruncation, | ||
| startInactiveSpan, | ||
| waitForTracingChannelBinding, | ||
| } from '@sentry/core'; | ||
| import { DEBUG_BUILD } from '../../debug-build'; | ||
| import { CHANNELS } from '../../orchestrion/channels'; | ||
| import { bindTracingChannelToSpan } from '../../tracing-channel'; | ||
|
|
||
| // Same name as the OTel integration by design: when enabled, the OTel 'Anthropic_AI' | ||
| // integration is dropped from the default set (see the Node opt-in loader). | ||
| const INTEGRATION_NAME = 'Anthropic_AI' as const; | ||
|
|
||
| // Distinct from the proxy's `auto.ai.anthropic` so spans from the orchestrion path | ||
| // are attributable separately from the OTel/proxy one. | ||
| const ORIGIN = 'auto.ai.orchestrion.anthropic'; | ||
|
|
||
| // `stream` determines how the span is ended | ||
| const INSTRUMENTED_CHANNELS = [ | ||
| { channel: CHANNELS.ANTHROPIC_CHAT, operation: 'chat', methodPath: 'messages.create', stream: 'async-iterable' }, | ||
|
sentry[bot] marked this conversation as resolved.
|
||
| { channel: CHANNELS.ANTHROPIC_MODELS, operation: 'models', methodPath: 'models.retrieve', stream: 'none' }, | ||
| { | ||
| channel: CHANNELS.ANTHROPIC_MESSAGES_STREAM, | ||
| operation: 'chat', | ||
| methodPath: 'messages.stream', | ||
| stream: 'message-stream', | ||
| }, | ||
| ] as const; | ||
|
|
||
| type StreamMode = (typeof INSTRUMENTED_CHANNELS)[number]['stream']; | ||
|
|
||
| interface AnthropicChannelContext { | ||
| arguments: unknown[]; | ||
| result?: unknown; | ||
| } | ||
|
|
||
| let subscribed = false; | ||
|
|
||
| const _anthropicChannelIntegration = ((options: AnthropicAiOptions = {}) => { | ||
| return { | ||
| name: INTEGRATION_NAME, | ||
| setupOnce() { | ||
| // tracingChannel is unavailable before Node 18.19 and prevent double-subscribe | ||
| if (!diagnosticsChannel.tracingChannel || subscribed) { | ||
| return; | ||
| } | ||
| subscribed = true; | ||
|
|
||
| // `bindTracingChannelToSpan` needs the async-context binding that `initOpenTelemetry()` registers | ||
| // after `setupOnce` runs, so wait for it before subscribing. | ||
| waitForTracingChannelBinding(() => { | ||
| for (const { channel, operation, methodPath, stream } of INSTRUMENTED_CHANNELS) { | ||
| DEBUG_BUILD && debug.log(`[orchestrion:anthropic] subscribing to channel "${channel}"`); | ||
| bindTracingChannelToSpan( | ||
| diagnosticsChannel.tracingChannel<AnthropicChannelContext>(channel), | ||
| data => createGenAiSpan(data, operation, methodPath, options), | ||
| { | ||
| beforeSpanEnd: (span, data) => { | ||
| addAnthropicResponseAttributes( | ||
| span, | ||
| data.result as AnthropicAiResponse, | ||
| resolveAIRecordingOptions(options).recordOutputs, | ||
| ); | ||
| }, | ||
|
nicohrubec marked this conversation as resolved.
|
||
| deferSpanEnd: ({ span, data }) => wrapStreamResult(span, data, stream, options), | ||
| }, | ||
| ); | ||
| } | ||
| }); | ||
| }, | ||
| }; | ||
| }) satisfies IntegrationFn; | ||
|
|
||
| /** | ||
| * Build the span for an instrumented call. | ||
| * Returning `undefined` opts the payload out so no span is opened. | ||
| */ | ||
| function createGenAiSpan( | ||
| data: AnthropicChannelContext, | ||
| operation: string, | ||
| methodPath: string, | ||
| options: AnthropicAiOptions, | ||
| ): Span | undefined { | ||
| const args = data.arguments ?? []; | ||
|
|
||
| // When LangChain (or another provider) is driving the SDK, it records the spans itself and marks this | ||
| // provider as skipped — mirror the OTel integration and don't double-instrument. | ||
| if (_INTERNAL_shouldSkipAiProviderWrapping(INTEGRATION_NAME)) { | ||
| return undefined; | ||
| } | ||
|
|
||
| // `messages.stream()` internally calls the instrumented `messages.create({ stream: true })` tagged with | ||
| // an `X-Stainless-Helper-Method: 'stream'` header. The messages-stream channel already covers it, so skip | ||
| // the nested create to avoid a duplicate span. | ||
| const requestOptions = args[1] as { headers?: Record<string, unknown> } | undefined; | ||
| if (requestOptions?.headers?.['X-Stainless-Helper-Method'] === 'stream') { | ||
| return undefined; | ||
| } | ||
|
|
||
| const params = typeof args[0] === 'object' && args[0] !== null ? (args[0] as Record<string, unknown>) : undefined; | ||
|
|
||
| const { recordInputs } = resolveAIRecordingOptions(options); | ||
| const enableTruncation = shouldEnableTruncation(options.enableTruncation); | ||
|
|
||
| const attributes = extractAnthropicRequestAttributes(args, methodPath, operation); | ||
| const model = (attributes[GEN_AI_REQUEST_MODEL_ATTRIBUTE] as string) || 'unknown'; | ||
| attributes[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN] = ORIGIN; | ||
|
|
||
| const span = startInactiveSpan({ | ||
| name: `${operation} ${model}`, | ||
| op: `gen_ai.${operation}`, | ||
| attributes: attributes as Record<string, SpanAttributeValue>, | ||
| }); | ||
|
|
||
| if (recordInputs && params) { | ||
| addAnthropicRequestAttributes(span, params, enableTruncation); | ||
| } | ||
|
|
||
| return span; | ||
| } | ||
|
|
||
| type AsyncIterableStream = { [Symbol.asyncIterator]: () => AsyncIterator<unknown> }; | ||
| type MessageStreamEmitter = { on: (...args: unknown[]) => void }; | ||
|
|
||
| function isAsyncIterable(value: unknown): value is AsyncIterableStream { | ||
| return !!value && typeof (value as AsyncIterableStream)[Symbol.asyncIterator] === 'function'; | ||
|
sentry[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| function isMessageStream(value: unknown): value is MessageStreamEmitter { | ||
| return !!value && typeof (value as MessageStreamEmitter).on === 'function'; | ||
| } | ||
|
|
||
| /** | ||
| * Hand span-ending ownership to a streamed result: returns `true` to skip the normal `beforeSpanEnd`, | ||
| * `false` for non-streaming results (which end via `beforeSpanEnd`). | ||
| * | ||
| * - `async-iterable`: patch the `Stream`'s async iterator in place so `instrumentAsyncIterableStream` ends | ||
| * the span when iteration finishes. | ||
| * - `message-stream`: `instrumentMessageStream` attaches `'message'`/`'error'` listeners that end the span. | ||
| */ | ||
| function wrapStreamResult( | ||
| span: Span, | ||
| data: AnthropicChannelContext, | ||
| stream: StreamMode, | ||
| options: AnthropicAiOptions, | ||
| ): boolean { | ||
| const { recordOutputs } = resolveAIRecordingOptions(options); | ||
| const result = data.result; | ||
|
|
||
| if (stream === 'async-iterable' && isAsyncIterable(result)) { | ||
| const iterate = result[Symbol.asyncIterator].bind(result); | ||
| const instrumented = instrumentAsyncIterableStream({ [Symbol.asyncIterator]: iterate }, span, recordOutputs); | ||
| result[Symbol.asyncIterator] = () => instrumented; | ||
|
nicohrubec marked this conversation as resolved.
|
||
| return true; | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| if (stream === 'message-stream' && isMessageStream(result)) { | ||
| instrumentMessageStream(result, span, recordOutputs); | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * EXPERIMENTAL — orchestrion-driven Anthropic integration. Subscribes to the `orchestrion:@anthropic-ai/sdk:*` | ||
| * diagnostics_channels injected into the SDK's chat (`messages`/`completions`/beta `messages`), `models`, and | ||
| * `messages.stream()` methods, so it requires the orchestrion runtime hook or bundler plugin. | ||
| */ | ||
| export const anthropicChannelIntegration = defineIntegration(_anthropicChannelIntegration); | ||
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
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.