Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- 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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
e56fd895b6fc6da2b6aa4d58409ec7f35f2File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -20,6 +20,7 @@ import { | ||
| GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE, | ||
| GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE, | ||
| } from '../../../../../packages/core/src/tracing/ai/gen-ai-attributes'; | ||
| import { isOrchestrionEnabled } from '../../../utils'; | ||
| import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../utils/runner'; | ||
| describe('Anthropic integration', () => { | ||
| @@ -93,8 +94,15 @@ describe('Anthropic integration', () => { | ||
| createEsmAndCjsTests(__dirname, 'scenario.mjs', 'instrument.mjs', (createRunner, test) => { | ||
| test('creates anthropic related spans with genAI recording disabled', async () => { | ||
| await createRunner() | ||
| .expect({ event: EXPECTED_MODEL_ERROR }) | ||
| const runner = createRunner(); | ||
| // The orchestrion path only marks the errored span; unlike the OTel path it does not | ||
| // capture the handled `error-model` rejection as an event. | ||
| if (!isOrchestrionEnabled()) { | ||
| runner.expect({ event: EXPECTED_MODEL_ERROR }); | ||
| } | ||
| await runner | ||
| .expect({ transaction: EXPECTED_TRANSACTION_DEFAULT_PII_FALSE }) | ||
| .expect({ | ||
| span: container => { | ||
| @@ -141,8 +149,15 @@ describe('Anthropic integration', () => { | ||
| createEsmAndCjsTests(__dirname, 'scenario.mjs', 'instrument-with-pii.mjs', (createRunner, test) => { | ||
| test('creates anthropic related spans with genAI recording enabled', async () => { | ||
| await createRunner() | ||
| .expect({ event: EXPECTED_MODEL_ERROR }) | ||
| const runner = createRunner(); | ||
| // The orchestrion path only marks the errored span; unlike the OTel path it does not | ||
| // capture the handled `error-model` rejection as an event. | ||
| if (!isOrchestrionEnabled()) { | ||
| runner.expect({ event: EXPECTED_MODEL_ERROR }); | ||
| } | ||
| await runner | ||
| .expect({ transaction: EXPECTED_TRANSACTION_DEFAULT_PII_TRUE }) | ||
| .expect({ | ||
| span: container => { | ||
| @@ -168,7 +183,9 @@ describe('Anthropic integration', () => { | ||
| expect(completionSpan!.attributes[GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE].value).toBe(15); | ||
| expect(completionSpan!.attributes[GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE].value).toBe(25); | ||
| expect(completionSpan!.attributes['sentry.op'].value).toBe('gen_ai.chat'); | ||
| expect(completionSpan!.attributes['sentry.origin'].value).toBe('auto.ai.anthropic'); | ||
| expect(completionSpan!.attributes['sentry.origin'].value).toBe( | ||
| isOrchestrionEnabled() ? 'auto.ai.orchestrion.anthropic' : 'auto.ai.anthropic', | ||
| ); | ||
nicohrubec marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| const errorSpan = container.items.find( | ||
| span => | ||
| 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.
Uh oh!There was an error while loading. Please reload this page. | ||
| { 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.
Uh oh!There was an error while loading. Please reload this page. | ||
| 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.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| 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.
Uh oh!There was an error while loading. Please reload this page. | ||
| return true; | ||
cursor[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| 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); | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.