@sentry/server-utils 11.14.0 → 11.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cjs/chunks/exports.js +526 -217
- package/build/cjs/chunks/exports.js.map +1 -1
- package/build/cjs/index.js +1028 -120
- package/build/cjs/index.js.map +1 -1
- package/build/esm/ai/anthropic-ai/index.js +32 -56
- package/build/esm/ai/anthropic-ai/index.js.map +1 -1
- package/build/esm/ai/anthropic-ai/streaming.js +10 -6
- package/build/esm/ai/anthropic-ai/streaming.js.map +1 -1
- package/build/esm/ai/anthropic-ai/tool-runner.js +18 -3
- package/build/esm/ai/anthropic-ai/tool-runner.js.map +1 -1
- package/build/esm/ai/anthropic-ai/utils.js +41 -3
- package/build/esm/ai/anthropic-ai/utils.js.map +1 -1
- package/build/esm/ai/core/span-hooks.js +23 -0
- package/build/esm/ai/core/span-hooks.js.map +1 -0
- package/build/esm/ai/core/time-to-first-chunk.js +14 -0
- package/build/esm/ai/core/time-to-first-chunk.js.map +1 -0
- package/build/esm/ai/core/utils.js +29 -3
- package/build/esm/ai/core/utils.js.map +1 -1
- package/build/esm/ai/flue/index.js +15 -12
- package/build/esm/ai/flue/index.js.map +1 -1
- package/build/esm/ai/flue/utils.js +24 -17
- package/build/esm/ai/flue/utils.js.map +1 -1
- package/build/esm/ai/google-genai/index.js +21 -8
- package/build/esm/ai/google-genai/index.js.map +1 -1
- package/build/esm/ai/google-genai/streaming.js +6 -2
- package/build/esm/ai/google-genai/streaming.js.map +1 -1
- package/build/esm/ai/langchain/embeddings.js +5 -2
- package/build/esm/ai/langchain/embeddings.js.map +1 -1
- package/build/esm/ai/langchain/index.js +82 -24
- package/build/esm/ai/langchain/index.js.map +1 -1
- package/build/esm/ai/langchain/utils.js +9 -1
- package/build/esm/ai/langchain/utils.js.map +1 -1
- package/build/esm/ai/langgraph/agent-span.js +13 -5
- package/build/esm/ai/langgraph/agent-span.js.map +1 -1
- package/build/esm/ai/langgraph/index.js +13 -15
- package/build/esm/ai/langgraph/index.js.map +1 -1
- package/build/esm/ai/langgraph/stream.js +4 -3
- package/build/esm/ai/langgraph/stream.js.map +1 -1
- package/build/esm/ai/langgraph/utils.js +17 -8
- package/build/esm/ai/langgraph/utils.js.map +1 -1
- package/build/esm/ai/mastra/classifier-evaluation.js.map +1 -1
- package/build/esm/ai/mastra/index.js +33 -16
- package/build/esm/ai/mastra/index.js.map +1 -1
- package/build/esm/ai/mastra/utils.js +3 -5
- package/build/esm/ai/mastra/utils.js.map +1 -1
- package/build/esm/ai/mistral/index.js +21 -7
- package/build/esm/ai/mistral/index.js.map +1 -1
- package/build/esm/ai/mistral/streaming.js +4 -2
- package/build/esm/ai/mistral/streaming.js.map +1 -1
- package/build/esm/ai/openai/index.js +19 -8
- package/build/esm/ai/openai/index.js.map +1 -1
- package/build/esm/ai/openai/streaming.js +7 -2
- package/build/esm/ai/openai/streaming.js.map +1 -1
- package/build/esm/ai/pi-ai/chat.js +18 -1
- package/build/esm/ai/pi-ai/chat.js.map +1 -1
- package/build/esm/ai/pi-ai/recording.js +34 -15
- package/build/esm/ai/pi-ai/recording.js.map +1 -1
- package/build/esm/ai/pi-durable/index.js +7 -2
- package/build/esm/ai/pi-durable/index.js.map +1 -1
- package/build/esm/ai/pi-durable/runs.js.map +1 -1
- package/build/esm/ai/pi-durable/tools.js +18 -3
- package/build/esm/ai/pi-durable/tools.js.map +1 -1
- package/build/esm/ai/typesafe/index.js +14 -6
- package/build/esm/ai/typesafe/index.js.map +1 -1
- package/build/esm/ai/workers-ai/index.js +26 -9
- package/build/esm/ai/workers-ai/index.js.map +1 -1
- package/build/esm/ai/workers-ai/streaming.js +6 -4
- package/build/esm/ai/workers-ai/streaming.js.map +1 -1
- package/build/esm/cron/common.js +16 -0
- package/build/esm/cron/common.js.map +1 -0
- package/build/esm/cron/cron.js +82 -0
- package/build/esm/cron/cron.js.map +1 -0
- package/build/esm/cron/index.js +13 -0
- package/build/esm/cron/index.js.map +1 -0
- package/build/esm/cron/node-cron.js +66 -0
- package/build/esm/cron/node-cron.js.map +1 -0
- package/build/esm/cron/node-schedule.js +43 -0
- package/build/esm/cron/node-schedule.js.map +1 -0
- package/build/esm/index.js +6 -2
- package/build/esm/index.js.map +1 -1
- package/build/esm/integrations/anthropic.js +25 -16
- package/build/esm/integrations/anthropic.js.map +1 -1
- package/build/esm/integrations/aws-sdk/index.js +1 -0
- package/build/esm/integrations/aws-sdk/index.js.map +1 -1
- package/build/esm/integrations/aws-sdk/services/ServicesExtensions.js +3 -0
- package/build/esm/integrations/aws-sdk/services/ServicesExtensions.js.map +1 -1
- package/build/esm/integrations/aws-sdk/services/bedrock-runtime.js +45 -12
- package/build/esm/integrations/aws-sdk/services/bedrock-runtime.js.map +1 -1
- package/build/esm/integrations/bullmq/contextManager.js +45 -0
- package/build/esm/integrations/bullmq/contextManager.js.map +1 -0
- package/build/esm/integrations/bullmq/index.js +44 -0
- package/build/esm/integrations/bullmq/index.js.map +1 -0
- package/build/esm/integrations/bullmq/meter.js +86 -0
- package/build/esm/integrations/bullmq/meter.js.map +1 -0
- package/build/esm/integrations/bullmq/span.js +114 -0
- package/build/esm/integrations/bullmq/span.js.map +1 -0
- package/build/esm/integrations/bullmq/tracer.js +62 -0
- package/build/esm/integrations/bullmq/tracer.js.map +1 -0
- package/build/esm/integrations/express/instrumentation.js +10 -1
- package/build/esm/integrations/express/instrumentation.js.map +1 -1
- package/build/esm/integrations/google-genai.js +17 -9
- package/build/esm/integrations/google-genai.js.map +1 -1
- package/build/esm/integrations/langgraph.js +5 -7
- package/build/esm/integrations/langgraph.js.map +1 -1
- package/build/esm/integrations/mastra-classifier.js +1 -1
- package/build/esm/integrations/mastra-classifier.js.map +1 -1
- package/build/esm/integrations/mistral.js +13 -4
- package/build/esm/integrations/mistral.js.map +1 -1
- package/build/esm/integrations/openai-compatible.js +16 -8
- package/build/esm/integrations/openai-compatible.js.map +1 -1
- package/build/esm/integrations/openai.js +16 -8
- package/build/esm/integrations/openai.js.map +1 -1
- package/build/esm/integrations/pi-ai.js +39 -18
- package/build/esm/integrations/pi-ai.js.map +1 -1
- package/build/esm/integrations/pino.js +131 -0
- package/build/esm/integrations/pino.js.map +1 -0
- package/build/esm/integrations/typesafe.js +17 -3
- package/build/esm/integrations/typesafe.js.map +1 -1
- package/build/esm/integrations/vercel-ai/index.js.map +1 -1
- package/build/esm/integrations/vercel-ai/util.js +3 -2
- package/build/esm/integrations/vercel-ai/util.js.map +1 -1
- package/build/esm/integrations/vercel-ai/vercel-ai-dc-subscriber.js +44 -17
- package/build/esm/integrations/vercel-ai/vercel-ai-dc-subscriber.js.map +1 -1
- package/build/esm/integrations/vercel-ai/vercel-ai-orchestrion-subscriber.js +22 -16
- package/build/esm/integrations/vercel-ai/vercel-ai-orchestrion-subscriber.js.map +1 -1
- package/build/esm/integrations/winston.js +90 -0
- package/build/esm/integrations/winston.js.map +1 -0
- package/build/esm/orchestrion/detect.js +1 -1
- package/build/esm/orchestrion/detect.js.map +1 -1
- package/build/esm/package.json +1 -1
- package/build/types/ai/anthropic-ai/index.d.ts +1 -5
- package/build/types/ai/anthropic-ai/index.d.ts.map +1 -1
- package/build/types/ai/anthropic-ai/streaming.d.ts +3 -2
- package/build/types/ai/anthropic-ai/streaming.d.ts.map +1 -1
- package/build/types/ai/anthropic-ai/tool-runner.d.ts.map +1 -1
- package/build/types/ai/anthropic-ai/types.d.ts +24 -3
- package/build/types/ai/anthropic-ai/types.d.ts.map +1 -1
- package/build/types/ai/anthropic-ai/utils.d.ts +2 -3
- package/build/types/ai/anthropic-ai/utils.d.ts.map +1 -1
- package/build/types/ai/core/span-hooks.d.ts +31 -0
- package/build/types/ai/core/span-hooks.d.ts.map +1 -0
- package/build/types/ai/core/time-to-first-chunk.d.ts +7 -0
- package/build/types/ai/core/time-to-first-chunk.d.ts.map +1 -0
- package/build/types/ai/core/utils.d.ts +30 -1
- package/build/types/ai/core/utils.d.ts.map +1 -1
- package/build/types/ai/flue/index.d.ts +13 -5
- package/build/types/ai/flue/index.d.ts.map +1 -1
- package/build/types/ai/flue/types.d.ts +20 -2
- package/build/types/ai/flue/types.d.ts.map +1 -1
- package/build/types/ai/flue/utils.d.ts +5 -9
- package/build/types/ai/flue/utils.d.ts.map +1 -1
- package/build/types/ai/google-genai/index.d.ts.map +1 -1
- package/build/types/ai/google-genai/streaming.d.ts +2 -1
- package/build/types/ai/google-genai/streaming.d.ts.map +1 -1
- package/build/types/ai/google-genai/types.d.ts +17 -3
- package/build/types/ai/google-genai/types.d.ts.map +1 -1
- package/build/types/ai/langchain/index.d.ts.map +1 -1
- package/build/types/ai/langchain/types.d.ts +49 -3
- package/build/types/ai/langchain/types.d.ts.map +1 -1
- package/build/types/ai/langchain/utils.d.ts +6 -0
- package/build/types/ai/langchain/utils.d.ts.map +1 -1
- package/build/types/ai/langgraph/agent-span.d.ts +4 -1
- package/build/types/ai/langgraph/agent-span.d.ts.map +1 -1
- package/build/types/ai/langgraph/index.d.ts +1 -1
- package/build/types/ai/langgraph/index.d.ts.map +1 -1
- package/build/types/ai/langgraph/stream.d.ts.map +1 -1
- package/build/types/ai/langgraph/types.d.ts +14 -3
- package/build/types/ai/langgraph/types.d.ts.map +1 -1
- package/build/types/ai/langgraph/utils.d.ts +3 -0
- package/build/types/ai/langgraph/utils.d.ts.map +1 -1
- package/build/types/ai/mastra/classifier-evaluation.d.ts +3 -3
- package/build/types/ai/mastra/classifier-evaluation.d.ts.map +1 -1
- package/build/types/ai/mastra/index.d.ts +17 -3
- package/build/types/ai/mastra/index.d.ts.map +1 -1
- package/build/types/ai/mastra/types.d.ts +2 -0
- package/build/types/ai/mastra/types.d.ts.map +1 -1
- package/build/types/ai/mastra/utils.d.ts.map +1 -1
- package/build/types/ai/mistral/index.d.ts.map +1 -1
- package/build/types/ai/mistral/streaming.d.ts +2 -1
- package/build/types/ai/mistral/streaming.d.ts.map +1 -1
- package/build/types/ai/mistral/types.d.ts +14 -3
- package/build/types/ai/mistral/types.d.ts.map +1 -1
- package/build/types/ai/openai/index.d.ts.map +1 -1
- package/build/types/ai/openai/streaming.d.ts +3 -1
- package/build/types/ai/openai/streaming.d.ts.map +1 -1
- package/build/types/ai/openai/types.d.ts +14 -3
- package/build/types/ai/openai/types.d.ts.map +1 -1
- package/build/types/ai/pi-ai/chat.d.ts +7 -1
- package/build/types/ai/pi-ai/chat.d.ts.map +1 -1
- package/build/types/ai/pi-ai/recording.d.ts +21 -9
- package/build/types/ai/pi-ai/recording.d.ts.map +1 -1
- package/build/types/ai/pi-ai/types.d.ts +16 -0
- package/build/types/ai/pi-ai/types.d.ts.map +1 -1
- package/build/types/ai/pi-durable/index.d.ts +12 -3
- package/build/types/ai/pi-durable/index.d.ts.map +1 -1
- package/build/types/ai/pi-durable/runs.d.ts +2 -0
- package/build/types/ai/pi-durable/runs.d.ts.map +1 -1
- package/build/types/ai/pi-durable/tools.d.ts +4 -3
- package/build/types/ai/pi-durable/tools.d.ts.map +1 -1
- package/build/types/ai/pi-durable/types.d.ts +15 -0
- package/build/types/ai/pi-durable/types.d.ts.map +1 -1
- package/build/types/ai/typesafe/index.d.ts +10 -2
- package/build/types/ai/typesafe/index.d.ts.map +1 -1
- package/build/types/ai/workers-ai/index.d.ts.map +1 -1
- package/build/types/ai/workers-ai/streaming.d.ts +2 -1
- package/build/types/ai/workers-ai/streaming.d.ts.map +1 -1
- package/build/types/ai/workers-ai/types.d.ts +18 -3
- package/build/types/ai/workers-ai/types.d.ts.map +1 -1
- package/build/types/cron/common.d.ts +8 -0
- package/build/types/cron/common.d.ts.map +1 -0
- package/build/types/cron/cron.d.ts +45 -0
- package/build/types/cron/cron.d.ts.map +1 -0
- package/build/types/cron/index.d.ts +10 -0
- package/build/types/cron/index.d.ts.map +1 -0
- package/build/types/cron/node-cron.d.ts +33 -0
- package/build/types/cron/node-cron.d.ts.map +1 -0
- package/build/types/cron/node-schedule.d.ts +19 -0
- package/build/types/cron/node-schedule.d.ts.map +1 -0
- package/build/types/index.d.ts +5 -2
- package/build/types/index.d.ts.map +1 -1
- package/build/types/integrations/anthropic.d.ts +2 -1
- package/build/types/integrations/anthropic.d.ts.map +1 -1
- package/build/types/integrations/aws-sdk/index.d.ts +3 -3
- package/build/types/integrations/aws-sdk/index.d.ts.map +1 -1
- package/build/types/integrations/aws-sdk/services/ServiceExtension.d.ts +1 -0
- package/build/types/integrations/aws-sdk/services/ServiceExtension.d.ts.map +1 -1
- package/build/types/integrations/aws-sdk/services/ServicesExtensions.d.ts +3 -2
- package/build/types/integrations/aws-sdk/services/ServicesExtensions.d.ts.map +1 -1
- package/build/types/integrations/aws-sdk/services/bedrock-runtime.d.ts +25 -4
- package/build/types/integrations/aws-sdk/services/bedrock-runtime.d.ts.map +1 -1
- package/build/types/integrations/bullmq/contextManager.d.ts +13 -0
- package/build/types/integrations/bullmq/contextManager.d.ts.map +1 -0
- package/build/types/integrations/bullmq/index.d.ts +43 -0
- package/build/types/integrations/bullmq/index.d.ts.map +1 -0
- package/build/types/integrations/bullmq/meter.d.ts +7 -0
- package/build/types/integrations/bullmq/meter.d.ts.map +1 -0
- package/build/types/integrations/bullmq/span.d.ts +35 -0
- package/build/types/integrations/bullmq/span.d.ts.map +1 -0
- package/build/types/integrations/bullmq/tracer.d.ts +5 -0
- package/build/types/integrations/bullmq/tracer.d.ts.map +1 -0
- package/build/types/integrations/bullmq/types.d.ts +72 -0
- package/build/types/integrations/bullmq/types.d.ts.map +1 -0
- package/build/types/integrations/express/types.d.ts +1 -0
- package/build/types/integrations/express/types.d.ts.map +1 -1
- package/build/types/integrations/flue.d.ts +2 -1
- package/build/types/integrations/flue.d.ts.map +1 -1
- package/build/types/integrations/google-genai.d.ts +2 -1
- package/build/types/integrations/google-genai.d.ts.map +1 -1
- package/build/types/integrations/groq.d.ts +1 -1
- package/build/types/integrations/langchain.d.ts +2 -1
- package/build/types/integrations/langchain.d.ts.map +1 -1
- package/build/types/integrations/langgraph.d.ts +2 -1
- package/build/types/integrations/langgraph.d.ts.map +1 -1
- package/build/types/integrations/mistral.d.ts +2 -1
- package/build/types/integrations/mistral.d.ts.map +1 -1
- package/build/types/integrations/openai-compatible.d.ts.map +1 -1
- package/build/types/integrations/openai.d.ts +2 -1
- package/build/types/integrations/openai.d.ts.map +1 -1
- package/build/types/integrations/pi-ai.d.ts +6 -3
- package/build/types/integrations/pi-ai.d.ts.map +1 -1
- package/build/types/integrations/pi-durable.d.ts +2 -1
- package/build/types/integrations/pi-durable.d.ts.map +1 -1
- package/build/types/integrations/pino.d.ts +61 -0
- package/build/types/integrations/pino.d.ts.map +1 -0
- package/build/types/integrations/together-ai.d.ts +1 -1
- package/build/types/integrations/typesafe.d.ts +2 -2
- package/build/types/integrations/typesafe.d.ts.map +1 -1
- package/build/types/integrations/vercel-ai/index.d.ts +3 -3
- package/build/types/integrations/vercel-ai/index.d.ts.map +1 -1
- package/build/types/integrations/vercel-ai/util.d.ts +2 -1
- package/build/types/integrations/vercel-ai/util.d.ts.map +1 -1
- package/build/types/integrations/vercel-ai/vercel-ai-dc-subscriber.d.ts +20 -4
- package/build/types/integrations/vercel-ai/vercel-ai-dc-subscriber.d.ts.map +1 -1
- package/build/types/integrations/vercel-ai/vercel-ai-orchestrion-subscriber.d.ts.map +1 -1
- package/build/types/integrations/winston.d.ts +55 -0
- package/build/types/integrations/winston.d.ts.map +1 -0
- package/package.json +3 -3
|
@@ -195,6 +195,29 @@ function resolveAIRecordingOptions(options) {
|
|
|
195
195
|
};
|
|
196
196
|
}
|
|
197
197
|
/**
|
|
198
|
+
* Resolves the recording options of one call, like {@link resolveAIRecordingOptions}, and calls the
|
|
199
|
+
* options that are functions with `call`. Call it once when the call starts, so the functions run in the
|
|
200
|
+
* caller's async context.
|
|
201
|
+
*/
|
|
202
|
+
function resolveGenAiCallRecording(options, call) {
|
|
203
|
+
const genAI = (0, _sentry_core.getClient)()?.getDataCollectionOptions().genAI;
|
|
204
|
+
return {
|
|
205
|
+
recordInputs: resolveRecordingOption(options?.recordInputs, call) ?? genAI?.inputs ?? true,
|
|
206
|
+
recordOutputs: resolveRecordingOption(options?.recordOutputs, call) ?? genAI?.outputs ?? true
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
/** Calls `option` with `call` when it is a function; `undefined` means the option does not decide. */
|
|
210
|
+
function resolveRecordingOption(option, call) {
|
|
211
|
+
if (typeof option !== "function") return option;
|
|
212
|
+
try {
|
|
213
|
+
const result = option(call);
|
|
214
|
+
return result === void 0 ? void 0 : result === true;
|
|
215
|
+
} catch (error) {
|
|
216
|
+
require_debug_build.DEBUG_BUILD && _sentry_core.debug.warn("[gen_ai] `recordInputs`/`recordOutputs` threw, not recording content", error);
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
198
221
|
* Build method path from current traversal
|
|
199
222
|
*/
|
|
200
223
|
function buildMethodPath(currentPath, prop) {
|
|
@@ -314,8 +337,9 @@ function markCancelledIfAborted(span, abortSignal) {
|
|
|
314
337
|
/**
|
|
315
338
|
* Ends a streaming span by setting all accumulated response attributes and ending the span.
|
|
316
339
|
* Shared across OpenAI, Anthropic, and Google GenAI streaming implementations.
|
|
340
|
+
* `beforeSpanEnd` runs after the attributes are set, right before the span ends.
|
|
317
341
|
*/
|
|
318
|
-
function endStreamSpan(span, state, recordOutputs) {
|
|
342
|
+
function endStreamSpan(span, state, recordOutputs, beforeSpanEnd) {
|
|
319
343
|
if (!span.isRecording()) return;
|
|
320
344
|
const attrs = { [_sentry_conventions_attributes.GEN_AI_RESPONSE_STREAMING]: true };
|
|
321
345
|
if (state.responseId) attrs[_sentry_conventions_attributes.GEN_AI_RESPONSE_ID] = state.responseId;
|
|
@@ -326,6 +350,7 @@ function endStreamSpan(span, state, recordOutputs) {
|
|
|
326
350
|
if (recordOutputs && state.responseTexts.length) attrs[_sentry_conventions_attributes.GEN_AI_RESPONSE_TEXT] = state.responseTexts.join("");
|
|
327
351
|
if (recordOutputs && state.toolCalls.length) attrs[_sentry_conventions_attributes.GEN_AI_RESPONSE_TOOL_CALLS] = JSON.stringify(state.toolCalls);
|
|
328
352
|
span.setAttributes(attrs);
|
|
353
|
+
beforeSpanEnd?.();
|
|
329
354
|
span.end();
|
|
330
355
|
}
|
|
331
356
|
/**
|
|
@@ -390,6 +415,24 @@ function wrapPromiseWithMethods(originalPromiseLike, instrumentedPromise) {
|
|
|
390
415
|
return typeof value === "function" ? value.bind(source) : value;
|
|
391
416
|
} });
|
|
392
417
|
}
|
|
418
|
+
// src/ai/core/span-hooks.ts
|
|
419
|
+
/** Runs {@link GenAiSpanHooks.onSpanStart}. */
|
|
420
|
+
function runGenAiSpanStart(hooks, span, call) {
|
|
421
|
+
const onSpanStart = hooks.onSpanStart;
|
|
422
|
+
if (onSpanStart && span.isRecording()) runGenAiSpanHook("onSpanStart", () => onSpanStart(span, call));
|
|
423
|
+
}
|
|
424
|
+
/** Runs {@link GenAiSpanHooks.onSpanEnd}. Call it right before the span ends. */
|
|
425
|
+
function runGenAiSpanEnd(hooks, span, call, result) {
|
|
426
|
+
const onSpanEnd = hooks.onSpanEnd;
|
|
427
|
+
if (onSpanEnd && span.isRecording()) runGenAiSpanHook("onSpanEnd", () => onSpanEnd(span, call, result));
|
|
428
|
+
}
|
|
429
|
+
function runGenAiSpanHook(name, hook) {
|
|
430
|
+
try {
|
|
431
|
+
hook();
|
|
432
|
+
} catch (error) {
|
|
433
|
+
require_debug_build.DEBUG_BUILD && _sentry_core.debug.warn(`[gen_ai] \`${name}\` threw`, error);
|
|
434
|
+
}
|
|
435
|
+
}
|
|
393
436
|
// src/ai/openai/constants.ts
|
|
394
437
|
const OPENAI_INTEGRATION_NAME = "OpenAI";
|
|
395
438
|
const OPENAI_METHOD_REGISTRY = {
|
|
@@ -448,6 +491,15 @@ function getToolCallKey(toolCall, name, accumulator) {
|
|
|
448
491
|
if (toolCall.id || name || accumulator.lastKey === void 0) return `no-index-${calls.size}`;
|
|
449
492
|
return accumulator.lastKey;
|
|
450
493
|
}
|
|
494
|
+
// src/ai/core/time-to-first-chunk.ts
|
|
495
|
+
/**
|
|
496
|
+
* Sets `gen_ai.response.time_to_first_chunk` on a streamed span, in seconds from the span start.
|
|
497
|
+
* Call it for each received chunk. Only the first call per span records a value.
|
|
498
|
+
*/
|
|
499
|
+
function recordTimeToFirstChunk(span) {
|
|
500
|
+
const { attributes, start_timestamp } = (0, _sentry_core.spanToJSON)(span);
|
|
501
|
+
if (attributes[_sentry_conventions_attributes.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK] === void 0) span.setAttribute(_sentry_conventions_attributes.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK, (0, _sentry_core.timestampInSeconds)() - start_timestamp);
|
|
502
|
+
}
|
|
451
503
|
// src/ai/core/gen-ai-attributes.ts
|
|
452
504
|
/**
|
|
453
505
|
* Gen-AI telemetry attributes that are not (yet) covered by `@sentry/conventions`.
|
|
@@ -777,9 +829,10 @@ function processResponsesApiEvent(streamEvent, state, recordOutputs, span) {
|
|
|
777
829
|
* @param stream - The async iterable stream of events to instrument.
|
|
778
830
|
* @param span - The span to add attributes to and to finish at the end of the stream.
|
|
779
831
|
* @param recordOutputs - Whether to record output text fragments in the span.
|
|
832
|
+
* @param beforeSpanEnd - Runs right before the span ends, with the error when the stream failed.
|
|
780
833
|
* @returns An async generator yielding each event from the input stream.
|
|
781
834
|
*/
|
|
782
|
-
async function* instrumentStream$1(stream, span, recordOutputs) {
|
|
835
|
+
async function* instrumentStream$1(stream, span, recordOutputs, beforeSpanEnd) {
|
|
783
836
|
const state = {
|
|
784
837
|
eventTypes: [],
|
|
785
838
|
responseTexts: [],
|
|
@@ -794,8 +847,10 @@ async function* instrumentStream$1(stream, span, recordOutputs) {
|
|
|
794
847
|
chatCompletionToolCalls: /* @__PURE__ */ new Map(),
|
|
795
848
|
responsesApiToolCalls: []
|
|
796
849
|
};
|
|
850
|
+
const result = {};
|
|
797
851
|
try {
|
|
798
852
|
for await (const event of stream) {
|
|
853
|
+
recordTimeToFirstChunk(span);
|
|
799
854
|
if (isChatCompletionChunk(event)) processChatCompletionChunk(event, state, recordOutputs);
|
|
800
855
|
else if (isResponsesApiStreamEvent(event)) processResponsesApiEvent(event, state, recordOutputs, span);
|
|
801
856
|
yield event;
|
|
@@ -805,13 +860,14 @@ async function* instrumentStream$1(stream, span, recordOutputs) {
|
|
|
805
860
|
code: _sentry_core.SPAN_STATUS_ERROR,
|
|
806
861
|
message: "internal_error"
|
|
807
862
|
});
|
|
863
|
+
result.error = error;
|
|
808
864
|
throw error;
|
|
809
865
|
} finally {
|
|
810
866
|
const allToolCalls = [...state.chatCompletionToolCalls.values(), ...state.responsesApiToolCalls];
|
|
811
867
|
endStreamSpan(span, {
|
|
812
868
|
...state,
|
|
813
869
|
toolCalls: allToolCalls
|
|
814
|
-
}, recordOutputs);
|
|
870
|
+
}, recordOutputs, () => beforeSpanEnd?.(result));
|
|
815
871
|
}
|
|
816
872
|
}
|
|
817
873
|
// src/ai/openai/index.ts
|
|
@@ -875,9 +931,14 @@ function addRequestAttributes$2(span, params, operationName) {
|
|
|
875
931
|
function instrumentMethod$3(originalMethod, methodPath, instrumentedMethod, context, options) {
|
|
876
932
|
return function instrumentedCall(...args) {
|
|
877
933
|
const operationName = instrumentedMethod.operation || "unknown";
|
|
878
|
-
const requestAttributes = extractRequestAttributes$4(args, operationName, !!options.recordInputs);
|
|
879
|
-
const model = requestAttributes[_sentry_conventions_attributes.GEN_AI_REQUEST_MODEL] || "unknown";
|
|
880
934
|
const params = args[0];
|
|
935
|
+
const call = {
|
|
936
|
+
operation: operationName,
|
|
937
|
+
request: params
|
|
938
|
+
};
|
|
939
|
+
const { recordInputs, recordOutputs } = resolveGenAiCallRecording(options, call);
|
|
940
|
+
const requestAttributes = extractRequestAttributes$4(args, operationName, recordInputs);
|
|
941
|
+
const model = requestAttributes[_sentry_conventions_attributes.GEN_AI_REQUEST_MODEL] || "unknown";
|
|
881
942
|
const isStreamRequested = params && typeof params === "object" && params.stream === true;
|
|
882
943
|
const abortSignal = getRequestSignal(args[1]);
|
|
883
944
|
const client = (0, _sentry_core.getClient)();
|
|
@@ -890,12 +951,14 @@ function instrumentMethod$3(originalMethod, methodPath, instrumentedMethod, cont
|
|
|
890
951
|
let originalResult;
|
|
891
952
|
const instrumentedPromise = (0, _sentry_core.startSpanManual)(spanConfig, (span) => {
|
|
892
953
|
originalResult = originalMethod.apply(context, args);
|
|
893
|
-
if (
|
|
954
|
+
if (recordInputs && params) addRequestAttributes$2(span, params, operationName);
|
|
955
|
+
runGenAiSpanStart(options, span, call);
|
|
894
956
|
return (async () => {
|
|
895
957
|
try {
|
|
896
|
-
return instrumentStream$1(await originalResult, span,
|
|
958
|
+
return instrumentStream$1(await originalResult, span, recordOutputs, (streamResult) => runGenAiSpanEnd(options, span, call, streamResult));
|
|
897
959
|
} catch (error) {
|
|
898
960
|
span.setStatus(getErrorStatus(abortSignal));
|
|
961
|
+
runGenAiSpanEnd(options, span, call, { error });
|
|
899
962
|
span.end();
|
|
900
963
|
throw error;
|
|
901
964
|
}
|
|
@@ -906,12 +969,15 @@ function instrumentMethod$3(originalMethod, methodPath, instrumentedMethod, cont
|
|
|
906
969
|
let originalResult;
|
|
907
970
|
const instrumentedPromise = (0, _sentry_core.startSpan)(spanConfig, (span) => {
|
|
908
971
|
originalResult = originalMethod.apply(context, args);
|
|
909
|
-
if (
|
|
972
|
+
if (recordInputs && params) addRequestAttributes$2(span, params, operationName);
|
|
973
|
+
runGenAiSpanStart(options, span, call);
|
|
910
974
|
return originalResult.then((result) => {
|
|
911
|
-
addResponseAttributes$5(span, result,
|
|
975
|
+
addResponseAttributes$5(span, result, recordOutputs, operationName);
|
|
976
|
+
runGenAiSpanEnd(options, span, call, { response: result });
|
|
912
977
|
return result;
|
|
913
978
|
}, (error) => {
|
|
914
979
|
span.setStatus(getErrorStatus(abortSignal));
|
|
980
|
+
runGenAiSpanEnd(options, span, call, { error });
|
|
915
981
|
throw error;
|
|
916
982
|
});
|
|
917
983
|
});
|
|
@@ -964,7 +1030,7 @@ function createDeepProxy$2(target, currentPath = "", options) {
|
|
|
964
1030
|
* Can be used across Node.js, Cloudflare Workers, and Vercel Edge
|
|
965
1031
|
*/
|
|
966
1032
|
function instrumentOpenAiClient(client, options) {
|
|
967
|
-
return createDeepProxy$2(client, "",
|
|
1033
|
+
return createDeepProxy$2(client, "", options ?? {});
|
|
968
1034
|
}
|
|
969
1035
|
// src/ai/anthropic-ai/constants.ts
|
|
970
1036
|
const ANTHROPIC_AI_INTEGRATION_NAME = "Anthropic_AI";
|
|
@@ -1032,6 +1098,44 @@ function handleResponseError(span, response) {
|
|
|
1032
1098
|
} });
|
|
1033
1099
|
}
|
|
1034
1100
|
}
|
|
1101
|
+
/**
|
|
1102
|
+
* Add content attributes when recordOutputs is enabled
|
|
1103
|
+
*/
|
|
1104
|
+
function addContentAttributes(span, response) {
|
|
1105
|
+
if ("content" in response) {
|
|
1106
|
+
if (Array.isArray(response.content)) {
|
|
1107
|
+
span.setAttributes({ [_sentry_conventions_attributes.GEN_AI_RESPONSE_TEXT]: response.content.map((item) => item.text).filter((text) => !!text).join("") });
|
|
1108
|
+
const toolCalls = [];
|
|
1109
|
+
for (const item of response.content) if (item.type === "tool_use" || item.type === "server_tool_use") toolCalls.push(item);
|
|
1110
|
+
if (toolCalls.length > 0) span.setAttributes({ [_sentry_conventions_attributes.GEN_AI_RESPONSE_TOOL_CALLS]: JSON.stringify(toolCalls) });
|
|
1111
|
+
}
|
|
1112
|
+
}
|
|
1113
|
+
if ("completion" in response) span.setAttributes({ [_sentry_conventions_attributes.GEN_AI_RESPONSE_TEXT]: response.completion });
|
|
1114
|
+
}
|
|
1115
|
+
/**
|
|
1116
|
+
* Add basic metadata attributes from the response
|
|
1117
|
+
*/
|
|
1118
|
+
function addMetadataAttributes(span, response) {
|
|
1119
|
+
if ("id" in response && "model" in response) {
|
|
1120
|
+
span.setAttributes({
|
|
1121
|
+
[_sentry_conventions_attributes.GEN_AI_RESPONSE_ID]: response.id,
|
|
1122
|
+
[_sentry_conventions_attributes.GEN_AI_RESPONSE_MODEL]: response.model
|
|
1123
|
+
});
|
|
1124
|
+
if ("usage" in response && response.usage) span.setAttributes(getTokenUsageAttributes(response.usage.input_tokens + (response.usage.cache_creation_input_tokens ?? 0) + (response.usage.cache_read_input_tokens ?? 0), response.usage.output_tokens, response.usage.cache_creation_input_tokens, response.usage.cache_read_input_tokens));
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
/**
|
|
1128
|
+
* Add response attributes to spans
|
|
1129
|
+
*/
|
|
1130
|
+
function addResponseAttributes$4(span, response, recordOutputs) {
|
|
1131
|
+
if (!response || typeof response !== "object") return;
|
|
1132
|
+
if ("type" in response && response.type === "error") {
|
|
1133
|
+
handleResponseError(span, response);
|
|
1134
|
+
return;
|
|
1135
|
+
}
|
|
1136
|
+
if (recordOutputs) addContentAttributes(span, response);
|
|
1137
|
+
addMetadataAttributes(span, response);
|
|
1138
|
+
}
|
|
1035
1139
|
/** Anthropic accepts the system prompt as a string or as an array of text blocks. */
|
|
1036
1140
|
function systemToText(system) {
|
|
1037
1141
|
if (!Array.isArray(system)) return typeof system === "string" ? system : void 0;
|
|
@@ -1160,6 +1264,7 @@ function handleContentBlockStop(event, state) {
|
|
|
1160
1264
|
function processEvent$1(event, state, recordOutputs, span) {
|
|
1161
1265
|
if (!(event && typeof event === "object")) return;
|
|
1162
1266
|
if (isErrorEvent(event, span)) return;
|
|
1267
|
+
recordTimeToFirstChunk(span);
|
|
1163
1268
|
handleMessageMetadata(event, state);
|
|
1164
1269
|
handleContentBlockStart(event, state);
|
|
1165
1270
|
handleContentBlockDelta(event, state, recordOutputs);
|
|
@@ -1170,7 +1275,7 @@ function processEvent$1(event, state, recordOutputs, span) {
|
|
|
1170
1275
|
* streaming attributes and (optionally) the aggregated output text, and yields
|
|
1171
1276
|
* each event from the input stream unchanged.
|
|
1172
1277
|
*/
|
|
1173
|
-
async function* instrumentAsyncIterableStream(stream, span, recordOutputs) {
|
|
1278
|
+
async function* instrumentAsyncIterableStream(stream, span, recordOutputs, beforeSpanEnd) {
|
|
1174
1279
|
const state = {
|
|
1175
1280
|
responseTexts: [],
|
|
1176
1281
|
finishReasons: [],
|
|
@@ -1184,6 +1289,7 @@ async function* instrumentAsyncIterableStream(stream, span, recordOutputs) {
|
|
|
1184
1289
|
toolCalls: [],
|
|
1185
1290
|
activeToolBlocks: {}
|
|
1186
1291
|
};
|
|
1292
|
+
const result = {};
|
|
1187
1293
|
try {
|
|
1188
1294
|
for await (const event of stream) {
|
|
1189
1295
|
processEvent$1(event, state, recordOutputs, span);
|
|
@@ -1194,15 +1300,16 @@ async function* instrumentAsyncIterableStream(stream, span, recordOutputs) {
|
|
|
1194
1300
|
code: _sentry_core.SPAN_STATUS_ERROR,
|
|
1195
1301
|
message: "internal_error"
|
|
1196
1302
|
});
|
|
1303
|
+
result.error = error;
|
|
1197
1304
|
throw error;
|
|
1198
1305
|
} finally {
|
|
1199
|
-
endStreamSpan(span, state, recordOutputs);
|
|
1306
|
+
endStreamSpan(span, state, recordOutputs, () => beforeSpanEnd?.(result));
|
|
1200
1307
|
}
|
|
1201
1308
|
}
|
|
1202
1309
|
/**
|
|
1203
1310
|
* Instruments a MessageStream by registering event handlers and preserving the original stream API.
|
|
1204
1311
|
*/
|
|
1205
|
-
function instrumentMessageStream(stream, span, recordOutputs, abortSignal) {
|
|
1312
|
+
function instrumentMessageStream(stream, span, recordOutputs, abortSignal, beforeSpanEnd) {
|
|
1206
1313
|
const state = {
|
|
1207
1314
|
responseTexts: [],
|
|
1208
1315
|
finishReasons: [],
|
|
@@ -1220,14 +1327,14 @@ function instrumentMessageStream(stream, span, recordOutputs, abortSignal) {
|
|
|
1220
1327
|
processEvent$1(event, state, recordOutputs, span);
|
|
1221
1328
|
});
|
|
1222
1329
|
stream.on("message", () => {
|
|
1223
|
-
endStreamSpan(span, state, recordOutputs);
|
|
1330
|
+
endStreamSpan(span, state, recordOutputs, () => beforeSpanEnd?.({}));
|
|
1224
1331
|
});
|
|
1225
1332
|
stream.on("end", () => {
|
|
1226
1333
|
if (span.isRecording() && stream.aborted) span.setStatus(abortSignal?.aborted ? getErrorStatus(abortSignal) : {
|
|
1227
1334
|
code: _sentry_core.SPAN_STATUS_ERROR,
|
|
1228
1335
|
message: "cancelled"
|
|
1229
1336
|
});
|
|
1230
|
-
endStreamSpan(span, state, recordOutputs);
|
|
1337
|
+
endStreamSpan(span, state, recordOutputs, () => beforeSpanEnd?.({}));
|
|
1231
1338
|
});
|
|
1232
1339
|
stream.on("error", (error) => {
|
|
1233
1340
|
(0, _sentry_core.captureException)(error, { mechanism: {
|
|
@@ -1239,7 +1346,7 @@ function instrumentMessageStream(stream, span, recordOutputs, abortSignal) {
|
|
|
1239
1346
|
code: _sentry_core.SPAN_STATUS_ERROR,
|
|
1240
1347
|
message: "internal_error"
|
|
1241
1348
|
});
|
|
1242
|
-
endStreamSpan(span, state, recordOutputs);
|
|
1349
|
+
endStreamSpan(span, state, recordOutputs, () => beforeSpanEnd?.({ error }));
|
|
1243
1350
|
}
|
|
1244
1351
|
});
|
|
1245
1352
|
return stream;
|
|
@@ -1344,8 +1451,13 @@ function wrapInPlace(owner, key, call) {
|
|
|
1344
1451
|
});
|
|
1345
1452
|
}
|
|
1346
1453
|
function runWithSpan(tool, run, thisArg, args, options) {
|
|
1347
|
-
const { recordInputs, recordOutputs } = resolveAIRecordingOptions(options);
|
|
1348
1454
|
const [input, context] = args;
|
|
1455
|
+
const call = {
|
|
1456
|
+
operation: "execute_tool",
|
|
1457
|
+
tool,
|
|
1458
|
+
input
|
|
1459
|
+
};
|
|
1460
|
+
const { recordInputs, recordOutputs } = resolveGenAiCallRecording(options, call);
|
|
1349
1461
|
const callId = (context?.toolUse ?? context?.toolUseBlock)?.id;
|
|
1350
1462
|
return (0, _sentry_core.startSpan)({
|
|
1351
1463
|
name: `execute_tool ${tool.name}`,
|
|
@@ -1358,7 +1470,16 @@ function runWithSpan(tool, run, thisArg, args, options) {
|
|
|
1358
1470
|
...typeof callId === "string" ? { [GEN_AI_TOOL_CALL_ID_ATTRIBUTE]: callId } : {},
|
|
1359
1471
|
...recordInputs ? { [_sentry_conventions_attributes.GEN_AI_TOOL_CALL_ARGUMENTS]: (0, _sentry_core.stringify)(input) } : {}
|
|
1360
1472
|
}
|
|
1361
|
-
}, (span) =>
|
|
1473
|
+
}, (span) => {
|
|
1474
|
+
runGenAiSpanStart(options, span, call);
|
|
1475
|
+
return (0, _sentry_core.handleCallbackErrors)(() => run.apply(thisArg, args), (error) => {
|
|
1476
|
+
handleToolError(span, error, context);
|
|
1477
|
+
runGenAiSpanEnd(options, span, call, { error });
|
|
1478
|
+
}, void 0, (result) => {
|
|
1479
|
+
if (recordOutputs) span.setAttribute(_sentry_conventions_attributes.GEN_AI_TOOL_CALL_RESULT, (0, _sentry_core.stringify)(result));
|
|
1480
|
+
runGenAiSpanEnd(options, span, call, { response: result });
|
|
1481
|
+
});
|
|
1482
|
+
});
|
|
1362
1483
|
}
|
|
1363
1484
|
function handleToolError(span, error, context) {
|
|
1364
1485
|
span.setStatus(getErrorStatus(context?.signal));
|
|
@@ -1402,49 +1523,12 @@ function addPrivateRequestAttributes$1(span, params) {
|
|
|
1402
1523
|
span.setAttributes(getMessagesAttributes(messages));
|
|
1403
1524
|
}
|
|
1404
1525
|
/**
|
|
1405
|
-
* Add content attributes when recordOutputs is enabled
|
|
1406
|
-
*/
|
|
1407
|
-
function addContentAttributes(span, response) {
|
|
1408
|
-
if ("content" in response) {
|
|
1409
|
-
if (Array.isArray(response.content)) {
|
|
1410
|
-
span.setAttributes({ [_sentry_conventions_attributes.GEN_AI_RESPONSE_TEXT]: response.content.map((item) => item.text).filter((text) => !!text).join("") });
|
|
1411
|
-
const toolCalls = [];
|
|
1412
|
-
for (const item of response.content) if (item.type === "tool_use" || item.type === "server_tool_use") toolCalls.push(item);
|
|
1413
|
-
if (toolCalls.length > 0) span.setAttributes({ [_sentry_conventions_attributes.GEN_AI_RESPONSE_TOOL_CALLS]: JSON.stringify(toolCalls) });
|
|
1414
|
-
}
|
|
1415
|
-
}
|
|
1416
|
-
if ("completion" in response) span.setAttributes({ [_sentry_conventions_attributes.GEN_AI_RESPONSE_TEXT]: response.completion });
|
|
1417
|
-
}
|
|
1418
|
-
/**
|
|
1419
|
-
* Add basic metadata attributes from the response
|
|
1420
|
-
*/
|
|
1421
|
-
function addMetadataAttributes(span, response) {
|
|
1422
|
-
if ("id" in response && "model" in response) {
|
|
1423
|
-
span.setAttributes({
|
|
1424
|
-
[_sentry_conventions_attributes.GEN_AI_RESPONSE_ID]: response.id,
|
|
1425
|
-
[_sentry_conventions_attributes.GEN_AI_RESPONSE_MODEL]: response.model
|
|
1426
|
-
});
|
|
1427
|
-
if ("usage" in response && response.usage) span.setAttributes(getTokenUsageAttributes(response.usage.input_tokens + (response.usage.cache_creation_input_tokens ?? 0) + (response.usage.cache_read_input_tokens ?? 0), response.usage.output_tokens, response.usage.cache_creation_input_tokens, response.usage.cache_read_input_tokens));
|
|
1428
|
-
}
|
|
1429
|
-
}
|
|
1430
|
-
/**
|
|
1431
|
-
* Add response attributes to spans
|
|
1432
|
-
*/
|
|
1433
|
-
function addResponseAttributes$4(span, response, recordOutputs) {
|
|
1434
|
-
if (!response || typeof response !== "object") return;
|
|
1435
|
-
if ("type" in response && response.type === "error") {
|
|
1436
|
-
handleResponseError(span, response);
|
|
1437
|
-
return;
|
|
1438
|
-
}
|
|
1439
|
-
if (recordOutputs) addContentAttributes(span, response);
|
|
1440
|
-
addMetadataAttributes(span, response);
|
|
1441
|
-
}
|
|
1442
|
-
/**
|
|
1443
1526
|
* Handle common error catching and reporting for streaming requests
|
|
1444
1527
|
*/
|
|
1445
|
-
function handleStreamingError(error, span, abortSignal) {
|
|
1528
|
+
function handleStreamingError(error, span, abortSignal, onSpanEnd) {
|
|
1446
1529
|
if (span.isRecording()) {
|
|
1447
1530
|
span.setStatus(getErrorStatus(abortSignal));
|
|
1531
|
+
onSpanEnd({ error });
|
|
1448
1532
|
span.end();
|
|
1449
1533
|
}
|
|
1450
1534
|
throw error;
|
|
@@ -1452,32 +1536,36 @@ function handleStreamingError(error, span, abortSignal) {
|
|
|
1452
1536
|
/**
|
|
1453
1537
|
* Handle streaming cases with common logic
|
|
1454
1538
|
*/
|
|
1455
|
-
function handleStreamingRequest(target, invocationThis, args, spanConfig, params,
|
|
1539
|
+
function handleStreamingRequest(target, invocationThis, args, spanConfig, params, { recordInputs, recordOutputs }, isStreamRequested, isStreamingMethod, options, call) {
|
|
1456
1540
|
const abortSignal = getRequestSignal(args[1]);
|
|
1457
1541
|
if (isStreamRequested && !isStreamingMethod) {
|
|
1458
1542
|
let originalResult;
|
|
1459
1543
|
const instrumentedPromise = (0, _sentry_core.startSpanManual)(spanConfig, (span) => {
|
|
1460
1544
|
originalResult = target.apply(invocationThis, args);
|
|
1461
|
-
if (
|
|
1545
|
+
if (recordInputs && params) addPrivateRequestAttributes$1(span, params);
|
|
1546
|
+
runGenAiSpanStart(options, span, call);
|
|
1547
|
+
const onSpanEnd = (result) => runGenAiSpanEnd(options, span, call, result);
|
|
1462
1548
|
return (async () => {
|
|
1463
1549
|
try {
|
|
1464
|
-
return instrumentAsyncIterableStream(await originalResult, span,
|
|
1550
|
+
return instrumentAsyncIterableStream(await originalResult, span, recordOutputs, onSpanEnd);
|
|
1465
1551
|
} catch (error) {
|
|
1466
|
-
return handleStreamingError(error, span, abortSignal);
|
|
1552
|
+
return handleStreamingError(error, span, abortSignal, onSpanEnd);
|
|
1467
1553
|
}
|
|
1468
1554
|
})();
|
|
1469
1555
|
});
|
|
1470
1556
|
return wrapPromiseWithMethods(originalResult, instrumentedPromise);
|
|
1471
1557
|
} else return (0, _sentry_core.startSpanManual)(spanConfig, (span) => {
|
|
1558
|
+
const onSpanEnd = (result) => runGenAiSpanEnd(options, span, call, result);
|
|
1472
1559
|
try {
|
|
1473
|
-
if (
|
|
1560
|
+
if (recordInputs && params) addPrivateRequestAttributes$1(span, params);
|
|
1561
|
+
runGenAiSpanStart(options, span, call);
|
|
1474
1562
|
suppressDelegatedCreate = true;
|
|
1475
1563
|
const messageStream = target.apply(invocationThis, args);
|
|
1476
1564
|
suppressDelegatedCreate = false;
|
|
1477
|
-
return instrumentMessageStream(messageStream, span,
|
|
1565
|
+
return instrumentMessageStream(messageStream, span, recordOutputs, abortSignal, onSpanEnd);
|
|
1478
1566
|
} catch (error) {
|
|
1479
1567
|
suppressDelegatedCreate = false;
|
|
1480
|
-
return handleStreamingError(error, span, abortSignal);
|
|
1568
|
+
return handleStreamingError(error, span, abortSignal, onSpanEnd);
|
|
1481
1569
|
}
|
|
1482
1570
|
});
|
|
1483
1571
|
}
|
|
@@ -1492,7 +1580,13 @@ function instrumentMethod$2(originalMethod, methodPath, instrumentedMethod, cont
|
|
|
1492
1580
|
const isStreamingMethod = instrumentedMethod.streaming === true;
|
|
1493
1581
|
if (!isStreamingMethod && suppressDelegatedCreate) return target.apply(invocationThis, args);
|
|
1494
1582
|
const operationName = instrumentedMethod.operation || "unknown";
|
|
1495
|
-
const
|
|
1583
|
+
const params = typeof args[0] === "object" && args[0] !== null ? args[0] : void 0;
|
|
1584
|
+
const call = {
|
|
1585
|
+
operation: "chat",
|
|
1586
|
+
request: params
|
|
1587
|
+
};
|
|
1588
|
+
const recording = resolveGenAiCallRecording(options, call);
|
|
1589
|
+
const requestAttributes = extractRequestAttributes$3(args, operationName, recording.recordInputs, getProviderName(invocationThis));
|
|
1496
1590
|
const model = requestAttributes[_sentry_conventions_attributes.GEN_AI_REQUEST_MODEL] || "unknown";
|
|
1497
1591
|
const client = (0, _sentry_core.getClient)();
|
|
1498
1592
|
const spanConfig = {
|
|
@@ -1500,19 +1594,21 @@ function instrumentMethod$2(originalMethod, methodPath, instrumentedMethod, cont
|
|
|
1500
1594
|
op: getGenAiSpanOp(operationName),
|
|
1501
1595
|
attributes: requestAttributes
|
|
1502
1596
|
};
|
|
1503
|
-
const params = typeof args[0] === "object" ? args[0] : void 0;
|
|
1504
1597
|
const isStreamRequested = Boolean(params?.stream);
|
|
1505
|
-
if (isStreamRequested || isStreamingMethod) return handleStreamingRequest(target, invocationThis, args, spanConfig, params,
|
|
1598
|
+
if (isStreamRequested || isStreamingMethod) return handleStreamingRequest(target, invocationThis, args, spanConfig, params, recording, isStreamRequested, isStreamingMethod, options, call);
|
|
1506
1599
|
const abortSignal = getRequestSignal(args[1]);
|
|
1507
1600
|
let originalResult;
|
|
1508
1601
|
const instrumentedPromise = (0, _sentry_core.startSpan)(spanConfig, (span) => {
|
|
1509
1602
|
originalResult = target.apply(invocationThis, args);
|
|
1510
|
-
if (
|
|
1603
|
+
if (recording.recordInputs && params) addPrivateRequestAttributes$1(span, params);
|
|
1604
|
+
runGenAiSpanStart(options, span, call);
|
|
1511
1605
|
return originalResult.then((result) => {
|
|
1512
|
-
addResponseAttributes$4(span, result,
|
|
1606
|
+
addResponseAttributes$4(span, result, recording.recordOutputs);
|
|
1607
|
+
runGenAiSpanEnd(options, span, call, { response: result });
|
|
1513
1608
|
return result;
|
|
1514
1609
|
}, (error) => {
|
|
1515
1610
|
span.setStatus(getErrorStatus(abortSignal));
|
|
1611
|
+
runGenAiSpanEnd(options, span, call, { error });
|
|
1516
1612
|
throw error;
|
|
1517
1613
|
});
|
|
1518
1614
|
});
|
|
@@ -1558,7 +1654,7 @@ function instrumentClientInPlace(client, options) {
|
|
|
1558
1654
|
* @returns The instrumented client with the same type as the input
|
|
1559
1655
|
*/
|
|
1560
1656
|
function instrumentAnthropicAiClient(anthropicAiClient, options) {
|
|
1561
|
-
return instrumentClientInPlace(anthropicAiClient,
|
|
1657
|
+
return instrumentClientInPlace(anthropicAiClient, options ?? {});
|
|
1562
1658
|
}
|
|
1563
1659
|
// src/ai/google-genai/constants.ts
|
|
1564
1660
|
const GOOGLE_GENAI_INTEGRATION_NAME = "Google_GenAI";
|
|
@@ -1795,6 +1891,7 @@ function handleCandidateContent(chunk, state, recordOutputs) {
|
|
|
1795
1891
|
*/
|
|
1796
1892
|
function processChunk$1(chunk, state, recordOutputs, span) {
|
|
1797
1893
|
if (!chunk || isErrorChunk(chunk, span)) return;
|
|
1894
|
+
recordTimeToFirstChunk(span);
|
|
1798
1895
|
handleResponseMetadata(chunk, state);
|
|
1799
1896
|
handleCandidateContent(chunk, state, recordOutputs);
|
|
1800
1897
|
}
|
|
@@ -1803,13 +1900,14 @@ function processChunk$1(chunk, state, recordOutputs, span) {
|
|
|
1803
1900
|
* streaming attributes and (optionally) the aggregated output text, and yields
|
|
1804
1901
|
* each chunk from the input stream unchanged.
|
|
1805
1902
|
*/
|
|
1806
|
-
async function* instrumentStream(stream, span, recordOutputs, abortSignal) {
|
|
1903
|
+
async function* instrumentStream(stream, span, recordOutputs, abortSignal, beforeSpanEnd) {
|
|
1807
1904
|
const state = {
|
|
1808
1905
|
responseTexts: [],
|
|
1809
1906
|
finishReasons: [],
|
|
1810
1907
|
toolCalls: [],
|
|
1811
1908
|
outputParts: []
|
|
1812
1909
|
};
|
|
1910
|
+
const result = {};
|
|
1813
1911
|
try {
|
|
1814
1912
|
for await (const chunk of stream) {
|
|
1815
1913
|
processChunk$1(chunk, state, recordOutputs, span);
|
|
@@ -1817,11 +1915,12 @@ async function* instrumentStream(stream, span, recordOutputs, abortSignal) {
|
|
|
1817
1915
|
}
|
|
1818
1916
|
} catch (error) {
|
|
1819
1917
|
span.setStatus(getErrorStatus(abortSignal));
|
|
1918
|
+
result.error = error;
|
|
1820
1919
|
throw error;
|
|
1821
1920
|
} finally {
|
|
1822
1921
|
if (recordOutputs) setOutputMessagesAttribute(span, state.outputParts);
|
|
1823
1922
|
if (typeof state.promptTokens === "number") state.promptTokens += state.toolUsePromptTokens ?? 0;
|
|
1824
|
-
endStreamSpan(span, state, recordOutputs);
|
|
1923
|
+
endStreamSpan(span, state, recordOutputs, () => beforeSpanEnd?.(result));
|
|
1825
1924
|
}
|
|
1826
1925
|
}
|
|
1827
1926
|
// src/ai/google-genai/index.ts
|
|
@@ -1972,9 +2071,14 @@ function instrumentMethod$1(originalMethod, methodPath, instrumentedMethod, cont
|
|
|
1972
2071
|
return new Proxy(originalMethod, { apply(target, _, args) {
|
|
1973
2072
|
const operationName = instrumentedMethod.operation || "unknown";
|
|
1974
2073
|
const params = args[0];
|
|
2074
|
+
const call = {
|
|
2075
|
+
operation: operationName,
|
|
2076
|
+
request: params
|
|
2077
|
+
};
|
|
2078
|
+
const { recordInputs, recordOutputs } = resolveGenAiCallRecording(options, call);
|
|
1975
2079
|
const attributeParams = resolveChatParams(operationName, params, context);
|
|
1976
2080
|
const abortSignal = getCallAbortSignal(operationName, attributeParams, args[1]);
|
|
1977
|
-
const requestAttributes = extractRequestAttributes$2(operationName, attributeParams, context,
|
|
2081
|
+
const requestAttributes = extractRequestAttributes$2(operationName, attributeParams, context, recordInputs);
|
|
1978
2082
|
const model = requestAttributes[_sentry_conventions_attributes.GEN_AI_REQUEST_MODEL] || "unknown";
|
|
1979
2083
|
const client = (0, _sentry_core.getClient)();
|
|
1980
2084
|
const spanName = typeof model === "string" && model !== "unknown" || !(client && (0, _sentry_core.hasSpanStreamingEnabled)(client)) ? `${operationName} ${model}` : operationName;
|
|
@@ -1984,10 +2088,12 @@ function instrumentMethod$1(originalMethod, methodPath, instrumentedMethod, cont
|
|
|
1984
2088
|
attributes: requestAttributes
|
|
1985
2089
|
}, async (span) => {
|
|
1986
2090
|
try {
|
|
1987
|
-
if (
|
|
1988
|
-
|
|
2091
|
+
if (recordInputs && attributeParams) addPrivateRequestAttributes(span, attributeParams, operationName);
|
|
2092
|
+
runGenAiSpanStart(options, span, call);
|
|
2093
|
+
return instrumentStream(await target.apply(context, args), span, recordOutputs, abortSignal, (result) => runGenAiSpanEnd(options, span, call, result));
|
|
1989
2094
|
} catch (error) {
|
|
1990
2095
|
span.setStatus(getErrorStatus(abortSignal));
|
|
2096
|
+
runGenAiSpanEnd(options, span, call, { error });
|
|
1991
2097
|
span.end();
|
|
1992
2098
|
throw error;
|
|
1993
2099
|
}
|
|
@@ -1997,9 +2103,14 @@ function instrumentMethod$1(originalMethod, methodPath, instrumentedMethod, cont
|
|
|
1997
2103
|
op: getGenAiSpanOp(operationName),
|
|
1998
2104
|
attributes: requestAttributes
|
|
1999
2105
|
}, (span) => {
|
|
2000
|
-
if (
|
|
2001
|
-
|
|
2002
|
-
|
|
2106
|
+
if (recordInputs && attributeParams) addPrivateRequestAttributes(span, attributeParams, operationName);
|
|
2107
|
+
runGenAiSpanStart(options, span, call);
|
|
2108
|
+
return (0, _sentry_core.handleCallbackErrors)(() => target.apply(context, args), (error) => {
|
|
2109
|
+
span.setStatus(getErrorStatus(abortSignal));
|
|
2110
|
+
runGenAiSpanEnd(options, span, call, { error });
|
|
2111
|
+
}, () => {}, (result) => {
|
|
2112
|
+
if (!isEmbeddings) addResponseAttributes$3(span, result, recordOutputs, operationName);
|
|
2113
|
+
runGenAiSpanEnd(options, span, call, { response: result });
|
|
2003
2114
|
});
|
|
2004
2115
|
});
|
|
2005
2116
|
} });
|
|
@@ -2081,7 +2192,7 @@ function createDeepProxy$1(target, currentPath = "", options) {
|
|
|
2081
2192
|
* ```
|
|
2082
2193
|
*/
|
|
2083
2194
|
function instrumentGoogleGenAIClient(client, options) {
|
|
2084
|
-
return createDeepProxy$1(client, "",
|
|
2195
|
+
return createDeepProxy$1(client, "", options ?? {});
|
|
2085
2196
|
}
|
|
2086
2197
|
// src/ai/mistral/constants.ts
|
|
2087
2198
|
const MISTRAL_INTEGRATION_NAME = "Mistral";
|
|
@@ -2324,13 +2435,14 @@ function instrumentedSource(readable) {
|
|
|
2324
2435
|
*
|
|
2325
2436
|
* Returns `false` for a value that is not a stream, leaving it untouched.
|
|
2326
2437
|
*/
|
|
2327
|
-
function instrumentEventStream(stream, span, recordOutputs, abortSignal) {
|
|
2438
|
+
function instrumentEventStream(stream, span, recordOutputs, abortSignal, beforeSpanEnd) {
|
|
2328
2439
|
if (!isAsyncIterable(stream)) return false;
|
|
2329
2440
|
const state = createStreamingState();
|
|
2330
2441
|
let consumer;
|
|
2331
2442
|
let settled = false;
|
|
2332
2443
|
let cancelled = false;
|
|
2333
2444
|
const claim = (candidate) => {
|
|
2445
|
+
recordTimeToFirstChunk(span);
|
|
2334
2446
|
consumer ?? (consumer = candidate);
|
|
2335
2447
|
return consumer === candidate;
|
|
2336
2448
|
};
|
|
@@ -2350,7 +2462,7 @@ function instrumentEventStream(stream, span, recordOutputs, abortSignal) {
|
|
|
2350
2462
|
endStreamSpan(span, {
|
|
2351
2463
|
...state,
|
|
2352
2464
|
toolCalls
|
|
2353
|
-
}, recordOutputs);
|
|
2465
|
+
}, recordOutputs, () => beforeSpanEnd?.(error !== void 0 && !cancelled ? { error } : {}));
|
|
2354
2466
|
};
|
|
2355
2467
|
const instrumented = instrumentIterator(stream[Symbol.asyncIterator].bind(stream), state, recordOutputs, claim, settle);
|
|
2356
2468
|
stream[Symbol.asyncIterator] = () => instrumented;
|
|
@@ -2432,9 +2544,14 @@ function instrumentMethod(originalMethod, instrumentedMethod, context, options)
|
|
|
2432
2544
|
return function instrumentedCall(...args) {
|
|
2433
2545
|
const operationName = instrumentedMethod.operation || "unknown";
|
|
2434
2546
|
const isStreamRequested = !!instrumentedMethod.streaming;
|
|
2435
|
-
const requestAttributes = extractRequestAttributes$1(args, operationName, !!options.recordInputs, isStreamRequested);
|
|
2436
2547
|
const params = args[0];
|
|
2437
2548
|
const abortSignal = getMistralRequestSignal(args[1]);
|
|
2549
|
+
const call = {
|
|
2550
|
+
operation: operationName,
|
|
2551
|
+
request: params
|
|
2552
|
+
};
|
|
2553
|
+
const { recordInputs, recordOutputs } = resolveGenAiCallRecording(options, call);
|
|
2554
|
+
const requestAttributes = extractRequestAttributes$1(args, operationName, recordInputs, isStreamRequested);
|
|
2438
2555
|
const spanConfig = {
|
|
2439
2556
|
name: getSpanName$1(operationName, requestAttributes),
|
|
2440
2557
|
op: getGenAiSpanOp(operationName),
|
|
@@ -2444,12 +2561,17 @@ function instrumentMethod(originalMethod, instrumentedMethod, context, options)
|
|
|
2444
2561
|
let originalResult;
|
|
2445
2562
|
const instrumentedPromise = (0, _sentry_core.startSpanManual)(spanConfig, (span) => {
|
|
2446
2563
|
originalResult = originalMethod.apply(context, args);
|
|
2447
|
-
if (
|
|
2564
|
+
if (recordInputs && params) addRequestAttributes$1(span, params, operationName);
|
|
2565
|
+
runGenAiSpanStart(options, span, call);
|
|
2448
2566
|
return originalResult.then((result) => {
|
|
2449
|
-
if (!instrumentEventStream(result, span,
|
|
2567
|
+
if (!instrumentEventStream(result, span, recordOutputs, abortSignal, (streamResult) => runGenAiSpanEnd(options, span, call, streamResult))) {
|
|
2568
|
+
runGenAiSpanEnd(options, span, call, { response: result });
|
|
2569
|
+
span.end();
|
|
2570
|
+
}
|
|
2450
2571
|
return result;
|
|
2451
2572
|
}, (error) => {
|
|
2452
2573
|
span.setStatus(getErrorStatus(abortSignal));
|
|
2574
|
+
runGenAiSpanEnd(options, span, call, { error });
|
|
2453
2575
|
span.end();
|
|
2454
2576
|
throw error;
|
|
2455
2577
|
});
|
|
@@ -2459,12 +2581,15 @@ function instrumentMethod(originalMethod, instrumentedMethod, context, options)
|
|
|
2459
2581
|
let originalResult;
|
|
2460
2582
|
const instrumentedPromise = (0, _sentry_core.startSpan)(spanConfig, (span) => {
|
|
2461
2583
|
originalResult = originalMethod.apply(context, args);
|
|
2462
|
-
if (
|
|
2584
|
+
if (recordInputs && params) addRequestAttributes$1(span, params, operationName);
|
|
2585
|
+
runGenAiSpanStart(options, span, call);
|
|
2463
2586
|
return originalResult.then((result) => {
|
|
2464
|
-
addResponseAttributes$2(span, result,
|
|
2587
|
+
addResponseAttributes$2(span, result, recordOutputs);
|
|
2588
|
+
runGenAiSpanEnd(options, span, call, { response: result });
|
|
2465
2589
|
return result;
|
|
2466
2590
|
}, (error) => {
|
|
2467
2591
|
span.setStatus(getErrorStatus(abortSignal));
|
|
2592
|
+
runGenAiSpanEnd(options, span, call, { error });
|
|
2468
2593
|
throw error;
|
|
2469
2594
|
});
|
|
2470
2595
|
});
|
|
@@ -2490,7 +2615,7 @@ function createDeepProxy(target, currentPath = "", options) {
|
|
|
2490
2615
|
* Can be used across Node.js, Cloudflare Workers, and Vercel Edge.
|
|
2491
2616
|
*/
|
|
2492
2617
|
function instrumentMistralAiClient(client, options) {
|
|
2493
|
-
return createDeepProxy(client, "",
|
|
2618
|
+
return createDeepProxy(client, "", options ?? {});
|
|
2494
2619
|
}
|
|
2495
2620
|
// src/ai/typesafe/constants.ts
|
|
2496
2621
|
const TYPESAFE_INTEGRATION_NAME = "TypeSafe";
|
|
@@ -2556,31 +2681,38 @@ function instrumentTypeSafeClient(client, options) {
|
|
|
2556
2681
|
return new Proxy(client, { get(target, prop) {
|
|
2557
2682
|
const value = Reflect.get(target, prop, target);
|
|
2558
2683
|
if (typeof value !== "function") return value;
|
|
2559
|
-
if (prop === "systemOne") return (...args) => instrumentSystemOne(value, target, args, options);
|
|
2684
|
+
if (prop === "systemOne") return (...args) => instrumentSystemOne(value, target, args, options ?? {});
|
|
2560
2685
|
return value.bind(target);
|
|
2561
2686
|
} });
|
|
2562
2687
|
}
|
|
2563
2688
|
function instrumentSystemOne(systemOne, client, args, options) {
|
|
2564
|
-
const {
|
|
2689
|
+
const call = { request: args[0] };
|
|
2690
|
+
const { recordInputs, recordOutputs } = resolveGenAiCallRecording(options, call);
|
|
2565
2691
|
const span = startDecideSpan(args[0], client, recordInputs);
|
|
2566
|
-
|
|
2692
|
+
runGenAiSpanStart(options, span, call);
|
|
2693
|
+
const endWithError = (error) => {
|
|
2567
2694
|
span.setStatus({
|
|
2568
2695
|
code: _sentry_core.SPAN_STATUS_ERROR,
|
|
2569
2696
|
message: "internal_error"
|
|
2570
2697
|
});
|
|
2698
|
+
runGenAiSpanEnd(options, span, call, { error });
|
|
2571
2699
|
span.end();
|
|
2572
2700
|
};
|
|
2573
2701
|
let result;
|
|
2574
2702
|
try {
|
|
2575
2703
|
result = (0, _sentry_core.withActiveSpan)(span, () => systemOne.apply(client, args));
|
|
2576
2704
|
} catch (error) {
|
|
2577
|
-
endWithError();
|
|
2705
|
+
endWithError(error);
|
|
2578
2706
|
throw error;
|
|
2579
2707
|
}
|
|
2580
2708
|
if (!onSystemOneResponse(result, (body) => {
|
|
2581
2709
|
addResponseAttributes$1(span, body, recordOutputs);
|
|
2710
|
+
runGenAiSpanEnd(options, span, call, { response: body });
|
|
2582
2711
|
span.end();
|
|
2583
|
-
}, endWithError))
|
|
2712
|
+
}, endWithError)) {
|
|
2713
|
+
runGenAiSpanEnd(options, span, call, { response: result });
|
|
2714
|
+
span.end();
|
|
2715
|
+
}
|
|
2584
2716
|
return result;
|
|
2585
2717
|
}
|
|
2586
2718
|
// src/ai/pi-ai/constants.ts
|
|
@@ -2823,7 +2955,7 @@ function processLine(line, state, recordOutputs, toolCallAccumulator) {
|
|
|
2823
2955
|
*
|
|
2824
2956
|
* The span is ended once the consumer finishes reading (or cancels) the stream.
|
|
2825
2957
|
*/
|
|
2826
|
-
function instrumentWorkersAiStream(stream, span, recordOutputs, abortSignal) {
|
|
2958
|
+
function instrumentWorkersAiStream(stream, span, recordOutputs, abortSignal, beforeSpanEnd) {
|
|
2827
2959
|
const reader = stream.getReader();
|
|
2828
2960
|
const decoder = new TextDecoder();
|
|
2829
2961
|
const state = {
|
|
@@ -2842,7 +2974,7 @@ function instrumentWorkersAiStream(stream, span, recordOutputs, abortSignal) {
|
|
|
2842
2974
|
};
|
|
2843
2975
|
let buffer = "";
|
|
2844
2976
|
let spanEnded = false;
|
|
2845
|
-
const finish = () => {
|
|
2977
|
+
const finish = (result = {}) => {
|
|
2846
2978
|
if (spanEnded) return;
|
|
2847
2979
|
spanEnded = true;
|
|
2848
2980
|
if (recordOutputs) {
|
|
@@ -2854,7 +2986,7 @@ function instrumentWorkersAiStream(stream, span, recordOutputs, abortSignal) {
|
|
|
2854
2986
|
finishReason: state.finishReasons[0]
|
|
2855
2987
|
});
|
|
2856
2988
|
}
|
|
2857
|
-
endStreamSpan(span, state, recordOutputs);
|
|
2989
|
+
endStreamSpan(span, state, recordOutputs, () => beforeSpanEnd?.(result));
|
|
2858
2990
|
};
|
|
2859
2991
|
const flushBuffer = (isDone) => {
|
|
2860
2992
|
const lines = buffer.split("\n");
|
|
@@ -2872,12 +3004,13 @@ function instrumentWorkersAiStream(stream, span, recordOutputs, abortSignal) {
|
|
|
2872
3004
|
controller.close();
|
|
2873
3005
|
return;
|
|
2874
3006
|
}
|
|
3007
|
+
recordTimeToFirstChunk(span);
|
|
2875
3008
|
buffer += decoder.decode(value, { stream: true });
|
|
2876
3009
|
flushBuffer(false);
|
|
2877
3010
|
controller.enqueue(value);
|
|
2878
3011
|
} catch (error) {
|
|
2879
3012
|
span.setStatus(getErrorStatus(abortSignal));
|
|
2880
|
-
finish();
|
|
3013
|
+
finish({ error });
|
|
2881
3014
|
controller.error(error);
|
|
2882
3015
|
}
|
|
2883
3016
|
},
|
|
@@ -2888,6 +3021,7 @@ function instrumentWorkersAiStream(stream, span, recordOutputs, abortSignal) {
|
|
|
2888
3021
|
});
|
|
2889
3022
|
}
|
|
2890
3023
|
// src/ai/workers-ai/index.ts
|
|
3024
|
+
const originalClients = /* @__PURE__ */ new WeakMap();
|
|
2891
3025
|
/**
|
|
2892
3026
|
* Wrap the `run` method of the Workers AI binding with Sentry tracing.
|
|
2893
3027
|
*/
|
|
@@ -2896,6 +3030,13 @@ function instrumentRun(originalRun, context, options) {
|
|
|
2896
3030
|
if (shouldSkipAiProviderSpan("WorkersAI")) return originalRun.apply(context, args);
|
|
2897
3031
|
const [model, inputs, runOptions] = args;
|
|
2898
3032
|
const operationName = getOperationName(model, inputs);
|
|
3033
|
+
const call = {
|
|
3034
|
+
operation: operationName,
|
|
3035
|
+
model,
|
|
3036
|
+
inputs,
|
|
3037
|
+
options: runOptions
|
|
3038
|
+
};
|
|
3039
|
+
const { recordInputs, recordOutputs } = resolveGenAiCallRecording(options, call);
|
|
2899
3040
|
const requestAttributes = extractRequestAttributes(model, inputs, operationName);
|
|
2900
3041
|
const modelName = typeof model === "string" && model ? model : "unknown";
|
|
2901
3042
|
const abortSignal = getRequestSignal(runOptions);
|
|
@@ -2912,6 +3053,7 @@ function instrumentRun(originalRun, context, options) {
|
|
|
2912
3053
|
if (isStreamRequested && !returnsRawResponse) return (0, _sentry_core.startSpanManual)(spanConfig, (span) => {
|
|
2913
3054
|
const handleError = (error) => {
|
|
2914
3055
|
span.setStatus(getErrorStatus(abortSignal));
|
|
3056
|
+
runGenAiSpanEnd(options, span, call, { error });
|
|
2915
3057
|
span.end();
|
|
2916
3058
|
throw error;
|
|
2917
3059
|
};
|
|
@@ -2921,22 +3063,27 @@ function instrumentRun(originalRun, context, options) {
|
|
|
2921
3063
|
} catch (error) {
|
|
2922
3064
|
return handleError(error);
|
|
2923
3065
|
}
|
|
2924
|
-
if (
|
|
3066
|
+
if (recordInputs) addRequestAttributes(span, inputs, operationName);
|
|
3067
|
+
runGenAiSpanStart(options, span, call);
|
|
2925
3068
|
return originalResult.then((result) => {
|
|
2926
|
-
if (isReadableStream(result)) return instrumentWorkersAiStream(result, span,
|
|
2927
|
-
addResponseAttributes(span, result,
|
|
3069
|
+
if (isReadableStream(result)) return instrumentWorkersAiStream(result, span, recordOutputs, abortSignal, (streamResult) => runGenAiSpanEnd(options, span, call, streamResult));
|
|
3070
|
+
addResponseAttributes(span, result, recordOutputs, operationName);
|
|
3071
|
+
runGenAiSpanEnd(options, span, call, { response: result });
|
|
2928
3072
|
span.end();
|
|
2929
3073
|
return result;
|
|
2930
3074
|
}, handleError);
|
|
2931
3075
|
});
|
|
2932
3076
|
return (0, _sentry_core.startSpan)(spanConfig, (span) => {
|
|
2933
3077
|
const originalResult = originalRun.apply(context, args);
|
|
2934
|
-
if (
|
|
3078
|
+
if (recordInputs) addRequestAttributes(span, inputs, operationName);
|
|
3079
|
+
runGenAiSpanStart(options, span, call);
|
|
2935
3080
|
return originalResult.then((result) => {
|
|
2936
|
-
if (!returnsRawResponse) addResponseAttributes(span, result,
|
|
3081
|
+
if (!returnsRawResponse) addResponseAttributes(span, result, recordOutputs, operationName);
|
|
3082
|
+
runGenAiSpanEnd(options, span, call, { response: result });
|
|
2937
3083
|
return result;
|
|
2938
3084
|
}, (error) => {
|
|
2939
3085
|
span.setStatus(getErrorStatus(abortSignal));
|
|
3086
|
+
runGenAiSpanEnd(options, span, call, { error });
|
|
2940
3087
|
throw error;
|
|
2941
3088
|
});
|
|
2942
3089
|
});
|
|
@@ -2958,12 +3105,14 @@ function instrumentRun(originalRun, context, options) {
|
|
|
2958
3105
|
* ```
|
|
2959
3106
|
*/
|
|
2960
3107
|
function instrumentWorkersAiClient(client, options) {
|
|
2961
|
-
const
|
|
2962
|
-
|
|
3108
|
+
const original = originalClients.get(client) ?? client;
|
|
3109
|
+
const instrumented = new Proxy(original, { get(target, prop, receiver) {
|
|
2963
3110
|
const value = Reflect.get(target, prop, receiver);
|
|
2964
|
-
if (prop === "run" && typeof value === "function") return instrumentRun(value, target,
|
|
3111
|
+
if (prop === "run" && typeof value === "function") return instrumentRun(value, target, options ?? {});
|
|
2965
3112
|
return typeof value === "function" ? value.bind(target) : value;
|
|
2966
3113
|
} });
|
|
3114
|
+
originalClients.set(instrumented, original);
|
|
3115
|
+
return instrumented;
|
|
2967
3116
|
}
|
|
2968
3117
|
// src/ai/langchain/constants.ts
|
|
2969
3118
|
const LANGCHAIN_INTEGRATION_NAME = "LangChain";
|
|
@@ -3270,6 +3419,14 @@ function findConversationIdEntry(source) {
|
|
|
3270
3419
|
}
|
|
3271
3420
|
}
|
|
3272
3421
|
/**
|
|
3422
|
+
* The recording decision of the LangGraph graph run that `metadata` belongs to. The graph's `invoke()`/`stream()`
|
|
3423
|
+
* call decides once, and child runs inherit the decision through their metadata.
|
|
3424
|
+
*/
|
|
3425
|
+
function getLangGraphRunRecording(metadata) {
|
|
3426
|
+
const recording = metadata?.__sentry_langgraph__;
|
|
3427
|
+
return (0, _sentry_core.isObjectLike)(recording) ? recording : void 0;
|
|
3428
|
+
}
|
|
3429
|
+
/**
|
|
3273
3430
|
* Derive `gen_ai.conversation.id` from run metadata, which child runs inherit from their parent.
|
|
3274
3431
|
* An id from `Sentry.setConversationId()` still wins, since `conversationIdIntegration` applies it on `spanStart`.
|
|
3275
3432
|
*/
|
|
@@ -3431,7 +3588,10 @@ function extractEmbeddingAttributes(instance) {
|
|
|
3431
3588
|
* span as the prototype-patching path below.
|
|
3432
3589
|
*/
|
|
3433
3590
|
function _INTERNAL_getLangChainEmbeddingsSpanOptions(instance, input, options = {}) {
|
|
3434
|
-
const { recordInputs } =
|
|
3591
|
+
const { recordInputs } = resolveGenAiCallRecording(options, {
|
|
3592
|
+
operation: "embeddings",
|
|
3593
|
+
input
|
|
3594
|
+
});
|
|
3435
3595
|
const attributes = extractEmbeddingAttributes(instance);
|
|
3436
3596
|
const modelName = attributes[_sentry_conventions_attributes.GEN_AI_REQUEST_MODEL] || "unknown";
|
|
3437
3597
|
const client = (0, _sentry_core.getClient)();
|
|
@@ -3485,7 +3645,7 @@ function instrumentLangChainEmbeddings(instance, options) {
|
|
|
3485
3645
|
* This is a stateful handler that tracks spans across multiple LangChain executions.
|
|
3486
3646
|
*/
|
|
3487
3647
|
function createLangChainCallbackHandler(options = {}) {
|
|
3488
|
-
const
|
|
3648
|
+
const resolveRecording = (call) => getLangGraphRunRecording(call.metadata) ?? resolveGenAiCallRecording(options, call);
|
|
3489
3649
|
const runs = new _sentry_core.LRUMap(MAX_TRACKED_LANGCHAIN_RUNS);
|
|
3490
3650
|
const getRun = (runId) => runId ? runs.get(runId) : void 0;
|
|
3491
3651
|
/**
|
|
@@ -3518,7 +3678,7 @@ function createLangChainCallbackHandler(options = {}) {
|
|
|
3518
3678
|
if (!(0, _sentry_core.spanIsIgnored)(run.span) || (0, _sentry_core.getRootSpan)(run.span) === run.span) return run.span;
|
|
3519
3679
|
}
|
|
3520
3680
|
};
|
|
3521
|
-
const startRunSpan = (runId, parentRunId, start) => {
|
|
3681
|
+
const startRunSpan = (runId, parentRunId, recordOutputs, start) => {
|
|
3522
3682
|
const ancestors = getAncestors(parentRunId);
|
|
3523
3683
|
const activeSpan = (0, _sentry_core.getActiveSpan)();
|
|
3524
3684
|
const parentSpan = getParentRunSpan(ancestors, activeSpan);
|
|
@@ -3530,7 +3690,8 @@ function createLangChainCallbackHandler(options = {}) {
|
|
|
3530
3690
|
span,
|
|
3531
3691
|
parentRunId,
|
|
3532
3692
|
activeSpan,
|
|
3533
|
-
conversationId
|
|
3693
|
+
conversationId,
|
|
3694
|
+
recordOutputs
|
|
3534
3695
|
};
|
|
3535
3696
|
trackRun(runId, run);
|
|
3536
3697
|
if (conversationId) for (const ancestor of ancestors) {
|
|
@@ -3569,13 +3730,23 @@ function createLangChainCallbackHandler(options = {}) {
|
|
|
3569
3730
|
ignoreCustomEvent: false,
|
|
3570
3731
|
raiseError: false,
|
|
3571
3732
|
awaitHandlers: true,
|
|
3572
|
-
handleLLMStart(llm, prompts, runId, parentRunId,
|
|
3573
|
-
const
|
|
3574
|
-
const
|
|
3733
|
+
handleLLMStart(llm, prompts, runId, parentRunId, extraParams, tags, metadata, _runName) {
|
|
3734
|
+
const serialized = llm;
|
|
3735
|
+
const { recordInputs, recordOutputs } = resolveRecording({
|
|
3736
|
+
operation: "chat",
|
|
3737
|
+
serialized,
|
|
3738
|
+
prompts,
|
|
3739
|
+
runId,
|
|
3740
|
+
parentRunId,
|
|
3741
|
+
extraParams,
|
|
3742
|
+
tags,
|
|
3743
|
+
metadata
|
|
3744
|
+
});
|
|
3745
|
+
const attributes = extractLLMRequestAttributes(serialized, prompts, recordInputs, getInvocationParams(tags), metadata);
|
|
3575
3746
|
const modelName = attributes[_sentry_conventions_attributes.GEN_AI_REQUEST_MODEL] || "unknown";
|
|
3576
3747
|
const operationName = typeof attributes[_sentry_conventions_attributes.GEN_AI_OPERATION_NAME] === "string" ? attributes[_sentry_conventions_attributes.GEN_AI_OPERATION_NAME] : "unknown";
|
|
3577
3748
|
const client = (0, _sentry_core.getClient)();
|
|
3578
|
-
startRunSpan(runId, parentRunId, () => (0, _sentry_core.startInactiveSpan)({
|
|
3749
|
+
startRunSpan(runId, parentRunId, recordOutputs, () => (0, _sentry_core.startInactiveSpan)({
|
|
3579
3750
|
name: typeof modelName === "string" && modelName !== "unknown" || !(client && (0, _sentry_core.hasSpanStreamingEnabled)(client)) ? `${operationName} ${modelName}` : operationName,
|
|
3580
3751
|
attributes: {
|
|
3581
3752
|
...getAgentNameFromMetadata(metadata),
|
|
@@ -3586,14 +3757,25 @@ function createLangChainCallbackHandler(options = {}) {
|
|
|
3586
3757
|
}));
|
|
3587
3758
|
},
|
|
3588
3759
|
handleChatModelStart(llm, messages, runId, parentRunId, extraParams, tags, metadata, _runName) {
|
|
3589
|
-
const
|
|
3590
|
-
const
|
|
3760
|
+
const serialized = llm;
|
|
3761
|
+
const chatMessages = messages;
|
|
3762
|
+
const { recordInputs, recordOutputs } = resolveRecording({
|
|
3763
|
+
operation: "chat",
|
|
3764
|
+
serialized,
|
|
3765
|
+
messages: chatMessages,
|
|
3766
|
+
runId,
|
|
3767
|
+
parentRunId,
|
|
3768
|
+
extraParams,
|
|
3769
|
+
tags,
|
|
3770
|
+
metadata
|
|
3771
|
+
});
|
|
3772
|
+
const attributes = extractChatModelRequestAttributes(serialized, chatMessages, recordInputs, getInvocationParams(tags), metadata);
|
|
3591
3773
|
const toolDefsJson = recordInputs ? extractToolDefinitions(extraParams) : void 0;
|
|
3592
3774
|
if (toolDefsJson) attributes[_sentry_conventions_attributes.GEN_AI_TOOL_DEFINITIONS] = toolDefsJson;
|
|
3593
3775
|
const modelName = attributes[_sentry_conventions_attributes.GEN_AI_REQUEST_MODEL] || "unknown";
|
|
3594
3776
|
const operationName = typeof attributes[_sentry_conventions_attributes.GEN_AI_OPERATION_NAME] === "string" ? attributes[_sentry_conventions_attributes.GEN_AI_OPERATION_NAME] : "unknown";
|
|
3595
3777
|
const client = (0, _sentry_core.getClient)();
|
|
3596
|
-
startRunSpan(runId, parentRunId, () => (0, _sentry_core.startInactiveSpan)({
|
|
3778
|
+
startRunSpan(runId, parentRunId, recordOutputs, () => (0, _sentry_core.startInactiveSpan)({
|
|
3597
3779
|
name: typeof modelName === "string" && modelName !== "unknown" || !(client && (0, _sentry_core.hasSpanStreamingEnabled)(client)) ? `${operationName} ${modelName}` : operationName,
|
|
3598
3780
|
attributes: {
|
|
3599
3781
|
...getAgentNameFromMetadata(metadata),
|
|
@@ -3603,10 +3785,15 @@ function createLangChainCallbackHandler(options = {}) {
|
|
|
3603
3785
|
}
|
|
3604
3786
|
}));
|
|
3605
3787
|
},
|
|
3606
|
-
|
|
3788
|
+
handleLLMNewToken(_token, _idx, runId) {
|
|
3607
3789
|
const span = runs.get(runId)?.span;
|
|
3608
|
-
if (span
|
|
3609
|
-
|
|
3790
|
+
if (span) recordTimeToFirstChunk(span);
|
|
3791
|
+
},
|
|
3792
|
+
handleLLMEnd(output, runId, _parentRunId, _tags, _extraParams) {
|
|
3793
|
+
const run = runs.get(runId);
|
|
3794
|
+
const span = run?.span;
|
|
3795
|
+
if (run && span?.isRecording()) {
|
|
3796
|
+
const attributes = extractLlmResponseAttributes(output, run.recordOutputs);
|
|
3610
3797
|
if (attributes) span.setAttributes(attributes);
|
|
3611
3798
|
}
|
|
3612
3799
|
exitSpan(runId);
|
|
@@ -3619,12 +3806,30 @@ function createLangChainCallbackHandler(options = {}) {
|
|
|
3619
3806
|
});
|
|
3620
3807
|
exitSpan(runId);
|
|
3621
3808
|
},
|
|
3622
|
-
handleChainStart(chain, inputs, runId, parentRunId,
|
|
3809
|
+
handleChainStart(chain, inputs, runId, parentRunId, tags, metadata, _runType, runName) {
|
|
3623
3810
|
if (isTypeSafeClassifier(chain)) {
|
|
3624
|
-
|
|
3811
|
+
const { recordInputs, recordOutputs } = resolveRecording({
|
|
3812
|
+
operation: "decide",
|
|
3813
|
+
serialized: chain,
|
|
3814
|
+
inputs,
|
|
3815
|
+
runId,
|
|
3816
|
+
parentRunId,
|
|
3817
|
+
tags,
|
|
3818
|
+
metadata
|
|
3819
|
+
});
|
|
3820
|
+
startRunSpan(runId, parentRunId, recordOutputs, () => startTypeSafeClassifierSpan(chain, inputs, metadata, recordInputs)).decide = true;
|
|
3625
3821
|
return;
|
|
3626
3822
|
}
|
|
3627
3823
|
if (metadata?.__sentry_langgraph__) return;
|
|
3824
|
+
const { recordInputs, recordOutputs } = resolveRecording({
|
|
3825
|
+
operation: "invoke_agent",
|
|
3826
|
+
serialized: chain,
|
|
3827
|
+
inputs,
|
|
3828
|
+
runId,
|
|
3829
|
+
parentRunId,
|
|
3830
|
+
tags,
|
|
3831
|
+
metadata
|
|
3832
|
+
});
|
|
3628
3833
|
const chainName = runName || chain.name;
|
|
3629
3834
|
const attributes = {
|
|
3630
3835
|
...getConversationIdFromMetadata(metadata),
|
|
@@ -3634,7 +3839,7 @@ function createLangChainCallbackHandler(options = {}) {
|
|
|
3634
3839
|
if (chainName) attributes[_sentry_conventions_attributes.GEN_AI_PIPELINE_NAME] = chainName;
|
|
3635
3840
|
if (recordInputs) attributes["langchain.chain.inputs"] = JSON.stringify(inputs);
|
|
3636
3841
|
const client = (0, _sentry_core.getClient)();
|
|
3637
|
-
startRunSpan(runId, parentRunId, () => (0, _sentry_core.startInactiveSpan)({
|
|
3842
|
+
startRunSpan(runId, parentRunId, recordOutputs, () => (0, _sentry_core.startInactiveSpan)({
|
|
3638
3843
|
name: !(client && (0, _sentry_core.hasSpanStreamingEnabled)(client)) ? `chain ${chainName || "unknown_chain"}` : chainName ? `invoke_agent ${chainName}` : "invoke_agent",
|
|
3639
3844
|
attributes: {
|
|
3640
3845
|
...attributes,
|
|
@@ -3645,9 +3850,9 @@ function createLangChainCallbackHandler(options = {}) {
|
|
|
3645
3850
|
handleChainEnd(outputs, runId) {
|
|
3646
3851
|
const run = runs.get(runId);
|
|
3647
3852
|
const span = run?.span;
|
|
3648
|
-
if (span?.isRecording()) {
|
|
3649
|
-
if (run
|
|
3650
|
-
else if (recordOutputs) span.setAttributes({ "langchain.chain.outputs": JSON.stringify(outputs) });
|
|
3853
|
+
if (run && span?.isRecording()) {
|
|
3854
|
+
if (run.decide) addTypeSafeClassifierResponseAttributes(span, outputs, run.recordOutputs);
|
|
3855
|
+
else if (run.recordOutputs) span.setAttributes({ "langchain.chain.outputs": JSON.stringify(outputs) });
|
|
3651
3856
|
}
|
|
3652
3857
|
exitSpan(runId);
|
|
3653
3858
|
},
|
|
@@ -3659,8 +3864,17 @@ function createLangChainCallbackHandler(options = {}) {
|
|
|
3659
3864
|
});
|
|
3660
3865
|
exitSpan(runId);
|
|
3661
3866
|
},
|
|
3662
|
-
handleToolStart(tool, input, runId, parentRunId,
|
|
3867
|
+
handleToolStart(tool, input, runId, parentRunId, tags, metadata, runName) {
|
|
3663
3868
|
if (metadata?.__sentry_langgraph__) return;
|
|
3869
|
+
const { recordInputs, recordOutputs } = resolveRecording({
|
|
3870
|
+
operation: "execute_tool",
|
|
3871
|
+
serialized: tool,
|
|
3872
|
+
input,
|
|
3873
|
+
runId,
|
|
3874
|
+
parentRunId,
|
|
3875
|
+
tags,
|
|
3876
|
+
metadata
|
|
3877
|
+
});
|
|
3664
3878
|
const toolName = runName || tool.name || "unknown_tool";
|
|
3665
3879
|
const attributes = {
|
|
3666
3880
|
...getAgentNameFromMetadata(metadata),
|
|
@@ -3670,7 +3884,7 @@ function createLangChainCallbackHandler(options = {}) {
|
|
|
3670
3884
|
[_sentry_conventions_attributes.GEN_AI_TOOL_NAME]: toolName
|
|
3671
3885
|
};
|
|
3672
3886
|
if (recordInputs) attributes[_sentry_conventions_attributes.GEN_AI_TOOL_CALL_ARGUMENTS] = input;
|
|
3673
|
-
startRunSpan(runId, parentRunId, () => (0, _sentry_core.startInactiveSpan)({
|
|
3887
|
+
startRunSpan(runId, parentRunId, recordOutputs, () => (0, _sentry_core.startInactiveSpan)({
|
|
3674
3888
|
name: `execute_tool ${toolName}`,
|
|
3675
3889
|
attributes: {
|
|
3676
3890
|
...attributes,
|
|
@@ -3679,9 +3893,10 @@ function createLangChainCallbackHandler(options = {}) {
|
|
|
3679
3893
|
}));
|
|
3680
3894
|
},
|
|
3681
3895
|
handleToolEnd(output, runId) {
|
|
3682
|
-
const
|
|
3683
|
-
|
|
3684
|
-
|
|
3896
|
+
const run = runs.get(runId);
|
|
3897
|
+
const span = run?.span;
|
|
3898
|
+
if (run && span?.isRecording()) {
|
|
3899
|
+
if (run.recordOutputs) {
|
|
3685
3900
|
const outputObj = output;
|
|
3686
3901
|
const content = outputObj && typeof outputObj === "object" && "content" in outputObj ? outputObj.content : output;
|
|
3687
3902
|
span.setAttributes({ [_sentry_conventions_attributes.GEN_AI_TOOL_CALL_RESULT]: (0, _sentry_core.stringify)(content, String) });
|
|
@@ -3739,6 +3954,14 @@ function extractAgentNameFromParams(args) {
|
|
|
3739
3954
|
if (typeof arg === "object" && !!arg && "name" in arg && typeof arg.name === "string") return arg.name;
|
|
3740
3955
|
return null;
|
|
3741
3956
|
}
|
|
3957
|
+
/** The options that hold outside a graph run, where no `invoke()`/`stream()` call decided: a function needs the graph input. */
|
|
3958
|
+
function getBooleanRecordingOptions(options) {
|
|
3959
|
+
const { recordInputs, recordOutputs } = options;
|
|
3960
|
+
return {
|
|
3961
|
+
recordInputs: typeof recordInputs === "boolean" ? recordInputs : void 0,
|
|
3962
|
+
recordOutputs: typeof recordOutputs === "boolean" ? recordOutputs : void 0
|
|
3963
|
+
};
|
|
3964
|
+
}
|
|
3742
3965
|
/**
|
|
3743
3966
|
* Wraps an array of LangChain tools so each invocation creates a gen_ai.execute_tool span.
|
|
3744
3967
|
*
|
|
@@ -3760,15 +3983,16 @@ function wrapToolsWithSpans(tools, options, agentName) {
|
|
|
3760
3983
|
[_sentry_conventions_attributes.GEN_AI_OPERATION_NAME]: "execute_tool",
|
|
3761
3984
|
[_sentry_conventions_attributes.GEN_AI_TOOL_NAME]: toolName
|
|
3762
3985
|
};
|
|
3763
|
-
const
|
|
3764
|
-
const
|
|
3986
|
+
const metadata = args[1]?.metadata;
|
|
3987
|
+
const { recordInputs, recordOutputs } = getLangGraphRunRecording(metadata) ?? resolveAIRecordingOptions(getBooleanRecordingOptions(options));
|
|
3988
|
+
const callAgentName = metadata?.lc_agent_name ?? agentName;
|
|
3765
3989
|
if (typeof callAgentName === "string") spanAttributes[_sentry_conventions_attributes.GEN_AI_AGENT_NAME] = callAgentName;
|
|
3766
|
-
Object.assign(spanAttributes, getConversationIdFromMetadata(
|
|
3990
|
+
Object.assign(spanAttributes, getConversationIdFromMetadata(metadata));
|
|
3767
3991
|
if (toolDescription) spanAttributes[_sentry_conventions_attributes.GEN_AI_TOOL_DESCRIPTION] = toolDescription;
|
|
3768
3992
|
const input = args[0];
|
|
3769
3993
|
if (typeof input === "object" && !!input) {
|
|
3770
3994
|
if ("id" in input && typeof input.id === "string") spanAttributes[GEN_AI_TOOL_CALL_ID_ATTRIBUTE] = input.id;
|
|
3771
|
-
if (
|
|
3995
|
+
if (recordInputs) {
|
|
3772
3996
|
const toolArgs = "args" in input && typeof input.args === "object" ? input.args : input;
|
|
3773
3997
|
try {
|
|
3774
3998
|
spanAttributes[_sentry_conventions_attributes.GEN_AI_TOOL_CALL_ARGUMENTS] = JSON.stringify(toolArgs);
|
|
@@ -3781,7 +4005,7 @@ function wrapToolsWithSpans(tools, options, agentName) {
|
|
|
3781
4005
|
}, async (span) => {
|
|
3782
4006
|
try {
|
|
3783
4007
|
const result = await Reflect.apply(target, thisArg, args);
|
|
3784
|
-
if (
|
|
4008
|
+
if (recordOutputs) try {
|
|
3785
4009
|
const resultObj = result;
|
|
3786
4010
|
const content = resultObj && typeof resultObj === "object" && "content" in resultObj ? resultObj.content : result;
|
|
3787
4011
|
span.setAttribute(_sentry_conventions_attributes.GEN_AI_TOOL_CALL_RESULT, typeof content === "string" ? content : JSON.stringify(content));
|
|
@@ -3927,8 +4151,16 @@ function getAgentSpanOptions(args, extraAttributes) {
|
|
|
3927
4151
|
}
|
|
3928
4152
|
};
|
|
3929
4153
|
}
|
|
4154
|
+
/** Decides for one `invoke()`/`stream()` call, from its arguments as passed. */
|
|
4155
|
+
function resolveGraphCallRecording(options, args) {
|
|
4156
|
+
return resolveGenAiCallRecording(options, {
|
|
4157
|
+
operation: "invoke_agent",
|
|
4158
|
+
input: args[0],
|
|
4159
|
+
config: args[1]
|
|
4160
|
+
});
|
|
4161
|
+
}
|
|
3930
4162
|
/** Sets the request attributes on an agent span and injects the callback handler into the call's config. */
|
|
3931
|
-
function prepareAgentSpan(span, args, graphInstance, compileOptions,
|
|
4163
|
+
function prepareAgentSpan(span, args, graphInstance, compileOptions, recording, llm, sentryCallbackHandler) {
|
|
3932
4164
|
const graphName = compileOptions?.name;
|
|
3933
4165
|
if (graphName && typeof graphName === "string") {
|
|
3934
4166
|
span.setAttribute(_sentry_conventions_attributes.GEN_AI_PIPELINE_NAME, graphName);
|
|
@@ -3946,12 +4178,12 @@ function prepareAgentSpan(span, args, graphInstance, compileOptions, options, ll
|
|
|
3946
4178
|
config.metadata = {
|
|
3947
4179
|
...getConversationIdMetadataFromConfig(config),
|
|
3948
4180
|
...existingMetadata,
|
|
3949
|
-
__sentry_langgraph__:
|
|
4181
|
+
__sentry_langgraph__: recording,
|
|
3950
4182
|
...typeof graphName === "string" ? { lc_agent_name: graphName } : {}
|
|
3951
4183
|
};
|
|
3952
4184
|
config.callbacks = _INTERNAL_mergeLangChainCallbackHandler(config.callbacks, sentryCallbackHandler);
|
|
3953
4185
|
}
|
|
3954
|
-
const recordInputs =
|
|
4186
|
+
const { recordInputs } = recording;
|
|
3955
4187
|
const tools = recordInputs ? extractToolsFromCompiledGraph(graphInstance) : null;
|
|
3956
4188
|
if (tools) span.setAttribute(_sentry_conventions_attributes.GEN_AI_TOOL_DEFINITIONS, JSON.stringify(tools));
|
|
3957
4189
|
const inputState = args[0];
|
|
@@ -3979,6 +4211,7 @@ function prepareAgentSpan(span, args, graphInstance, compileOptions, options, ll
|
|
|
3979
4211
|
function instrumentCompiledGraphStream(originalStream, graphInstance, compileOptions, options, llm, sentryCallbackHandler) {
|
|
3980
4212
|
return new Proxy(originalStream, { apply(target, thisArg, args) {
|
|
3981
4213
|
if (isInvokeInternalStream(graphInstance)) return Reflect.apply(target, thisArg, args);
|
|
4214
|
+
const recording = resolveGraphCallRecording(options, args);
|
|
3982
4215
|
return (0, _sentry_core.startSpanManual)(getAgentSpanOptions(args, { [_sentry_conventions_attributes.GEN_AI_RESPONSE_STREAMING]: true }), async (span) => {
|
|
3983
4216
|
let inputMessages = null;
|
|
3984
4217
|
let stream;
|
|
@@ -3992,7 +4225,7 @@ function instrumentCompiledGraphStream(originalStream, graphInstance, compileOpt
|
|
|
3992
4225
|
code: _sentry_core.SPAN_STATUS_ERROR,
|
|
3993
4226
|
message: "internal_error"
|
|
3994
4227
|
});
|
|
3995
|
-
} else if (outcome &&
|
|
4228
|
+
} else if (outcome && recording.recordOutputs) setResponseAttributes(span, inputMessages, outcome.outputs);
|
|
3996
4229
|
} catch {}
|
|
3997
4230
|
span.end();
|
|
3998
4231
|
inputMessages = null;
|
|
@@ -4015,7 +4248,7 @@ function instrumentCompiledGraphStream(originalStream, graphInstance, compileOpt
|
|
|
4015
4248
|
}
|
|
4016
4249
|
};
|
|
4017
4250
|
try {
|
|
4018
|
-
inputMessages = prepareAgentSpan(span, args, graphInstance, compileOptions,
|
|
4251
|
+
inputMessages = prepareAgentSpan(span, args, graphInstance, compileOptions, recording, llm, sentryCallbackHandler);
|
|
4019
4252
|
const config = args[1];
|
|
4020
4253
|
config.callbacks = _INTERNAL_addLangChainCallbackHandler(config.callbacks, listener);
|
|
4021
4254
|
stream = await Reflect.apply(target, thisArg, args);
|
|
@@ -4071,10 +4304,9 @@ function runManually(callback) {
|
|
|
4071
4304
|
* Instruments StateGraph's compile method to wrap the returned compiled graph's invoke() and stream() with a
|
|
4072
4305
|
* `gen_ai.invoke_agent` span.
|
|
4073
4306
|
*/
|
|
4074
|
-
function instrumentStateGraphCompile(originalCompile,
|
|
4307
|
+
function instrumentStateGraphCompile(originalCompile, options) {
|
|
4075
4308
|
if (Object.prototype.hasOwnProperty.call(originalCompile, SENTRY_PATCHED)) return originalCompile;
|
|
4076
|
-
const
|
|
4077
|
-
const sentryHandler = createLangChainCallbackHandler(options);
|
|
4309
|
+
const sentryHandler = createLangChainCallbackHandler(getBooleanRecordingOptions(options));
|
|
4078
4310
|
const wrapped = new Proxy(originalCompile, { apply(target, thisArg, args) {
|
|
4079
4311
|
if (_insideManualInstrumentation) return Reflect.apply(target, thisArg, args);
|
|
4080
4312
|
const compiledGraph = runManually(() => Reflect.apply(target, thisArg, args));
|
|
@@ -4105,11 +4337,12 @@ function instrumentCompiledGraph(graph, compileOptions, options, llm, sentryCall
|
|
|
4105
4337
|
*/
|
|
4106
4338
|
function instrumentCompiledGraphInvoke(originalInvoke, graphInstance, compileOptions, options, llm, sentryCallbackHandler) {
|
|
4107
4339
|
return new Proxy(originalInvoke, { apply(target, thisArg, args) {
|
|
4340
|
+
const recording = resolveGraphCallRecording(options, args);
|
|
4108
4341
|
return (0, _sentry_core.startSpan)(getAgentSpanOptions(args), async (span) => {
|
|
4109
4342
|
try {
|
|
4110
|
-
const inputMessages = prepareAgentSpan(span, args, graphInstance, compileOptions,
|
|
4343
|
+
const inputMessages = prepareAgentSpan(span, args, graphInstance, compileOptions, recording, llm, sentryCallbackHandler);
|
|
4111
4344
|
const result = await callInvoke(graphInstance, () => Reflect.apply(target, thisArg, args));
|
|
4112
|
-
if (
|
|
4345
|
+
if (recording.recordOutputs) setResponseAttributes(span, inputMessages, result);
|
|
4113
4346
|
return result;
|
|
4114
4347
|
} catch (error) {
|
|
4115
4348
|
span.setStatus({
|
|
@@ -4124,17 +4357,16 @@ function instrumentCompiledGraphInvoke(originalInvoke, graphInstance, compileOpt
|
|
|
4124
4357
|
/**
|
|
4125
4358
|
* Instruments createReactAgent to create invoke_agent and execute_tool spans.
|
|
4126
4359
|
*/
|
|
4127
|
-
function instrumentCreateReactAgent(originalCreateReactAgent, options) {
|
|
4360
|
+
function instrumentCreateReactAgent(originalCreateReactAgent, options = {}) {
|
|
4128
4361
|
if (Object.prototype.hasOwnProperty.call(originalCreateReactAgent, SENTRY_PATCHED)) return originalCreateReactAgent;
|
|
4129
|
-
const
|
|
4130
|
-
const sentryHandler = createLangChainCallbackHandler(resolvedOptions);
|
|
4362
|
+
const sentryHandler = createLangChainCallbackHandler(getBooleanRecordingOptions(options));
|
|
4131
4363
|
const wrapped = new Proxy(originalCreateReactAgent, { apply(target, thisArg, args) {
|
|
4132
4364
|
const llm = extractLLMFromParams(args);
|
|
4133
4365
|
const agentName = extractAgentNameFromParams(args);
|
|
4134
4366
|
const params = args[0];
|
|
4135
|
-
if (params && Array.isArray(params.tools) && params.tools.length > 0) wrapToolsWithSpans(params.tools,
|
|
4367
|
+
if (params && Array.isArray(params.tools) && params.tools.length > 0) wrapToolsWithSpans(params.tools, options, agentName ?? void 0);
|
|
4136
4368
|
const compiledGraph = runManually(() => Reflect.apply(target, thisArg, args));
|
|
4137
|
-
instrumentCompiledGraph(compiledGraph, agentName ? { name: agentName } : {},
|
|
4369
|
+
instrumentCompiledGraph(compiledGraph, agentName ? { name: agentName } : {}, options, llm, sentryHandler);
|
|
4138
4370
|
return compiledGraph;
|
|
4139
4371
|
} });
|
|
4140
4372
|
Object.defineProperty(wrapped, SENTRY_PATCHED, {
|
|
@@ -4166,8 +4398,8 @@ function instrumentCreateReactAgent(originalCreateReactAgent, options) {
|
|
|
4166
4398
|
* const compiled = graph.compile({ name: 'my_agent' });
|
|
4167
4399
|
* ```
|
|
4168
4400
|
*/
|
|
4169
|
-
function instrumentStateGraph(stateGraph, options) {
|
|
4170
|
-
stateGraph.compile = instrumentStateGraphCompile(stateGraph.compile,
|
|
4401
|
+
function instrumentStateGraph(stateGraph, options = {}) {
|
|
4402
|
+
stateGraph.compile = instrumentStateGraphCompile(stateGraph.compile, options);
|
|
4171
4403
|
return stateGraph;
|
|
4172
4404
|
}
|
|
4173
4405
|
// src/ai/mastra/constants.ts
|
|
@@ -4309,10 +4541,7 @@ function mergeUsageAttributes(existing, incoming) {
|
|
|
4309
4541
|
/** Convention attributes only. Called on start and end — Mastra fills fields progressively. */
|
|
4310
4542
|
function getSpanAttributes(span, options) {
|
|
4311
4543
|
const operationName = getOperation(span.type)?.operationName;
|
|
4312
|
-
const attributes = {
|
|
4313
|
-
[_sentry_conventions_attributes.GEN_AI_OPERATION_NAME]: operationName,
|
|
4314
|
-
[_sentry_conventions_attributes.GEN_AI_CONVERSATION_ID]: span.attributes?.conversationId ?? span.metadata?.threadId
|
|
4315
|
-
};
|
|
4544
|
+
const attributes = { [_sentry_conventions_attributes.GEN_AI_OPERATION_NAME]: operationName };
|
|
4316
4545
|
if (AGENT_SPAN_TYPES.has(span.type)) addAgentAttributes(attributes, span, options);
|
|
4317
4546
|
if (MODEL_SPAN_TYPES.has(span.type) || span.type === "rag_embedding") addModelAttributes(attributes, span, options);
|
|
4318
4547
|
if (TOOL_SPAN_TYPES.has(span.type)) addToolAttributes(attributes, span, options);
|
|
@@ -4343,6 +4572,7 @@ function addModelAttributes(attributes, span, options) {
|
|
|
4343
4572
|
attributes[_sentry_conventions_attributes.GEN_AI_RESPONSE_MODEL] = attrs.responseModel;
|
|
4344
4573
|
attributes[_sentry_conventions_attributes.GEN_AI_RESPONSE_ID] = attrs.responseId;
|
|
4345
4574
|
attributes[_sentry_conventions_attributes.GEN_AI_RESPONSE_STREAMING] = attrs.streaming;
|
|
4575
|
+
attributes[_sentry_conventions_attributes.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK] = attrs.completionStartTime ? (attrs.completionStartTime.getTime() - span.startTime.getTime()) / 1e3 : void 0;
|
|
4346
4576
|
attributes[_sentry_conventions_attributes.GEN_AI_RESPONSE_FINISH_REASONS] = attrs.finishReason ? JSON.stringify([attrs.finishReason]) : void 0;
|
|
4347
4577
|
attributes[_sentry_conventions_attributes.GEN_AI_AGENT_NAME] = span.entityName;
|
|
4348
4578
|
attributes[_sentry_conventions_attributes.GEN_AI_REQUEST_TEMPERATURE] = parameters.temperature;
|
|
@@ -4467,7 +4697,7 @@ var SentryMastraExporter = class {
|
|
|
4467
4697
|
/** End open spans, then flush. Does not close the Sentry client. */
|
|
4468
4698
|
async shutdown() {
|
|
4469
4699
|
for (const id of this._spans.keys()) unregisterMastraSpan(id);
|
|
4470
|
-
for (const
|
|
4700
|
+
for (const tracked of this._spans.values()) this._endSpan(tracked, {});
|
|
4471
4701
|
this._spans.clear();
|
|
4472
4702
|
this._skipped.clear();
|
|
4473
4703
|
await this.flush();
|
|
@@ -4480,18 +4710,19 @@ var SentryMastraExporter = class {
|
|
|
4480
4710
|
else if (event.type === "span_ended") this._skipped.remove(span.id);
|
|
4481
4711
|
return;
|
|
4482
4712
|
}
|
|
4483
|
-
const recordingOptions = resolveAIRecordingOptions(this._options);
|
|
4484
4713
|
switch (event.type) {
|
|
4485
4714
|
case "span_started":
|
|
4486
|
-
this._onSpanStarted(span
|
|
4715
|
+
this._onSpanStarted(span);
|
|
4487
4716
|
break;
|
|
4488
4717
|
case "span_updated":
|
|
4489
|
-
this._onSpanUpdated(span
|
|
4718
|
+
this._onSpanUpdated(span);
|
|
4490
4719
|
break;
|
|
4491
|
-
case "span_ended": this._onSpanEnded(span
|
|
4720
|
+
case "span_ended": this._onSpanEnded(span);
|
|
4492
4721
|
}
|
|
4493
4722
|
}
|
|
4494
|
-
_onSpanStarted(span
|
|
4723
|
+
_onSpanStarted(span) {
|
|
4724
|
+
const call = { span };
|
|
4725
|
+
const recordingOptions = resolveGenAiCallRecording(this._options, call);
|
|
4495
4726
|
const parentId = this._resolveParentId(span.parentSpanId);
|
|
4496
4727
|
const parentSpan = parentId ? this._spans.get(parentId)?.span : void 0;
|
|
4497
4728
|
const activeSpan = (0, _sentry_core.getActiveSpan)();
|
|
@@ -4501,10 +4732,12 @@ var SentryMastraExporter = class {
|
|
|
4501
4732
|
parentSpan: parentSpan ?? activeSpan,
|
|
4502
4733
|
attributes: {
|
|
4503
4734
|
...getSpanAttributes(span, recordingOptions),
|
|
4735
|
+
[_sentry_conventions_attributes.GEN_AI_CONVERSATION_ID]: span.attributes?.conversationId ?? span.metadata?.threadId,
|
|
4504
4736
|
[_sentry_conventions_attributes.SENTRY_OP]: getOperation(span.type)?.op,
|
|
4505
4737
|
[_sentry_conventions_attributes.SENTRY_ORIGIN]: MASTRA_ORIGIN
|
|
4506
4738
|
}
|
|
4507
4739
|
});
|
|
4740
|
+
runGenAiSpanStart(this._options, sentrySpan, call);
|
|
4508
4741
|
const evaluation = span.type === "classifier_evaluation" ? takeStartingClassifierEvaluation() : void 0;
|
|
4509
4742
|
if (evaluation) Object.assign(evaluation, {
|
|
4510
4743
|
span: sentrySpan,
|
|
@@ -4515,37 +4748,47 @@ var SentryMastraExporter = class {
|
|
|
4515
4748
|
});
|
|
4516
4749
|
this._trackSpan(span.id, {
|
|
4517
4750
|
span: sentrySpan,
|
|
4751
|
+
call,
|
|
4518
4752
|
spanType: span.type,
|
|
4519
4753
|
usage: {},
|
|
4754
|
+
recordingOptions,
|
|
4520
4755
|
evaluation
|
|
4521
4756
|
});
|
|
4522
4757
|
}
|
|
4523
4758
|
/** Track a started span, ending any Sentry span that would otherwise be dropped without `end()`. */
|
|
4524
4759
|
_trackSpan(id, tracked) {
|
|
4525
|
-
this.
|
|
4760
|
+
this._endTracked(id);
|
|
4526
4761
|
if (this._spans.size >= 1e3) {
|
|
4527
4762
|
const oldestId = this._spans.keys()[0];
|
|
4528
|
-
if (oldestId !== void 0) this.
|
|
4763
|
+
if (oldestId !== void 0) this._endTracked(oldestId);
|
|
4529
4764
|
}
|
|
4530
4765
|
this._spans.set(id, tracked);
|
|
4531
4766
|
registerMastraSpan(id, tracked.span);
|
|
4532
4767
|
}
|
|
4768
|
+
_endTracked(id) {
|
|
4769
|
+
const tracked = this._removeTracked(id);
|
|
4770
|
+
if (tracked) this._endSpan(tracked, {});
|
|
4771
|
+
}
|
|
4772
|
+
_endSpan(tracked, result, endTime) {
|
|
4773
|
+
runGenAiSpanEnd(this._options, tracked.span, tracked.call, result);
|
|
4774
|
+
tracked.span.end(endTime);
|
|
4775
|
+
}
|
|
4533
4776
|
/** Remove a tracked span and keep the id→span registry in lockstep. */
|
|
4534
4777
|
_removeTracked(id) {
|
|
4535
4778
|
unregisterMastraSpan(id);
|
|
4536
4779
|
return this._spans.remove(id);
|
|
4537
4780
|
}
|
|
4538
|
-
_onSpanUpdated(span
|
|
4781
|
+
_onSpanUpdated(span) {
|
|
4539
4782
|
const tracked = this._spans.get(span.id);
|
|
4540
|
-
if (tracked) tracked.span.setAttributes(getSpanAttributes(span, recordingOptions));
|
|
4783
|
+
if (tracked) tracked.span.setAttributes(getSpanAttributes(span, tracked.recordingOptions));
|
|
4541
4784
|
}
|
|
4542
|
-
_onSpanEnded(span
|
|
4785
|
+
_onSpanEnded(span) {
|
|
4543
4786
|
const tracked = this._spans.get(span.id);
|
|
4544
4787
|
if (!tracked) {
|
|
4545
4788
|
require_debug_build.DEBUG_BUILD && _sentry_core.debug.warn(`[Mastra] no Sentry span open for ended span ${span.id} (${span.name})`);
|
|
4546
4789
|
return;
|
|
4547
4790
|
}
|
|
4548
|
-
const { span: sentrySpan } = tracked;
|
|
4791
|
+
const { span: sentrySpan, recordingOptions } = tracked;
|
|
4549
4792
|
sentrySpan.setAttributes(getSpanAttributes(span, recordingOptions));
|
|
4550
4793
|
sentrySpan.updateName(getSpanName(span));
|
|
4551
4794
|
if (MODEL_SPAN_TYPES.has(span.type)) this._rollUpToParentAgent(span);
|
|
@@ -4553,9 +4796,12 @@ var SentryMastraExporter = class {
|
|
|
4553
4796
|
code: _sentry_core.SPAN_STATUS_ERROR,
|
|
4554
4797
|
message: span.errorInfo.message
|
|
4555
4798
|
});
|
|
4799
|
+
const result = span.errorInfo ? { error: span.errorInfo } : { response: span };
|
|
4556
4800
|
const { evaluation } = tracked;
|
|
4557
|
-
if (evaluation && !evaluation.settled)
|
|
4558
|
-
|
|
4801
|
+
if (evaluation && !evaluation.settled) {
|
|
4802
|
+
const endTime = span.endTime ?? (0, _sentry_core.timestampInSeconds)();
|
|
4803
|
+
evaluation.end = () => this._endSpan(tracked, result, endTime);
|
|
4804
|
+
} else this._endSpan(tracked, result, span.endTime);
|
|
4559
4805
|
this._removeTracked(span.id);
|
|
4560
4806
|
}
|
|
4561
4807
|
/** Copy model/usage onto the parent agent so the AI Agents view shows totals. */
|
|
@@ -4636,7 +4882,7 @@ function detectOrchestrionSetup() {
|
|
|
4636
4882
|
if (!marker.warnedBunRunNotInjected) {
|
|
4637
4883
|
marker.warnedBunRunNotInjected = true;
|
|
4638
4884
|
(0, _sentry_core.consoleSandbox)(() => {
|
|
4639
|
-
console.warn("[Sentry] No diagnostics-channel injection detected. Channel-based integrations will not record spans or capture errors
|
|
4885
|
+
console.warn("[Sentry] No diagnostics-channel injection detected. Channel-based integrations will not record spans or capture errors. On Bun 1.4.6 or later, run your app with `bun --preload @sentry/bun/import`. Or build your app with `@sentry/bun/plugin`.");
|
|
4640
4886
|
});
|
|
4641
4887
|
}
|
|
4642
4888
|
return;
|
|
@@ -4648,26 +4894,44 @@ function detectOrchestrionSetup() {
|
|
|
4648
4894
|
_sentry_core.debug.log(bundler ? `[Sentry] Bundler plugin ran, injected libraries=${JSON.stringify(bundler instanceof Set ? [...bundler] : bundler)}` : "[Sentry] Bundler plugin did not run");
|
|
4649
4895
|
}
|
|
4650
4896
|
// src/ai/pi-ai/recording.ts
|
|
4651
|
-
const
|
|
4652
|
-
/**
|
|
4653
|
-
* Apply the recording options of an integration that sends its model calls through
|
|
4654
|
-
* Flue, to the `gen_ai.chat` spans of the requests sent under `scope`.
|
|
4655
|
-
|
|
4656
|
-
|
|
4657
|
-
|
|
4897
|
+
const DRIVER_KEY = "piAiDriver";
|
|
4898
|
+
/**
|
|
4899
|
+
* Apply the recording options and span hooks of an integration that sends its model calls through
|
|
4900
|
+
* pi-ai, such as Flue, to the `gen_ai.chat` spans of the requests sent under `scope`. Options and
|
|
4901
|
+
* hooks that are functions get `call`.
|
|
4902
|
+
*/
|
|
4903
|
+
function setPiAiDriverOptions(scope, options, call) {
|
|
4904
|
+
const driver = {
|
|
4905
|
+
recording: () => ({
|
|
4906
|
+
recordInputs: resolveRecordingOption(options.recordInputs, call),
|
|
4907
|
+
recordOutputs: resolveRecordingOption(options.recordOutputs, call)
|
|
4908
|
+
}),
|
|
4909
|
+
onSpanStart: (span) => runGenAiSpanStart(options, span, call),
|
|
4910
|
+
onSpanEnd: (span, result) => runGenAiSpanEnd(options, span, call, result)
|
|
4911
|
+
};
|
|
4912
|
+
scope.setSDKProcessingMetadata({ [DRIVER_KEY]: driver });
|
|
4913
|
+
}
|
|
4914
|
+
/** The integration driving the pi-ai requests sent from the current scope, if any. */
|
|
4915
|
+
function getPiAiDriver() {
|
|
4916
|
+
return (0, _sentry_core.getCurrentScope)().getScopeData().sdkProcessingMetadata[DRIVER_KEY];
|
|
4658
4917
|
}
|
|
4659
4918
|
/**
|
|
4660
4919
|
* The recording options of a pi-ai request, read when it starts: its response can settle outside the
|
|
4661
|
-
* scope it was sent from. An explicit `false` on either integration
|
|
4662
|
-
* opt-out must not depend on which integration it was set on.
|
|
4663
|
-
* pi-ai comes first, then `piAiIntegration`, then
|
|
4664
|
-
|
|
4665
|
-
|
|
4666
|
-
|
|
4667
|
-
const
|
|
4920
|
+
* scope it was sent from. An explicit `false` on either integration, or from either one's function,
|
|
4921
|
+
* keeps the content out, since an opt-out must not depend on which integration it was set on.
|
|
4922
|
+
* Otherwise the integration driving pi-ai comes first, then `piAiIntegration`, then
|
|
4923
|
+
* `dataCollection.genAI`.
|
|
4924
|
+
*/
|
|
4925
|
+
function resolvePiAiRecordingOptions(options, call, driver = getPiAiDriver()) {
|
|
4926
|
+
const driverRecording = driver?.recording();
|
|
4927
|
+
const own = {
|
|
4928
|
+
recordInputs: resolveRecordingOption(options.recordInputs, call),
|
|
4929
|
+
recordOutputs: resolveRecordingOption(options.recordOutputs, call)
|
|
4930
|
+
};
|
|
4931
|
+
const { recordInputs, recordOutputs } = resolveGenAiCallRecording(own, call);
|
|
4668
4932
|
return {
|
|
4669
|
-
recordInputs:
|
|
4670
|
-
recordOutputs:
|
|
4933
|
+
recordInputs: own.recordInputs !== false && (driverRecording?.recordInputs ?? recordInputs),
|
|
4934
|
+
recordOutputs: own.recordOutputs !== false && (driverRecording?.recordOutputs ?? recordOutputs)
|
|
4671
4935
|
};
|
|
4672
4936
|
}
|
|
4673
4937
|
// src/ai/flue/constants.ts
|
|
@@ -4837,16 +5101,8 @@ function piAiPartToGenAiPart(part) {
|
|
|
4837
5101
|
}
|
|
4838
5102
|
}
|
|
4839
5103
|
// src/ai/flue/utils.ts
|
|
4840
|
-
|
|
4841
|
-
|
|
4842
|
-
* Sentry's own propagation uses `sentry-trace`, so the carrier has to be converted before it can
|
|
4843
|
-
* continue the trace. Kept local until something else needs a W3C parser.
|
|
4844
|
-
*/
|
|
4845
|
-
function sentryTraceFromTraceparent(traceparent) {
|
|
4846
|
-
const [version, traceId, spanId, flags] = traceparent.split("-");
|
|
4847
|
-
if (version !== "00" || !traceId || !spanId || !flags) return;
|
|
4848
|
-
return `${traceId}-${spanId}-${parseInt(flags, 16) % 2 === 1 ? "1" : "0"}`;
|
|
4849
|
-
}
|
|
5104
|
+
const TOOL_SPANS_RECORDING_OUTPUTS = /* @__PURE__ */ new WeakSet();
|
|
5105
|
+
const TOOL_SPAN_END_HOOKS = /* @__PURE__ */ new WeakMap();
|
|
4850
5106
|
/**
|
|
4851
5107
|
* Store a span under `key`. Callers only reach this with a key the tracker does not hold, so the
|
|
4852
5108
|
* one span at risk of being dropped without `end()` is the oldest entry, which `LRUMap.set` silently
|
|
@@ -4887,10 +5143,16 @@ function captureToolError(span, errorInfo) {
|
|
|
4887
5143
|
* OpenTelemetry adapter projects them: siblings of `chat`, correlated to model output by tool call
|
|
4888
5144
|
* id. Keyed by `toolCallId` so concurrent tool calls in one turn cannot cross-attribute.
|
|
4889
5145
|
*/
|
|
4890
|
-
function startToolSpan(observation, toolSpans,
|
|
5146
|
+
function startToolSpan(observation, context, toolSpans, options) {
|
|
4891
5147
|
const { toolCallId, toolName } = observation;
|
|
4892
5148
|
if (!toolCallId || toolSpans.get(toolCallId)) return;
|
|
4893
|
-
|
|
5149
|
+
const call = {
|
|
5150
|
+
operation: "execute_tool",
|
|
5151
|
+
observation,
|
|
5152
|
+
context
|
|
5153
|
+
};
|
|
5154
|
+
const { recordInputs, recordOutputs } = resolveGenAiCallRecording(options, call);
|
|
5155
|
+
const span = (0, _sentry_core.startInactiveSpan)({
|
|
4894
5156
|
name: `execute_tool ${toolName ?? "unknown"}`,
|
|
4895
5157
|
op: getGenAiSpanOp("execute_tool"),
|
|
4896
5158
|
attributes: {
|
|
@@ -4900,14 +5162,21 @@ function startToolSpan(observation, toolSpans, recordInputs) {
|
|
|
4900
5162
|
[_sentry_conventions_attributes.GEN_AI_CONVERSATION_ID]: observation.conversationId,
|
|
4901
5163
|
...recordInputs && observation.args !== void 0 ? { [_sentry_conventions_attributes.GEN_AI_TOOL_CALL_ARGUMENTS]: (0, _sentry_core.stringify)(observation.args) } : {}
|
|
4902
5164
|
}
|
|
4903
|
-
})
|
|
4904
|
-
|
|
4905
|
-
|
|
5165
|
+
});
|
|
5166
|
+
if (recordOutputs) TOOL_SPANS_RECORDING_OUTPUTS.add(span);
|
|
5167
|
+
runGenAiSpanStart(options, span, call);
|
|
5168
|
+
TOOL_SPAN_END_HOOKS.set(span, (end) => runGenAiSpanEnd(options, span, call, end.isError ? {
|
|
5169
|
+
response: end,
|
|
5170
|
+
error: end.errorInfo
|
|
5171
|
+
} : { response: end }));
|
|
5172
|
+
trackSpan(toolSpans, toolCallId, span);
|
|
5173
|
+
}
|
|
5174
|
+
function endToolSpan(observation, toolSpans) {
|
|
4906
5175
|
const { toolCallId } = observation;
|
|
4907
5176
|
const span = toolCallId ? toolSpans.get(toolCallId) : void 0;
|
|
4908
5177
|
if (!span || !toolCallId) return;
|
|
4909
5178
|
toolSpans.remove(toolCallId);
|
|
4910
|
-
if (
|
|
5179
|
+
if (TOOL_SPANS_RECORDING_OUTPUTS.has(span)) {
|
|
4911
5180
|
const result = toolResultForModel(observation);
|
|
4912
5181
|
if (result !== void 0) span.setAttribute(_sentry_conventions_attributes.GEN_AI_TOOL_CALL_RESULT, result);
|
|
4913
5182
|
}
|
|
@@ -4918,6 +5187,7 @@ function endToolSpan(observation, toolSpans, recordOutputs) {
|
|
|
4918
5187
|
});
|
|
4919
5188
|
captureToolError(span, observation.errorInfo);
|
|
4920
5189
|
}
|
|
5190
|
+
TOOL_SPAN_END_HOOKS.get(span)?.(observation);
|
|
4921
5191
|
span.end();
|
|
4922
5192
|
}
|
|
4923
5193
|
/**
|
|
@@ -4950,7 +5220,7 @@ function warnIfPiAiNotInstrumented() {
|
|
|
4950
5220
|
});
|
|
4951
5221
|
}
|
|
4952
5222
|
function getPiAiInstrumentationHint() {
|
|
4953
|
-
if (isBun()) return "
|
|
5223
|
+
if (isBun()) return "On Bun 1.4.6 or later, run your app with `bun --preload @sentry/bun/import app.ts`. Or build it with `sentryBunPlugin()` from `@sentry/bun/plugin`.";
|
|
4954
5224
|
if (isDeno()) return "Run your app with `deno run --preload=npm:@sentry/deno/import app.ts`.";
|
|
4955
5225
|
if (isCloudflareWorkers()) return "Add `sentryCloudflareVitePlugin()` from `@sentry/cloudflare/vite` to your Vite config.";
|
|
4956
5226
|
return "Load Sentry with `--import`, for example `node --import ./instrument.mjs app.mjs`.";
|
|
@@ -4979,8 +5249,8 @@ function runInAdmissionContext(admission, next) {
|
|
|
4979
5249
|
* as a `gen_ai.chat` span under the agent span.
|
|
4980
5250
|
*
|
|
4981
5251
|
* Tool arguments and results are gated on `recordInputs`/`recordOutputs`, which fall back to the
|
|
4982
|
-
* current client's `dataCollection.genAI` settings and are
|
|
4983
|
-
* the message content of the `gen_ai.chat` spans.
|
|
5252
|
+
* current client's `dataCollection.genAI` settings and are decided when each tool span starts. The
|
|
5253
|
+
* same options apply to the message content of the `gen_ai.chat` spans.
|
|
4984
5254
|
*/
|
|
4985
5255
|
function createFlueInstrumentation(options = {}) {
|
|
4986
5256
|
const agentSpans = /* @__PURE__ */ new Map();
|
|
@@ -4998,7 +5268,11 @@ function createFlueInstrumentation(options = {}) {
|
|
|
4998
5268
|
const { conversationId } = ctx;
|
|
4999
5269
|
return (0, _sentry_core.withScope)((scope) => {
|
|
5000
5270
|
if (conversationId) scope.setConversationId(conversationId);
|
|
5001
|
-
|
|
5271
|
+
setPiAiDriverOptions(scope, options, {
|
|
5272
|
+
operation: "chat",
|
|
5273
|
+
execution: operation,
|
|
5274
|
+
context: ctx
|
|
5275
|
+
});
|
|
5002
5276
|
return next();
|
|
5003
5277
|
});
|
|
5004
5278
|
}
|
|
@@ -5023,14 +5297,13 @@ function createFlueInstrumentation(options = {}) {
|
|
|
5023
5297
|
agentSpans.delete(operationId);
|
|
5024
5298
|
}
|
|
5025
5299
|
});
|
|
5026
|
-
const sentryTrace = ctx.traceCarrier?.traceparent ? sentryTraceFromTraceparent(ctx.traceCarrier.traceparent) : void 0;
|
|
5300
|
+
const sentryTrace = ctx.traceCarrier?.traceparent ? (0, _sentry_core.sentryTraceFromTraceparent)(ctx.traceCarrier.traceparent) : void 0;
|
|
5027
5301
|
return sentryTrace && !(0, _sentry_core.getActiveSpan)() ? (0, _sentry_core.continueTrace)({
|
|
5028
5302
|
sentryTrace,
|
|
5029
5303
|
baggage: void 0
|
|
5030
5304
|
}, openAgentSpan) : openAgentSpan();
|
|
5031
5305
|
},
|
|
5032
|
-
observe: (observation) => {
|
|
5033
|
-
const { recordInputs, recordOutputs } = resolveAIRecordingOptions(options);
|
|
5306
|
+
observe: (observation, ctx) => {
|
|
5034
5307
|
const agentSpan = observation.operationId ? agentSpans.get(observation.operationId) : void 0;
|
|
5035
5308
|
if (agentSpan) {
|
|
5036
5309
|
if (observation.conversationId) agentSpan.setAttribute(_sentry_conventions_attributes.GEN_AI_CONVERSATION_ID, observation.conversationId);
|
|
@@ -5050,10 +5323,10 @@ function createFlueInstrumentation(options = {}) {
|
|
|
5050
5323
|
if (observation.submissionId) admissions.remove(observation.submissionId);
|
|
5051
5324
|
return;
|
|
5052
5325
|
case "tool_start":
|
|
5053
|
-
startToolSpan(observation, toolSpans,
|
|
5326
|
+
startToolSpan(observation, ctx, toolSpans, options);
|
|
5054
5327
|
return;
|
|
5055
5328
|
case "tool":
|
|
5056
|
-
endToolSpan(observation, toolSpans
|
|
5329
|
+
endToolSpan(observation, toolSpans);
|
|
5057
5330
|
return;
|
|
5058
5331
|
default: return;
|
|
5059
5332
|
}
|
|
@@ -6604,6 +6877,12 @@ Object.defineProperty(exports, "getAbortSignal", {
|
|
|
6604
6877
|
return getAbortSignal;
|
|
6605
6878
|
}
|
|
6606
6879
|
});
|
|
6880
|
+
Object.defineProperty(exports, "getBooleanRecordingOptions", {
|
|
6881
|
+
enumerable: true,
|
|
6882
|
+
get: function() {
|
|
6883
|
+
return getBooleanRecordingOptions;
|
|
6884
|
+
}
|
|
6885
|
+
});
|
|
6607
6886
|
Object.defineProperty(exports, "getCallAbortSignal", {
|
|
6608
6887
|
enumerable: true,
|
|
6609
6888
|
get: function() {
|
|
@@ -6682,6 +6961,12 @@ Object.defineProperty(exports, "getOtlpTracesEndpoint", {
|
|
|
6682
6961
|
return getOtlpTracesEndpoint;
|
|
6683
6962
|
}
|
|
6684
6963
|
});
|
|
6964
|
+
Object.defineProperty(exports, "getPiAiDriver", {
|
|
6965
|
+
enumerable: true,
|
|
6966
|
+
get: function() {
|
|
6967
|
+
return getPiAiDriver;
|
|
6968
|
+
}
|
|
6969
|
+
});
|
|
6685
6970
|
Object.defineProperty(exports, "getProviderName", {
|
|
6686
6971
|
enumerable: true,
|
|
6687
6972
|
get: function() {
|
|
@@ -6958,16 +7243,16 @@ Object.defineProperty(exports, "processClassifierEvaluationData", {
|
|
|
6958
7243
|
return processClassifierEvaluationData;
|
|
6959
7244
|
}
|
|
6960
7245
|
});
|
|
6961
|
-
Object.defineProperty(exports, "
|
|
7246
|
+
Object.defineProperty(exports, "recordTimeToFirstChunk", {
|
|
6962
7247
|
enumerable: true,
|
|
6963
7248
|
get: function() {
|
|
6964
|
-
return
|
|
7249
|
+
return recordTimeToFirstChunk;
|
|
6965
7250
|
}
|
|
6966
7251
|
});
|
|
6967
|
-
Object.defineProperty(exports, "
|
|
7252
|
+
Object.defineProperty(exports, "recordTypeSafeClassifierState", {
|
|
6968
7253
|
enumerable: true,
|
|
6969
7254
|
get: function() {
|
|
6970
|
-
return
|
|
7255
|
+
return recordTypeSafeClassifierState;
|
|
6971
7256
|
}
|
|
6972
7257
|
});
|
|
6973
7258
|
Object.defineProperty(exports, "resolveChatParams", {
|
|
@@ -6976,12 +7261,36 @@ Object.defineProperty(exports, "resolveChatParams", {
|
|
|
6976
7261
|
return resolveChatParams;
|
|
6977
7262
|
}
|
|
6978
7263
|
});
|
|
7264
|
+
Object.defineProperty(exports, "resolveGenAiCallRecording", {
|
|
7265
|
+
enumerable: true,
|
|
7266
|
+
get: function() {
|
|
7267
|
+
return resolveGenAiCallRecording;
|
|
7268
|
+
}
|
|
7269
|
+
});
|
|
6979
7270
|
Object.defineProperty(exports, "resolvePiAiRecordingOptions", {
|
|
6980
7271
|
enumerable: true,
|
|
6981
7272
|
get: function() {
|
|
6982
7273
|
return resolvePiAiRecordingOptions;
|
|
6983
7274
|
}
|
|
6984
7275
|
});
|
|
7276
|
+
Object.defineProperty(exports, "resolveRecordingOption", {
|
|
7277
|
+
enumerable: true,
|
|
7278
|
+
get: function() {
|
|
7279
|
+
return resolveRecordingOption;
|
|
7280
|
+
}
|
|
7281
|
+
});
|
|
7282
|
+
Object.defineProperty(exports, "runGenAiSpanEnd", {
|
|
7283
|
+
enumerable: true,
|
|
7284
|
+
get: function() {
|
|
7285
|
+
return runGenAiSpanEnd;
|
|
7286
|
+
}
|
|
7287
|
+
});
|
|
7288
|
+
Object.defineProperty(exports, "runGenAiSpanStart", {
|
|
7289
|
+
enumerable: true,
|
|
7290
|
+
get: function() {
|
|
7291
|
+
return runGenAiSpanStart;
|
|
7292
|
+
}
|
|
7293
|
+
});
|
|
6985
7294
|
Object.defineProperty(exports, "sanitizeSqlQuery", {
|
|
6986
7295
|
enumerable: true,
|
|
6987
7296
|
get: function() {
|
|
@@ -7000,10 +7309,10 @@ Object.defineProperty(exports, "setHttpServerSpanRouteAttribute", {
|
|
|
7000
7309
|
return setHttpServerSpanRouteAttribute;
|
|
7001
7310
|
}
|
|
7002
7311
|
});
|
|
7003
|
-
Object.defineProperty(exports, "
|
|
7312
|
+
Object.defineProperty(exports, "setPiAiDriverOptions", {
|
|
7004
7313
|
enumerable: true,
|
|
7005
7314
|
get: function() {
|
|
7006
|
-
return
|
|
7315
|
+
return setPiAiDriverOptions;
|
|
7007
7316
|
}
|
|
7008
7317
|
});
|
|
7009
7318
|
Object.defineProperty(exports, "setStartingClassifierEvaluation", {
|