import * as diagnosticsChannel from '../utils/diagnosticsChannel'; import type { Integration, IntegrationFn, Span, SpanAttributeValue } from '@sentry/core'; import { _INTERNAL_shouldSkipAiProviderWrapping, getClient, hasSpanStreamingEnabled, startInactiveSpan, } from '@sentry/core'; import { GEN_AI_PROVIDER_NAME, SENTRY_ORIGIN } from '@sentry/conventions/attributes'; import { getGenAiSpanOp, getRequestSignal, markCancelledIfAborted, resolveGenAiCallRecording } from '../ai/core/utils'; import { wrapApiPromiseResponse } from '../ai/core/apiPromise'; import { type GenAiSpanResult, runGenAiSpanEnd, runGenAiSpanStart } from '../ai/core/span-hooks'; import { addRequestAttributes, extractRequestAttributes } from '../ai/openai'; import { instrumentStream } from '../ai/openai/streaming'; import type { OpenAiCallInfo, OpenAiOptions } from '../ai/openai/types'; import { addResponseAttributes } from '../ai/openai/utils'; import type { OrchestrionChannelContext } from '../orchestrion/types'; import { invokeOrchestrionInstrumentation } from '../orchestrion/instrumentation'; import { bindTracingChannelToSpan } from '../tracing-channel'; // The recording decision is made once, when the span starts, in the caller's async context. type OpenAiChannelContext = OrchestrionChannelContext & { _sentryRecordOutputs?: boolean; _sentryOnSpanEnd?: (result: GenAiSpanResult) => void; }; /** * Describes an OpenAI-compatible provider whose SDK mirrors the openai wire format (Groq, Together, ...), * so the span building, streaming and response parsing can all be reused from `ai/openai`. */ export interface OpenAiCompatibleProvider { /** Integration name; also the key used to skip double-wrapping when another provider drives the SDK. */ integrationName: string; /** Value reported on `gen_ai.provider.name`. */ providerName: string; /** Span origin, `auto.ai.`. */ origin: string; /** The instrumented `module.name`s (from the orchestrion config's `getModuleNames`). */ moduleNames: string[]; /** The fully-qualified orchestrion channels this provider publishes to. */ channels: { chat: string; embeddings: string }; } /** * Builds a diagnostics-channel integration for an OpenAI-compatible provider SDK. It subscribes to the * `orchestrion::{chat,embeddings}` channels injected into the SDK's `create` methods, so it * requires the Sentry runtime hook or bundler plugin. Everything below the channel — request/response * attributes and streaming — is shared with the openai integration; only the provider name and origin * differ. */ export function createOpenAiCompatibleIntegration( provider: T, ): (options?: OpenAiOptions) => Integration & { name: T['integrationName'] } { const instrumentedChannels = [ { channel: provider.channels.chat, operation: 'chat' }, { channel: provider.channels.embeddings, operation: 'embeddings' }, ] as const; function instrument(options: OpenAiOptions): void { for (const { channel, operation } of instrumentedChannels) { bindTracingChannelToSpan( diagnosticsChannel.tracingChannel(channel), data => createGenAiSpan(data, operation, provider, options), { beforeSpanEnd: (span, data) => { if ('error' in data) { markCancelledIfAborted(span, getRequestSignal(data.arguments?.[1])); } addResponseAttributes(span, data.result, !!data._sentryRecordOutputs); data._sentryOnSpanEnd?.('error' in data ? { error: data.error } : { response: data.result }); }, deferSpanEnd: ({ span, data, end }) => wrapApiPromiseResponse( data.result, response => { data.result = response; if (!wrapStreamResult(span, data)) { end(); } }, end, ) || wrapStreamResult(span, data), }, ); } } return ((options: OpenAiOptions = {}) => { return { name: provider.integrationName, setup(client) { invokeOrchestrionInstrumentation(client, provider.moduleNames, instrument, [options]); }, }; }) satisfies IntegrationFn; } /** * Build the span for an instrumented `create` call. * Returning `undefined` opts the payload out so no span is opened. */ function createGenAiSpan( data: OpenAiChannelContext, operation: string, provider: OpenAiCompatibleProvider, options: OpenAiOptions, ): Span | undefined { // When another provider (e.g. LangChain) is driving the SDK, it records the spans itself and marks this // provider as skipped; skip here to avoid double spans. if (_INTERNAL_shouldSkipAiProviderWrapping(provider.integrationName)) { return undefined; } const args = data.arguments ?? []; const params = args[0] as Record | undefined; const call: OpenAiCallInfo = { operation, request: params }; const { recordInputs, recordOutputs } = resolveGenAiCallRecording(options, call); data._sentryRecordOutputs = recordOutputs; // `extractRequestAttributes` defaults the provider to openai; override it for the concrete provider. const attributes = extractRequestAttributes(args, operation, recordInputs); attributes[GEN_AI_PROVIDER_NAME] = provider.providerName; attributes[SENTRY_ORIGIN] = provider.origin; const model = (params?.model as string) || 'unknown'; const client = getClient(); const span = startInactiveSpan({ // With span streaming, omit the `'unknown'` model sentinel so the name stays low-cardinality. name: model !== 'unknown' || !(client && hasSpanStreamingEnabled(client)) ? `${operation} ${model}` : operation, op: getGenAiSpanOp(operation), attributes: attributes as Record, }); if (recordInputs && params) { addRequestAttributes(span, params, operation); } runGenAiSpanStart(options, span, call); data._sentryOnSpanEnd = result => runGenAiSpanEnd(options, span, call, result); return span; } type AsyncIterableStream = { [Symbol.asyncIterator]: () => AsyncIterator }; function isAsyncIterable(value: unknown): value is AsyncIterableStream { return !!value && typeof (value as AsyncIterableStream)[Symbol.asyncIterator] === 'function'; } /** * For a streaming `create({ stream: true })` the result is a `Stream` the caller consumes later. We can't * swap what `create` returns, but the `Stream` in `data.result` is the same instance the caller holds and * we observe it before the caller iterates — so we patch its async iterator in place to run through * `instrumentStream`, which accumulates the streamed attributes and ends the span when iteration finishes. * Only a streaming call resolves to an async-iterable, so that check alone distinguishes it. Returns `true` * to hand span-ending ownership to `instrumentStream`; `false` for non-streaming/errored results, which end * via the normal `beforeSpanEnd` path. */ function wrapStreamResult(span: Span, data: OpenAiChannelContext): boolean { const result = data.result; if (!isAsyncIterable(result)) { return false; } const iterate = result[Symbol.asyncIterator].bind(result); const instrumented = instrumentStream( { [Symbol.asyncIterator]: iterate }, span, !!data._sentryRecordOutputs, data._sentryOnSpanEnd, ); result[Symbol.asyncIterator] = () => instrumented; return true; }