@sentry/server-utils 11.14.0 → 11.16.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 +604 -219
- 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/cjs/index.request-handler.js +1 -0
- package/build/cjs/index.request-handler.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 +25 -17
- package/build/esm/ai/flue/utils.js.map +1 -1
- package/build/esm/ai/google-genai/decisions.js +29 -1
- package/build/esm/ai/google-genai/decisions.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/mapping.js +48 -1
- package/build/esm/ai/openai/mapping.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/esm/utils/wrapServerRequestHandler.js +2 -1
- package/build/esm/utils/wrapServerRequestHandler.js.map +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/decisions.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/mapping.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/types.d.ts +1 -1
- package/build/types/ai/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/build/types/utils/wrapServerRequestHandler.d.ts.map +1 -1
- package/package.json +4 -4
|
@@ -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`.
|
|
@@ -492,10 +544,57 @@ const GEN_AI_TOOL_CALL_ID_ATTRIBUTE = "gen_ai.tool.call.id";
|
|
|
492
544
|
function getDecisionInputMessages$1(params) {
|
|
493
545
|
const questions = Array.isArray(params.questions) ? params.questions.map(toJevQuestionFormat) : params.questions;
|
|
494
546
|
return getEvaluationInputMessages({
|
|
495
|
-
state: params.input,
|
|
547
|
+
state: toStateMessages$1(params.input),
|
|
496
548
|
questions: keyByName(questions)
|
|
497
549
|
});
|
|
498
550
|
}
|
|
551
|
+
/**
|
|
552
|
+
* Lay out evidence messages as gen_ai messages, so they render like Google's. Other input items (tool calls and
|
|
553
|
+
* their outputs) keep their layout, with output parts mapped like message parts.
|
|
554
|
+
*/
|
|
555
|
+
function toStateMessages$1(input) {
|
|
556
|
+
if (!Array.isArray(input)) return input;
|
|
557
|
+
return input.map((item) => {
|
|
558
|
+
if (!(0, _sentry_core.isObjectLike)(item)) return item;
|
|
559
|
+
if (typeof item.role === "string") return {
|
|
560
|
+
role: item.role === "developer" ? "system" : item.role,
|
|
561
|
+
parts: Array.isArray(item.content) ? item.content.filter(_sentry_core.isObjectLike).map(toMessagePart) : [{
|
|
562
|
+
type: "text",
|
|
563
|
+
content: item.content
|
|
564
|
+
}]
|
|
565
|
+
};
|
|
566
|
+
return Array.isArray(item.output) ? {
|
|
567
|
+
...item,
|
|
568
|
+
output: item.output.filter(_sentry_core.isObjectLike).map(toMessagePart)
|
|
569
|
+
} : item;
|
|
570
|
+
});
|
|
571
|
+
}
|
|
572
|
+
/**
|
|
573
|
+
* An image in a data URL is reported by its media type only: the URL holds base64, which the conventions require to be
|
|
574
|
+
* dropped.
|
|
575
|
+
*/
|
|
576
|
+
function toMessagePart(part) {
|
|
577
|
+
if (part.type === "input_image" && typeof part.image_url === "string") {
|
|
578
|
+
const dataUrlMimeType = /^data:([^;,]+)/.exec(part.image_url)?.[1];
|
|
579
|
+
return part.image_url.startsWith("data:") ? {
|
|
580
|
+
type: "blob",
|
|
581
|
+
modality: "image",
|
|
582
|
+
mime_type: dataUrlMimeType
|
|
583
|
+
} : {
|
|
584
|
+
type: "uri",
|
|
585
|
+
modality: "image",
|
|
586
|
+
uri: part.image_url
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
if (part.type === "input_json") return {
|
|
590
|
+
type: "object",
|
|
591
|
+
content: part.data
|
|
592
|
+
};
|
|
593
|
+
return {
|
|
594
|
+
type: "text",
|
|
595
|
+
content: part.text ?? part.refusal
|
|
596
|
+
};
|
|
597
|
+
}
|
|
499
598
|
/** Serialize the answers of a decisions response, laid out and keyed like Jev answers. */
|
|
500
599
|
function getDecisionOutputMessages(answers) {
|
|
501
600
|
return getEvaluationOutputMessages(keyByName(answers.map(toJevAnswerFormat)));
|
|
@@ -777,9 +876,10 @@ function processResponsesApiEvent(streamEvent, state, recordOutputs, span) {
|
|
|
777
876
|
* @param stream - The async iterable stream of events to instrument.
|
|
778
877
|
* @param span - The span to add attributes to and to finish at the end of the stream.
|
|
779
878
|
* @param recordOutputs - Whether to record output text fragments in the span.
|
|
879
|
+
* @param beforeSpanEnd - Runs right before the span ends, with the error when the stream failed.
|
|
780
880
|
* @returns An async generator yielding each event from the input stream.
|
|
781
881
|
*/
|
|
782
|
-
async function* instrumentStream$1(stream, span, recordOutputs) {
|
|
882
|
+
async function* instrumentStream$1(stream, span, recordOutputs, beforeSpanEnd) {
|
|
783
883
|
const state = {
|
|
784
884
|
eventTypes: [],
|
|
785
885
|
responseTexts: [],
|
|
@@ -794,8 +894,10 @@ async function* instrumentStream$1(stream, span, recordOutputs) {
|
|
|
794
894
|
chatCompletionToolCalls: /* @__PURE__ */ new Map(),
|
|
795
895
|
responsesApiToolCalls: []
|
|
796
896
|
};
|
|
897
|
+
const result = {};
|
|
797
898
|
try {
|
|
798
899
|
for await (const event of stream) {
|
|
900
|
+
recordTimeToFirstChunk(span);
|
|
799
901
|
if (isChatCompletionChunk(event)) processChatCompletionChunk(event, state, recordOutputs);
|
|
800
902
|
else if (isResponsesApiStreamEvent(event)) processResponsesApiEvent(event, state, recordOutputs, span);
|
|
801
903
|
yield event;
|
|
@@ -805,13 +907,14 @@ async function* instrumentStream$1(stream, span, recordOutputs) {
|
|
|
805
907
|
code: _sentry_core.SPAN_STATUS_ERROR,
|
|
806
908
|
message: "internal_error"
|
|
807
909
|
});
|
|
910
|
+
result.error = error;
|
|
808
911
|
throw error;
|
|
809
912
|
} finally {
|
|
810
913
|
const allToolCalls = [...state.chatCompletionToolCalls.values(), ...state.responsesApiToolCalls];
|
|
811
914
|
endStreamSpan(span, {
|
|
812
915
|
...state,
|
|
813
916
|
toolCalls: allToolCalls
|
|
814
|
-
}, recordOutputs);
|
|
917
|
+
}, recordOutputs, () => beforeSpanEnd?.(result));
|
|
815
918
|
}
|
|
816
919
|
}
|
|
817
920
|
// src/ai/openai/index.ts
|
|
@@ -875,9 +978,14 @@ function addRequestAttributes$2(span, params, operationName) {
|
|
|
875
978
|
function instrumentMethod$3(originalMethod, methodPath, instrumentedMethod, context, options) {
|
|
876
979
|
return function instrumentedCall(...args) {
|
|
877
980
|
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
981
|
const params = args[0];
|
|
982
|
+
const call = {
|
|
983
|
+
operation: operationName,
|
|
984
|
+
request: params
|
|
985
|
+
};
|
|
986
|
+
const { recordInputs, recordOutputs } = resolveGenAiCallRecording(options, call);
|
|
987
|
+
const requestAttributes = extractRequestAttributes$4(args, operationName, recordInputs);
|
|
988
|
+
const model = requestAttributes[_sentry_conventions_attributes.GEN_AI_REQUEST_MODEL] || "unknown";
|
|
881
989
|
const isStreamRequested = params && typeof params === "object" && params.stream === true;
|
|
882
990
|
const abortSignal = getRequestSignal(args[1]);
|
|
883
991
|
const client = (0, _sentry_core.getClient)();
|
|
@@ -890,12 +998,14 @@ function instrumentMethod$3(originalMethod, methodPath, instrumentedMethod, cont
|
|
|
890
998
|
let originalResult;
|
|
891
999
|
const instrumentedPromise = (0, _sentry_core.startSpanManual)(spanConfig, (span) => {
|
|
892
1000
|
originalResult = originalMethod.apply(context, args);
|
|
893
|
-
if (
|
|
1001
|
+
if (recordInputs && params) addRequestAttributes$2(span, params, operationName);
|
|
1002
|
+
runGenAiSpanStart(options, span, call);
|
|
894
1003
|
return (async () => {
|
|
895
1004
|
try {
|
|
896
|
-
return instrumentStream$1(await originalResult, span,
|
|
1005
|
+
return instrumentStream$1(await originalResult, span, recordOutputs, (streamResult) => runGenAiSpanEnd(options, span, call, streamResult));
|
|
897
1006
|
} catch (error) {
|
|
898
1007
|
span.setStatus(getErrorStatus(abortSignal));
|
|
1008
|
+
runGenAiSpanEnd(options, span, call, { error });
|
|
899
1009
|
span.end();
|
|
900
1010
|
throw error;
|
|
901
1011
|
}
|
|
@@ -906,12 +1016,15 @@ function instrumentMethod$3(originalMethod, methodPath, instrumentedMethod, cont
|
|
|
906
1016
|
let originalResult;
|
|
907
1017
|
const instrumentedPromise = (0, _sentry_core.startSpan)(spanConfig, (span) => {
|
|
908
1018
|
originalResult = originalMethod.apply(context, args);
|
|
909
|
-
if (
|
|
1019
|
+
if (recordInputs && params) addRequestAttributes$2(span, params, operationName);
|
|
1020
|
+
runGenAiSpanStart(options, span, call);
|
|
910
1021
|
return originalResult.then((result) => {
|
|
911
|
-
addResponseAttributes$5(span, result,
|
|
1022
|
+
addResponseAttributes$5(span, result, recordOutputs, operationName);
|
|
1023
|
+
runGenAiSpanEnd(options, span, call, { response: result });
|
|
912
1024
|
return result;
|
|
913
1025
|
}, (error) => {
|
|
914
1026
|
span.setStatus(getErrorStatus(abortSignal));
|
|
1027
|
+
runGenAiSpanEnd(options, span, call, { error });
|
|
915
1028
|
throw error;
|
|
916
1029
|
});
|
|
917
1030
|
});
|
|
@@ -964,7 +1077,7 @@ function createDeepProxy$2(target, currentPath = "", options) {
|
|
|
964
1077
|
* Can be used across Node.js, Cloudflare Workers, and Vercel Edge
|
|
965
1078
|
*/
|
|
966
1079
|
function instrumentOpenAiClient(client, options) {
|
|
967
|
-
return createDeepProxy$2(client, "",
|
|
1080
|
+
return createDeepProxy$2(client, "", options ?? {});
|
|
968
1081
|
}
|
|
969
1082
|
// src/ai/anthropic-ai/constants.ts
|
|
970
1083
|
const ANTHROPIC_AI_INTEGRATION_NAME = "Anthropic_AI";
|
|
@@ -1032,6 +1145,44 @@ function handleResponseError(span, response) {
|
|
|
1032
1145
|
} });
|
|
1033
1146
|
}
|
|
1034
1147
|
}
|
|
1148
|
+
/**
|
|
1149
|
+
* Add content attributes when recordOutputs is enabled
|
|
1150
|
+
*/
|
|
1151
|
+
function addContentAttributes(span, response) {
|
|
1152
|
+
if ("content" in response) {
|
|
1153
|
+
if (Array.isArray(response.content)) {
|
|
1154
|
+
span.setAttributes({ [_sentry_conventions_attributes.GEN_AI_RESPONSE_TEXT]: response.content.map((item) => item.text).filter((text) => !!text).join("") });
|
|
1155
|
+
const toolCalls = [];
|
|
1156
|
+
for (const item of response.content) if (item.type === "tool_use" || item.type === "server_tool_use") toolCalls.push(item);
|
|
1157
|
+
if (toolCalls.length > 0) span.setAttributes({ [_sentry_conventions_attributes.GEN_AI_RESPONSE_TOOL_CALLS]: JSON.stringify(toolCalls) });
|
|
1158
|
+
}
|
|
1159
|
+
}
|
|
1160
|
+
if ("completion" in response) span.setAttributes({ [_sentry_conventions_attributes.GEN_AI_RESPONSE_TEXT]: response.completion });
|
|
1161
|
+
}
|
|
1162
|
+
/**
|
|
1163
|
+
* Add basic metadata attributes from the response
|
|
1164
|
+
*/
|
|
1165
|
+
function addMetadataAttributes(span, response) {
|
|
1166
|
+
if ("id" in response && "model" in response) {
|
|
1167
|
+
span.setAttributes({
|
|
1168
|
+
[_sentry_conventions_attributes.GEN_AI_RESPONSE_ID]: response.id,
|
|
1169
|
+
[_sentry_conventions_attributes.GEN_AI_RESPONSE_MODEL]: response.model
|
|
1170
|
+
});
|
|
1171
|
+
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));
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
/**
|
|
1175
|
+
* Add response attributes to spans
|
|
1176
|
+
*/
|
|
1177
|
+
function addResponseAttributes$4(span, response, recordOutputs) {
|
|
1178
|
+
if (!response || typeof response !== "object") return;
|
|
1179
|
+
if ("type" in response && response.type === "error") {
|
|
1180
|
+
handleResponseError(span, response);
|
|
1181
|
+
return;
|
|
1182
|
+
}
|
|
1183
|
+
if (recordOutputs) addContentAttributes(span, response);
|
|
1184
|
+
addMetadataAttributes(span, response);
|
|
1185
|
+
}
|
|
1035
1186
|
/** Anthropic accepts the system prompt as a string or as an array of text blocks. */
|
|
1036
1187
|
function systemToText(system) {
|
|
1037
1188
|
if (!Array.isArray(system)) return typeof system === "string" ? system : void 0;
|
|
@@ -1160,6 +1311,7 @@ function handleContentBlockStop(event, state) {
|
|
|
1160
1311
|
function processEvent$1(event, state, recordOutputs, span) {
|
|
1161
1312
|
if (!(event && typeof event === "object")) return;
|
|
1162
1313
|
if (isErrorEvent(event, span)) return;
|
|
1314
|
+
recordTimeToFirstChunk(span);
|
|
1163
1315
|
handleMessageMetadata(event, state);
|
|
1164
1316
|
handleContentBlockStart(event, state);
|
|
1165
1317
|
handleContentBlockDelta(event, state, recordOutputs);
|
|
@@ -1170,7 +1322,7 @@ function processEvent$1(event, state, recordOutputs, span) {
|
|
|
1170
1322
|
* streaming attributes and (optionally) the aggregated output text, and yields
|
|
1171
1323
|
* each event from the input stream unchanged.
|
|
1172
1324
|
*/
|
|
1173
|
-
async function* instrumentAsyncIterableStream(stream, span, recordOutputs) {
|
|
1325
|
+
async function* instrumentAsyncIterableStream(stream, span, recordOutputs, beforeSpanEnd) {
|
|
1174
1326
|
const state = {
|
|
1175
1327
|
responseTexts: [],
|
|
1176
1328
|
finishReasons: [],
|
|
@@ -1184,6 +1336,7 @@ async function* instrumentAsyncIterableStream(stream, span, recordOutputs) {
|
|
|
1184
1336
|
toolCalls: [],
|
|
1185
1337
|
activeToolBlocks: {}
|
|
1186
1338
|
};
|
|
1339
|
+
const result = {};
|
|
1187
1340
|
try {
|
|
1188
1341
|
for await (const event of stream) {
|
|
1189
1342
|
processEvent$1(event, state, recordOutputs, span);
|
|
@@ -1194,15 +1347,16 @@ async function* instrumentAsyncIterableStream(stream, span, recordOutputs) {
|
|
|
1194
1347
|
code: _sentry_core.SPAN_STATUS_ERROR,
|
|
1195
1348
|
message: "internal_error"
|
|
1196
1349
|
});
|
|
1350
|
+
result.error = error;
|
|
1197
1351
|
throw error;
|
|
1198
1352
|
} finally {
|
|
1199
|
-
endStreamSpan(span, state, recordOutputs);
|
|
1353
|
+
endStreamSpan(span, state, recordOutputs, () => beforeSpanEnd?.(result));
|
|
1200
1354
|
}
|
|
1201
1355
|
}
|
|
1202
1356
|
/**
|
|
1203
1357
|
* Instruments a MessageStream by registering event handlers and preserving the original stream API.
|
|
1204
1358
|
*/
|
|
1205
|
-
function instrumentMessageStream(stream, span, recordOutputs, abortSignal) {
|
|
1359
|
+
function instrumentMessageStream(stream, span, recordOutputs, abortSignal, beforeSpanEnd) {
|
|
1206
1360
|
const state = {
|
|
1207
1361
|
responseTexts: [],
|
|
1208
1362
|
finishReasons: [],
|
|
@@ -1220,14 +1374,14 @@ function instrumentMessageStream(stream, span, recordOutputs, abortSignal) {
|
|
|
1220
1374
|
processEvent$1(event, state, recordOutputs, span);
|
|
1221
1375
|
});
|
|
1222
1376
|
stream.on("message", () => {
|
|
1223
|
-
endStreamSpan(span, state, recordOutputs);
|
|
1377
|
+
endStreamSpan(span, state, recordOutputs, () => beforeSpanEnd?.({}));
|
|
1224
1378
|
});
|
|
1225
1379
|
stream.on("end", () => {
|
|
1226
1380
|
if (span.isRecording() && stream.aborted) span.setStatus(abortSignal?.aborted ? getErrorStatus(abortSignal) : {
|
|
1227
1381
|
code: _sentry_core.SPAN_STATUS_ERROR,
|
|
1228
1382
|
message: "cancelled"
|
|
1229
1383
|
});
|
|
1230
|
-
endStreamSpan(span, state, recordOutputs);
|
|
1384
|
+
endStreamSpan(span, state, recordOutputs, () => beforeSpanEnd?.({}));
|
|
1231
1385
|
});
|
|
1232
1386
|
stream.on("error", (error) => {
|
|
1233
1387
|
(0, _sentry_core.captureException)(error, { mechanism: {
|
|
@@ -1239,7 +1393,7 @@ function instrumentMessageStream(stream, span, recordOutputs, abortSignal) {
|
|
|
1239
1393
|
code: _sentry_core.SPAN_STATUS_ERROR,
|
|
1240
1394
|
message: "internal_error"
|
|
1241
1395
|
});
|
|
1242
|
-
endStreamSpan(span, state, recordOutputs);
|
|
1396
|
+
endStreamSpan(span, state, recordOutputs, () => beforeSpanEnd?.({ error }));
|
|
1243
1397
|
}
|
|
1244
1398
|
});
|
|
1245
1399
|
return stream;
|
|
@@ -1344,8 +1498,13 @@ function wrapInPlace(owner, key, call) {
|
|
|
1344
1498
|
});
|
|
1345
1499
|
}
|
|
1346
1500
|
function runWithSpan(tool, run, thisArg, args, options) {
|
|
1347
|
-
const { recordInputs, recordOutputs } = resolveAIRecordingOptions(options);
|
|
1348
1501
|
const [input, context] = args;
|
|
1502
|
+
const call = {
|
|
1503
|
+
operation: "execute_tool",
|
|
1504
|
+
tool,
|
|
1505
|
+
input
|
|
1506
|
+
};
|
|
1507
|
+
const { recordInputs, recordOutputs } = resolveGenAiCallRecording(options, call);
|
|
1349
1508
|
const callId = (context?.toolUse ?? context?.toolUseBlock)?.id;
|
|
1350
1509
|
return (0, _sentry_core.startSpan)({
|
|
1351
1510
|
name: `execute_tool ${tool.name}`,
|
|
@@ -1358,7 +1517,16 @@ function runWithSpan(tool, run, thisArg, args, options) {
|
|
|
1358
1517
|
...typeof callId === "string" ? { [GEN_AI_TOOL_CALL_ID_ATTRIBUTE]: callId } : {},
|
|
1359
1518
|
...recordInputs ? { [_sentry_conventions_attributes.GEN_AI_TOOL_CALL_ARGUMENTS]: (0, _sentry_core.stringify)(input) } : {}
|
|
1360
1519
|
}
|
|
1361
|
-
}, (span) =>
|
|
1520
|
+
}, (span) => {
|
|
1521
|
+
runGenAiSpanStart(options, span, call);
|
|
1522
|
+
return (0, _sentry_core.handleCallbackErrors)(() => run.apply(thisArg, args), (error) => {
|
|
1523
|
+
handleToolError(span, error, context);
|
|
1524
|
+
runGenAiSpanEnd(options, span, call, { error });
|
|
1525
|
+
}, void 0, (result) => {
|
|
1526
|
+
if (recordOutputs) span.setAttribute(_sentry_conventions_attributes.GEN_AI_TOOL_CALL_RESULT, (0, _sentry_core.stringify)(result));
|
|
1527
|
+
runGenAiSpanEnd(options, span, call, { response: result });
|
|
1528
|
+
});
|
|
1529
|
+
});
|
|
1362
1530
|
}
|
|
1363
1531
|
function handleToolError(span, error, context) {
|
|
1364
1532
|
span.setStatus(getErrorStatus(context?.signal));
|
|
@@ -1402,49 +1570,12 @@ function addPrivateRequestAttributes$1(span, params) {
|
|
|
1402
1570
|
span.setAttributes(getMessagesAttributes(messages));
|
|
1403
1571
|
}
|
|
1404
1572
|
/**
|
|
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
1573
|
* Handle common error catching and reporting for streaming requests
|
|
1444
1574
|
*/
|
|
1445
|
-
function handleStreamingError(error, span, abortSignal) {
|
|
1575
|
+
function handleStreamingError(error, span, abortSignal, onSpanEnd) {
|
|
1446
1576
|
if (span.isRecording()) {
|
|
1447
1577
|
span.setStatus(getErrorStatus(abortSignal));
|
|
1578
|
+
onSpanEnd({ error });
|
|
1448
1579
|
span.end();
|
|
1449
1580
|
}
|
|
1450
1581
|
throw error;
|
|
@@ -1452,32 +1583,36 @@ function handleStreamingError(error, span, abortSignal) {
|
|
|
1452
1583
|
/**
|
|
1453
1584
|
* Handle streaming cases with common logic
|
|
1454
1585
|
*/
|
|
1455
|
-
function handleStreamingRequest(target, invocationThis, args, spanConfig, params,
|
|
1586
|
+
function handleStreamingRequest(target, invocationThis, args, spanConfig, params, { recordInputs, recordOutputs }, isStreamRequested, isStreamingMethod, options, call) {
|
|
1456
1587
|
const abortSignal = getRequestSignal(args[1]);
|
|
1457
1588
|
if (isStreamRequested && !isStreamingMethod) {
|
|
1458
1589
|
let originalResult;
|
|
1459
1590
|
const instrumentedPromise = (0, _sentry_core.startSpanManual)(spanConfig, (span) => {
|
|
1460
1591
|
originalResult = target.apply(invocationThis, args);
|
|
1461
|
-
if (
|
|
1592
|
+
if (recordInputs && params) addPrivateRequestAttributes$1(span, params);
|
|
1593
|
+
runGenAiSpanStart(options, span, call);
|
|
1594
|
+
const onSpanEnd = (result) => runGenAiSpanEnd(options, span, call, result);
|
|
1462
1595
|
return (async () => {
|
|
1463
1596
|
try {
|
|
1464
|
-
return instrumentAsyncIterableStream(await originalResult, span,
|
|
1597
|
+
return instrumentAsyncIterableStream(await originalResult, span, recordOutputs, onSpanEnd);
|
|
1465
1598
|
} catch (error) {
|
|
1466
|
-
return handleStreamingError(error, span, abortSignal);
|
|
1599
|
+
return handleStreamingError(error, span, abortSignal, onSpanEnd);
|
|
1467
1600
|
}
|
|
1468
1601
|
})();
|
|
1469
1602
|
});
|
|
1470
1603
|
return wrapPromiseWithMethods(originalResult, instrumentedPromise);
|
|
1471
1604
|
} else return (0, _sentry_core.startSpanManual)(spanConfig, (span) => {
|
|
1605
|
+
const onSpanEnd = (result) => runGenAiSpanEnd(options, span, call, result);
|
|
1472
1606
|
try {
|
|
1473
|
-
if (
|
|
1607
|
+
if (recordInputs && params) addPrivateRequestAttributes$1(span, params);
|
|
1608
|
+
runGenAiSpanStart(options, span, call);
|
|
1474
1609
|
suppressDelegatedCreate = true;
|
|
1475
1610
|
const messageStream = target.apply(invocationThis, args);
|
|
1476
1611
|
suppressDelegatedCreate = false;
|
|
1477
|
-
return instrumentMessageStream(messageStream, span,
|
|
1612
|
+
return instrumentMessageStream(messageStream, span, recordOutputs, abortSignal, onSpanEnd);
|
|
1478
1613
|
} catch (error) {
|
|
1479
1614
|
suppressDelegatedCreate = false;
|
|
1480
|
-
return handleStreamingError(error, span, abortSignal);
|
|
1615
|
+
return handleStreamingError(error, span, abortSignal, onSpanEnd);
|
|
1481
1616
|
}
|
|
1482
1617
|
});
|
|
1483
1618
|
}
|
|
@@ -1492,7 +1627,13 @@ function instrumentMethod$2(originalMethod, methodPath, instrumentedMethod, cont
|
|
|
1492
1627
|
const isStreamingMethod = instrumentedMethod.streaming === true;
|
|
1493
1628
|
if (!isStreamingMethod && suppressDelegatedCreate) return target.apply(invocationThis, args);
|
|
1494
1629
|
const operationName = instrumentedMethod.operation || "unknown";
|
|
1495
|
-
const
|
|
1630
|
+
const params = typeof args[0] === "object" && args[0] !== null ? args[0] : void 0;
|
|
1631
|
+
const call = {
|
|
1632
|
+
operation: "chat",
|
|
1633
|
+
request: params
|
|
1634
|
+
};
|
|
1635
|
+
const recording = resolveGenAiCallRecording(options, call);
|
|
1636
|
+
const requestAttributes = extractRequestAttributes$3(args, operationName, recording.recordInputs, getProviderName(invocationThis));
|
|
1496
1637
|
const model = requestAttributes[_sentry_conventions_attributes.GEN_AI_REQUEST_MODEL] || "unknown";
|
|
1497
1638
|
const client = (0, _sentry_core.getClient)();
|
|
1498
1639
|
const spanConfig = {
|
|
@@ -1500,19 +1641,21 @@ function instrumentMethod$2(originalMethod, methodPath, instrumentedMethod, cont
|
|
|
1500
1641
|
op: getGenAiSpanOp(operationName),
|
|
1501
1642
|
attributes: requestAttributes
|
|
1502
1643
|
};
|
|
1503
|
-
const params = typeof args[0] === "object" ? args[0] : void 0;
|
|
1504
1644
|
const isStreamRequested = Boolean(params?.stream);
|
|
1505
|
-
if (isStreamRequested || isStreamingMethod) return handleStreamingRequest(target, invocationThis, args, spanConfig, params,
|
|
1645
|
+
if (isStreamRequested || isStreamingMethod) return handleStreamingRequest(target, invocationThis, args, spanConfig, params, recording, isStreamRequested, isStreamingMethod, options, call);
|
|
1506
1646
|
const abortSignal = getRequestSignal(args[1]);
|
|
1507
1647
|
let originalResult;
|
|
1508
1648
|
const instrumentedPromise = (0, _sentry_core.startSpan)(spanConfig, (span) => {
|
|
1509
1649
|
originalResult = target.apply(invocationThis, args);
|
|
1510
|
-
if (
|
|
1650
|
+
if (recording.recordInputs && params) addPrivateRequestAttributes$1(span, params);
|
|
1651
|
+
runGenAiSpanStart(options, span, call);
|
|
1511
1652
|
return originalResult.then((result) => {
|
|
1512
|
-
addResponseAttributes$4(span, result,
|
|
1653
|
+
addResponseAttributes$4(span, result, recording.recordOutputs);
|
|
1654
|
+
runGenAiSpanEnd(options, span, call, { response: result });
|
|
1513
1655
|
return result;
|
|
1514
1656
|
}, (error) => {
|
|
1515
1657
|
span.setStatus(getErrorStatus(abortSignal));
|
|
1658
|
+
runGenAiSpanEnd(options, span, call, { error });
|
|
1516
1659
|
throw error;
|
|
1517
1660
|
});
|
|
1518
1661
|
});
|
|
@@ -1558,7 +1701,7 @@ function instrumentClientInPlace(client, options) {
|
|
|
1558
1701
|
* @returns The instrumented client with the same type as the input
|
|
1559
1702
|
*/
|
|
1560
1703
|
function instrumentAnthropicAiClient(anthropicAiClient, options) {
|
|
1561
|
-
return instrumentClientInPlace(anthropicAiClient,
|
|
1704
|
+
return instrumentClientInPlace(anthropicAiClient, options ?? {});
|
|
1562
1705
|
}
|
|
1563
1706
|
// src/ai/google-genai/constants.ts
|
|
1564
1707
|
const GOOGLE_GENAI_INTEGRATION_NAME = "Google_GenAI";
|
|
@@ -1585,10 +1728,38 @@ const GOOGLE_GENAI_SYSTEM_NAME = "google_genai";
|
|
|
1585
1728
|
*/
|
|
1586
1729
|
function getDecisionInputMessages(params) {
|
|
1587
1730
|
return getEvaluationInputMessages({
|
|
1588
|
-
state: params.input,
|
|
1731
|
+
state: toStateMessages(params.input),
|
|
1589
1732
|
questions: params.questions
|
|
1590
1733
|
});
|
|
1591
1734
|
}
|
|
1735
|
+
/**
|
|
1736
|
+
* Multimodal evidence is one part or a list of parts, recorded as one user message so it renders like OpenAI's.
|
|
1737
|
+
* Inline media is reported by modality and media type only: its `data` is base64, which the conventions require
|
|
1738
|
+
* to be dropped.
|
|
1739
|
+
*/
|
|
1740
|
+
function toStateMessages(input) {
|
|
1741
|
+
if (typeof input === "string") return input;
|
|
1742
|
+
return [{
|
|
1743
|
+
role: "user",
|
|
1744
|
+
parts: (Array.isArray(input) ? input : [input]).filter(_sentry_core.isObjectLike).map((part) => {
|
|
1745
|
+
if (part.type === "text") return {
|
|
1746
|
+
type: "text",
|
|
1747
|
+
content: part.text
|
|
1748
|
+
};
|
|
1749
|
+
if (typeof part.uri === "string") return {
|
|
1750
|
+
type: "uri",
|
|
1751
|
+
modality: part.type,
|
|
1752
|
+
mime_type: part.mime_type,
|
|
1753
|
+
uri: part.uri
|
|
1754
|
+
};
|
|
1755
|
+
return {
|
|
1756
|
+
type: "blob",
|
|
1757
|
+
modality: part.type,
|
|
1758
|
+
mime_type: part.mime_type
|
|
1759
|
+
};
|
|
1760
|
+
})
|
|
1761
|
+
}];
|
|
1762
|
+
}
|
|
1592
1763
|
function addDecisionResponseAttributes(span, response, recordOutputs) {
|
|
1593
1764
|
if (!(0, _sentry_core.isObjectLike)(response)) return;
|
|
1594
1765
|
if ((0, _sentry_core.isObjectLike)(response.usage)) {
|
|
@@ -1795,6 +1966,7 @@ function handleCandidateContent(chunk, state, recordOutputs) {
|
|
|
1795
1966
|
*/
|
|
1796
1967
|
function processChunk$1(chunk, state, recordOutputs, span) {
|
|
1797
1968
|
if (!chunk || isErrorChunk(chunk, span)) return;
|
|
1969
|
+
recordTimeToFirstChunk(span);
|
|
1798
1970
|
handleResponseMetadata(chunk, state);
|
|
1799
1971
|
handleCandidateContent(chunk, state, recordOutputs);
|
|
1800
1972
|
}
|
|
@@ -1803,13 +1975,14 @@ function processChunk$1(chunk, state, recordOutputs, span) {
|
|
|
1803
1975
|
* streaming attributes and (optionally) the aggregated output text, and yields
|
|
1804
1976
|
* each chunk from the input stream unchanged.
|
|
1805
1977
|
*/
|
|
1806
|
-
async function* instrumentStream(stream, span, recordOutputs, abortSignal) {
|
|
1978
|
+
async function* instrumentStream(stream, span, recordOutputs, abortSignal, beforeSpanEnd) {
|
|
1807
1979
|
const state = {
|
|
1808
1980
|
responseTexts: [],
|
|
1809
1981
|
finishReasons: [],
|
|
1810
1982
|
toolCalls: [],
|
|
1811
1983
|
outputParts: []
|
|
1812
1984
|
};
|
|
1985
|
+
const result = {};
|
|
1813
1986
|
try {
|
|
1814
1987
|
for await (const chunk of stream) {
|
|
1815
1988
|
processChunk$1(chunk, state, recordOutputs, span);
|
|
@@ -1817,11 +1990,12 @@ async function* instrumentStream(stream, span, recordOutputs, abortSignal) {
|
|
|
1817
1990
|
}
|
|
1818
1991
|
} catch (error) {
|
|
1819
1992
|
span.setStatus(getErrorStatus(abortSignal));
|
|
1993
|
+
result.error = error;
|
|
1820
1994
|
throw error;
|
|
1821
1995
|
} finally {
|
|
1822
1996
|
if (recordOutputs) setOutputMessagesAttribute(span, state.outputParts);
|
|
1823
1997
|
if (typeof state.promptTokens === "number") state.promptTokens += state.toolUsePromptTokens ?? 0;
|
|
1824
|
-
endStreamSpan(span, state, recordOutputs);
|
|
1998
|
+
endStreamSpan(span, state, recordOutputs, () => beforeSpanEnd?.(result));
|
|
1825
1999
|
}
|
|
1826
2000
|
}
|
|
1827
2001
|
// src/ai/google-genai/index.ts
|
|
@@ -1972,9 +2146,14 @@ function instrumentMethod$1(originalMethod, methodPath, instrumentedMethod, cont
|
|
|
1972
2146
|
return new Proxy(originalMethod, { apply(target, _, args) {
|
|
1973
2147
|
const operationName = instrumentedMethod.operation || "unknown";
|
|
1974
2148
|
const params = args[0];
|
|
2149
|
+
const call = {
|
|
2150
|
+
operation: operationName,
|
|
2151
|
+
request: params
|
|
2152
|
+
};
|
|
2153
|
+
const { recordInputs, recordOutputs } = resolveGenAiCallRecording(options, call);
|
|
1975
2154
|
const attributeParams = resolveChatParams(operationName, params, context);
|
|
1976
2155
|
const abortSignal = getCallAbortSignal(operationName, attributeParams, args[1]);
|
|
1977
|
-
const requestAttributes = extractRequestAttributes$2(operationName, attributeParams, context,
|
|
2156
|
+
const requestAttributes = extractRequestAttributes$2(operationName, attributeParams, context, recordInputs);
|
|
1978
2157
|
const model = requestAttributes[_sentry_conventions_attributes.GEN_AI_REQUEST_MODEL] || "unknown";
|
|
1979
2158
|
const client = (0, _sentry_core.getClient)();
|
|
1980
2159
|
const spanName = typeof model === "string" && model !== "unknown" || !(client && (0, _sentry_core.hasSpanStreamingEnabled)(client)) ? `${operationName} ${model}` : operationName;
|
|
@@ -1984,10 +2163,12 @@ function instrumentMethod$1(originalMethod, methodPath, instrumentedMethod, cont
|
|
|
1984
2163
|
attributes: requestAttributes
|
|
1985
2164
|
}, async (span) => {
|
|
1986
2165
|
try {
|
|
1987
|
-
if (
|
|
1988
|
-
|
|
2166
|
+
if (recordInputs && attributeParams) addPrivateRequestAttributes(span, attributeParams, operationName);
|
|
2167
|
+
runGenAiSpanStart(options, span, call);
|
|
2168
|
+
return instrumentStream(await target.apply(context, args), span, recordOutputs, abortSignal, (result) => runGenAiSpanEnd(options, span, call, result));
|
|
1989
2169
|
} catch (error) {
|
|
1990
2170
|
span.setStatus(getErrorStatus(abortSignal));
|
|
2171
|
+
runGenAiSpanEnd(options, span, call, { error });
|
|
1991
2172
|
span.end();
|
|
1992
2173
|
throw error;
|
|
1993
2174
|
}
|
|
@@ -1997,9 +2178,14 @@ function instrumentMethod$1(originalMethod, methodPath, instrumentedMethod, cont
|
|
|
1997
2178
|
op: getGenAiSpanOp(operationName),
|
|
1998
2179
|
attributes: requestAttributes
|
|
1999
2180
|
}, (span) => {
|
|
2000
|
-
if (
|
|
2001
|
-
|
|
2002
|
-
|
|
2181
|
+
if (recordInputs && attributeParams) addPrivateRequestAttributes(span, attributeParams, operationName);
|
|
2182
|
+
runGenAiSpanStart(options, span, call);
|
|
2183
|
+
return (0, _sentry_core.handleCallbackErrors)(() => target.apply(context, args), (error) => {
|
|
2184
|
+
span.setStatus(getErrorStatus(abortSignal));
|
|
2185
|
+
runGenAiSpanEnd(options, span, call, { error });
|
|
2186
|
+
}, () => {}, (result) => {
|
|
2187
|
+
if (!isEmbeddings) addResponseAttributes$3(span, result, recordOutputs, operationName);
|
|
2188
|
+
runGenAiSpanEnd(options, span, call, { response: result });
|
|
2003
2189
|
});
|
|
2004
2190
|
});
|
|
2005
2191
|
} });
|
|
@@ -2081,7 +2267,7 @@ function createDeepProxy$1(target, currentPath = "", options) {
|
|
|
2081
2267
|
* ```
|
|
2082
2268
|
*/
|
|
2083
2269
|
function instrumentGoogleGenAIClient(client, options) {
|
|
2084
|
-
return createDeepProxy$1(client, "",
|
|
2270
|
+
return createDeepProxy$1(client, "", options ?? {});
|
|
2085
2271
|
}
|
|
2086
2272
|
// src/ai/mistral/constants.ts
|
|
2087
2273
|
const MISTRAL_INTEGRATION_NAME = "Mistral";
|
|
@@ -2324,13 +2510,14 @@ function instrumentedSource(readable) {
|
|
|
2324
2510
|
*
|
|
2325
2511
|
* Returns `false` for a value that is not a stream, leaving it untouched.
|
|
2326
2512
|
*/
|
|
2327
|
-
function instrumentEventStream(stream, span, recordOutputs, abortSignal) {
|
|
2513
|
+
function instrumentEventStream(stream, span, recordOutputs, abortSignal, beforeSpanEnd) {
|
|
2328
2514
|
if (!isAsyncIterable(stream)) return false;
|
|
2329
2515
|
const state = createStreamingState();
|
|
2330
2516
|
let consumer;
|
|
2331
2517
|
let settled = false;
|
|
2332
2518
|
let cancelled = false;
|
|
2333
2519
|
const claim = (candidate) => {
|
|
2520
|
+
recordTimeToFirstChunk(span);
|
|
2334
2521
|
consumer ?? (consumer = candidate);
|
|
2335
2522
|
return consumer === candidate;
|
|
2336
2523
|
};
|
|
@@ -2350,7 +2537,7 @@ function instrumentEventStream(stream, span, recordOutputs, abortSignal) {
|
|
|
2350
2537
|
endStreamSpan(span, {
|
|
2351
2538
|
...state,
|
|
2352
2539
|
toolCalls
|
|
2353
|
-
}, recordOutputs);
|
|
2540
|
+
}, recordOutputs, () => beforeSpanEnd?.(error !== void 0 && !cancelled ? { error } : {}));
|
|
2354
2541
|
};
|
|
2355
2542
|
const instrumented = instrumentIterator(stream[Symbol.asyncIterator].bind(stream), state, recordOutputs, claim, settle);
|
|
2356
2543
|
stream[Symbol.asyncIterator] = () => instrumented;
|
|
@@ -2432,9 +2619,14 @@ function instrumentMethod(originalMethod, instrumentedMethod, context, options)
|
|
|
2432
2619
|
return function instrumentedCall(...args) {
|
|
2433
2620
|
const operationName = instrumentedMethod.operation || "unknown";
|
|
2434
2621
|
const isStreamRequested = !!instrumentedMethod.streaming;
|
|
2435
|
-
const requestAttributes = extractRequestAttributes$1(args, operationName, !!options.recordInputs, isStreamRequested);
|
|
2436
2622
|
const params = args[0];
|
|
2437
2623
|
const abortSignal = getMistralRequestSignal(args[1]);
|
|
2624
|
+
const call = {
|
|
2625
|
+
operation: operationName,
|
|
2626
|
+
request: params
|
|
2627
|
+
};
|
|
2628
|
+
const { recordInputs, recordOutputs } = resolveGenAiCallRecording(options, call);
|
|
2629
|
+
const requestAttributes = extractRequestAttributes$1(args, operationName, recordInputs, isStreamRequested);
|
|
2438
2630
|
const spanConfig = {
|
|
2439
2631
|
name: getSpanName$1(operationName, requestAttributes),
|
|
2440
2632
|
op: getGenAiSpanOp(operationName),
|
|
@@ -2444,12 +2636,17 @@ function instrumentMethod(originalMethod, instrumentedMethod, context, options)
|
|
|
2444
2636
|
let originalResult;
|
|
2445
2637
|
const instrumentedPromise = (0, _sentry_core.startSpanManual)(spanConfig, (span) => {
|
|
2446
2638
|
originalResult = originalMethod.apply(context, args);
|
|
2447
|
-
if (
|
|
2639
|
+
if (recordInputs && params) addRequestAttributes$1(span, params, operationName);
|
|
2640
|
+
runGenAiSpanStart(options, span, call);
|
|
2448
2641
|
return originalResult.then((result) => {
|
|
2449
|
-
if (!instrumentEventStream(result, span,
|
|
2642
|
+
if (!instrumentEventStream(result, span, recordOutputs, abortSignal, (streamResult) => runGenAiSpanEnd(options, span, call, streamResult))) {
|
|
2643
|
+
runGenAiSpanEnd(options, span, call, { response: result });
|
|
2644
|
+
span.end();
|
|
2645
|
+
}
|
|
2450
2646
|
return result;
|
|
2451
2647
|
}, (error) => {
|
|
2452
2648
|
span.setStatus(getErrorStatus(abortSignal));
|
|
2649
|
+
runGenAiSpanEnd(options, span, call, { error });
|
|
2453
2650
|
span.end();
|
|
2454
2651
|
throw error;
|
|
2455
2652
|
});
|
|
@@ -2459,12 +2656,15 @@ function instrumentMethod(originalMethod, instrumentedMethod, context, options)
|
|
|
2459
2656
|
let originalResult;
|
|
2460
2657
|
const instrumentedPromise = (0, _sentry_core.startSpan)(spanConfig, (span) => {
|
|
2461
2658
|
originalResult = originalMethod.apply(context, args);
|
|
2462
|
-
if (
|
|
2659
|
+
if (recordInputs && params) addRequestAttributes$1(span, params, operationName);
|
|
2660
|
+
runGenAiSpanStart(options, span, call);
|
|
2463
2661
|
return originalResult.then((result) => {
|
|
2464
|
-
addResponseAttributes$2(span, result,
|
|
2662
|
+
addResponseAttributes$2(span, result, recordOutputs);
|
|
2663
|
+
runGenAiSpanEnd(options, span, call, { response: result });
|
|
2465
2664
|
return result;
|
|
2466
2665
|
}, (error) => {
|
|
2467
2666
|
span.setStatus(getErrorStatus(abortSignal));
|
|
2667
|
+
runGenAiSpanEnd(options, span, call, { error });
|
|
2468
2668
|
throw error;
|
|
2469
2669
|
});
|
|
2470
2670
|
});
|
|
@@ -2490,7 +2690,7 @@ function createDeepProxy(target, currentPath = "", options) {
|
|
|
2490
2690
|
* Can be used across Node.js, Cloudflare Workers, and Vercel Edge.
|
|
2491
2691
|
*/
|
|
2492
2692
|
function instrumentMistralAiClient(client, options) {
|
|
2493
|
-
return createDeepProxy(client, "",
|
|
2693
|
+
return createDeepProxy(client, "", options ?? {});
|
|
2494
2694
|
}
|
|
2495
2695
|
// src/ai/typesafe/constants.ts
|
|
2496
2696
|
const TYPESAFE_INTEGRATION_NAME = "TypeSafe";
|
|
@@ -2556,31 +2756,38 @@ function instrumentTypeSafeClient(client, options) {
|
|
|
2556
2756
|
return new Proxy(client, { get(target, prop) {
|
|
2557
2757
|
const value = Reflect.get(target, prop, target);
|
|
2558
2758
|
if (typeof value !== "function") return value;
|
|
2559
|
-
if (prop === "systemOne") return (...args) => instrumentSystemOne(value, target, args, options);
|
|
2759
|
+
if (prop === "systemOne") return (...args) => instrumentSystemOne(value, target, args, options ?? {});
|
|
2560
2760
|
return value.bind(target);
|
|
2561
2761
|
} });
|
|
2562
2762
|
}
|
|
2563
2763
|
function instrumentSystemOne(systemOne, client, args, options) {
|
|
2564
|
-
const {
|
|
2764
|
+
const call = { request: args[0] };
|
|
2765
|
+
const { recordInputs, recordOutputs } = resolveGenAiCallRecording(options, call);
|
|
2565
2766
|
const span = startDecideSpan(args[0], client, recordInputs);
|
|
2566
|
-
|
|
2767
|
+
runGenAiSpanStart(options, span, call);
|
|
2768
|
+
const endWithError = (error) => {
|
|
2567
2769
|
span.setStatus({
|
|
2568
2770
|
code: _sentry_core.SPAN_STATUS_ERROR,
|
|
2569
2771
|
message: "internal_error"
|
|
2570
2772
|
});
|
|
2773
|
+
runGenAiSpanEnd(options, span, call, { error });
|
|
2571
2774
|
span.end();
|
|
2572
2775
|
};
|
|
2573
2776
|
let result;
|
|
2574
2777
|
try {
|
|
2575
2778
|
result = (0, _sentry_core.withActiveSpan)(span, () => systemOne.apply(client, args));
|
|
2576
2779
|
} catch (error) {
|
|
2577
|
-
endWithError();
|
|
2780
|
+
endWithError(error);
|
|
2578
2781
|
throw error;
|
|
2579
2782
|
}
|
|
2580
2783
|
if (!onSystemOneResponse(result, (body) => {
|
|
2581
2784
|
addResponseAttributes$1(span, body, recordOutputs);
|
|
2785
|
+
runGenAiSpanEnd(options, span, call, { response: body });
|
|
2582
2786
|
span.end();
|
|
2583
|
-
}, endWithError))
|
|
2787
|
+
}, endWithError)) {
|
|
2788
|
+
runGenAiSpanEnd(options, span, call, { response: result });
|
|
2789
|
+
span.end();
|
|
2790
|
+
}
|
|
2584
2791
|
return result;
|
|
2585
2792
|
}
|
|
2586
2793
|
// src/ai/pi-ai/constants.ts
|
|
@@ -2823,7 +3030,7 @@ function processLine(line, state, recordOutputs, toolCallAccumulator) {
|
|
|
2823
3030
|
*
|
|
2824
3031
|
* The span is ended once the consumer finishes reading (or cancels) the stream.
|
|
2825
3032
|
*/
|
|
2826
|
-
function instrumentWorkersAiStream(stream, span, recordOutputs, abortSignal) {
|
|
3033
|
+
function instrumentWorkersAiStream(stream, span, recordOutputs, abortSignal, beforeSpanEnd) {
|
|
2827
3034
|
const reader = stream.getReader();
|
|
2828
3035
|
const decoder = new TextDecoder();
|
|
2829
3036
|
const state = {
|
|
@@ -2842,7 +3049,7 @@ function instrumentWorkersAiStream(stream, span, recordOutputs, abortSignal) {
|
|
|
2842
3049
|
};
|
|
2843
3050
|
let buffer = "";
|
|
2844
3051
|
let spanEnded = false;
|
|
2845
|
-
const finish = () => {
|
|
3052
|
+
const finish = (result = {}) => {
|
|
2846
3053
|
if (spanEnded) return;
|
|
2847
3054
|
spanEnded = true;
|
|
2848
3055
|
if (recordOutputs) {
|
|
@@ -2854,7 +3061,7 @@ function instrumentWorkersAiStream(stream, span, recordOutputs, abortSignal) {
|
|
|
2854
3061
|
finishReason: state.finishReasons[0]
|
|
2855
3062
|
});
|
|
2856
3063
|
}
|
|
2857
|
-
endStreamSpan(span, state, recordOutputs);
|
|
3064
|
+
endStreamSpan(span, state, recordOutputs, () => beforeSpanEnd?.(result));
|
|
2858
3065
|
};
|
|
2859
3066
|
const flushBuffer = (isDone) => {
|
|
2860
3067
|
const lines = buffer.split("\n");
|
|
@@ -2872,12 +3079,13 @@ function instrumentWorkersAiStream(stream, span, recordOutputs, abortSignal) {
|
|
|
2872
3079
|
controller.close();
|
|
2873
3080
|
return;
|
|
2874
3081
|
}
|
|
3082
|
+
recordTimeToFirstChunk(span);
|
|
2875
3083
|
buffer += decoder.decode(value, { stream: true });
|
|
2876
3084
|
flushBuffer(false);
|
|
2877
3085
|
controller.enqueue(value);
|
|
2878
3086
|
} catch (error) {
|
|
2879
3087
|
span.setStatus(getErrorStatus(abortSignal));
|
|
2880
|
-
finish();
|
|
3088
|
+
finish({ error });
|
|
2881
3089
|
controller.error(error);
|
|
2882
3090
|
}
|
|
2883
3091
|
},
|
|
@@ -2888,6 +3096,7 @@ function instrumentWorkersAiStream(stream, span, recordOutputs, abortSignal) {
|
|
|
2888
3096
|
});
|
|
2889
3097
|
}
|
|
2890
3098
|
// src/ai/workers-ai/index.ts
|
|
3099
|
+
const originalClients = /* @__PURE__ */ new WeakMap();
|
|
2891
3100
|
/**
|
|
2892
3101
|
* Wrap the `run` method of the Workers AI binding with Sentry tracing.
|
|
2893
3102
|
*/
|
|
@@ -2896,6 +3105,13 @@ function instrumentRun(originalRun, context, options) {
|
|
|
2896
3105
|
if (shouldSkipAiProviderSpan("WorkersAI")) return originalRun.apply(context, args);
|
|
2897
3106
|
const [model, inputs, runOptions] = args;
|
|
2898
3107
|
const operationName = getOperationName(model, inputs);
|
|
3108
|
+
const call = {
|
|
3109
|
+
operation: operationName,
|
|
3110
|
+
model,
|
|
3111
|
+
inputs,
|
|
3112
|
+
options: runOptions
|
|
3113
|
+
};
|
|
3114
|
+
const { recordInputs, recordOutputs } = resolveGenAiCallRecording(options, call);
|
|
2899
3115
|
const requestAttributes = extractRequestAttributes(model, inputs, operationName);
|
|
2900
3116
|
const modelName = typeof model === "string" && model ? model : "unknown";
|
|
2901
3117
|
const abortSignal = getRequestSignal(runOptions);
|
|
@@ -2912,6 +3128,7 @@ function instrumentRun(originalRun, context, options) {
|
|
|
2912
3128
|
if (isStreamRequested && !returnsRawResponse) return (0, _sentry_core.startSpanManual)(spanConfig, (span) => {
|
|
2913
3129
|
const handleError = (error) => {
|
|
2914
3130
|
span.setStatus(getErrorStatus(abortSignal));
|
|
3131
|
+
runGenAiSpanEnd(options, span, call, { error });
|
|
2915
3132
|
span.end();
|
|
2916
3133
|
throw error;
|
|
2917
3134
|
};
|
|
@@ -2921,22 +3138,27 @@ function instrumentRun(originalRun, context, options) {
|
|
|
2921
3138
|
} catch (error) {
|
|
2922
3139
|
return handleError(error);
|
|
2923
3140
|
}
|
|
2924
|
-
if (
|
|
3141
|
+
if (recordInputs) addRequestAttributes(span, inputs, operationName);
|
|
3142
|
+
runGenAiSpanStart(options, span, call);
|
|
2925
3143
|
return originalResult.then((result) => {
|
|
2926
|
-
if (isReadableStream(result)) return instrumentWorkersAiStream(result, span,
|
|
2927
|
-
addResponseAttributes(span, result,
|
|
3144
|
+
if (isReadableStream(result)) return instrumentWorkersAiStream(result, span, recordOutputs, abortSignal, (streamResult) => runGenAiSpanEnd(options, span, call, streamResult));
|
|
3145
|
+
addResponseAttributes(span, result, recordOutputs, operationName);
|
|
3146
|
+
runGenAiSpanEnd(options, span, call, { response: result });
|
|
2928
3147
|
span.end();
|
|
2929
3148
|
return result;
|
|
2930
3149
|
}, handleError);
|
|
2931
3150
|
});
|
|
2932
3151
|
return (0, _sentry_core.startSpan)(spanConfig, (span) => {
|
|
2933
3152
|
const originalResult = originalRun.apply(context, args);
|
|
2934
|
-
if (
|
|
3153
|
+
if (recordInputs) addRequestAttributes(span, inputs, operationName);
|
|
3154
|
+
runGenAiSpanStart(options, span, call);
|
|
2935
3155
|
return originalResult.then((result) => {
|
|
2936
|
-
if (!returnsRawResponse) addResponseAttributes(span, result,
|
|
3156
|
+
if (!returnsRawResponse) addResponseAttributes(span, result, recordOutputs, operationName);
|
|
3157
|
+
runGenAiSpanEnd(options, span, call, { response: result });
|
|
2937
3158
|
return result;
|
|
2938
3159
|
}, (error) => {
|
|
2939
3160
|
span.setStatus(getErrorStatus(abortSignal));
|
|
3161
|
+
runGenAiSpanEnd(options, span, call, { error });
|
|
2940
3162
|
throw error;
|
|
2941
3163
|
});
|
|
2942
3164
|
});
|
|
@@ -2958,12 +3180,14 @@ function instrumentRun(originalRun, context, options) {
|
|
|
2958
3180
|
* ```
|
|
2959
3181
|
*/
|
|
2960
3182
|
function instrumentWorkersAiClient(client, options) {
|
|
2961
|
-
const
|
|
2962
|
-
|
|
3183
|
+
const original = originalClients.get(client) ?? client;
|
|
3184
|
+
const instrumented = new Proxy(original, { get(target, prop, receiver) {
|
|
2963
3185
|
const value = Reflect.get(target, prop, receiver);
|
|
2964
|
-
if (prop === "run" && typeof value === "function") return instrumentRun(value, target,
|
|
3186
|
+
if (prop === "run" && typeof value === "function") return instrumentRun(value, target, options ?? {});
|
|
2965
3187
|
return typeof value === "function" ? value.bind(target) : value;
|
|
2966
3188
|
} });
|
|
3189
|
+
originalClients.set(instrumented, original);
|
|
3190
|
+
return instrumented;
|
|
2967
3191
|
}
|
|
2968
3192
|
// src/ai/langchain/constants.ts
|
|
2969
3193
|
const LANGCHAIN_INTEGRATION_NAME = "LangChain";
|
|
@@ -3270,6 +3494,14 @@ function findConversationIdEntry(source) {
|
|
|
3270
3494
|
}
|
|
3271
3495
|
}
|
|
3272
3496
|
/**
|
|
3497
|
+
* The recording decision of the LangGraph graph run that `metadata` belongs to. The graph's `invoke()`/`stream()`
|
|
3498
|
+
* call decides once, and child runs inherit the decision through their metadata.
|
|
3499
|
+
*/
|
|
3500
|
+
function getLangGraphRunRecording(metadata) {
|
|
3501
|
+
const recording = metadata?.__sentry_langgraph__;
|
|
3502
|
+
return (0, _sentry_core.isObjectLike)(recording) ? recording : void 0;
|
|
3503
|
+
}
|
|
3504
|
+
/**
|
|
3273
3505
|
* Derive `gen_ai.conversation.id` from run metadata, which child runs inherit from their parent.
|
|
3274
3506
|
* An id from `Sentry.setConversationId()` still wins, since `conversationIdIntegration` applies it on `spanStart`.
|
|
3275
3507
|
*/
|
|
@@ -3431,7 +3663,10 @@ function extractEmbeddingAttributes(instance) {
|
|
|
3431
3663
|
* span as the prototype-patching path below.
|
|
3432
3664
|
*/
|
|
3433
3665
|
function _INTERNAL_getLangChainEmbeddingsSpanOptions(instance, input, options = {}) {
|
|
3434
|
-
const { recordInputs } =
|
|
3666
|
+
const { recordInputs } = resolveGenAiCallRecording(options, {
|
|
3667
|
+
operation: "embeddings",
|
|
3668
|
+
input
|
|
3669
|
+
});
|
|
3435
3670
|
const attributes = extractEmbeddingAttributes(instance);
|
|
3436
3671
|
const modelName = attributes[_sentry_conventions_attributes.GEN_AI_REQUEST_MODEL] || "unknown";
|
|
3437
3672
|
const client = (0, _sentry_core.getClient)();
|
|
@@ -3485,7 +3720,7 @@ function instrumentLangChainEmbeddings(instance, options) {
|
|
|
3485
3720
|
* This is a stateful handler that tracks spans across multiple LangChain executions.
|
|
3486
3721
|
*/
|
|
3487
3722
|
function createLangChainCallbackHandler(options = {}) {
|
|
3488
|
-
const
|
|
3723
|
+
const resolveRecording = (call) => getLangGraphRunRecording(call.metadata) ?? resolveGenAiCallRecording(options, call);
|
|
3489
3724
|
const runs = new _sentry_core.LRUMap(MAX_TRACKED_LANGCHAIN_RUNS);
|
|
3490
3725
|
const getRun = (runId) => runId ? runs.get(runId) : void 0;
|
|
3491
3726
|
/**
|
|
@@ -3518,7 +3753,7 @@ function createLangChainCallbackHandler(options = {}) {
|
|
|
3518
3753
|
if (!(0, _sentry_core.spanIsIgnored)(run.span) || (0, _sentry_core.getRootSpan)(run.span) === run.span) return run.span;
|
|
3519
3754
|
}
|
|
3520
3755
|
};
|
|
3521
|
-
const startRunSpan = (runId, parentRunId, start) => {
|
|
3756
|
+
const startRunSpan = (runId, parentRunId, recordOutputs, start) => {
|
|
3522
3757
|
const ancestors = getAncestors(parentRunId);
|
|
3523
3758
|
const activeSpan = (0, _sentry_core.getActiveSpan)();
|
|
3524
3759
|
const parentSpan = getParentRunSpan(ancestors, activeSpan);
|
|
@@ -3530,7 +3765,8 @@ function createLangChainCallbackHandler(options = {}) {
|
|
|
3530
3765
|
span,
|
|
3531
3766
|
parentRunId,
|
|
3532
3767
|
activeSpan,
|
|
3533
|
-
conversationId
|
|
3768
|
+
conversationId,
|
|
3769
|
+
recordOutputs
|
|
3534
3770
|
};
|
|
3535
3771
|
trackRun(runId, run);
|
|
3536
3772
|
if (conversationId) for (const ancestor of ancestors) {
|
|
@@ -3569,13 +3805,23 @@ function createLangChainCallbackHandler(options = {}) {
|
|
|
3569
3805
|
ignoreCustomEvent: false,
|
|
3570
3806
|
raiseError: false,
|
|
3571
3807
|
awaitHandlers: true,
|
|
3572
|
-
handleLLMStart(llm, prompts, runId, parentRunId,
|
|
3573
|
-
const
|
|
3574
|
-
const
|
|
3808
|
+
handleLLMStart(llm, prompts, runId, parentRunId, extraParams, tags, metadata, _runName) {
|
|
3809
|
+
const serialized = llm;
|
|
3810
|
+
const { recordInputs, recordOutputs } = resolveRecording({
|
|
3811
|
+
operation: "chat",
|
|
3812
|
+
serialized,
|
|
3813
|
+
prompts,
|
|
3814
|
+
runId,
|
|
3815
|
+
parentRunId,
|
|
3816
|
+
extraParams,
|
|
3817
|
+
tags,
|
|
3818
|
+
metadata
|
|
3819
|
+
});
|
|
3820
|
+
const attributes = extractLLMRequestAttributes(serialized, prompts, recordInputs, getInvocationParams(tags), metadata);
|
|
3575
3821
|
const modelName = attributes[_sentry_conventions_attributes.GEN_AI_REQUEST_MODEL] || "unknown";
|
|
3576
3822
|
const operationName = typeof attributes[_sentry_conventions_attributes.GEN_AI_OPERATION_NAME] === "string" ? attributes[_sentry_conventions_attributes.GEN_AI_OPERATION_NAME] : "unknown";
|
|
3577
3823
|
const client = (0, _sentry_core.getClient)();
|
|
3578
|
-
startRunSpan(runId, parentRunId, () => (0, _sentry_core.startInactiveSpan)({
|
|
3824
|
+
startRunSpan(runId, parentRunId, recordOutputs, () => (0, _sentry_core.startInactiveSpan)({
|
|
3579
3825
|
name: typeof modelName === "string" && modelName !== "unknown" || !(client && (0, _sentry_core.hasSpanStreamingEnabled)(client)) ? `${operationName} ${modelName}` : operationName,
|
|
3580
3826
|
attributes: {
|
|
3581
3827
|
...getAgentNameFromMetadata(metadata),
|
|
@@ -3586,14 +3832,25 @@ function createLangChainCallbackHandler(options = {}) {
|
|
|
3586
3832
|
}));
|
|
3587
3833
|
},
|
|
3588
3834
|
handleChatModelStart(llm, messages, runId, parentRunId, extraParams, tags, metadata, _runName) {
|
|
3589
|
-
const
|
|
3590
|
-
const
|
|
3835
|
+
const serialized = llm;
|
|
3836
|
+
const chatMessages = messages;
|
|
3837
|
+
const { recordInputs, recordOutputs } = resolveRecording({
|
|
3838
|
+
operation: "chat",
|
|
3839
|
+
serialized,
|
|
3840
|
+
messages: chatMessages,
|
|
3841
|
+
runId,
|
|
3842
|
+
parentRunId,
|
|
3843
|
+
extraParams,
|
|
3844
|
+
tags,
|
|
3845
|
+
metadata
|
|
3846
|
+
});
|
|
3847
|
+
const attributes = extractChatModelRequestAttributes(serialized, chatMessages, recordInputs, getInvocationParams(tags), metadata);
|
|
3591
3848
|
const toolDefsJson = recordInputs ? extractToolDefinitions(extraParams) : void 0;
|
|
3592
3849
|
if (toolDefsJson) attributes[_sentry_conventions_attributes.GEN_AI_TOOL_DEFINITIONS] = toolDefsJson;
|
|
3593
3850
|
const modelName = attributes[_sentry_conventions_attributes.GEN_AI_REQUEST_MODEL] || "unknown";
|
|
3594
3851
|
const operationName = typeof attributes[_sentry_conventions_attributes.GEN_AI_OPERATION_NAME] === "string" ? attributes[_sentry_conventions_attributes.GEN_AI_OPERATION_NAME] : "unknown";
|
|
3595
3852
|
const client = (0, _sentry_core.getClient)();
|
|
3596
|
-
startRunSpan(runId, parentRunId, () => (0, _sentry_core.startInactiveSpan)({
|
|
3853
|
+
startRunSpan(runId, parentRunId, recordOutputs, () => (0, _sentry_core.startInactiveSpan)({
|
|
3597
3854
|
name: typeof modelName === "string" && modelName !== "unknown" || !(client && (0, _sentry_core.hasSpanStreamingEnabled)(client)) ? `${operationName} ${modelName}` : operationName,
|
|
3598
3855
|
attributes: {
|
|
3599
3856
|
...getAgentNameFromMetadata(metadata),
|
|
@@ -3603,10 +3860,15 @@ function createLangChainCallbackHandler(options = {}) {
|
|
|
3603
3860
|
}
|
|
3604
3861
|
}));
|
|
3605
3862
|
},
|
|
3606
|
-
|
|
3863
|
+
handleLLMNewToken(_token, _idx, runId) {
|
|
3607
3864
|
const span = runs.get(runId)?.span;
|
|
3608
|
-
if (span
|
|
3609
|
-
|
|
3865
|
+
if (span) recordTimeToFirstChunk(span);
|
|
3866
|
+
},
|
|
3867
|
+
handleLLMEnd(output, runId, _parentRunId, _tags, _extraParams) {
|
|
3868
|
+
const run = runs.get(runId);
|
|
3869
|
+
const span = run?.span;
|
|
3870
|
+
if (run && span?.isRecording()) {
|
|
3871
|
+
const attributes = extractLlmResponseAttributes(output, run.recordOutputs);
|
|
3610
3872
|
if (attributes) span.setAttributes(attributes);
|
|
3611
3873
|
}
|
|
3612
3874
|
exitSpan(runId);
|
|
@@ -3619,12 +3881,30 @@ function createLangChainCallbackHandler(options = {}) {
|
|
|
3619
3881
|
});
|
|
3620
3882
|
exitSpan(runId);
|
|
3621
3883
|
},
|
|
3622
|
-
handleChainStart(chain, inputs, runId, parentRunId,
|
|
3884
|
+
handleChainStart(chain, inputs, runId, parentRunId, tags, metadata, _runType, runName) {
|
|
3623
3885
|
if (isTypeSafeClassifier(chain)) {
|
|
3624
|
-
|
|
3886
|
+
const { recordInputs, recordOutputs } = resolveRecording({
|
|
3887
|
+
operation: "decide",
|
|
3888
|
+
serialized: chain,
|
|
3889
|
+
inputs,
|
|
3890
|
+
runId,
|
|
3891
|
+
parentRunId,
|
|
3892
|
+
tags,
|
|
3893
|
+
metadata
|
|
3894
|
+
});
|
|
3895
|
+
startRunSpan(runId, parentRunId, recordOutputs, () => startTypeSafeClassifierSpan(chain, inputs, metadata, recordInputs)).decide = true;
|
|
3625
3896
|
return;
|
|
3626
3897
|
}
|
|
3627
3898
|
if (metadata?.__sentry_langgraph__) return;
|
|
3899
|
+
const { recordInputs, recordOutputs } = resolveRecording({
|
|
3900
|
+
operation: "invoke_agent",
|
|
3901
|
+
serialized: chain,
|
|
3902
|
+
inputs,
|
|
3903
|
+
runId,
|
|
3904
|
+
parentRunId,
|
|
3905
|
+
tags,
|
|
3906
|
+
metadata
|
|
3907
|
+
});
|
|
3628
3908
|
const chainName = runName || chain.name;
|
|
3629
3909
|
const attributes = {
|
|
3630
3910
|
...getConversationIdFromMetadata(metadata),
|
|
@@ -3634,7 +3914,7 @@ function createLangChainCallbackHandler(options = {}) {
|
|
|
3634
3914
|
if (chainName) attributes[_sentry_conventions_attributes.GEN_AI_PIPELINE_NAME] = chainName;
|
|
3635
3915
|
if (recordInputs) attributes["langchain.chain.inputs"] = JSON.stringify(inputs);
|
|
3636
3916
|
const client = (0, _sentry_core.getClient)();
|
|
3637
|
-
startRunSpan(runId, parentRunId, () => (0, _sentry_core.startInactiveSpan)({
|
|
3917
|
+
startRunSpan(runId, parentRunId, recordOutputs, () => (0, _sentry_core.startInactiveSpan)({
|
|
3638
3918
|
name: !(client && (0, _sentry_core.hasSpanStreamingEnabled)(client)) ? `chain ${chainName || "unknown_chain"}` : chainName ? `invoke_agent ${chainName}` : "invoke_agent",
|
|
3639
3919
|
attributes: {
|
|
3640
3920
|
...attributes,
|
|
@@ -3645,9 +3925,9 @@ function createLangChainCallbackHandler(options = {}) {
|
|
|
3645
3925
|
handleChainEnd(outputs, runId) {
|
|
3646
3926
|
const run = runs.get(runId);
|
|
3647
3927
|
const span = run?.span;
|
|
3648
|
-
if (span?.isRecording()) {
|
|
3649
|
-
if (run
|
|
3650
|
-
else if (recordOutputs) span.setAttributes({ "langchain.chain.outputs": JSON.stringify(outputs) });
|
|
3928
|
+
if (run && span?.isRecording()) {
|
|
3929
|
+
if (run.decide) addTypeSafeClassifierResponseAttributes(span, outputs, run.recordOutputs);
|
|
3930
|
+
else if (run.recordOutputs) span.setAttributes({ "langchain.chain.outputs": JSON.stringify(outputs) });
|
|
3651
3931
|
}
|
|
3652
3932
|
exitSpan(runId);
|
|
3653
3933
|
},
|
|
@@ -3659,8 +3939,17 @@ function createLangChainCallbackHandler(options = {}) {
|
|
|
3659
3939
|
});
|
|
3660
3940
|
exitSpan(runId);
|
|
3661
3941
|
},
|
|
3662
|
-
handleToolStart(tool, input, runId, parentRunId,
|
|
3942
|
+
handleToolStart(tool, input, runId, parentRunId, tags, metadata, runName) {
|
|
3663
3943
|
if (metadata?.__sentry_langgraph__) return;
|
|
3944
|
+
const { recordInputs, recordOutputs } = resolveRecording({
|
|
3945
|
+
operation: "execute_tool",
|
|
3946
|
+
serialized: tool,
|
|
3947
|
+
input,
|
|
3948
|
+
runId,
|
|
3949
|
+
parentRunId,
|
|
3950
|
+
tags,
|
|
3951
|
+
metadata
|
|
3952
|
+
});
|
|
3664
3953
|
const toolName = runName || tool.name || "unknown_tool";
|
|
3665
3954
|
const attributes = {
|
|
3666
3955
|
...getAgentNameFromMetadata(metadata),
|
|
@@ -3670,7 +3959,7 @@ function createLangChainCallbackHandler(options = {}) {
|
|
|
3670
3959
|
[_sentry_conventions_attributes.GEN_AI_TOOL_NAME]: toolName
|
|
3671
3960
|
};
|
|
3672
3961
|
if (recordInputs) attributes[_sentry_conventions_attributes.GEN_AI_TOOL_CALL_ARGUMENTS] = input;
|
|
3673
|
-
startRunSpan(runId, parentRunId, () => (0, _sentry_core.startInactiveSpan)({
|
|
3962
|
+
startRunSpan(runId, parentRunId, recordOutputs, () => (0, _sentry_core.startInactiveSpan)({
|
|
3674
3963
|
name: `execute_tool ${toolName}`,
|
|
3675
3964
|
attributes: {
|
|
3676
3965
|
...attributes,
|
|
@@ -3679,9 +3968,10 @@ function createLangChainCallbackHandler(options = {}) {
|
|
|
3679
3968
|
}));
|
|
3680
3969
|
},
|
|
3681
3970
|
handleToolEnd(output, runId) {
|
|
3682
|
-
const
|
|
3683
|
-
|
|
3684
|
-
|
|
3971
|
+
const run = runs.get(runId);
|
|
3972
|
+
const span = run?.span;
|
|
3973
|
+
if (run && span?.isRecording()) {
|
|
3974
|
+
if (run.recordOutputs) {
|
|
3685
3975
|
const outputObj = output;
|
|
3686
3976
|
const content = outputObj && typeof outputObj === "object" && "content" in outputObj ? outputObj.content : output;
|
|
3687
3977
|
span.setAttributes({ [_sentry_conventions_attributes.GEN_AI_TOOL_CALL_RESULT]: (0, _sentry_core.stringify)(content, String) });
|
|
@@ -3739,6 +4029,14 @@ function extractAgentNameFromParams(args) {
|
|
|
3739
4029
|
if (typeof arg === "object" && !!arg && "name" in arg && typeof arg.name === "string") return arg.name;
|
|
3740
4030
|
return null;
|
|
3741
4031
|
}
|
|
4032
|
+
/** The options that hold outside a graph run, where no `invoke()`/`stream()` call decided: a function needs the graph input. */
|
|
4033
|
+
function getBooleanRecordingOptions(options) {
|
|
4034
|
+
const { recordInputs, recordOutputs } = options;
|
|
4035
|
+
return {
|
|
4036
|
+
recordInputs: typeof recordInputs === "boolean" ? recordInputs : void 0,
|
|
4037
|
+
recordOutputs: typeof recordOutputs === "boolean" ? recordOutputs : void 0
|
|
4038
|
+
};
|
|
4039
|
+
}
|
|
3742
4040
|
/**
|
|
3743
4041
|
* Wraps an array of LangChain tools so each invocation creates a gen_ai.execute_tool span.
|
|
3744
4042
|
*
|
|
@@ -3760,15 +4058,16 @@ function wrapToolsWithSpans(tools, options, agentName) {
|
|
|
3760
4058
|
[_sentry_conventions_attributes.GEN_AI_OPERATION_NAME]: "execute_tool",
|
|
3761
4059
|
[_sentry_conventions_attributes.GEN_AI_TOOL_NAME]: toolName
|
|
3762
4060
|
};
|
|
3763
|
-
const
|
|
3764
|
-
const
|
|
4061
|
+
const metadata = args[1]?.metadata;
|
|
4062
|
+
const { recordInputs, recordOutputs } = getLangGraphRunRecording(metadata) ?? resolveAIRecordingOptions(getBooleanRecordingOptions(options));
|
|
4063
|
+
const callAgentName = metadata?.lc_agent_name ?? agentName;
|
|
3765
4064
|
if (typeof callAgentName === "string") spanAttributes[_sentry_conventions_attributes.GEN_AI_AGENT_NAME] = callAgentName;
|
|
3766
|
-
Object.assign(spanAttributes, getConversationIdFromMetadata(
|
|
4065
|
+
Object.assign(spanAttributes, getConversationIdFromMetadata(metadata));
|
|
3767
4066
|
if (toolDescription) spanAttributes[_sentry_conventions_attributes.GEN_AI_TOOL_DESCRIPTION] = toolDescription;
|
|
3768
4067
|
const input = args[0];
|
|
3769
4068
|
if (typeof input === "object" && !!input) {
|
|
3770
4069
|
if ("id" in input && typeof input.id === "string") spanAttributes[GEN_AI_TOOL_CALL_ID_ATTRIBUTE] = input.id;
|
|
3771
|
-
if (
|
|
4070
|
+
if (recordInputs) {
|
|
3772
4071
|
const toolArgs = "args" in input && typeof input.args === "object" ? input.args : input;
|
|
3773
4072
|
try {
|
|
3774
4073
|
spanAttributes[_sentry_conventions_attributes.GEN_AI_TOOL_CALL_ARGUMENTS] = JSON.stringify(toolArgs);
|
|
@@ -3781,7 +4080,7 @@ function wrapToolsWithSpans(tools, options, agentName) {
|
|
|
3781
4080
|
}, async (span) => {
|
|
3782
4081
|
try {
|
|
3783
4082
|
const result = await Reflect.apply(target, thisArg, args);
|
|
3784
|
-
if (
|
|
4083
|
+
if (recordOutputs) try {
|
|
3785
4084
|
const resultObj = result;
|
|
3786
4085
|
const content = resultObj && typeof resultObj === "object" && "content" in resultObj ? resultObj.content : result;
|
|
3787
4086
|
span.setAttribute(_sentry_conventions_attributes.GEN_AI_TOOL_CALL_RESULT, typeof content === "string" ? content : JSON.stringify(content));
|
|
@@ -3927,8 +4226,16 @@ function getAgentSpanOptions(args, extraAttributes) {
|
|
|
3927
4226
|
}
|
|
3928
4227
|
};
|
|
3929
4228
|
}
|
|
4229
|
+
/** Decides for one `invoke()`/`stream()` call, from its arguments as passed. */
|
|
4230
|
+
function resolveGraphCallRecording(options, args) {
|
|
4231
|
+
return resolveGenAiCallRecording(options, {
|
|
4232
|
+
operation: "invoke_agent",
|
|
4233
|
+
input: args[0],
|
|
4234
|
+
config: args[1]
|
|
4235
|
+
});
|
|
4236
|
+
}
|
|
3930
4237
|
/** 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,
|
|
4238
|
+
function prepareAgentSpan(span, args, graphInstance, compileOptions, recording, llm, sentryCallbackHandler) {
|
|
3932
4239
|
const graphName = compileOptions?.name;
|
|
3933
4240
|
if (graphName && typeof graphName === "string") {
|
|
3934
4241
|
span.setAttribute(_sentry_conventions_attributes.GEN_AI_PIPELINE_NAME, graphName);
|
|
@@ -3946,12 +4253,12 @@ function prepareAgentSpan(span, args, graphInstance, compileOptions, options, ll
|
|
|
3946
4253
|
config.metadata = {
|
|
3947
4254
|
...getConversationIdMetadataFromConfig(config),
|
|
3948
4255
|
...existingMetadata,
|
|
3949
|
-
__sentry_langgraph__:
|
|
4256
|
+
__sentry_langgraph__: recording,
|
|
3950
4257
|
...typeof graphName === "string" ? { lc_agent_name: graphName } : {}
|
|
3951
4258
|
};
|
|
3952
4259
|
config.callbacks = _INTERNAL_mergeLangChainCallbackHandler(config.callbacks, sentryCallbackHandler);
|
|
3953
4260
|
}
|
|
3954
|
-
const recordInputs =
|
|
4261
|
+
const { recordInputs } = recording;
|
|
3955
4262
|
const tools = recordInputs ? extractToolsFromCompiledGraph(graphInstance) : null;
|
|
3956
4263
|
if (tools) span.setAttribute(_sentry_conventions_attributes.GEN_AI_TOOL_DEFINITIONS, JSON.stringify(tools));
|
|
3957
4264
|
const inputState = args[0];
|
|
@@ -3979,6 +4286,7 @@ function prepareAgentSpan(span, args, graphInstance, compileOptions, options, ll
|
|
|
3979
4286
|
function instrumentCompiledGraphStream(originalStream, graphInstance, compileOptions, options, llm, sentryCallbackHandler) {
|
|
3980
4287
|
return new Proxy(originalStream, { apply(target, thisArg, args) {
|
|
3981
4288
|
if (isInvokeInternalStream(graphInstance)) return Reflect.apply(target, thisArg, args);
|
|
4289
|
+
const recording = resolveGraphCallRecording(options, args);
|
|
3982
4290
|
return (0, _sentry_core.startSpanManual)(getAgentSpanOptions(args, { [_sentry_conventions_attributes.GEN_AI_RESPONSE_STREAMING]: true }), async (span) => {
|
|
3983
4291
|
let inputMessages = null;
|
|
3984
4292
|
let stream;
|
|
@@ -3992,7 +4300,7 @@ function instrumentCompiledGraphStream(originalStream, graphInstance, compileOpt
|
|
|
3992
4300
|
code: _sentry_core.SPAN_STATUS_ERROR,
|
|
3993
4301
|
message: "internal_error"
|
|
3994
4302
|
});
|
|
3995
|
-
} else if (outcome &&
|
|
4303
|
+
} else if (outcome && recording.recordOutputs) setResponseAttributes(span, inputMessages, outcome.outputs);
|
|
3996
4304
|
} catch {}
|
|
3997
4305
|
span.end();
|
|
3998
4306
|
inputMessages = null;
|
|
@@ -4015,7 +4323,7 @@ function instrumentCompiledGraphStream(originalStream, graphInstance, compileOpt
|
|
|
4015
4323
|
}
|
|
4016
4324
|
};
|
|
4017
4325
|
try {
|
|
4018
|
-
inputMessages = prepareAgentSpan(span, args, graphInstance, compileOptions,
|
|
4326
|
+
inputMessages = prepareAgentSpan(span, args, graphInstance, compileOptions, recording, llm, sentryCallbackHandler);
|
|
4019
4327
|
const config = args[1];
|
|
4020
4328
|
config.callbacks = _INTERNAL_addLangChainCallbackHandler(config.callbacks, listener);
|
|
4021
4329
|
stream = await Reflect.apply(target, thisArg, args);
|
|
@@ -4071,10 +4379,9 @@ function runManually(callback) {
|
|
|
4071
4379
|
* Instruments StateGraph's compile method to wrap the returned compiled graph's invoke() and stream() with a
|
|
4072
4380
|
* `gen_ai.invoke_agent` span.
|
|
4073
4381
|
*/
|
|
4074
|
-
function instrumentStateGraphCompile(originalCompile,
|
|
4382
|
+
function instrumentStateGraphCompile(originalCompile, options) {
|
|
4075
4383
|
if (Object.prototype.hasOwnProperty.call(originalCompile, SENTRY_PATCHED)) return originalCompile;
|
|
4076
|
-
const
|
|
4077
|
-
const sentryHandler = createLangChainCallbackHandler(options);
|
|
4384
|
+
const sentryHandler = createLangChainCallbackHandler(getBooleanRecordingOptions(options));
|
|
4078
4385
|
const wrapped = new Proxy(originalCompile, { apply(target, thisArg, args) {
|
|
4079
4386
|
if (_insideManualInstrumentation) return Reflect.apply(target, thisArg, args);
|
|
4080
4387
|
const compiledGraph = runManually(() => Reflect.apply(target, thisArg, args));
|
|
@@ -4105,11 +4412,12 @@ function instrumentCompiledGraph(graph, compileOptions, options, llm, sentryCall
|
|
|
4105
4412
|
*/
|
|
4106
4413
|
function instrumentCompiledGraphInvoke(originalInvoke, graphInstance, compileOptions, options, llm, sentryCallbackHandler) {
|
|
4107
4414
|
return new Proxy(originalInvoke, { apply(target, thisArg, args) {
|
|
4415
|
+
const recording = resolveGraphCallRecording(options, args);
|
|
4108
4416
|
return (0, _sentry_core.startSpan)(getAgentSpanOptions(args), async (span) => {
|
|
4109
4417
|
try {
|
|
4110
|
-
const inputMessages = prepareAgentSpan(span, args, graphInstance, compileOptions,
|
|
4418
|
+
const inputMessages = prepareAgentSpan(span, args, graphInstance, compileOptions, recording, llm, sentryCallbackHandler);
|
|
4111
4419
|
const result = await callInvoke(graphInstance, () => Reflect.apply(target, thisArg, args));
|
|
4112
|
-
if (
|
|
4420
|
+
if (recording.recordOutputs) setResponseAttributes(span, inputMessages, result);
|
|
4113
4421
|
return result;
|
|
4114
4422
|
} catch (error) {
|
|
4115
4423
|
span.setStatus({
|
|
@@ -4124,17 +4432,16 @@ function instrumentCompiledGraphInvoke(originalInvoke, graphInstance, compileOpt
|
|
|
4124
4432
|
/**
|
|
4125
4433
|
* Instruments createReactAgent to create invoke_agent and execute_tool spans.
|
|
4126
4434
|
*/
|
|
4127
|
-
function instrumentCreateReactAgent(originalCreateReactAgent, options) {
|
|
4435
|
+
function instrumentCreateReactAgent(originalCreateReactAgent, options = {}) {
|
|
4128
4436
|
if (Object.prototype.hasOwnProperty.call(originalCreateReactAgent, SENTRY_PATCHED)) return originalCreateReactAgent;
|
|
4129
|
-
const
|
|
4130
|
-
const sentryHandler = createLangChainCallbackHandler(resolvedOptions);
|
|
4437
|
+
const sentryHandler = createLangChainCallbackHandler(getBooleanRecordingOptions(options));
|
|
4131
4438
|
const wrapped = new Proxy(originalCreateReactAgent, { apply(target, thisArg, args) {
|
|
4132
4439
|
const llm = extractLLMFromParams(args);
|
|
4133
4440
|
const agentName = extractAgentNameFromParams(args);
|
|
4134
4441
|
const params = args[0];
|
|
4135
|
-
if (params && Array.isArray(params.tools) && params.tools.length > 0) wrapToolsWithSpans(params.tools,
|
|
4442
|
+
if (params && Array.isArray(params.tools) && params.tools.length > 0) wrapToolsWithSpans(params.tools, options, agentName ?? void 0);
|
|
4136
4443
|
const compiledGraph = runManually(() => Reflect.apply(target, thisArg, args));
|
|
4137
|
-
instrumentCompiledGraph(compiledGraph, agentName ? { name: agentName } : {},
|
|
4444
|
+
instrumentCompiledGraph(compiledGraph, agentName ? { name: agentName } : {}, options, llm, sentryHandler);
|
|
4138
4445
|
return compiledGraph;
|
|
4139
4446
|
} });
|
|
4140
4447
|
Object.defineProperty(wrapped, SENTRY_PATCHED, {
|
|
@@ -4166,8 +4473,8 @@ function instrumentCreateReactAgent(originalCreateReactAgent, options) {
|
|
|
4166
4473
|
* const compiled = graph.compile({ name: 'my_agent' });
|
|
4167
4474
|
* ```
|
|
4168
4475
|
*/
|
|
4169
|
-
function instrumentStateGraph(stateGraph, options) {
|
|
4170
|
-
stateGraph.compile = instrumentStateGraphCompile(stateGraph.compile,
|
|
4476
|
+
function instrumentStateGraph(stateGraph, options = {}) {
|
|
4477
|
+
stateGraph.compile = instrumentStateGraphCompile(stateGraph.compile, options);
|
|
4171
4478
|
return stateGraph;
|
|
4172
4479
|
}
|
|
4173
4480
|
// src/ai/mastra/constants.ts
|
|
@@ -4309,10 +4616,7 @@ function mergeUsageAttributes(existing, incoming) {
|
|
|
4309
4616
|
/** Convention attributes only. Called on start and end — Mastra fills fields progressively. */
|
|
4310
4617
|
function getSpanAttributes(span, options) {
|
|
4311
4618
|
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
|
-
};
|
|
4619
|
+
const attributes = { [_sentry_conventions_attributes.GEN_AI_OPERATION_NAME]: operationName };
|
|
4316
4620
|
if (AGENT_SPAN_TYPES.has(span.type)) addAgentAttributes(attributes, span, options);
|
|
4317
4621
|
if (MODEL_SPAN_TYPES.has(span.type) || span.type === "rag_embedding") addModelAttributes(attributes, span, options);
|
|
4318
4622
|
if (TOOL_SPAN_TYPES.has(span.type)) addToolAttributes(attributes, span, options);
|
|
@@ -4343,6 +4647,7 @@ function addModelAttributes(attributes, span, options) {
|
|
|
4343
4647
|
attributes[_sentry_conventions_attributes.GEN_AI_RESPONSE_MODEL] = attrs.responseModel;
|
|
4344
4648
|
attributes[_sentry_conventions_attributes.GEN_AI_RESPONSE_ID] = attrs.responseId;
|
|
4345
4649
|
attributes[_sentry_conventions_attributes.GEN_AI_RESPONSE_STREAMING] = attrs.streaming;
|
|
4650
|
+
attributes[_sentry_conventions_attributes.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK] = attrs.completionStartTime ? (attrs.completionStartTime.getTime() - span.startTime.getTime()) / 1e3 : void 0;
|
|
4346
4651
|
attributes[_sentry_conventions_attributes.GEN_AI_RESPONSE_FINISH_REASONS] = attrs.finishReason ? JSON.stringify([attrs.finishReason]) : void 0;
|
|
4347
4652
|
attributes[_sentry_conventions_attributes.GEN_AI_AGENT_NAME] = span.entityName;
|
|
4348
4653
|
attributes[_sentry_conventions_attributes.GEN_AI_REQUEST_TEMPERATURE] = parameters.temperature;
|
|
@@ -4467,7 +4772,7 @@ var SentryMastraExporter = class {
|
|
|
4467
4772
|
/** End open spans, then flush. Does not close the Sentry client. */
|
|
4468
4773
|
async shutdown() {
|
|
4469
4774
|
for (const id of this._spans.keys()) unregisterMastraSpan(id);
|
|
4470
|
-
for (const
|
|
4775
|
+
for (const tracked of this._spans.values()) this._endSpan(tracked, {});
|
|
4471
4776
|
this._spans.clear();
|
|
4472
4777
|
this._skipped.clear();
|
|
4473
4778
|
await this.flush();
|
|
@@ -4480,18 +4785,19 @@ var SentryMastraExporter = class {
|
|
|
4480
4785
|
else if (event.type === "span_ended") this._skipped.remove(span.id);
|
|
4481
4786
|
return;
|
|
4482
4787
|
}
|
|
4483
|
-
const recordingOptions = resolveAIRecordingOptions(this._options);
|
|
4484
4788
|
switch (event.type) {
|
|
4485
4789
|
case "span_started":
|
|
4486
|
-
this._onSpanStarted(span
|
|
4790
|
+
this._onSpanStarted(span);
|
|
4487
4791
|
break;
|
|
4488
4792
|
case "span_updated":
|
|
4489
|
-
this._onSpanUpdated(span
|
|
4793
|
+
this._onSpanUpdated(span);
|
|
4490
4794
|
break;
|
|
4491
|
-
case "span_ended": this._onSpanEnded(span
|
|
4795
|
+
case "span_ended": this._onSpanEnded(span);
|
|
4492
4796
|
}
|
|
4493
4797
|
}
|
|
4494
|
-
_onSpanStarted(span
|
|
4798
|
+
_onSpanStarted(span) {
|
|
4799
|
+
const call = { span };
|
|
4800
|
+
const recordingOptions = resolveGenAiCallRecording(this._options, call);
|
|
4495
4801
|
const parentId = this._resolveParentId(span.parentSpanId);
|
|
4496
4802
|
const parentSpan = parentId ? this._spans.get(parentId)?.span : void 0;
|
|
4497
4803
|
const activeSpan = (0, _sentry_core.getActiveSpan)();
|
|
@@ -4501,10 +4807,12 @@ var SentryMastraExporter = class {
|
|
|
4501
4807
|
parentSpan: parentSpan ?? activeSpan,
|
|
4502
4808
|
attributes: {
|
|
4503
4809
|
...getSpanAttributes(span, recordingOptions),
|
|
4810
|
+
[_sentry_conventions_attributes.GEN_AI_CONVERSATION_ID]: span.attributes?.conversationId ?? span.metadata?.threadId,
|
|
4504
4811
|
[_sentry_conventions_attributes.SENTRY_OP]: getOperation(span.type)?.op,
|
|
4505
4812
|
[_sentry_conventions_attributes.SENTRY_ORIGIN]: MASTRA_ORIGIN
|
|
4506
4813
|
}
|
|
4507
4814
|
});
|
|
4815
|
+
runGenAiSpanStart(this._options, sentrySpan, call);
|
|
4508
4816
|
const evaluation = span.type === "classifier_evaluation" ? takeStartingClassifierEvaluation() : void 0;
|
|
4509
4817
|
if (evaluation) Object.assign(evaluation, {
|
|
4510
4818
|
span: sentrySpan,
|
|
@@ -4515,37 +4823,47 @@ var SentryMastraExporter = class {
|
|
|
4515
4823
|
});
|
|
4516
4824
|
this._trackSpan(span.id, {
|
|
4517
4825
|
span: sentrySpan,
|
|
4826
|
+
call,
|
|
4518
4827
|
spanType: span.type,
|
|
4519
4828
|
usage: {},
|
|
4829
|
+
recordingOptions,
|
|
4520
4830
|
evaluation
|
|
4521
4831
|
});
|
|
4522
4832
|
}
|
|
4523
4833
|
/** Track a started span, ending any Sentry span that would otherwise be dropped without `end()`. */
|
|
4524
4834
|
_trackSpan(id, tracked) {
|
|
4525
|
-
this.
|
|
4835
|
+
this._endTracked(id);
|
|
4526
4836
|
if (this._spans.size >= 1e3) {
|
|
4527
4837
|
const oldestId = this._spans.keys()[0];
|
|
4528
|
-
if (oldestId !== void 0) this.
|
|
4838
|
+
if (oldestId !== void 0) this._endTracked(oldestId);
|
|
4529
4839
|
}
|
|
4530
4840
|
this._spans.set(id, tracked);
|
|
4531
4841
|
registerMastraSpan(id, tracked.span);
|
|
4532
4842
|
}
|
|
4843
|
+
_endTracked(id) {
|
|
4844
|
+
const tracked = this._removeTracked(id);
|
|
4845
|
+
if (tracked) this._endSpan(tracked, {});
|
|
4846
|
+
}
|
|
4847
|
+
_endSpan(tracked, result, endTime) {
|
|
4848
|
+
runGenAiSpanEnd(this._options, tracked.span, tracked.call, result);
|
|
4849
|
+
tracked.span.end(endTime);
|
|
4850
|
+
}
|
|
4533
4851
|
/** Remove a tracked span and keep the id→span registry in lockstep. */
|
|
4534
4852
|
_removeTracked(id) {
|
|
4535
4853
|
unregisterMastraSpan(id);
|
|
4536
4854
|
return this._spans.remove(id);
|
|
4537
4855
|
}
|
|
4538
|
-
_onSpanUpdated(span
|
|
4856
|
+
_onSpanUpdated(span) {
|
|
4539
4857
|
const tracked = this._spans.get(span.id);
|
|
4540
|
-
if (tracked) tracked.span.setAttributes(getSpanAttributes(span, recordingOptions));
|
|
4858
|
+
if (tracked) tracked.span.setAttributes(getSpanAttributes(span, tracked.recordingOptions));
|
|
4541
4859
|
}
|
|
4542
|
-
_onSpanEnded(span
|
|
4860
|
+
_onSpanEnded(span) {
|
|
4543
4861
|
const tracked = this._spans.get(span.id);
|
|
4544
4862
|
if (!tracked) {
|
|
4545
4863
|
require_debug_build.DEBUG_BUILD && _sentry_core.debug.warn(`[Mastra] no Sentry span open for ended span ${span.id} (${span.name})`);
|
|
4546
4864
|
return;
|
|
4547
4865
|
}
|
|
4548
|
-
const { span: sentrySpan } = tracked;
|
|
4866
|
+
const { span: sentrySpan, recordingOptions } = tracked;
|
|
4549
4867
|
sentrySpan.setAttributes(getSpanAttributes(span, recordingOptions));
|
|
4550
4868
|
sentrySpan.updateName(getSpanName(span));
|
|
4551
4869
|
if (MODEL_SPAN_TYPES.has(span.type)) this._rollUpToParentAgent(span);
|
|
@@ -4553,9 +4871,12 @@ var SentryMastraExporter = class {
|
|
|
4553
4871
|
code: _sentry_core.SPAN_STATUS_ERROR,
|
|
4554
4872
|
message: span.errorInfo.message
|
|
4555
4873
|
});
|
|
4874
|
+
const result = span.errorInfo ? { error: span.errorInfo } : { response: span };
|
|
4556
4875
|
const { evaluation } = tracked;
|
|
4557
|
-
if (evaluation && !evaluation.settled)
|
|
4558
|
-
|
|
4876
|
+
if (evaluation && !evaluation.settled) {
|
|
4877
|
+
const endTime = span.endTime ?? (0, _sentry_core.timestampInSeconds)();
|
|
4878
|
+
evaluation.end = () => this._endSpan(tracked, result, endTime);
|
|
4879
|
+
} else this._endSpan(tracked, result, span.endTime);
|
|
4559
4880
|
this._removeTracked(span.id);
|
|
4560
4881
|
}
|
|
4561
4882
|
/** Copy model/usage onto the parent agent so the AI Agents view shows totals. */
|
|
@@ -4636,7 +4957,7 @@ function detectOrchestrionSetup() {
|
|
|
4636
4957
|
if (!marker.warnedBunRunNotInjected) {
|
|
4637
4958
|
marker.warnedBunRunNotInjected = true;
|
|
4638
4959
|
(0, _sentry_core.consoleSandbox)(() => {
|
|
4639
|
-
console.warn("[Sentry] No diagnostics-channel injection detected. Channel-based integrations will not record spans or capture errors
|
|
4960
|
+
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
4961
|
});
|
|
4641
4962
|
}
|
|
4642
4963
|
return;
|
|
@@ -4648,26 +4969,44 @@ function detectOrchestrionSetup() {
|
|
|
4648
4969
|
_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
4970
|
}
|
|
4650
4971
|
// 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
|
-
|
|
4972
|
+
const DRIVER_KEY = "piAiDriver";
|
|
4973
|
+
/**
|
|
4974
|
+
* Apply the recording options and span hooks of an integration that sends its model calls through
|
|
4975
|
+
* pi-ai, such as Flue, to the `gen_ai.chat` spans of the requests sent under `scope`. Options and
|
|
4976
|
+
* hooks that are functions get `call`.
|
|
4977
|
+
*/
|
|
4978
|
+
function setPiAiDriverOptions(scope, options, call) {
|
|
4979
|
+
const driver = {
|
|
4980
|
+
recording: () => ({
|
|
4981
|
+
recordInputs: resolveRecordingOption(options.recordInputs, call),
|
|
4982
|
+
recordOutputs: resolveRecordingOption(options.recordOutputs, call)
|
|
4983
|
+
}),
|
|
4984
|
+
onSpanStart: (span) => runGenAiSpanStart(options, span, call),
|
|
4985
|
+
onSpanEnd: (span, result) => runGenAiSpanEnd(options, span, call, result)
|
|
4986
|
+
};
|
|
4987
|
+
scope.setSDKProcessingMetadata({ [DRIVER_KEY]: driver });
|
|
4988
|
+
}
|
|
4989
|
+
/** The integration driving the pi-ai requests sent from the current scope, if any. */
|
|
4990
|
+
function getPiAiDriver() {
|
|
4991
|
+
return (0, _sentry_core.getCurrentScope)().getScopeData().sdkProcessingMetadata[DRIVER_KEY];
|
|
4658
4992
|
}
|
|
4659
4993
|
/**
|
|
4660
4994
|
* 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
|
|
4995
|
+
* scope it was sent from. An explicit `false` on either integration, or from either one's function,
|
|
4996
|
+
* keeps the content out, since an opt-out must not depend on which integration it was set on.
|
|
4997
|
+
* Otherwise the integration driving pi-ai comes first, then `piAiIntegration`, then
|
|
4998
|
+
* `dataCollection.genAI`.
|
|
4999
|
+
*/
|
|
5000
|
+
function resolvePiAiRecordingOptions(options, call, driver = getPiAiDriver()) {
|
|
5001
|
+
const driverRecording = driver?.recording();
|
|
5002
|
+
const own = {
|
|
5003
|
+
recordInputs: resolveRecordingOption(options.recordInputs, call),
|
|
5004
|
+
recordOutputs: resolveRecordingOption(options.recordOutputs, call)
|
|
5005
|
+
};
|
|
5006
|
+
const { recordInputs, recordOutputs } = resolveGenAiCallRecording(own, call);
|
|
4668
5007
|
return {
|
|
4669
|
-
recordInputs:
|
|
4670
|
-
recordOutputs:
|
|
5008
|
+
recordInputs: own.recordInputs !== false && (driverRecording?.recordInputs ?? recordInputs),
|
|
5009
|
+
recordOutputs: own.recordOutputs !== false && (driverRecording?.recordOutputs ?? recordOutputs)
|
|
4671
5010
|
};
|
|
4672
5011
|
}
|
|
4673
5012
|
// src/ai/flue/constants.ts
|
|
@@ -4837,16 +5176,8 @@ function piAiPartToGenAiPart(part) {
|
|
|
4837
5176
|
}
|
|
4838
5177
|
}
|
|
4839
5178
|
// 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
|
-
}
|
|
5179
|
+
const TOOL_SPANS_RECORDING_OUTPUTS = /* @__PURE__ */ new WeakSet();
|
|
5180
|
+
const TOOL_SPAN_END_HOOKS = /* @__PURE__ */ new WeakMap();
|
|
4850
5181
|
/**
|
|
4851
5182
|
* Store a span under `key`. Callers only reach this with a key the tracker does not hold, so the
|
|
4852
5183
|
* one span at risk of being dropped without `end()` is the oldest entry, which `LRUMap.set` silently
|
|
@@ -4887,10 +5218,16 @@ function captureToolError(span, errorInfo) {
|
|
|
4887
5218
|
* OpenTelemetry adapter projects them: siblings of `chat`, correlated to model output by tool call
|
|
4888
5219
|
* id. Keyed by `toolCallId` so concurrent tool calls in one turn cannot cross-attribute.
|
|
4889
5220
|
*/
|
|
4890
|
-
function startToolSpan(observation, toolSpans,
|
|
5221
|
+
function startToolSpan(observation, context, toolSpans, options) {
|
|
4891
5222
|
const { toolCallId, toolName } = observation;
|
|
4892
5223
|
if (!toolCallId || toolSpans.get(toolCallId)) return;
|
|
4893
|
-
|
|
5224
|
+
const call = {
|
|
5225
|
+
operation: "execute_tool",
|
|
5226
|
+
observation,
|
|
5227
|
+
context
|
|
5228
|
+
};
|
|
5229
|
+
const { recordInputs, recordOutputs } = resolveGenAiCallRecording(options, call);
|
|
5230
|
+
const span = (0, _sentry_core.startInactiveSpan)({
|
|
4894
5231
|
name: `execute_tool ${toolName ?? "unknown"}`,
|
|
4895
5232
|
op: getGenAiSpanOp("execute_tool"),
|
|
4896
5233
|
attributes: {
|
|
@@ -4900,14 +5237,22 @@ function startToolSpan(observation, toolSpans, recordInputs) {
|
|
|
4900
5237
|
[_sentry_conventions_attributes.GEN_AI_CONVERSATION_ID]: observation.conversationId,
|
|
4901
5238
|
...recordInputs && observation.args !== void 0 ? { [_sentry_conventions_attributes.GEN_AI_TOOL_CALL_ARGUMENTS]: (0, _sentry_core.stringify)(observation.args) } : {}
|
|
4902
5239
|
}
|
|
4903
|
-
})
|
|
4904
|
-
|
|
4905
|
-
|
|
5240
|
+
});
|
|
5241
|
+
if (observation.conversationId) span.setAttribute(_sentry_conventions_attributes.GEN_AI_CONVERSATION_ID, observation.conversationId);
|
|
5242
|
+
if (recordOutputs) TOOL_SPANS_RECORDING_OUTPUTS.add(span);
|
|
5243
|
+
runGenAiSpanStart(options, span, call);
|
|
5244
|
+
TOOL_SPAN_END_HOOKS.set(span, (end) => runGenAiSpanEnd(options, span, call, end.isError ? {
|
|
5245
|
+
response: end,
|
|
5246
|
+
error: end.errorInfo
|
|
5247
|
+
} : { response: end }));
|
|
5248
|
+
trackSpan(toolSpans, toolCallId, span);
|
|
5249
|
+
}
|
|
5250
|
+
function endToolSpan(observation, toolSpans) {
|
|
4906
5251
|
const { toolCallId } = observation;
|
|
4907
5252
|
const span = toolCallId ? toolSpans.get(toolCallId) : void 0;
|
|
4908
5253
|
if (!span || !toolCallId) return;
|
|
4909
5254
|
toolSpans.remove(toolCallId);
|
|
4910
|
-
if (
|
|
5255
|
+
if (TOOL_SPANS_RECORDING_OUTPUTS.has(span)) {
|
|
4911
5256
|
const result = toolResultForModel(observation);
|
|
4912
5257
|
if (result !== void 0) span.setAttribute(_sentry_conventions_attributes.GEN_AI_TOOL_CALL_RESULT, result);
|
|
4913
5258
|
}
|
|
@@ -4918,6 +5263,7 @@ function endToolSpan(observation, toolSpans, recordOutputs) {
|
|
|
4918
5263
|
});
|
|
4919
5264
|
captureToolError(span, observation.errorInfo);
|
|
4920
5265
|
}
|
|
5266
|
+
TOOL_SPAN_END_HOOKS.get(span)?.(observation);
|
|
4921
5267
|
span.end();
|
|
4922
5268
|
}
|
|
4923
5269
|
/**
|
|
@@ -4950,7 +5296,7 @@ function warnIfPiAiNotInstrumented() {
|
|
|
4950
5296
|
});
|
|
4951
5297
|
}
|
|
4952
5298
|
function getPiAiInstrumentationHint() {
|
|
4953
|
-
if (isBun()) return "
|
|
5299
|
+
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
5300
|
if (isDeno()) return "Run your app with `deno run --preload=npm:@sentry/deno/import app.ts`.";
|
|
4955
5301
|
if (isCloudflareWorkers()) return "Add `sentryCloudflareVitePlugin()` from `@sentry/cloudflare/vite` to your Vite config.";
|
|
4956
5302
|
return "Load Sentry with `--import`, for example `node --import ./instrument.mjs app.mjs`.";
|
|
@@ -4979,8 +5325,8 @@ function runInAdmissionContext(admission, next) {
|
|
|
4979
5325
|
* as a `gen_ai.chat` span under the agent span.
|
|
4980
5326
|
*
|
|
4981
5327
|
* 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.
|
|
5328
|
+
* current client's `dataCollection.genAI` settings and are decided when each tool span starts. The
|
|
5329
|
+
* same options apply to the message content of the `gen_ai.chat` spans.
|
|
4984
5330
|
*/
|
|
4985
5331
|
function createFlueInstrumentation(options = {}) {
|
|
4986
5332
|
const agentSpans = /* @__PURE__ */ new Map();
|
|
@@ -4998,7 +5344,11 @@ function createFlueInstrumentation(options = {}) {
|
|
|
4998
5344
|
const { conversationId } = ctx;
|
|
4999
5345
|
return (0, _sentry_core.withScope)((scope) => {
|
|
5000
5346
|
if (conversationId) scope.setConversationId(conversationId);
|
|
5001
|
-
|
|
5347
|
+
setPiAiDriverOptions(scope, options, {
|
|
5348
|
+
operation: "chat",
|
|
5349
|
+
execution: operation,
|
|
5350
|
+
context: ctx
|
|
5351
|
+
});
|
|
5002
5352
|
return next();
|
|
5003
5353
|
});
|
|
5004
5354
|
}
|
|
@@ -5023,14 +5373,13 @@ function createFlueInstrumentation(options = {}) {
|
|
|
5023
5373
|
agentSpans.delete(operationId);
|
|
5024
5374
|
}
|
|
5025
5375
|
});
|
|
5026
|
-
const sentryTrace = ctx.traceCarrier?.traceparent ? sentryTraceFromTraceparent(ctx.traceCarrier.traceparent) : void 0;
|
|
5376
|
+
const sentryTrace = ctx.traceCarrier?.traceparent ? (0, _sentry_core.sentryTraceFromTraceparent)(ctx.traceCarrier.traceparent) : void 0;
|
|
5027
5377
|
return sentryTrace && !(0, _sentry_core.getActiveSpan)() ? (0, _sentry_core.continueTrace)({
|
|
5028
5378
|
sentryTrace,
|
|
5029
5379
|
baggage: void 0
|
|
5030
5380
|
}, openAgentSpan) : openAgentSpan();
|
|
5031
5381
|
},
|
|
5032
|
-
observe: (observation) => {
|
|
5033
|
-
const { recordInputs, recordOutputs } = resolveAIRecordingOptions(options);
|
|
5382
|
+
observe: (observation, ctx) => {
|
|
5034
5383
|
const agentSpan = observation.operationId ? agentSpans.get(observation.operationId) : void 0;
|
|
5035
5384
|
if (agentSpan) {
|
|
5036
5385
|
if (observation.conversationId) agentSpan.setAttribute(_sentry_conventions_attributes.GEN_AI_CONVERSATION_ID, observation.conversationId);
|
|
@@ -5050,10 +5399,10 @@ function createFlueInstrumentation(options = {}) {
|
|
|
5050
5399
|
if (observation.submissionId) admissions.remove(observation.submissionId);
|
|
5051
5400
|
return;
|
|
5052
5401
|
case "tool_start":
|
|
5053
|
-
startToolSpan(observation, toolSpans,
|
|
5402
|
+
startToolSpan(observation, ctx, toolSpans, options);
|
|
5054
5403
|
return;
|
|
5055
5404
|
case "tool":
|
|
5056
|
-
endToolSpan(observation, toolSpans
|
|
5405
|
+
endToolSpan(observation, toolSpans);
|
|
5057
5406
|
return;
|
|
5058
5407
|
default: return;
|
|
5059
5408
|
}
|
|
@@ -6604,6 +6953,12 @@ Object.defineProperty(exports, "getAbortSignal", {
|
|
|
6604
6953
|
return getAbortSignal;
|
|
6605
6954
|
}
|
|
6606
6955
|
});
|
|
6956
|
+
Object.defineProperty(exports, "getBooleanRecordingOptions", {
|
|
6957
|
+
enumerable: true,
|
|
6958
|
+
get: function() {
|
|
6959
|
+
return getBooleanRecordingOptions;
|
|
6960
|
+
}
|
|
6961
|
+
});
|
|
6607
6962
|
Object.defineProperty(exports, "getCallAbortSignal", {
|
|
6608
6963
|
enumerable: true,
|
|
6609
6964
|
get: function() {
|
|
@@ -6682,6 +7037,12 @@ Object.defineProperty(exports, "getOtlpTracesEndpoint", {
|
|
|
6682
7037
|
return getOtlpTracesEndpoint;
|
|
6683
7038
|
}
|
|
6684
7039
|
});
|
|
7040
|
+
Object.defineProperty(exports, "getPiAiDriver", {
|
|
7041
|
+
enumerable: true,
|
|
7042
|
+
get: function() {
|
|
7043
|
+
return getPiAiDriver;
|
|
7044
|
+
}
|
|
7045
|
+
});
|
|
6685
7046
|
Object.defineProperty(exports, "getProviderName", {
|
|
6686
7047
|
enumerable: true,
|
|
6687
7048
|
get: function() {
|
|
@@ -6958,16 +7319,16 @@ Object.defineProperty(exports, "processClassifierEvaluationData", {
|
|
|
6958
7319
|
return processClassifierEvaluationData;
|
|
6959
7320
|
}
|
|
6960
7321
|
});
|
|
6961
|
-
Object.defineProperty(exports, "
|
|
7322
|
+
Object.defineProperty(exports, "recordTimeToFirstChunk", {
|
|
6962
7323
|
enumerable: true,
|
|
6963
7324
|
get: function() {
|
|
6964
|
-
return
|
|
7325
|
+
return recordTimeToFirstChunk;
|
|
6965
7326
|
}
|
|
6966
7327
|
});
|
|
6967
|
-
Object.defineProperty(exports, "
|
|
7328
|
+
Object.defineProperty(exports, "recordTypeSafeClassifierState", {
|
|
6968
7329
|
enumerable: true,
|
|
6969
7330
|
get: function() {
|
|
6970
|
-
return
|
|
7331
|
+
return recordTypeSafeClassifierState;
|
|
6971
7332
|
}
|
|
6972
7333
|
});
|
|
6973
7334
|
Object.defineProperty(exports, "resolveChatParams", {
|
|
@@ -6976,12 +7337,36 @@ Object.defineProperty(exports, "resolveChatParams", {
|
|
|
6976
7337
|
return resolveChatParams;
|
|
6977
7338
|
}
|
|
6978
7339
|
});
|
|
7340
|
+
Object.defineProperty(exports, "resolveGenAiCallRecording", {
|
|
7341
|
+
enumerable: true,
|
|
7342
|
+
get: function() {
|
|
7343
|
+
return resolveGenAiCallRecording;
|
|
7344
|
+
}
|
|
7345
|
+
});
|
|
6979
7346
|
Object.defineProperty(exports, "resolvePiAiRecordingOptions", {
|
|
6980
7347
|
enumerable: true,
|
|
6981
7348
|
get: function() {
|
|
6982
7349
|
return resolvePiAiRecordingOptions;
|
|
6983
7350
|
}
|
|
6984
7351
|
});
|
|
7352
|
+
Object.defineProperty(exports, "resolveRecordingOption", {
|
|
7353
|
+
enumerable: true,
|
|
7354
|
+
get: function() {
|
|
7355
|
+
return resolveRecordingOption;
|
|
7356
|
+
}
|
|
7357
|
+
});
|
|
7358
|
+
Object.defineProperty(exports, "runGenAiSpanEnd", {
|
|
7359
|
+
enumerable: true,
|
|
7360
|
+
get: function() {
|
|
7361
|
+
return runGenAiSpanEnd;
|
|
7362
|
+
}
|
|
7363
|
+
});
|
|
7364
|
+
Object.defineProperty(exports, "runGenAiSpanStart", {
|
|
7365
|
+
enumerable: true,
|
|
7366
|
+
get: function() {
|
|
7367
|
+
return runGenAiSpanStart;
|
|
7368
|
+
}
|
|
7369
|
+
});
|
|
6985
7370
|
Object.defineProperty(exports, "sanitizeSqlQuery", {
|
|
6986
7371
|
enumerable: true,
|
|
6987
7372
|
get: function() {
|
|
@@ -7000,10 +7385,10 @@ Object.defineProperty(exports, "setHttpServerSpanRouteAttribute", {
|
|
|
7000
7385
|
return setHttpServerSpanRouteAttribute;
|
|
7001
7386
|
}
|
|
7002
7387
|
});
|
|
7003
|
-
Object.defineProperty(exports, "
|
|
7388
|
+
Object.defineProperty(exports, "setPiAiDriverOptions", {
|
|
7004
7389
|
enumerable: true,
|
|
7005
7390
|
get: function() {
|
|
7006
|
-
return
|
|
7391
|
+
return setPiAiDriverOptions;
|
|
7007
7392
|
}
|
|
7008
7393
|
});
|
|
7009
7394
|
Object.defineProperty(exports, "setStartingClassifierEvaluation", {
|