@mastra/sentry 1.2.15-alpha.2 → 1.2.16-alpha.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/dist/index.cjs CHANGED
@@ -27,88 +27,110 @@ let _mastra_otel_exporter = require("@mastra/otel-exporter");
27
27
  let _sentry_node = require("@sentry/node");
28
28
  _sentry_node = __toESM(_sentry_node, 1);
29
29
  //#region src/tracing.ts
30
- const SPAN_TYPE_CONFIG = {
31
- [_mastra_core_observability.SpanType.AGENT_RUN]: {
30
+ /**
31
+ * Builds the span-type map, dropping any entry whose span type does not exist
32
+ * in the paired `@mastra/core`.
33
+ *
34
+ * The peer range admits a core older than the one that introduced a given
35
+ * `SpanType` member, where `SpanType.X` is `undefined` at runtime. As a plain
36
+ * object literal that lands in the map under a literal `"undefined"` key, which
37
+ * then matches any span whose type is undefined and mislabels it.
38
+ *
39
+ * @internal Exported for tests.
40
+ */
41
+ function buildSpanTypeConfig(entries) {
42
+ return Object.fromEntries(entries.filter((entry) => entry[0] !== void 0));
43
+ }
44
+ const SPAN_TYPE_CONFIG = buildSpanTypeConfig([
45
+ [_mastra_core_observability.SpanType.AGENT_RUN, {
32
46
  opType: "gen_ai.invoke_agent",
33
47
  opName: "invoke_agent"
34
- },
35
- [_mastra_core_observability.SpanType.MODEL_GENERATION]: {
48
+ }],
49
+ [_mastra_core_observability.SpanType.MODEL_GENERATION, {
36
50
  opType: "gen_ai.chat",
37
51
  opName: "chat"
38
- },
39
- [_mastra_core_observability.SpanType.TOOL_CALL]: {
52
+ }],
53
+ [_mastra_core_observability.SpanType.TOOL_CALL, {
40
54
  opType: "gen_ai.execute_tool",
41
55
  opName: "execute_tool"
42
- },
43
- [_mastra_core_observability.SpanType.MCP_TOOL_CALL]: {
56
+ }],
57
+ [_mastra_core_observability.SpanType.MCP_TOOL_CALL, {
44
58
  opType: "gen_ai.execute_tool",
45
59
  opName: "execute_tool"
46
- },
47
- [_mastra_core_observability.SpanType.PROVIDER_TOOL_CALL]: {
60
+ }],
61
+ [_mastra_core_observability.SpanType.PROVIDER_TOOL_CALL, {
48
62
  opType: "gen_ai.execute_tool",
49
63
  opName: "execute_tool"
50
- },
51
- [_mastra_core_observability.SpanType.WORKFLOW_RUN]: {
64
+ }],
65
+ [_mastra_core_observability.SpanType.WORKFLOW_RUN, {
52
66
  opType: "workflow.run",
53
67
  opName: "workflow"
54
- },
55
- [_mastra_core_observability.SpanType.WORKFLOW_STEP]: {
68
+ }],
69
+ [_mastra_core_observability.SpanType.WORKFLOW_STEP, {
56
70
  opType: "workflow.step",
57
71
  opName: "step"
58
- },
59
- [_mastra_core_observability.SpanType.WORKFLOW_CONDITIONAL]: {
72
+ }],
73
+ [_mastra_core_observability.SpanType.WORKFLOW_CONDITIONAL, {
60
74
  opType: "workflow.conditional",
61
75
  opName: "step"
62
- },
63
- [_mastra_core_observability.SpanType.WORKFLOW_CONDITIONAL_EVAL]: {
76
+ }],
77
+ [_mastra_core_observability.SpanType.WORKFLOW_CONDITIONAL_EVAL, {
64
78
  opType: "workflow.conditional",
65
79
  opName: "step"
66
- },
67
- [_mastra_core_observability.SpanType.WORKFLOW_PARALLEL]: {
80
+ }],
81
+ [_mastra_core_observability.SpanType.WORKFLOW_PARALLEL, {
68
82
  opType: "workflow.parallel",
69
83
  opName: "step"
70
- },
71
- [_mastra_core_observability.SpanType.WORKFLOW_LOOP]: {
84
+ }],
85
+ [_mastra_core_observability.SpanType.WORKFLOW_LOOP, {
72
86
  opType: "workflow.loop",
73
87
  opName: "step"
74
- },
75
- [_mastra_core_observability.SpanType.WORKFLOW_SLEEP]: {
88
+ }],
89
+ [_mastra_core_observability.SpanType.WORKFLOW_SLEEP, {
76
90
  opType: "workflow.sleep",
77
91
  opName: "step"
78
- },
79
- [_mastra_core_observability.SpanType.WORKFLOW_WAIT_EVENT]: {
92
+ }],
93
+ [_mastra_core_observability.SpanType.WORKFLOW_WAIT_EVENT, {
80
94
  opType: "workflow.wait",
81
95
  opName: "step"
82
- },
83
- [_mastra_core_observability.SpanType.PROCESSOR_RUN]: {
96
+ }],
97
+ [_mastra_core_observability.SpanType.PROCESSOR_RUN, {
84
98
  opType: "ai.processor",
85
99
  opName: "step"
86
- },
87
- [_mastra_core_observability.SpanType.GENERIC]: {
100
+ }],
101
+ [_mastra_core_observability.SpanType.GENERIC, {
88
102
  opType: "ai.span",
89
103
  opName: "span"
90
- },
91
- [_mastra_core_observability.SpanType.MODEL_STEP]: {
104
+ }],
105
+ [_mastra_core_observability.SpanType.MODEL_STEP, {
92
106
  opType: "ai.span",
93
107
  opName: "step"
94
- },
95
- [_mastra_core_observability.SpanType.MODEL_CHUNK]: {
108
+ }],
109
+ [_mastra_core_observability.SpanType.MODEL_CHUNK, {
96
110
  opType: "ai.span",
97
111
  opName: "step"
98
- },
99
- [_mastra_core_observability.SpanType.SCORER_RUN]: {
112
+ }],
113
+ [_mastra_core_observability.SpanType.SCORER_RUN, {
100
114
  opType: "workflow.run",
101
115
  opName: "eval"
102
- },
103
- [_mastra_core_observability.SpanType.SCORER_STEP]: {
116
+ }],
117
+ [_mastra_core_observability.SpanType.SCORER_STEP, {
104
118
  opType: "workflow.step",
105
119
  opName: "step"
106
- },
107
- [_mastra_core_observability.SpanType.MEMORY_OPERATION]: {
120
+ }],
121
+ [_mastra_core_observability.SpanType.MEMORY_OPERATION, {
108
122
  opType: "ai.memory",
109
123
  opName: "memory"
110
- }
111
- };
124
+ }],
125
+ [_mastra_core_observability.SpanType.WORKSPACE_ACTION, {
126
+ opType: "ai.workspace",
127
+ opName: "workspace"
128
+ }],
129
+ [_mastra_core_observability.SpanType.SKILL_ACTION, {
130
+ opType: "ai.skill",
131
+ opName: "skill"
132
+ }]
133
+ ]);
112
134
  const ATTRIBUTE_KEYS = {
113
135
  SPAN_TYPE: "ai.span.type",
114
136
  ORIGIN: "sentry.origin",
@@ -484,5 +506,6 @@ var SentryExporter = class extends _mastra_observability.BaseExporter {
484
506
  };
485
507
  //#endregion
486
508
  exports.SentryExporter = SentryExporter;
509
+ exports.buildSpanTypeConfig = buildSpanTypeConfig;
487
510
 
488
511
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","names":["SpanType","BaseExporter","TracingEventType","Sentry"],"sources":["../src/tracing.ts"],"sourcesContent":["/**\n * Sentry Exporter for Mastra Observability\n *\n * Sends observability data to Sentry for AI tracing and monitoring.\n * Uses Sentry's modern span model (v8+) with OpenTelemetry semantic conventions.\n *\n * Spans are hierarchically organized: AGENT_RUN -> MODEL_GENERATION -> TOOL_CALL\n * MODEL_STEP and MODEL_CHUNK spans are skipped to simplify the trace hierarchy.\n */\n\nimport type {\n TracingEvent,\n AnyExportedSpan,\n ModelGenerationAttributes,\n ToolCallAttributes,\n AgentRunAttributes,\n WorkflowRunAttributes,\n WorkflowStepAttributes,\n UsageStats,\n} from '@mastra/core/observability';\nimport { SpanType, TracingEventType } from '@mastra/core/observability';\nimport type { BaseExporterConfig } from '@mastra/observability';\nimport { BaseExporter } from '@mastra/observability';\nimport { getAttributes as getGenAIAttributes, getSpanName as getGenAISpanName } from '@mastra/otel-exporter';\nimport * as Sentry from '@sentry/node';\n\nconst SPAN_TYPE_CONFIG: Partial<Record<SpanType, { opType: string; opName: string }>> = {\n [SpanType.AGENT_RUN]: { opType: 'gen_ai.invoke_agent', opName: 'invoke_agent' },\n [SpanType.MODEL_GENERATION]: { opType: 'gen_ai.chat', opName: 'chat' },\n [SpanType.TOOL_CALL]: { opType: 'gen_ai.execute_tool', opName: 'execute_tool' },\n [SpanType.MCP_TOOL_CALL]: { opType: 'gen_ai.execute_tool', opName: 'execute_tool' },\n [SpanType.PROVIDER_TOOL_CALL]: { opType: 'gen_ai.execute_tool', opName: 'execute_tool' },\n [SpanType.WORKFLOW_RUN]: { opType: 'workflow.run', opName: 'workflow' },\n [SpanType.WORKFLOW_STEP]: { opType: 'workflow.step', opName: 'step' },\n [SpanType.WORKFLOW_CONDITIONAL]: { opType: 'workflow.conditional', opName: 'step' },\n [SpanType.WORKFLOW_CONDITIONAL_EVAL]: { opType: 'workflow.conditional', opName: 'step' },\n [SpanType.WORKFLOW_PARALLEL]: { opType: 'workflow.parallel', opName: 'step' },\n [SpanType.WORKFLOW_LOOP]: { opType: 'workflow.loop', opName: 'step' },\n [SpanType.WORKFLOW_SLEEP]: { opType: 'workflow.sleep', opName: 'step' },\n [SpanType.WORKFLOW_WAIT_EVENT]: { opType: 'workflow.wait', opName: 'step' },\n [SpanType.PROCESSOR_RUN]: { opType: 'ai.processor', opName: 'step' },\n [SpanType.GENERIC]: { opType: 'ai.span', opName: 'span' },\n [SpanType.MODEL_STEP]: { opType: 'ai.span', opName: 'step' },\n [SpanType.MODEL_CHUNK]: { opType: 'ai.span', opName: 'step' },\n [SpanType.SCORER_RUN]: { opType: 'workflow.run', opName: 'eval' },\n [SpanType.SCORER_STEP]: { opType: 'workflow.step', opName: 'step' },\n [SpanType.MEMORY_OPERATION]: { opType: 'ai.memory', opName: 'memory' },\n};\n\nconst ATTRIBUTE_KEYS = {\n SPAN_TYPE: 'ai.span.type',\n ORIGIN: 'sentry.origin',\n TAGS: 'tags',\n INPUT: 'input',\n OUTPUT: 'output',\n GEN_AI_REQUEST_STREAM: 'gen_ai.request.stream',\n GEN_AI_RESPONSE_MODEL: 'gen_ai.response.model',\n GEN_AI_RESPONSE_STREAMING: 'gen_ai.response.streaming',\n GEN_AI_RESPONSE_TOOL_CALLS: 'gen_ai.response.tool_calls',\n GEN_AI_RESPONSE_TEXT: 'gen_ai.response.text',\n GEN_AI_CONVERSATION_ID: 'gen_ai.conversation.id',\n GEN_AI_COMPLETION_START_TIME: 'gen_ai.completion_start_time',\n GEN_AI_TOOL_CALL_ID: 'gen_ai.tool.call.id',\n TOOL_SUCCESS: 'tool.success',\n GEN_AI_PIPELINE_NAME: 'gen_ai.pipeline.name',\n GEN_AI_AGENT_PROMPT: 'gen_ai.agent.prompt',\n WORKFLOW_ID: 'workflow.id',\n WORKFLOW_STATUS: 'workflow.status',\n WORKFLOW_STEP_ID: 'workflow.step.id',\n WORKFLOW_STEP_STATUS: 'workflow.step.status',\n GEN_AI_USAGE_INPUT_TOKENS: 'gen_ai.usage.input_tokens',\n GEN_AI_USAGE_OUTPUT_TOKENS: 'gen_ai.usage.output_tokens',\n GEN_AI_USAGE_TOTAL_TOKENS: 'gen_ai.usage.total_tokens',\n GEN_AI_USAGE_CACHE_READ_TOKENS: 'gen_ai.usage.cache_read.input_tokens',\n GEN_AI_USAGE_CACHE_WRITE_TOKENS: 'gen_ai.usage.cache_creation.input_tokens',\n GEN_AI_USAGE_REASONING_TOKENS: 'gen_ai.usage.reasoning_tokens',\n} as const;\n\nexport interface SentryExporterConfig extends BaseExporterConfig {\n // Sentry SDK options (passed to Sentry.init())\n /** Data Source Name - tells the SDK where to send events */\n dsn?: string;\n /** Deployment environment (enables filtering issues and alerts by environment) */\n environment?: string;\n /** Percentage of transactions sent to Sentry (0.0 = 0%, 1.0 = 100%) */\n tracesSampleRate?: number;\n /** Version of your code deployed (helps identify regressions and track deployments) */\n release?: string;\n /** Additional Sentry SDK options (integrations, beforeSend, etc.) */\n options?: Partial<Sentry.NodeOptions>;\n}\n\n/**\n * Internal span tracking data.\n * generation tracks the single MODEL_GENERATION for AGENT_RUN response attributes.\n * toolCalls tracks child tool calls for MODEL_GENERATION spans.\n */\ntype SpanData = {\n span: Sentry.Span;\n spanType: SpanType;\n generation?: {\n model?: string;\n output?: any;\n usage?: UsageStats;\n };\n toolCalls?: Array<{\n name: string;\n id?: string;\n type?: string;\n }>;\n};\n\n/** Config type with Sentry-specific fields resolved */\ntype ResolvedSentryConfig = Required<\n Pick<SentryExporterConfig, 'dsn' | 'environment' | 'tracesSampleRate' | 'release'>\n>;\n\nexport class SentryExporter extends BaseExporter {\n name = 'sentry';\n private sentryConfig: ResolvedSentryConfig;\n private spanMap = new Map<string, SpanData>();\n private skippedSpans = new Map<string, string>();\n private initialized = false;\n\n constructor(config: SentryExporterConfig = {}) {\n super(config);\n\n this.sentryConfig = {\n dsn: config.dsn ?? process.env.SENTRY_DSN ?? '',\n environment: config.environment ?? process.env.SENTRY_ENVIRONMENT ?? 'production',\n tracesSampleRate: config.tracesSampleRate ?? 1.0,\n release: config.release ?? process.env.SENTRY_RELEASE ?? '',\n };\n\n if (!this.sentryConfig.dsn) {\n const dsnSource = config.dsn ? 'from config' : process.env.SENTRY_DSN ? 'from env' : 'missing';\n this.setDisabled(\n `Missing required DSN (dsn: ${dsnSource}). Set SENTRY_DSN environment variable or pass it in config.`,\n );\n return;\n }\n\n try {\n Sentry.init({\n dsn: this.sentryConfig.dsn,\n environment: this.sentryConfig.environment,\n tracesSampleRate: this.sentryConfig.tracesSampleRate,\n release: this.sentryConfig.release,\n ...config.options,\n });\n this.initialized = true;\n } catch (error) {\n this.setDisabled(`Failed to initialize Sentry: ${error}`);\n }\n }\n\n // ============================================================================\n // Main Event Handlers\n // ============================================================================\n\n protected async _exportTracingEvent(event: TracingEvent): Promise<void> {\n if (!this.initialized) return;\n\n const { type, exportedSpan } = event;\n\n if (exportedSpan.isEvent) {\n this.handleEventSpan(exportedSpan);\n return;\n }\n\n // Skip MODEL_CHUNK and MODEL_STEP spans to simplify trace hierarchy.\n // We store them in skippedSpans to preserve parent-child relationships:\n // when a child span references a skipped span as parent, resolveParentSpanId()\n // walks up the chain to find the first non-skipped ancestor.\n if (exportedSpan.type === SpanType.MODEL_CHUNK || exportedSpan.type === SpanType.MODEL_STEP) {\n if (type === TracingEventType.SPAN_STARTED) {\n this.skippedSpans.set(exportedSpan.id, exportedSpan.parentSpanId || '');\n } else if (type === TracingEventType.SPAN_ENDED) {\n this.skippedSpans.delete(exportedSpan.id);\n }\n return;\n }\n\n switch (type) {\n case TracingEventType.SPAN_STARTED:\n await this.handleSpanStarted(exportedSpan);\n break;\n case TracingEventType.SPAN_UPDATED:\n await this.handleSpanUpdated(exportedSpan);\n break;\n case TracingEventType.SPAN_ENDED:\n await this.handleSpanEnded(exportedSpan);\n break;\n }\n }\n\n private handleEventSpan(span: AnyExportedSpan): void {\n Sentry.addBreadcrumb({\n type: 'default',\n category: span.type,\n message: span.name,\n level: span.errorInfo ? 'error' : 'info',\n data: {\n spanId: span.id,\n traceId: span.traceId,\n ...(span.input && { input: this.serializeValue(span.input) }),\n ...(span.output && { output: this.serializeValue(span.output) }),\n ...(span.metadata && { metadata: span.metadata }),\n ...(span.attributes && { attributes: span.attributes }),\n },\n timestamp: span.startTime.getTime() / 1000,\n });\n }\n\n private async handleSpanStarted(span: AnyExportedSpan): Promise<void> {\n const resolvedParentId = this.resolveParentSpanId(span.parentSpanId);\n\n const sentrySpan = Sentry.startInactiveSpan({\n op: this.getOperationType(span),\n name: getGenAISpanName(span),\n startTime: span.startTime.getTime(),\n forceTransaction: span.isRootSpan,\n parentSpan: resolvedParentId ? this.spanMap.get(resolvedParentId)?.span : undefined,\n });\n\n sentrySpan.setAttributes(this.buildSpanAttributes(span));\n\n this.spanMap.set(span.id, {\n span: sentrySpan,\n spanType: span.type,\n });\n\n // Track tool calls as children of MODEL_GENERATION spans for gen_ai.response.tool_calls attribute\n if ((span.type === SpanType.TOOL_CALL || span.type === SpanType.PROVIDER_TOOL_CALL) && resolvedParentId) {\n this.trackToolCallForParent(span, resolvedParentId);\n }\n }\n\n private async handleSpanUpdated(span: AnyExportedSpan): Promise<void> {\n const spanData = this.spanMap.get(span.id);\n if (!spanData) {\n this.logMissingSpan(span, 'span update');\n return;\n }\n // Attributes are set on SPAN_STARTED and finalized on SPAN_ENDED.\n // If dynamic updates become necessary, add spanData.span.setAttributes() here.\n }\n\n private async handleSpanEnded(span: AnyExportedSpan): Promise<void> {\n const spanData = this.spanMap.get(span.id);\n if (!spanData) {\n this.logMissingSpan(span, 'span end');\n return;\n }\n\n const { span: sentrySpan } = spanData;\n\n sentrySpan.setAttributes(this.buildSpanAttributes(span));\n\n if (span.type === SpanType.MODEL_GENERATION) {\n // Set gen_ai.response.tool_calls if this generation had tool calls\n this.applyToolCallsAttribute(spanData);\n\n const resolvedParentId = this.resolveParentSpanId(span.parentSpanId);\n if (resolvedParentId) {\n const parentData = this.spanMap.get(resolvedParentId);\n if (parentData?.spanType === SpanType.AGENT_RUN) {\n const modelAttr = span.attributes as ModelGenerationAttributes;\n parentData.generation = {\n model: modelAttr.model,\n output: span.output,\n usage: modelAttr.usage,\n };\n }\n }\n }\n\n if (span.type === SpanType.AGENT_RUN) {\n // Apply token usage from the single child MODEL_GENERATION span\n // (there is only ever one MODEL_GENERATION span per AGENT_RUN)\n this.applyUsageFromGeneration(spanData);\n\n this.setGenerationResponseAttributes(spanData);\n }\n\n if (span.errorInfo) {\n sentrySpan.setStatus({\n code: 2,\n message: span.errorInfo.message,\n });\n\n // Build an Error instance so Sentry can use the real stack trace captured\n // by observability rather than synthesizing one from this exporter's call site.\n // Passing a string to Sentry.captureException produces a stack that points to\n // handleSpanEnded, hiding the real error origin.\n const error = new Error(span.errorInfo.message);\n if (span.errorInfo.name) {\n error.name = span.errorInfo.name;\n }\n if (span.errorInfo.stack) {\n error.stack = span.errorInfo.stack;\n }\n\n Sentry.captureException(error, {\n contexts: {\n trace: { trace_id: span.traceId, span_id: span.id },\n span_info: {\n name: span.name,\n type: span.type,\n error_id: span.errorInfo.id,\n error_category: span.errorInfo.category,\n },\n },\n });\n }\n\n const endTime = span.endTime ? span.endTime.getTime() : undefined;\n sentrySpan.end(endTime);\n this.spanMap.delete(span.id);\n }\n\n // ============================================================================\n // Span Creation Helpers\n // ============================================================================\n\n private resolveParentSpanId(parentSpanId: string | undefined): string | undefined {\n if (!parentSpanId) return undefined;\n\n let currentParentId: string | undefined = parentSpanId;\n while (currentParentId && this.skippedSpans.has(currentParentId)) {\n currentParentId = this.skippedSpans.get(currentParentId);\n if (!currentParentId) break;\n }\n\n return currentParentId;\n }\n\n private getOperationType(span: AnyExportedSpan): string {\n const config = SPAN_TYPE_CONFIG[span.type];\n return config ? config.opType : 'ai.span';\n }\n\n private buildSpanAttributes(span: AnyExportedSpan): Record<string, any> {\n const attributes = getGenAIAttributes(span) as Record<string, any>;\n\n attributes[ATTRIBUTE_KEYS.SPAN_TYPE] = span.type;\n attributes[ATTRIBUTE_KEYS.ORIGIN] = 'auto.ai.mastra';\n\n if (span.metadata) {\n Object.entries(span.metadata).forEach(([key, value]) => {\n if (value !== undefined && value !== null && key !== 'langfuse') {\n attributes[`metadata.${key}`] = this.serializeValue(value);\n }\n });\n }\n\n this.setAttributeIfDefined(attributes, ATTRIBUTE_KEYS.TAGS, span.tags?.join(','));\n this.setAttributeIfDefined(attributes, ATTRIBUTE_KEYS.GEN_AI_CONVERSATION_ID, span.metadata?.threadId);\n\n this.addInputOutputAttributes(attributes, span);\n\n if (span.type === SpanType.MODEL_GENERATION) {\n this.addModelGenerationAttributes(attributes, span);\n }\n\n if (span.type === SpanType.TOOL_CALL || span.type === SpanType.PROVIDER_TOOL_CALL) {\n this.addToolCallAttributes(attributes, span);\n }\n\n if (span.type === SpanType.AGENT_RUN) {\n this.addAgentRunAttributes(attributes, span);\n }\n\n if (span.type === SpanType.WORKFLOW_RUN) {\n const workflowAttr = span.attributes as WorkflowRunAttributes;\n this.setAttributeIfDefined(attributes, ATTRIBUTE_KEYS.WORKFLOW_ID, this.getEntityName(span));\n this.setAttributeIfDefined(attributes, ATTRIBUTE_KEYS.WORKFLOW_STATUS, workflowAttr.status);\n }\n\n if (span.type === SpanType.WORKFLOW_STEP) {\n const stepAttr = span.attributes as WorkflowStepAttributes;\n this.setAttributeIfDefined(attributes, ATTRIBUTE_KEYS.WORKFLOW_STEP_ID, this.getEntityName(span));\n this.setAttributeIfDefined(attributes, ATTRIBUTE_KEYS.WORKFLOW_STEP_STATUS, stepAttr.status);\n }\n\n return attributes;\n }\n\n // ============================================================================\n // Sentry-Specific Attribute Formatters\n // ============================================================================\n\n /**\n * Adds Sentry-specific input/output attributes that complement GenAI semantic conventions.\n * Adds 'input' and 'output' keys for Sentry UI compatibility.\n */\n private addInputOutputAttributes(attributes: Record<string, any>, span: AnyExportedSpan): void {\n if (span.input !== undefined) {\n attributes[ATTRIBUTE_KEYS.INPUT] = this.serializeValue(span.input);\n }\n\n if (span.output !== undefined) {\n attributes[ATTRIBUTE_KEYS.OUTPUT] = this.serializeValue(span.output);\n\n // Extract text for MODEL_GENERATION spans\n if (span.type === SpanType.MODEL_GENERATION) {\n const outputText = this.extractOutputText(span.output);\n if (outputText) {\n attributes[ATTRIBUTE_KEYS.GEN_AI_RESPONSE_TEXT] = outputText;\n }\n }\n }\n }\n\n /**\n * Adds Sentry-specific MODEL_GENERATION attributes that complement GenAI semantic conventions.\n */\n private addModelGenerationAttributes(attributes: Record<string, any>, span: AnyExportedSpan): void {\n const modelAttr = span.attributes as ModelGenerationAttributes;\n\n if (modelAttr.streaming !== undefined) {\n attributes[ATTRIBUTE_KEYS.GEN_AI_REQUEST_STREAM] = modelAttr.streaming;\n attributes[ATTRIBUTE_KEYS.GEN_AI_RESPONSE_STREAMING] = modelAttr.streaming;\n }\n\n this.setAttributeIfDefined(\n attributes,\n ATTRIBUTE_KEYS.GEN_AI_COMPLETION_START_TIME,\n modelAttr.completionStartTime?.toISOString(),\n );\n\n if (modelAttr.usage) {\n const totalTokens = (modelAttr.usage.inputTokens || 0) + (modelAttr.usage.outputTokens || 0);\n if (totalTokens > 0) {\n attributes[ATTRIBUTE_KEYS.GEN_AI_USAGE_TOTAL_TOKENS] = totalTokens;\n }\n }\n }\n\n /**\n * Adds Sentry-specific TOOL_CALL attributes that complement GenAI semantic conventions.\n */\n private addToolCallAttributes(attributes: Record<string, any>, span: AnyExportedSpan): void {\n const toolAttr = span.attributes as ToolCallAttributes;\n\n this.setAttributeIfDefined(attributes, ATTRIBUTE_KEYS.TOOL_SUCCESS, toolAttr.success);\n this.setAttributeIfDefined(\n attributes,\n ATTRIBUTE_KEYS.GEN_AI_TOOL_CALL_ID,\n toolAttr.toolCallId ?? span.metadata?.toolCallId,\n );\n }\n\n /**\n * Adds Sentry-specific AGENT_RUN attributes that complement GenAI semantic conventions.\n */\n private addAgentRunAttributes(attributes: Record<string, any>, span: AnyExportedSpan): void {\n const agentAttr = span.attributes as AgentRunAttributes;\n\n const agentName = this.getEntityName(span);\n if (agentName) {\n attributes[ATTRIBUTE_KEYS.GEN_AI_PIPELINE_NAME] = agentName;\n }\n\n this.setAttributeIfDefined(attributes, ATTRIBUTE_KEYS.GEN_AI_AGENT_PROMPT, agentAttr.prompt);\n }\n\n // ============================================================================\n // Token Usage Management\n // ============================================================================\n\n /**\n * Applies token usage from the MODEL_GENERATION span to the AGENT_RUN span attributes.\n * Reads usage directly from the generation field.\n * Called when AGENT_RUN spans end to set gen_ai.usage.* attributes.\n */\n private applyUsageFromGeneration(spanData: SpanData): void {\n const usage = spanData.generation?.usage;\n if (!usage) return;\n\n const inputTokens = usage.inputTokens || 0;\n const outputTokens = usage.outputTokens || 0;\n\n if (inputTokens > 0) {\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_USAGE_INPUT_TOKENS, inputTokens);\n }\n if (outputTokens > 0) {\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_USAGE_OUTPUT_TOKENS, outputTokens);\n }\n\n const totalTokens = inputTokens + outputTokens;\n if (totalTokens > 0) {\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_USAGE_TOTAL_TOKENS, totalTokens);\n }\n\n const cacheReadTokens = usage.inputDetails?.cacheRead || 0;\n const cacheWriteTokens = usage.inputDetails?.cacheWrite || 0;\n const reasoningTokens = usage.outputDetails?.reasoning || 0;\n\n if (cacheReadTokens > 0) {\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_USAGE_CACHE_READ_TOKENS, cacheReadTokens);\n }\n if (cacheWriteTokens > 0) {\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_USAGE_CACHE_WRITE_TOKENS, cacheWriteTokens);\n }\n if (reasoningTokens > 0) {\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_USAGE_REASONING_TOKENS, reasoningTokens);\n }\n }\n\n /**\n * Sets gen_ai.response.model and gen_ai.response.text from the MODEL_GENERATION.\n * Only applies to AGENT_RUN spans.\n */\n private setGenerationResponseAttributes(spanData: SpanData): void {\n if (!spanData.generation) return;\n\n if (spanData.generation.model) {\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_RESPONSE_MODEL, spanData.generation.model);\n }\n\n if (spanData.generation.output) {\n const outputText = this.extractOutputText(spanData.generation.output);\n if (outputText) {\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_RESPONSE_TEXT, outputText);\n }\n }\n }\n\n /**\n * Tracks a TOOL_CALL span as a child of its parent MODEL_GENERATION span.\n * This builds the tool_calls array for gen_ai.response.tool_calls attribute.\n */\n private trackToolCallForParent(span: AnyExportedSpan, parentId: string): void {\n const parentSpanData = this.spanMap.get(parentId);\n if (!parentSpanData || parentSpanData.spanType !== SpanType.MODEL_GENERATION) {\n return;\n }\n\n const toolAttr = span.attributes as ToolCallAttributes;\n if (!parentSpanData.toolCalls) {\n parentSpanData.toolCalls = [];\n }\n\n parentSpanData.toolCalls.push({\n name: this.getEntityName(span),\n id: toolAttr.toolCallId ?? span.metadata?.toolCallId,\n type: toolAttr.toolType || 'function',\n });\n }\n\n /**\n * Applies the gen_ai.response.tool_calls attribute to MODEL_GENERATION spans.\n * Called when MODEL_GENERATION spans end if they have child tool calls.\n */\n private applyToolCallsAttribute(spanData: SpanData): void {\n if (!spanData.toolCalls || spanData.toolCalls.length === 0) {\n return;\n }\n\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_RESPONSE_TOOL_CALLS, JSON.stringify(spanData.toolCalls));\n }\n\n // ============================================================================\n // Utility Helpers\n // ============================================================================\n\n private logMissingSpan(span: AnyExportedSpan, operation: string): void {\n this.logger.warn(`Sentry exporter: No Sentry span found for ${operation}`, {\n traceId: span.traceId,\n spanId: span.id,\n spanName: span.name,\n });\n }\n\n private getEntityName(span: AnyExportedSpan): string {\n return span.entityName || span.entityId || 'unknown';\n }\n\n private extractOutputText(output: any): string | undefined {\n if (!output) return undefined;\n if (typeof output === 'string') return output;\n if (output.text && typeof output.text === 'string') return output.text;\n if (output.content && typeof output.content === 'string') return output.content;\n if (output.message?.content && typeof output.message.content === 'string') return output.message.content;\n return undefined;\n }\n\n private serializeValue(value: any): any {\n if (value === null || value === undefined) return value;\n if (typeof value === 'object') {\n try {\n return JSON.stringify(value);\n } catch {\n return String(value);\n }\n }\n return value;\n }\n\n private setAttributeIfDefined(attributes: Record<string, any>, key: string, value: any): void {\n if (value !== undefined && value !== null) {\n attributes[key] = value;\n }\n }\n\n // ============================================================================\n // Flush and Shutdown\n // ============================================================================\n\n /**\n * Force flush any buffered spans without shutting down the exporter.\n * This is useful in serverless environments where you need to ensure spans\n * are exported before the runtime instance is terminated.\n */\n async flush(): Promise<void> {\n if (!this.initialized) return;\n\n try {\n // Sentry.flush() sends any pending events to Sentry\n // The timeout is in milliseconds\n await Sentry.flush(2000);\n this.logger.debug('Sentry exporter: Flushed pending events');\n } catch (error) {\n this.logger.error('Sentry exporter: Error flushing events', { error });\n }\n }\n\n async shutdown(): Promise<void> {\n for (const [spanId, spanData] of this.spanMap.entries()) {\n try {\n spanData.span.end();\n } catch (error) {\n this.logger.error('Sentry exporter: Error ending span during shutdown', { spanId, error });\n }\n }\n\n this.spanMap.clear();\n this.skippedSpans.clear();\n\n if (this.initialized) {\n await Sentry.close(2000);\n }\n\n await super.shutdown();\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0BA,MAAM,mBAAkF;EACrFA,2BAAAA,SAAS,YAAY;EAAE,QAAQ;EAAuB,QAAQ;CAAe;EAC7EA,2BAAAA,SAAS,mBAAmB;EAAE,QAAQ;EAAe,QAAQ;CAAO;EACpEA,2BAAAA,SAAS,YAAY;EAAE,QAAQ;EAAuB,QAAQ;CAAe;EAC7EA,2BAAAA,SAAS,gBAAgB;EAAE,QAAQ;EAAuB,QAAQ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AAI,OAAO,UAAU,UACnB,IAAI;GACF,OAAO,KAAK,UAAU,KAAK;EAC7B,QAAQ;GACN,OAAO,OAAO,KAAK;EACrB;EAEF,OAAO;CACT;CAEA,sBAA8B,YAAiC,KAAa,OAAkB;EAC5F,IAAI,UAAU,KAAA,KAAa,UAAU,MACnC,WAAW,OAAO;CAEtB;;;;;;CAWA,MAAM,QAAuB;EAC3B,IAAI,CAAC,KAAK,aAAa;EAEvB,IAAI;GAGF,MAAMG,aAAO,MAAM,GAAI;GACvB,KAAK,OAAO,MAAM,yCAAyC;EAC7D,SAAS,OAAO;GACd,KAAK,OAAO,MAAM,0CAA0C,EAAE,MAAM,CAAC;EACvE;CACF;CAEA,MAAM,WAA0B;EAC9B,KAAK,MAAM,CAAC,QAAQ,aAAa,KAAK,QAAQ,QAAQ,GACpD,IAAI;GACF,SAAS,KAAK,IAAI;EACpB,SAAS,OAAO;GACd,KAAK,OAAO,MAAM,sDAAsD;IAAE;IAAQ;GAAM,CAAC;EAC3F;EAGF,KAAK,QAAQ,MAAM;EACnB,KAAK,aAAa,MAAM;EAExB,IAAI,KAAK,aACP,MAAMA,aAAO,MAAM,GAAI;EAGzB,MAAM,MAAM,SAAS;CACvB;AACF"}
1
+ {"version":3,"file":"index.cjs","names":["SpanType","BaseExporter","TracingEventType","Sentry"],"sources":["../src/tracing.ts"],"sourcesContent":["/**\n * Sentry Exporter for Mastra Observability\n *\n * Sends observability data to Sentry for AI tracing and monitoring.\n * Uses Sentry's modern span model (v8+) with OpenTelemetry semantic conventions.\n *\n * Spans are hierarchically organized: AGENT_RUN -> MODEL_GENERATION -> TOOL_CALL\n * MODEL_STEP and MODEL_CHUNK spans are skipped to simplify the trace hierarchy.\n */\n\nimport type {\n TracingEvent,\n AnyExportedSpan,\n ModelGenerationAttributes,\n ToolCallAttributes,\n AgentRunAttributes,\n WorkflowRunAttributes,\n WorkflowStepAttributes,\n UsageStats,\n} from '@mastra/core/observability';\nimport { SpanType, TracingEventType } from '@mastra/core/observability';\nimport type { BaseExporterConfig } from '@mastra/observability';\nimport { BaseExporter } from '@mastra/observability';\nimport { getAttributes as getGenAIAttributes, getSpanName as getGenAISpanName } from '@mastra/otel-exporter';\nimport * as Sentry from '@sentry/node';\n\ntype SentrySpanOp = { opType: string; opName: string };\n\n/**\n * Builds the span-type map, dropping any entry whose span type does not exist\n * in the paired `@mastra/core`.\n *\n * The peer range admits a core older than the one that introduced a given\n * `SpanType` member, where `SpanType.X` is `undefined` at runtime. As a plain\n * object literal that lands in the map under a literal `\"undefined\"` key, which\n * then matches any span whose type is undefined and mislabels it.\n *\n * @internal Exported for tests.\n */\nexport function buildSpanTypeConfig(\n entries: Array<[SpanType | undefined, SentrySpanOp]>,\n): Partial<Record<SpanType, SentrySpanOp>> {\n return Object.fromEntries(\n entries.filter((entry): entry is [SpanType, SentrySpanOp] => entry[0] !== undefined),\n ) as Partial<Record<SpanType, SentrySpanOp>>;\n}\n\nconst SPAN_TYPE_CONFIG: Partial<Record<SpanType, SentrySpanOp>> = buildSpanTypeConfig([\n [SpanType.AGENT_RUN, { opType: 'gen_ai.invoke_agent', opName: 'invoke_agent' }],\n [SpanType.MODEL_GENERATION, { opType: 'gen_ai.chat', opName: 'chat' }],\n [SpanType.TOOL_CALL, { opType: 'gen_ai.execute_tool', opName: 'execute_tool' }],\n [SpanType.MCP_TOOL_CALL, { opType: 'gen_ai.execute_tool', opName: 'execute_tool' }],\n [SpanType.PROVIDER_TOOL_CALL, { opType: 'gen_ai.execute_tool', opName: 'execute_tool' }],\n [SpanType.WORKFLOW_RUN, { opType: 'workflow.run', opName: 'workflow' }],\n [SpanType.WORKFLOW_STEP, { opType: 'workflow.step', opName: 'step' }],\n [SpanType.WORKFLOW_CONDITIONAL, { opType: 'workflow.conditional', opName: 'step' }],\n [SpanType.WORKFLOW_CONDITIONAL_EVAL, { opType: 'workflow.conditional', opName: 'step' }],\n [SpanType.WORKFLOW_PARALLEL, { opType: 'workflow.parallel', opName: 'step' }],\n [SpanType.WORKFLOW_LOOP, { opType: 'workflow.loop', opName: 'step' }],\n [SpanType.WORKFLOW_SLEEP, { opType: 'workflow.sleep', opName: 'step' }],\n [SpanType.WORKFLOW_WAIT_EVENT, { opType: 'workflow.wait', opName: 'step' }],\n [SpanType.PROCESSOR_RUN, { opType: 'ai.processor', opName: 'step' }],\n [SpanType.GENERIC, { opType: 'ai.span', opName: 'span' }],\n [SpanType.MODEL_STEP, { opType: 'ai.span', opName: 'step' }],\n [SpanType.MODEL_CHUNK, { opType: 'ai.span', opName: 'step' }],\n [SpanType.SCORER_RUN, { opType: 'workflow.run', opName: 'eval' }],\n [SpanType.SCORER_STEP, { opType: 'workflow.step', opName: 'step' }],\n [SpanType.MEMORY_OPERATION, { opType: 'ai.memory', opName: 'memory' }],\n // Skill and workspace spans come from two places: the tools the model calls,\n // and the processors Mastra derives from agent config. A processor-flavoured\n // op would mislabel the tool calls, so both map to their subsystem the way\n // MEMORY_OPERATION already does. Without an entry they fall back to the\n // catch-all 'ai.span'.\n //\n // Both arrived after this package's oldest supported core, so against an\n // older one they read as `undefined`. `buildSpanTypeConfig` drops those\n // entries rather than keying the map under an `undefined` member.\n [SpanType.WORKSPACE_ACTION, { opType: 'ai.workspace', opName: 'workspace' }],\n [SpanType.SKILL_ACTION, { opType: 'ai.skill', opName: 'skill' }],\n]);\n\nconst ATTRIBUTE_KEYS = {\n SPAN_TYPE: 'ai.span.type',\n ORIGIN: 'sentry.origin',\n TAGS: 'tags',\n INPUT: 'input',\n OUTPUT: 'output',\n GEN_AI_REQUEST_STREAM: 'gen_ai.request.stream',\n GEN_AI_RESPONSE_MODEL: 'gen_ai.response.model',\n GEN_AI_RESPONSE_STREAMING: 'gen_ai.response.streaming',\n GEN_AI_RESPONSE_TOOL_CALLS: 'gen_ai.response.tool_calls',\n GEN_AI_RESPONSE_TEXT: 'gen_ai.response.text',\n GEN_AI_CONVERSATION_ID: 'gen_ai.conversation.id',\n GEN_AI_COMPLETION_START_TIME: 'gen_ai.completion_start_time',\n GEN_AI_TOOL_CALL_ID: 'gen_ai.tool.call.id',\n TOOL_SUCCESS: 'tool.success',\n GEN_AI_PIPELINE_NAME: 'gen_ai.pipeline.name',\n GEN_AI_AGENT_PROMPT: 'gen_ai.agent.prompt',\n WORKFLOW_ID: 'workflow.id',\n WORKFLOW_STATUS: 'workflow.status',\n WORKFLOW_STEP_ID: 'workflow.step.id',\n WORKFLOW_STEP_STATUS: 'workflow.step.status',\n GEN_AI_USAGE_INPUT_TOKENS: 'gen_ai.usage.input_tokens',\n GEN_AI_USAGE_OUTPUT_TOKENS: 'gen_ai.usage.output_tokens',\n GEN_AI_USAGE_TOTAL_TOKENS: 'gen_ai.usage.total_tokens',\n GEN_AI_USAGE_CACHE_READ_TOKENS: 'gen_ai.usage.cache_read.input_tokens',\n GEN_AI_USAGE_CACHE_WRITE_TOKENS: 'gen_ai.usage.cache_creation.input_tokens',\n GEN_AI_USAGE_REASONING_TOKENS: 'gen_ai.usage.reasoning_tokens',\n} as const;\n\nexport interface SentryExporterConfig extends BaseExporterConfig {\n // Sentry SDK options (passed to Sentry.init())\n /** Data Source Name - tells the SDK where to send events */\n dsn?: string;\n /** Deployment environment (enables filtering issues and alerts by environment) */\n environment?: string;\n /** Percentage of transactions sent to Sentry (0.0 = 0%, 1.0 = 100%) */\n tracesSampleRate?: number;\n /** Version of your code deployed (helps identify regressions and track deployments) */\n release?: string;\n /** Additional Sentry SDK options (integrations, beforeSend, etc.) */\n options?: Partial<Sentry.NodeOptions>;\n}\n\n/**\n * Internal span tracking data.\n * generation tracks the single MODEL_GENERATION for AGENT_RUN response attributes.\n * toolCalls tracks child tool calls for MODEL_GENERATION spans.\n */\ntype SpanData = {\n span: Sentry.Span;\n spanType: SpanType;\n generation?: {\n model?: string;\n output?: any;\n usage?: UsageStats;\n };\n toolCalls?: Array<{\n name: string;\n id?: string;\n type?: string;\n }>;\n};\n\n/** Config type with Sentry-specific fields resolved */\ntype ResolvedSentryConfig = Required<\n Pick<SentryExporterConfig, 'dsn' | 'environment' | 'tracesSampleRate' | 'release'>\n>;\n\nexport class SentryExporter extends BaseExporter {\n name = 'sentry';\n private sentryConfig: ResolvedSentryConfig;\n private spanMap = new Map<string, SpanData>();\n private skippedSpans = new Map<string, string>();\n private initialized = false;\n\n constructor(config: SentryExporterConfig = {}) {\n super(config);\n\n this.sentryConfig = {\n dsn: config.dsn ?? process.env.SENTRY_DSN ?? '',\n environment: config.environment ?? process.env.SENTRY_ENVIRONMENT ?? 'production',\n tracesSampleRate: config.tracesSampleRate ?? 1.0,\n release: config.release ?? process.env.SENTRY_RELEASE ?? '',\n };\n\n if (!this.sentryConfig.dsn) {\n const dsnSource = config.dsn ? 'from config' : process.env.SENTRY_DSN ? 'from env' : 'missing';\n this.setDisabled(\n `Missing required DSN (dsn: ${dsnSource}). Set SENTRY_DSN environment variable or pass it in config.`,\n );\n return;\n }\n\n try {\n Sentry.init({\n dsn: this.sentryConfig.dsn,\n environment: this.sentryConfig.environment,\n tracesSampleRate: this.sentryConfig.tracesSampleRate,\n release: this.sentryConfig.release,\n ...config.options,\n });\n this.initialized = true;\n } catch (error) {\n this.setDisabled(`Failed to initialize Sentry: ${error}`);\n }\n }\n\n // ============================================================================\n // Main Event Handlers\n // ============================================================================\n\n protected async _exportTracingEvent(event: TracingEvent): Promise<void> {\n if (!this.initialized) return;\n\n const { type, exportedSpan } = event;\n\n if (exportedSpan.isEvent) {\n this.handleEventSpan(exportedSpan);\n return;\n }\n\n // Skip MODEL_CHUNK and MODEL_STEP spans to simplify trace hierarchy.\n // We store them in skippedSpans to preserve parent-child relationships:\n // when a child span references a skipped span as parent, resolveParentSpanId()\n // walks up the chain to find the first non-skipped ancestor.\n if (exportedSpan.type === SpanType.MODEL_CHUNK || exportedSpan.type === SpanType.MODEL_STEP) {\n if (type === TracingEventType.SPAN_STARTED) {\n this.skippedSpans.set(exportedSpan.id, exportedSpan.parentSpanId || '');\n } else if (type === TracingEventType.SPAN_ENDED) {\n this.skippedSpans.delete(exportedSpan.id);\n }\n return;\n }\n\n switch (type) {\n case TracingEventType.SPAN_STARTED:\n await this.handleSpanStarted(exportedSpan);\n break;\n case TracingEventType.SPAN_UPDATED:\n await this.handleSpanUpdated(exportedSpan);\n break;\n case TracingEventType.SPAN_ENDED:\n await this.handleSpanEnded(exportedSpan);\n break;\n }\n }\n\n private handleEventSpan(span: AnyExportedSpan): void {\n Sentry.addBreadcrumb({\n type: 'default',\n category: span.type,\n message: span.name,\n level: span.errorInfo ? 'error' : 'info',\n data: {\n spanId: span.id,\n traceId: span.traceId,\n ...(span.input && { input: this.serializeValue(span.input) }),\n ...(span.output && { output: this.serializeValue(span.output) }),\n ...(span.metadata && { metadata: span.metadata }),\n ...(span.attributes && { attributes: span.attributes }),\n },\n timestamp: span.startTime.getTime() / 1000,\n });\n }\n\n private async handleSpanStarted(span: AnyExportedSpan): Promise<void> {\n const resolvedParentId = this.resolveParentSpanId(span.parentSpanId);\n\n const sentrySpan = Sentry.startInactiveSpan({\n op: this.getOperationType(span),\n name: getGenAISpanName(span),\n startTime: span.startTime.getTime(),\n forceTransaction: span.isRootSpan,\n parentSpan: resolvedParentId ? this.spanMap.get(resolvedParentId)?.span : undefined,\n });\n\n sentrySpan.setAttributes(this.buildSpanAttributes(span));\n\n this.spanMap.set(span.id, {\n span: sentrySpan,\n spanType: span.type,\n });\n\n // Track tool calls as children of MODEL_GENERATION spans for gen_ai.response.tool_calls attribute\n if ((span.type === SpanType.TOOL_CALL || span.type === SpanType.PROVIDER_TOOL_CALL) && resolvedParentId) {\n this.trackToolCallForParent(span, resolvedParentId);\n }\n }\n\n private async handleSpanUpdated(span: AnyExportedSpan): Promise<void> {\n const spanData = this.spanMap.get(span.id);\n if (!spanData) {\n this.logMissingSpan(span, 'span update');\n return;\n }\n // Attributes are set on SPAN_STARTED and finalized on SPAN_ENDED.\n // If dynamic updates become necessary, add spanData.span.setAttributes() here.\n }\n\n private async handleSpanEnded(span: AnyExportedSpan): Promise<void> {\n const spanData = this.spanMap.get(span.id);\n if (!spanData) {\n this.logMissingSpan(span, 'span end');\n return;\n }\n\n const { span: sentrySpan } = spanData;\n\n sentrySpan.setAttributes(this.buildSpanAttributes(span));\n\n if (span.type === SpanType.MODEL_GENERATION) {\n // Set gen_ai.response.tool_calls if this generation had tool calls\n this.applyToolCallsAttribute(spanData);\n\n const resolvedParentId = this.resolveParentSpanId(span.parentSpanId);\n if (resolvedParentId) {\n const parentData = this.spanMap.get(resolvedParentId);\n if (parentData?.spanType === SpanType.AGENT_RUN) {\n const modelAttr = span.attributes as ModelGenerationAttributes;\n parentData.generation = {\n model: modelAttr.model,\n output: span.output,\n usage: modelAttr.usage,\n };\n }\n }\n }\n\n if (span.type === SpanType.AGENT_RUN) {\n // Apply token usage from the single child MODEL_GENERATION span\n // (there is only ever one MODEL_GENERATION span per AGENT_RUN)\n this.applyUsageFromGeneration(spanData);\n\n this.setGenerationResponseAttributes(spanData);\n }\n\n if (span.errorInfo) {\n sentrySpan.setStatus({\n code: 2,\n message: span.errorInfo.message,\n });\n\n // Build an Error instance so Sentry can use the real stack trace captured\n // by observability rather than synthesizing one from this exporter's call site.\n // Passing a string to Sentry.captureException produces a stack that points to\n // handleSpanEnded, hiding the real error origin.\n const error = new Error(span.errorInfo.message);\n if (span.errorInfo.name) {\n error.name = span.errorInfo.name;\n }\n if (span.errorInfo.stack) {\n error.stack = span.errorInfo.stack;\n }\n\n Sentry.captureException(error, {\n contexts: {\n trace: { trace_id: span.traceId, span_id: span.id },\n span_info: {\n name: span.name,\n type: span.type,\n error_id: span.errorInfo.id,\n error_category: span.errorInfo.category,\n },\n },\n });\n }\n\n const endTime = span.endTime ? span.endTime.getTime() : undefined;\n sentrySpan.end(endTime);\n this.spanMap.delete(span.id);\n }\n\n // ============================================================================\n // Span Creation Helpers\n // ============================================================================\n\n private resolveParentSpanId(parentSpanId: string | undefined): string | undefined {\n if (!parentSpanId) return undefined;\n\n let currentParentId: string | undefined = parentSpanId;\n while (currentParentId && this.skippedSpans.has(currentParentId)) {\n currentParentId = this.skippedSpans.get(currentParentId);\n if (!currentParentId) break;\n }\n\n return currentParentId;\n }\n\n private getOperationType(span: AnyExportedSpan): string {\n const config = SPAN_TYPE_CONFIG[span.type];\n return config ? config.opType : 'ai.span';\n }\n\n private buildSpanAttributes(span: AnyExportedSpan): Record<string, any> {\n const attributes = getGenAIAttributes(span) as Record<string, any>;\n\n attributes[ATTRIBUTE_KEYS.SPAN_TYPE] = span.type;\n attributes[ATTRIBUTE_KEYS.ORIGIN] = 'auto.ai.mastra';\n\n if (span.metadata) {\n Object.entries(span.metadata).forEach(([key, value]) => {\n if (value !== undefined && value !== null && key !== 'langfuse') {\n attributes[`metadata.${key}`] = this.serializeValue(value);\n }\n });\n }\n\n this.setAttributeIfDefined(attributes, ATTRIBUTE_KEYS.TAGS, span.tags?.join(','));\n this.setAttributeIfDefined(attributes, ATTRIBUTE_KEYS.GEN_AI_CONVERSATION_ID, span.metadata?.threadId);\n\n this.addInputOutputAttributes(attributes, span);\n\n if (span.type === SpanType.MODEL_GENERATION) {\n this.addModelGenerationAttributes(attributes, span);\n }\n\n if (span.type === SpanType.TOOL_CALL || span.type === SpanType.PROVIDER_TOOL_CALL) {\n this.addToolCallAttributes(attributes, span);\n }\n\n if (span.type === SpanType.AGENT_RUN) {\n this.addAgentRunAttributes(attributes, span);\n }\n\n if (span.type === SpanType.WORKFLOW_RUN) {\n const workflowAttr = span.attributes as WorkflowRunAttributes;\n this.setAttributeIfDefined(attributes, ATTRIBUTE_KEYS.WORKFLOW_ID, this.getEntityName(span));\n this.setAttributeIfDefined(attributes, ATTRIBUTE_KEYS.WORKFLOW_STATUS, workflowAttr.status);\n }\n\n if (span.type === SpanType.WORKFLOW_STEP) {\n const stepAttr = span.attributes as WorkflowStepAttributes;\n this.setAttributeIfDefined(attributes, ATTRIBUTE_KEYS.WORKFLOW_STEP_ID, this.getEntityName(span));\n this.setAttributeIfDefined(attributes, ATTRIBUTE_KEYS.WORKFLOW_STEP_STATUS, stepAttr.status);\n }\n\n return attributes;\n }\n\n // ============================================================================\n // Sentry-Specific Attribute Formatters\n // ============================================================================\n\n /**\n * Adds Sentry-specific input/output attributes that complement GenAI semantic conventions.\n * Adds 'input' and 'output' keys for Sentry UI compatibility.\n */\n private addInputOutputAttributes(attributes: Record<string, any>, span: AnyExportedSpan): void {\n if (span.input !== undefined) {\n attributes[ATTRIBUTE_KEYS.INPUT] = this.serializeValue(span.input);\n }\n\n if (span.output !== undefined) {\n attributes[ATTRIBUTE_KEYS.OUTPUT] = this.serializeValue(span.output);\n\n // Extract text for MODEL_GENERATION spans\n if (span.type === SpanType.MODEL_GENERATION) {\n const outputText = this.extractOutputText(span.output);\n if (outputText) {\n attributes[ATTRIBUTE_KEYS.GEN_AI_RESPONSE_TEXT] = outputText;\n }\n }\n }\n }\n\n /**\n * Adds Sentry-specific MODEL_GENERATION attributes that complement GenAI semantic conventions.\n */\n private addModelGenerationAttributes(attributes: Record<string, any>, span: AnyExportedSpan): void {\n const modelAttr = span.attributes as ModelGenerationAttributes;\n\n if (modelAttr.streaming !== undefined) {\n attributes[ATTRIBUTE_KEYS.GEN_AI_REQUEST_STREAM] = modelAttr.streaming;\n attributes[ATTRIBUTE_KEYS.GEN_AI_RESPONSE_STREAMING] = modelAttr.streaming;\n }\n\n this.setAttributeIfDefined(\n attributes,\n ATTRIBUTE_KEYS.GEN_AI_COMPLETION_START_TIME,\n modelAttr.completionStartTime?.toISOString(),\n );\n\n if (modelAttr.usage) {\n const totalTokens = (modelAttr.usage.inputTokens || 0) + (modelAttr.usage.outputTokens || 0);\n if (totalTokens > 0) {\n attributes[ATTRIBUTE_KEYS.GEN_AI_USAGE_TOTAL_TOKENS] = totalTokens;\n }\n }\n }\n\n /**\n * Adds Sentry-specific TOOL_CALL attributes that complement GenAI semantic conventions.\n */\n private addToolCallAttributes(attributes: Record<string, any>, span: AnyExportedSpan): void {\n const toolAttr = span.attributes as ToolCallAttributes;\n\n this.setAttributeIfDefined(attributes, ATTRIBUTE_KEYS.TOOL_SUCCESS, toolAttr.success);\n this.setAttributeIfDefined(\n attributes,\n ATTRIBUTE_KEYS.GEN_AI_TOOL_CALL_ID,\n toolAttr.toolCallId ?? span.metadata?.toolCallId,\n );\n }\n\n /**\n * Adds Sentry-specific AGENT_RUN attributes that complement GenAI semantic conventions.\n */\n private addAgentRunAttributes(attributes: Record<string, any>, span: AnyExportedSpan): void {\n const agentAttr = span.attributes as AgentRunAttributes;\n\n const agentName = this.getEntityName(span);\n if (agentName) {\n attributes[ATTRIBUTE_KEYS.GEN_AI_PIPELINE_NAME] = agentName;\n }\n\n this.setAttributeIfDefined(attributes, ATTRIBUTE_KEYS.GEN_AI_AGENT_PROMPT, agentAttr.prompt);\n }\n\n // ============================================================================\n // Token Usage Management\n // ============================================================================\n\n /**\n * Applies token usage from the MODEL_GENERATION span to the AGENT_RUN span attributes.\n * Reads usage directly from the generation field.\n * Called when AGENT_RUN spans end to set gen_ai.usage.* attributes.\n */\n private applyUsageFromGeneration(spanData: SpanData): void {\n const usage = spanData.generation?.usage;\n if (!usage) return;\n\n const inputTokens = usage.inputTokens || 0;\n const outputTokens = usage.outputTokens || 0;\n\n if (inputTokens > 0) {\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_USAGE_INPUT_TOKENS, inputTokens);\n }\n if (outputTokens > 0) {\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_USAGE_OUTPUT_TOKENS, outputTokens);\n }\n\n const totalTokens = inputTokens + outputTokens;\n if (totalTokens > 0) {\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_USAGE_TOTAL_TOKENS, totalTokens);\n }\n\n const cacheReadTokens = usage.inputDetails?.cacheRead || 0;\n const cacheWriteTokens = usage.inputDetails?.cacheWrite || 0;\n const reasoningTokens = usage.outputDetails?.reasoning || 0;\n\n if (cacheReadTokens > 0) {\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_USAGE_CACHE_READ_TOKENS, cacheReadTokens);\n }\n if (cacheWriteTokens > 0) {\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_USAGE_CACHE_WRITE_TOKENS, cacheWriteTokens);\n }\n if (reasoningTokens > 0) {\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_USAGE_REASONING_TOKENS, reasoningTokens);\n }\n }\n\n /**\n * Sets gen_ai.response.model and gen_ai.response.text from the MODEL_GENERATION.\n * Only applies to AGENT_RUN spans.\n */\n private setGenerationResponseAttributes(spanData: SpanData): void {\n if (!spanData.generation) return;\n\n if (spanData.generation.model) {\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_RESPONSE_MODEL, spanData.generation.model);\n }\n\n if (spanData.generation.output) {\n const outputText = this.extractOutputText(spanData.generation.output);\n if (outputText) {\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_RESPONSE_TEXT, outputText);\n }\n }\n }\n\n /**\n * Tracks a TOOL_CALL span as a child of its parent MODEL_GENERATION span.\n * This builds the tool_calls array for gen_ai.response.tool_calls attribute.\n */\n private trackToolCallForParent(span: AnyExportedSpan, parentId: string): void {\n const parentSpanData = this.spanMap.get(parentId);\n if (!parentSpanData || parentSpanData.spanType !== SpanType.MODEL_GENERATION) {\n return;\n }\n\n const toolAttr = span.attributes as ToolCallAttributes;\n if (!parentSpanData.toolCalls) {\n parentSpanData.toolCalls = [];\n }\n\n parentSpanData.toolCalls.push({\n name: this.getEntityName(span),\n id: toolAttr.toolCallId ?? span.metadata?.toolCallId,\n type: toolAttr.toolType || 'function',\n });\n }\n\n /**\n * Applies the gen_ai.response.tool_calls attribute to MODEL_GENERATION spans.\n * Called when MODEL_GENERATION spans end if they have child tool calls.\n */\n private applyToolCallsAttribute(spanData: SpanData): void {\n if (!spanData.toolCalls || spanData.toolCalls.length === 0) {\n return;\n }\n\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_RESPONSE_TOOL_CALLS, JSON.stringify(spanData.toolCalls));\n }\n\n // ============================================================================\n // Utility Helpers\n // ============================================================================\n\n private logMissingSpan(span: AnyExportedSpan, operation: string): void {\n this.logger.warn(`Sentry exporter: No Sentry span found for ${operation}`, {\n traceId: span.traceId,\n spanId: span.id,\n spanName: span.name,\n });\n }\n\n private getEntityName(span: AnyExportedSpan): string {\n return span.entityName || span.entityId || 'unknown';\n }\n\n private extractOutputText(output: any): string | undefined {\n if (!output) return undefined;\n if (typeof output === 'string') return output;\n if (output.text && typeof output.text === 'string') return output.text;\n if (output.content && typeof output.content === 'string') return output.content;\n if (output.message?.content && typeof output.message.content === 'string') return output.message.content;\n return undefined;\n }\n\n private serializeValue(value: any): any {\n if (value === null || value === undefined) return value;\n if (typeof value === 'object') {\n try {\n return JSON.stringify(value);\n } catch {\n return String(value);\n }\n }\n return value;\n }\n\n private setAttributeIfDefined(attributes: Record<string, any>, key: string, value: any): void {\n if (value !== undefined && value !== null) {\n attributes[key] = value;\n }\n }\n\n // ============================================================================\n // Flush and Shutdown\n // ============================================================================\n\n /**\n * Force flush any buffered spans without shutting down the exporter.\n * This is useful in serverless environments where you need to ensure spans\n * are exported before the runtime instance is terminated.\n */\n async flush(): Promise<void> {\n if (!this.initialized) return;\n\n try {\n // Sentry.flush() sends any pending events to Sentry\n // The timeout is in milliseconds\n await Sentry.flush(2000);\n this.logger.debug('Sentry exporter: Flushed pending events');\n } catch (error) {\n this.logger.error('Sentry exporter: Error flushing events', { error });\n }\n }\n\n async shutdown(): Promise<void> {\n for (const [spanId, spanData] of this.spanMap.entries()) {\n try {\n spanData.span.end();\n } catch (error) {\n this.logger.error('Sentry exporter: Error ending span during shutdown', { spanId, error });\n }\n }\n\n this.spanMap.clear();\n this.skippedSpans.clear();\n\n if (this.initialized) {\n await Sentry.close(2000);\n }\n\n await super.shutdown();\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuCA,SAAgB,oBACd,SACyC;CACzC,OAAO,OAAO,YACZ,QAAQ,Q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S,OAAO;GACd,KAAK,OAAO,MAAM,sDAAsD;IAAE;IAAQ;GAAM,CAAC;EAC3F;EAGF,KAAK,QAAQ,MAAM;EACnB,KAAK,aAAa,MAAM;EAExB,IAAI,KAAK,aACP,MAAMA,aAAO,MAAM,GAAI;EAGzB,MAAM,MAAM,SAAS;CACvB;AACF"}
package/dist/index.js CHANGED
@@ -3,88 +3,110 @@ import { BaseExporter } from "@mastra/observability";
3
3
  import { getAttributes, getSpanName } from "@mastra/otel-exporter";
4
4
  import * as Sentry from "@sentry/node";
5
5
  //#region src/tracing.ts
6
- const SPAN_TYPE_CONFIG = {
7
- [SpanType.AGENT_RUN]: {
6
+ /**
7
+ * Builds the span-type map, dropping any entry whose span type does not exist
8
+ * in the paired `@mastra/core`.
9
+ *
10
+ * The peer range admits a core older than the one that introduced a given
11
+ * `SpanType` member, where `SpanType.X` is `undefined` at runtime. As a plain
12
+ * object literal that lands in the map under a literal `"undefined"` key, which
13
+ * then matches any span whose type is undefined and mislabels it.
14
+ *
15
+ * @internal Exported for tests.
16
+ */
17
+ function buildSpanTypeConfig(entries) {
18
+ return Object.fromEntries(entries.filter((entry) => entry[0] !== void 0));
19
+ }
20
+ const SPAN_TYPE_CONFIG = buildSpanTypeConfig([
21
+ [SpanType.AGENT_RUN, {
8
22
  opType: "gen_ai.invoke_agent",
9
23
  opName: "invoke_agent"
10
- },
11
- [SpanType.MODEL_GENERATION]: {
24
+ }],
25
+ [SpanType.MODEL_GENERATION, {
12
26
  opType: "gen_ai.chat",
13
27
  opName: "chat"
14
- },
15
- [SpanType.TOOL_CALL]: {
28
+ }],
29
+ [SpanType.TOOL_CALL, {
16
30
  opType: "gen_ai.execute_tool",
17
31
  opName: "execute_tool"
18
- },
19
- [SpanType.MCP_TOOL_CALL]: {
32
+ }],
33
+ [SpanType.MCP_TOOL_CALL, {
20
34
  opType: "gen_ai.execute_tool",
21
35
  opName: "execute_tool"
22
- },
23
- [SpanType.PROVIDER_TOOL_CALL]: {
36
+ }],
37
+ [SpanType.PROVIDER_TOOL_CALL, {
24
38
  opType: "gen_ai.execute_tool",
25
39
  opName: "execute_tool"
26
- },
27
- [SpanType.WORKFLOW_RUN]: {
40
+ }],
41
+ [SpanType.WORKFLOW_RUN, {
28
42
  opType: "workflow.run",
29
43
  opName: "workflow"
30
- },
31
- [SpanType.WORKFLOW_STEP]: {
44
+ }],
45
+ [SpanType.WORKFLOW_STEP, {
32
46
  opType: "workflow.step",
33
47
  opName: "step"
34
- },
35
- [SpanType.WORKFLOW_CONDITIONAL]: {
48
+ }],
49
+ [SpanType.WORKFLOW_CONDITIONAL, {
36
50
  opType: "workflow.conditional",
37
51
  opName: "step"
38
- },
39
- [SpanType.WORKFLOW_CONDITIONAL_EVAL]: {
52
+ }],
53
+ [SpanType.WORKFLOW_CONDITIONAL_EVAL, {
40
54
  opType: "workflow.conditional",
41
55
  opName: "step"
42
- },
43
- [SpanType.WORKFLOW_PARALLEL]: {
56
+ }],
57
+ [SpanType.WORKFLOW_PARALLEL, {
44
58
  opType: "workflow.parallel",
45
59
  opName: "step"
46
- },
47
- [SpanType.WORKFLOW_LOOP]: {
60
+ }],
61
+ [SpanType.WORKFLOW_LOOP, {
48
62
  opType: "workflow.loop",
49
63
  opName: "step"
50
- },
51
- [SpanType.WORKFLOW_SLEEP]: {
64
+ }],
65
+ [SpanType.WORKFLOW_SLEEP, {
52
66
  opType: "workflow.sleep",
53
67
  opName: "step"
54
- },
55
- [SpanType.WORKFLOW_WAIT_EVENT]: {
68
+ }],
69
+ [SpanType.WORKFLOW_WAIT_EVENT, {
56
70
  opType: "workflow.wait",
57
71
  opName: "step"
58
- },
59
- [SpanType.PROCESSOR_RUN]: {
72
+ }],
73
+ [SpanType.PROCESSOR_RUN, {
60
74
  opType: "ai.processor",
61
75
  opName: "step"
62
- },
63
- [SpanType.GENERIC]: {
76
+ }],
77
+ [SpanType.GENERIC, {
64
78
  opType: "ai.span",
65
79
  opName: "span"
66
- },
67
- [SpanType.MODEL_STEP]: {
80
+ }],
81
+ [SpanType.MODEL_STEP, {
68
82
  opType: "ai.span",
69
83
  opName: "step"
70
- },
71
- [SpanType.MODEL_CHUNK]: {
84
+ }],
85
+ [SpanType.MODEL_CHUNK, {
72
86
  opType: "ai.span",
73
87
  opName: "step"
74
- },
75
- [SpanType.SCORER_RUN]: {
88
+ }],
89
+ [SpanType.SCORER_RUN, {
76
90
  opType: "workflow.run",
77
91
  opName: "eval"
78
- },
79
- [SpanType.SCORER_STEP]: {
92
+ }],
93
+ [SpanType.SCORER_STEP, {
80
94
  opType: "workflow.step",
81
95
  opName: "step"
82
- },
83
- [SpanType.MEMORY_OPERATION]: {
96
+ }],
97
+ [SpanType.MEMORY_OPERATION, {
84
98
  opType: "ai.memory",
85
99
  opName: "memory"
86
- }
87
- };
100
+ }],
101
+ [SpanType.WORKSPACE_ACTION, {
102
+ opType: "ai.workspace",
103
+ opName: "workspace"
104
+ }],
105
+ [SpanType.SKILL_ACTION, {
106
+ opType: "ai.skill",
107
+ opName: "skill"
108
+ }]
109
+ ]);
88
110
  const ATTRIBUTE_KEYS = {
89
111
  SPAN_TYPE: "ai.span.type",
90
112
  ORIGIN: "sentry.origin",
@@ -459,6 +481,6 @@ var SentryExporter = class extends BaseExporter {
459
481
  }
460
482
  };
461
483
  //#endregion
462
- export { SentryExporter };
484
+ export { SentryExporter, buildSpanTypeConfig };
463
485
 
464
486
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["getGenAISpanName","getGenAIAttributes"],"sources":["../src/tracing.ts"],"sourcesContent":["/**\n * Sentry Exporter for Mastra Observability\n *\n * Sends observability data to Sentry for AI tracing and monitoring.\n * Uses Sentry's modern span model (v8+) with OpenTelemetry semantic conventions.\n *\n * Spans are hierarchically organized: AGENT_RUN -> MODEL_GENERATION -> TOOL_CALL\n * MODEL_STEP and MODEL_CHUNK spans are skipped to simplify the trace hierarchy.\n */\n\nimport type {\n TracingEvent,\n AnyExportedSpan,\n ModelGenerationAttributes,\n ToolCallAttributes,\n AgentRunAttributes,\n WorkflowRunAttributes,\n WorkflowStepAttributes,\n UsageStats,\n} from '@mastra/core/observability';\nimport { SpanType, TracingEventType } from '@mastra/core/observability';\nimport type { BaseExporterConfig } from '@mastra/observability';\nimport { BaseExporter } from '@mastra/observability';\nimport { getAttributes as getGenAIAttributes, getSpanName as getGenAISpanName } from '@mastra/otel-exporter';\nimport * as Sentry from '@sentry/node';\n\nconst SPAN_TYPE_CONFIG: Partial<Record<SpanType, { opType: string; opName: string }>> = {\n [SpanType.AGENT_RUN]: { opType: 'gen_ai.invoke_agent', opName: 'invoke_agent' },\n [SpanType.MODEL_GENERATION]: { opType: 'gen_ai.chat', opName: 'chat' },\n [SpanType.TOOL_CALL]: { opType: 'gen_ai.execute_tool', opName: 'execute_tool' },\n [SpanType.MCP_TOOL_CALL]: { opType: 'gen_ai.execute_tool', opName: 'execute_tool' },\n [SpanType.PROVIDER_TOOL_CALL]: { opType: 'gen_ai.execute_tool', opName: 'execute_tool' },\n [SpanType.WORKFLOW_RUN]: { opType: 'workflow.run', opName: 'workflow' },\n [SpanType.WORKFLOW_STEP]: { opType: 'workflow.step', opName: 'step' },\n [SpanType.WORKFLOW_CONDITIONAL]: { opType: 'workflow.conditional', opName: 'step' },\n [SpanType.WORKFLOW_CONDITIONAL_EVAL]: { opType: 'workflow.conditional', opName: 'step' },\n [SpanType.WORKFLOW_PARALLEL]: { opType: 'workflow.parallel', opName: 'step' },\n [SpanType.WORKFLOW_LOOP]: { opType: 'workflow.loop', opName: 'step' },\n [SpanType.WORKFLOW_SLEEP]: { opType: 'workflow.sleep', opName: 'step' },\n [SpanType.WORKFLOW_WAIT_EVENT]: { opType: 'workflow.wait', opName: 'step' },\n [SpanType.PROCESSOR_RUN]: { opType: 'ai.processor', opName: 'step' },\n [SpanType.GENERIC]: { opType: 'ai.span', opName: 'span' },\n [SpanType.MODEL_STEP]: { opType: 'ai.span', opName: 'step' },\n [SpanType.MODEL_CHUNK]: { opType: 'ai.span', opName: 'step' },\n [SpanType.SCORER_RUN]: { opType: 'workflow.run', opName: 'eval' },\n [SpanType.SCORER_STEP]: { opType: 'workflow.step', opName: 'step' },\n [SpanType.MEMORY_OPERATION]: { opType: 'ai.memory', opName: 'memory' },\n};\n\nconst ATTRIBUTE_KEYS = {\n SPAN_TYPE: 'ai.span.type',\n ORIGIN: 'sentry.origin',\n TAGS: 'tags',\n INPUT: 'input',\n OUTPUT: 'output',\n GEN_AI_REQUEST_STREAM: 'gen_ai.request.stream',\n GEN_AI_RESPONSE_MODEL: 'gen_ai.response.model',\n GEN_AI_RESPONSE_STREAMING: 'gen_ai.response.streaming',\n GEN_AI_RESPONSE_TOOL_CALLS: 'gen_ai.response.tool_calls',\n GEN_AI_RESPONSE_TEXT: 'gen_ai.response.text',\n GEN_AI_CONVERSATION_ID: 'gen_ai.conversation.id',\n GEN_AI_COMPLETION_START_TIME: 'gen_ai.completion_start_time',\n GEN_AI_TOOL_CALL_ID: 'gen_ai.tool.call.id',\n TOOL_SUCCESS: 'tool.success',\n GEN_AI_PIPELINE_NAME: 'gen_ai.pipeline.name',\n GEN_AI_AGENT_PROMPT: 'gen_ai.agent.prompt',\n WORKFLOW_ID: 'workflow.id',\n WORKFLOW_STATUS: 'workflow.status',\n WORKFLOW_STEP_ID: 'workflow.step.id',\n WORKFLOW_STEP_STATUS: 'workflow.step.status',\n GEN_AI_USAGE_INPUT_TOKENS: 'gen_ai.usage.input_tokens',\n GEN_AI_USAGE_OUTPUT_TOKENS: 'gen_ai.usage.output_tokens',\n GEN_AI_USAGE_TOTAL_TOKENS: 'gen_ai.usage.total_tokens',\n GEN_AI_USAGE_CACHE_READ_TOKENS: 'gen_ai.usage.cache_read.input_tokens',\n GEN_AI_USAGE_CACHE_WRITE_TOKENS: 'gen_ai.usage.cache_creation.input_tokens',\n GEN_AI_USAGE_REASONING_TOKENS: 'gen_ai.usage.reasoning_tokens',\n} as const;\n\nexport interface SentryExporterConfig extends BaseExporterConfig {\n // Sentry SDK options (passed to Sentry.init())\n /** Data Source Name - tells the SDK where to send events */\n dsn?: string;\n /** Deployment environment (enables filtering issues and alerts by environment) */\n environment?: string;\n /** Percentage of transactions sent to Sentry (0.0 = 0%, 1.0 = 100%) */\n tracesSampleRate?: number;\n /** Version of your code deployed (helps identify regressions and track deployments) */\n release?: string;\n /** Additional Sentry SDK options (integrations, beforeSend, etc.) */\n options?: Partial<Sentry.NodeOptions>;\n}\n\n/**\n * Internal span tracking data.\n * generation tracks the single MODEL_GENERATION for AGENT_RUN response attributes.\n * toolCalls tracks child tool calls for MODEL_GENERATION spans.\n */\ntype SpanData = {\n span: Sentry.Span;\n spanType: SpanType;\n generation?: {\n model?: string;\n output?: any;\n usage?: UsageStats;\n };\n toolCalls?: Array<{\n name: string;\n id?: string;\n type?: string;\n }>;\n};\n\n/** Config type with Sentry-specific fields resolved */\ntype ResolvedSentryConfig = Required<\n Pick<SentryExporterConfig, 'dsn' | 'environment' | 'tracesSampleRate' | 'release'>\n>;\n\nexport class SentryExporter extends BaseExporter {\n name = 'sentry';\n private sentryConfig: ResolvedSentryConfig;\n private spanMap = new Map<string, SpanData>();\n private skippedSpans = new Map<string, string>();\n private initialized = false;\n\n constructor(config: SentryExporterConfig = {}) {\n super(config);\n\n this.sentryConfig = {\n dsn: config.dsn ?? process.env.SENTRY_DSN ?? '',\n environment: config.environment ?? process.env.SENTRY_ENVIRONMENT ?? 'production',\n tracesSampleRate: config.tracesSampleRate ?? 1.0,\n release: config.release ?? process.env.SENTRY_RELEASE ?? '',\n };\n\n if (!this.sentryConfig.dsn) {\n const dsnSource = config.dsn ? 'from config' : process.env.SENTRY_DSN ? 'from env' : 'missing';\n this.setDisabled(\n `Missing required DSN (dsn: ${dsnSource}). Set SENTRY_DSN environment variable or pass it in config.`,\n );\n return;\n }\n\n try {\n Sentry.init({\n dsn: this.sentryConfig.dsn,\n environment: this.sentryConfig.environment,\n tracesSampleRate: this.sentryConfig.tracesSampleRate,\n release: this.sentryConfig.release,\n ...config.options,\n });\n this.initialized = true;\n } catch (error) {\n this.setDisabled(`Failed to initialize Sentry: ${error}`);\n }\n }\n\n // ============================================================================\n // Main Event Handlers\n // ============================================================================\n\n protected async _exportTracingEvent(event: TracingEvent): Promise<void> {\n if (!this.initialized) return;\n\n const { type, exportedSpan } = event;\n\n if (exportedSpan.isEvent) {\n this.handleEventSpan(exportedSpan);\n return;\n }\n\n // Skip MODEL_CHUNK and MODEL_STEP spans to simplify trace hierarchy.\n // We store them in skippedSpans to preserve parent-child relationships:\n // when a child span references a skipped span as parent, resolveParentSpanId()\n // walks up the chain to find the first non-skipped ancestor.\n if (exportedSpan.type === SpanType.MODEL_CHUNK || exportedSpan.type === SpanType.MODEL_STEP) {\n if (type === TracingEventType.SPAN_STARTED) {\n this.skippedSpans.set(exportedSpan.id, exportedSpan.parentSpanId || '');\n } else if (type === TracingEventType.SPAN_ENDED) {\n this.skippedSpans.delete(exportedSpan.id);\n }\n return;\n }\n\n switch (type) {\n case TracingEventType.SPAN_STARTED:\n await this.handleSpanStarted(exportedSpan);\n break;\n case TracingEventType.SPAN_UPDATED:\n await this.handleSpanUpdated(exportedSpan);\n break;\n case TracingEventType.SPAN_ENDED:\n await this.handleSpanEnded(exportedSpan);\n break;\n }\n }\n\n private handleEventSpan(span: AnyExportedSpan): void {\n Sentry.addBreadcrumb({\n type: 'default',\n category: span.type,\n message: span.name,\n level: span.errorInfo ? 'error' : 'info',\n data: {\n spanId: span.id,\n traceId: span.traceId,\n ...(span.input && { input: this.serializeValue(span.input) }),\n ...(span.output && { output: this.serializeValue(span.output) }),\n ...(span.metadata && { metadata: span.metadata }),\n ...(span.attributes && { attributes: span.attributes }),\n },\n timestamp: span.startTime.getTime() / 1000,\n });\n }\n\n private async handleSpanStarted(span: AnyExportedSpan): Promise<void> {\n const resolvedParentId = this.resolveParentSpanId(span.parentSpanId);\n\n const sentrySpan = Sentry.startInactiveSpan({\n op: this.getOperationType(span),\n name: getGenAISpanName(span),\n startTime: span.startTime.getTime(),\n forceTransaction: span.isRootSpan,\n parentSpan: resolvedParentId ? this.spanMap.get(resolvedParentId)?.span : undefined,\n });\n\n sentrySpan.setAttributes(this.buildSpanAttributes(span));\n\n this.spanMap.set(span.id, {\n span: sentrySpan,\n spanType: span.type,\n });\n\n // Track tool calls as children of MODEL_GENERATION spans for gen_ai.response.tool_calls attribute\n if ((span.type === SpanType.TOOL_CALL || span.type === SpanType.PROVIDER_TOOL_CALL) && resolvedParentId) {\n this.trackToolCallForParent(span, resolvedParentId);\n }\n }\n\n private async handleSpanUpdated(span: AnyExportedSpan): Promise<void> {\n const spanData = this.spanMap.get(span.id);\n if (!spanData) {\n this.logMissingSpan(span, 'span update');\n return;\n }\n // Attributes are set on SPAN_STARTED and finalized on SPAN_ENDED.\n // If dynamic updates become necessary, add spanData.span.setAttributes() here.\n }\n\n private async handleSpanEnded(span: AnyExportedSpan): Promise<void> {\n const spanData = this.spanMap.get(span.id);\n if (!spanData) {\n this.logMissingSpan(span, 'span end');\n return;\n }\n\n const { span: sentrySpan } = spanData;\n\n sentrySpan.setAttributes(this.buildSpanAttributes(span));\n\n if (span.type === SpanType.MODEL_GENERATION) {\n // Set gen_ai.response.tool_calls if this generation had tool calls\n this.applyToolCallsAttribute(spanData);\n\n const resolvedParentId = this.resolveParentSpanId(span.parentSpanId);\n if (resolvedParentId) {\n const parentData = this.spanMap.get(resolvedParentId);\n if (parentData?.spanType === SpanType.AGENT_RUN) {\n const modelAttr = span.attributes as ModelGenerationAttributes;\n parentData.generation = {\n model: modelAttr.model,\n output: span.output,\n usage: modelAttr.usage,\n };\n }\n }\n }\n\n if (span.type === SpanType.AGENT_RUN) {\n // Apply token usage from the single child MODEL_GENERATION span\n // (there is only ever one MODEL_GENERATION span per AGENT_RUN)\n this.applyUsageFromGeneration(spanData);\n\n this.setGenerationResponseAttributes(spanData);\n }\n\n if (span.errorInfo) {\n sentrySpan.setStatus({\n code: 2,\n message: span.errorInfo.message,\n });\n\n // Build an Error instance so Sentry can use the real stack trace captured\n // by observability rather than synthesizing one from this exporter's call site.\n // Passing a string to Sentry.captureException produces a stack that points to\n // handleSpanEnded, hiding the real error origin.\n const error = new Error(span.errorInfo.message);\n if (span.errorInfo.name) {\n error.name = span.errorInfo.name;\n }\n if (span.errorInfo.stack) {\n error.stack = span.errorInfo.stack;\n }\n\n Sentry.captureException(error, {\n contexts: {\n trace: { trace_id: span.traceId, span_id: span.id },\n span_info: {\n name: span.name,\n type: span.type,\n error_id: span.errorInfo.id,\n error_category: span.errorInfo.category,\n },\n },\n });\n }\n\n const endTime = span.endTime ? span.endTime.getTime() : undefined;\n sentrySpan.end(endTime);\n this.spanMap.delete(span.id);\n }\n\n // ============================================================================\n // Span Creation Helpers\n // ============================================================================\n\n private resolveParentSpanId(parentSpanId: string | undefined): string | undefined {\n if (!parentSpanId) return undefined;\n\n let currentParentId: string | undefined = parentSpanId;\n while (currentParentId && this.skippedSpans.has(currentParentId)) {\n currentParentId = this.skippedSpans.get(currentParentId);\n if (!currentParentId) break;\n }\n\n return currentParentId;\n }\n\n private getOperationType(span: AnyExportedSpan): string {\n const config = SPAN_TYPE_CONFIG[span.type];\n return config ? config.opType : 'ai.span';\n }\n\n private buildSpanAttributes(span: AnyExportedSpan): Record<string, any> {\n const attributes = getGenAIAttributes(span) as Record<string, any>;\n\n attributes[ATTRIBUTE_KEYS.SPAN_TYPE] = span.type;\n attributes[ATTRIBUTE_KEYS.ORIGIN] = 'auto.ai.mastra';\n\n if (span.metadata) {\n Object.entries(span.metadata).forEach(([key, value]) => {\n if (value !== undefined && value !== null && key !== 'langfuse') {\n attributes[`metadata.${key}`] = this.serializeValue(value);\n }\n });\n }\n\n this.setAttributeIfDefined(attributes, ATTRIBUTE_KEYS.TAGS, span.tags?.join(','));\n this.setAttributeIfDefined(attributes, ATTRIBUTE_KEYS.GEN_AI_CONVERSATION_ID, span.metadata?.threadId);\n\n this.addInputOutputAttributes(attributes, span);\n\n if (span.type === SpanType.MODEL_GENERATION) {\n this.addModelGenerationAttributes(attributes, span);\n }\n\n if (span.type === SpanType.TOOL_CALL || span.type === SpanType.PROVIDER_TOOL_CALL) {\n this.addToolCallAttributes(attributes, span);\n }\n\n if (span.type === SpanType.AGENT_RUN) {\n this.addAgentRunAttributes(attributes, span);\n }\n\n if (span.type === SpanType.WORKFLOW_RUN) {\n const workflowAttr = span.attributes as WorkflowRunAttributes;\n this.setAttributeIfDefined(attributes, ATTRIBUTE_KEYS.WORKFLOW_ID, this.getEntityName(span));\n this.setAttributeIfDefined(attributes, ATTRIBUTE_KEYS.WORKFLOW_STATUS, workflowAttr.status);\n }\n\n if (span.type === SpanType.WORKFLOW_STEP) {\n const stepAttr = span.attributes as WorkflowStepAttributes;\n this.setAttributeIfDefined(attributes, ATTRIBUTE_KEYS.WORKFLOW_STEP_ID, this.getEntityName(span));\n this.setAttributeIfDefined(attributes, ATTRIBUTE_KEYS.WORKFLOW_STEP_STATUS, stepAttr.status);\n }\n\n return attributes;\n }\n\n // ============================================================================\n // Sentry-Specific Attribute Formatters\n // ============================================================================\n\n /**\n * Adds Sentry-specific input/output attributes that complement GenAI semantic conventions.\n * Adds 'input' and 'output' keys for Sentry UI compatibility.\n */\n private addInputOutputAttributes(attributes: Record<string, any>, span: AnyExportedSpan): void {\n if (span.input !== undefined) {\n attributes[ATTRIBUTE_KEYS.INPUT] = this.serializeValue(span.input);\n }\n\n if (span.output !== undefined) {\n attributes[ATTRIBUTE_KEYS.OUTPUT] = this.serializeValue(span.output);\n\n // Extract text for MODEL_GENERATION spans\n if (span.type === SpanType.MODEL_GENERATION) {\n const outputText = this.extractOutputText(span.output);\n if (outputText) {\n attributes[ATTRIBUTE_KEYS.GEN_AI_RESPONSE_TEXT] = outputText;\n }\n }\n }\n }\n\n /**\n * Adds Sentry-specific MODEL_GENERATION attributes that complement GenAI semantic conventions.\n */\n private addModelGenerationAttributes(attributes: Record<string, any>, span: AnyExportedSpan): void {\n const modelAttr = span.attributes as ModelGenerationAttributes;\n\n if (modelAttr.streaming !== undefined) {\n attributes[ATTRIBUTE_KEYS.GEN_AI_REQUEST_STREAM] = modelAttr.streaming;\n attributes[ATTRIBUTE_KEYS.GEN_AI_RESPONSE_STREAMING] = modelAttr.streaming;\n }\n\n this.setAttributeIfDefined(\n attributes,\n ATTRIBUTE_KEYS.GEN_AI_COMPLETION_START_TIME,\n modelAttr.completionStartTime?.toISOString(),\n );\n\n if (modelAttr.usage) {\n const totalTokens = (modelAttr.usage.inputTokens || 0) + (modelAttr.usage.outputTokens || 0);\n if (totalTokens > 0) {\n attributes[ATTRIBUTE_KEYS.GEN_AI_USAGE_TOTAL_TOKENS] = totalTokens;\n }\n }\n }\n\n /**\n * Adds Sentry-specific TOOL_CALL attributes that complement GenAI semantic conventions.\n */\n private addToolCallAttributes(attributes: Record<string, any>, span: AnyExportedSpan): void {\n const toolAttr = span.attributes as ToolCallAttributes;\n\n this.setAttributeIfDefined(attributes, ATTRIBUTE_KEYS.TOOL_SUCCESS, toolAttr.success);\n this.setAttributeIfDefined(\n attributes,\n ATTRIBUTE_KEYS.GEN_AI_TOOL_CALL_ID,\n toolAttr.toolCallId ?? span.metadata?.toolCallId,\n );\n }\n\n /**\n * Adds Sentry-specific AGENT_RUN attributes that complement GenAI semantic conventions.\n */\n private addAgentRunAttributes(attributes: Record<string, any>, span: AnyExportedSpan): void {\n const agentAttr = span.attributes as AgentRunAttributes;\n\n const agentName = this.getEntityName(span);\n if (agentName) {\n attributes[ATTRIBUTE_KEYS.GEN_AI_PIPELINE_NAME] = agentName;\n }\n\n this.setAttributeIfDefined(attributes, ATTRIBUTE_KEYS.GEN_AI_AGENT_PROMPT, agentAttr.prompt);\n }\n\n // ============================================================================\n // Token Usage Management\n // ============================================================================\n\n /**\n * Applies token usage from the MODEL_GENERATION span to the AGENT_RUN span attributes.\n * Reads usage directly from the generation field.\n * Called when AGENT_RUN spans end to set gen_ai.usage.* attributes.\n */\n private applyUsageFromGeneration(spanData: SpanData): void {\n const usage = spanData.generation?.usage;\n if (!usage) return;\n\n const inputTokens = usage.inputTokens || 0;\n const outputTokens = usage.outputTokens || 0;\n\n if (inputTokens > 0) {\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_USAGE_INPUT_TOKENS, inputTokens);\n }\n if (outputTokens > 0) {\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_USAGE_OUTPUT_TOKENS, outputTokens);\n }\n\n const totalTokens = inputTokens + outputTokens;\n if (totalTokens > 0) {\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_USAGE_TOTAL_TOKENS, totalTokens);\n }\n\n const cacheReadTokens = usage.inputDetails?.cacheRead || 0;\n const cacheWriteTokens = usage.inputDetails?.cacheWrite || 0;\n const reasoningTokens = usage.outputDetails?.reasoning || 0;\n\n if (cacheReadTokens > 0) {\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_USAGE_CACHE_READ_TOKENS, cacheReadTokens);\n }\n if (cacheWriteTokens > 0) {\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_USAGE_CACHE_WRITE_TOKENS, cacheWriteTokens);\n }\n if (reasoningTokens > 0) {\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_USAGE_REASONING_TOKENS, reasoningTokens);\n }\n }\n\n /**\n * Sets gen_ai.response.model and gen_ai.response.text from the MODEL_GENERATION.\n * Only applies to AGENT_RUN spans.\n */\n private setGenerationResponseAttributes(spanData: SpanData): void {\n if (!spanData.generation) return;\n\n if (spanData.generation.model) {\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_RESPONSE_MODEL, spanData.generation.model);\n }\n\n if (spanData.generation.output) {\n const outputText = this.extractOutputText(spanData.generation.output);\n if (outputText) {\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_RESPONSE_TEXT, outputText);\n }\n }\n }\n\n /**\n * Tracks a TOOL_CALL span as a child of its parent MODEL_GENERATION span.\n * This builds the tool_calls array for gen_ai.response.tool_calls attribute.\n */\n private trackToolCallForParent(span: AnyExportedSpan, parentId: string): void {\n const parentSpanData = this.spanMap.get(parentId);\n if (!parentSpanData || parentSpanData.spanType !== SpanType.MODEL_GENERATION) {\n return;\n }\n\n const toolAttr = span.attributes as ToolCallAttributes;\n if (!parentSpanData.toolCalls) {\n parentSpanData.toolCalls = [];\n }\n\n parentSpanData.toolCalls.push({\n name: this.getEntityName(span),\n id: toolAttr.toolCallId ?? span.metadata?.toolCallId,\n type: toolAttr.toolType || 'function',\n });\n }\n\n /**\n * Applies the gen_ai.response.tool_calls attribute to MODEL_GENERATION spans.\n * Called when MODEL_GENERATION spans end if they have child tool calls.\n */\n private applyToolCallsAttribute(spanData: SpanData): void {\n if (!spanData.toolCalls || spanData.toolCalls.length === 0) {\n return;\n }\n\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_RESPONSE_TOOL_CALLS, JSON.stringify(spanData.toolCalls));\n }\n\n // ============================================================================\n // Utility Helpers\n // ============================================================================\n\n private logMissingSpan(span: AnyExportedSpan, operation: string): void {\n this.logger.warn(`Sentry exporter: No Sentry span found for ${operation}`, {\n traceId: span.traceId,\n spanId: span.id,\n spanName: span.name,\n });\n }\n\n private getEntityName(span: AnyExportedSpan): string {\n return span.entityName || span.entityId || 'unknown';\n }\n\n private extractOutputText(output: any): string | undefined {\n if (!output) return undefined;\n if (typeof output === 'string') return output;\n if (output.text && typeof output.text === 'string') return output.text;\n if (output.content && typeof output.content === 'string') return output.content;\n if (output.message?.content && typeof output.message.content === 'string') return output.message.content;\n return undefined;\n }\n\n private serializeValue(value: any): any {\n if (value === null || value === undefined) return value;\n if (typeof value === 'object') {\n try {\n return JSON.stringify(value);\n } catch {\n return String(value);\n }\n }\n return value;\n }\n\n private setAttributeIfDefined(attributes: Record<string, any>, key: string, value: any): void {\n if (value !== undefined && value !== null) {\n attributes[key] = value;\n }\n }\n\n // ============================================================================\n // Flush and Shutdown\n // ============================================================================\n\n /**\n * Force flush any buffered spans without shutting down the exporter.\n * This is useful in serverless environments where you need to ensure spans\n * are exported before the runtime instance is terminated.\n */\n async flush(): Promise<void> {\n if (!this.initialized) return;\n\n try {\n // Sentry.flush() sends any pending events to Sentry\n // The timeout is in milliseconds\n await Sentry.flush(2000);\n this.logger.debug('Sentry exporter: Flushed pending events');\n } catch (error) {\n this.logger.error('Sentry exporter: Error flushing events', { error });\n }\n }\n\n async shutdown(): Promise<void> {\n for (const [spanId, spanData] of this.spanMap.entries()) {\n try {\n spanData.span.end();\n } catch (error) {\n this.logger.error('Sentry exporter: Error ending span during shutdown', { spanId, error });\n }\n }\n\n this.spanMap.clear();\n this.skippedSpans.clear();\n\n if (this.initialized) {\n await Sentry.close(2000);\n }\n\n await super.shutdown();\n }\n}\n"],"mappings":";;;;;AA0BA,MAAM,mBAAkF;EACrF,SAAS,YAAY;EAAE,QAAQ;EAAuB,QAAQ;CAAe;EAC7E,SAAS,mBAAmB;EAAE,QAAQ;EAAe,QAAQ;CAAO;EACpE,SAAS,YAAY;EAAE,QAAQ;EAAuB,QAAQ;CAAe;EAC7E,SAAS,gBAAgB;EAAE,QAAQ;EAAuB,QAAQ;CAAe;EACjF,SAAS,qBAAqB;EAAE,QAAQ;EAAuB,QAAQ;CAAe;EACtF,SAAS,eAAe;EAAE,QAAQ;EAAgB,QAAQ;CAAW;EACrE,SAAS,gBAAgB;EAAE,QAAQ;EAAiB,QAAQ;CAAO;EACnE,SAAS,uBAAuB;EAAE,QAAQ;EAAwB,QAAQ;CAAO;EACjF,SAAS,4BAA4B;EAAE,QAAQ;EAAwB,QAAQ;CAAO;EACtF,SAAS,oBAAoB;EAAE,QAAQ;EAAqB,QAAQ;CAAO;EAC3E,SAAS,gBAAgB;EAAE,QAAQ;EAAiB,QAAQ;CAAO;EACnE,SAAS,iBAAiB;EAAE,QAAQ;EAAkB,QAAQ;CAAO;EACrE,SAAS,sBAAsB;EAAE,QAAQ;EAAiB,QAAQ;CAAO;EACzE,SAAS,g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,CAAC,QAAQ,OAAO,KAAA;EACpB,IAAI,OAAO,WAAW,UAAU,OAAO;EACvC,IAAI,OAAO,QAAQ,OAAO,OAAO,SAAS,UAAU,OAAO,OAAO;EAClE,IAAI,OAAO,WAAW,OAAO,OAAO,YAAY,UAAU,OAAO,OAAO;EACxE,IAAI,OAAO,SAAS,WAAW,OAAO,OAAO,QAAQ,YAAY,UAAU,OAAO,OAAO,QAAQ;CAEnG;CAEA,eAAuB,OAAiB;EACtC,IAAI,UAAU,QAAQ,UAAU,KAAA,GAAW,OAAO;EAClD,IAAI,OAAO,UAAU,UACnB,IAAI;GACF,OAAO,KAAK,UAAU,KAAK;EAC7B,QAAQ;GACN,OAAO,OAAO,KAAK;EACrB;EAEF,OAAO;CACT;CAEA,sBAA8B,YAAiC,KAAa,OAAkB;EAC5F,IAAI,UAAU,KAAA,KAAa,UAAU,MACnC,WAAW,OAAO;CAEtB;;;;;;CAWA,MAAM,QAAuB;EAC3B,IAAI,CAAC,KAAK,aAAa;EAEvB,IAAI;GAGF,MAAM,OAAO,MAAM,GAAI;GACvB,KAAK,OAAO,MAAM,yCAAyC;EAC7D,SAAS,OAAO;GACd,KAAK,OAAO,MAAM,0CAA0C,EAAE,MAAM,CAAC;EACvE;CACF;CAEA,MAAM,WAA0B;EAC9B,KAAK,MAAM,CAAC,QAAQ,aAAa,KAAK,QAAQ,QAAQ,GACpD,IAAI;GACF,SAAS,KAAK,IAAI;EACpB,SAAS,OAAO;GACd,KAAK,OAAO,MAAM,sDAAsD;IAAE;IAAQ;GAAM,CAAC;EAC3F;EAGF,KAAK,QAAQ,MAAM;EACnB,KAAK,aAAa,MAAM;EAExB,IAAI,KAAK,aACP,MAAM,OAAO,MAAM,GAAI;EAGzB,MAAM,MAAM,SAAS;CACvB;AACF"}
1
+ {"version":3,"file":"index.js","names":["getGenAISpanName","getGenAIAttributes"],"sources":["../src/tracing.ts"],"sourcesContent":["/**\n * Sentry Exporter for Mastra Observability\n *\n * Sends observability data to Sentry for AI tracing and monitoring.\n * Uses Sentry's modern span model (v8+) with OpenTelemetry semantic conventions.\n *\n * Spans are hierarchically organized: AGENT_RUN -> MODEL_GENERATION -> TOOL_CALL\n * MODEL_STEP and MODEL_CHUNK spans are skipped to simplify the trace hierarchy.\n */\n\nimport type {\n TracingEvent,\n AnyExportedSpan,\n ModelGenerationAttributes,\n ToolCallAttributes,\n AgentRunAttributes,\n WorkflowRunAttributes,\n WorkflowStepAttributes,\n UsageStats,\n} from '@mastra/core/observability';\nimport { SpanType, TracingEventType } from '@mastra/core/observability';\nimport type { BaseExporterConfig } from '@mastra/observability';\nimport { BaseExporter } from '@mastra/observability';\nimport { getAttributes as getGenAIAttributes, getSpanName as getGenAISpanName } from '@mastra/otel-exporter';\nimport * as Sentry from '@sentry/node';\n\ntype SentrySpanOp = { opType: string; opName: string };\n\n/**\n * Builds the span-type map, dropping any entry whose span type does not exist\n * in the paired `@mastra/core`.\n *\n * The peer range admits a core older than the one that introduced a given\n * `SpanType` member, where `SpanType.X` is `undefined` at runtime. As a plain\n * object literal that lands in the map under a literal `\"undefined\"` key, which\n * then matches any span whose type is undefined and mislabels it.\n *\n * @internal Exported for tests.\n */\nexport function buildSpanTypeConfig(\n entries: Array<[SpanType | undefined, SentrySpanOp]>,\n): Partial<Record<SpanType, SentrySpanOp>> {\n return Object.fromEntries(\n entries.filter((entry): entry is [SpanType, SentrySpanOp] => entry[0] !== undefined),\n ) as Partial<Record<SpanType, SentrySpanOp>>;\n}\n\nconst SPAN_TYPE_CONFIG: Partial<Record<SpanType, SentrySpanOp>> = buildSpanTypeConfig([\n [SpanType.AGENT_RUN, { opType: 'gen_ai.invoke_agent', opName: 'invoke_agent' }],\n [SpanType.MODEL_GENERATION, { opType: 'gen_ai.chat', opName: 'chat' }],\n [SpanType.TOOL_CALL, { opType: 'gen_ai.execute_tool', opName: 'execute_tool' }],\n [SpanType.MCP_TOOL_CALL, { opType: 'gen_ai.execute_tool', opName: 'execute_tool' }],\n [SpanType.PROVIDER_TOOL_CALL, { opType: 'gen_ai.execute_tool', opName: 'execute_tool' }],\n [SpanType.WORKFLOW_RUN, { opType: 'workflow.run', opName: 'workflow' }],\n [SpanType.WORKFLOW_STEP, { opType: 'workflow.step', opName: 'step' }],\n [SpanType.WORKFLOW_CONDITIONAL, { opType: 'workflow.conditional', opName: 'step' }],\n [SpanType.WORKFLOW_CONDITIONAL_EVAL, { opType: 'workflow.conditional', opName: 'step' }],\n [SpanType.WORKFLOW_PARALLEL, { opType: 'workflow.parallel', opName: 'step' }],\n [SpanType.WORKFLOW_LOOP, { opType: 'workflow.loop', opName: 'step' }],\n [SpanType.WORKFLOW_SLEEP, { opType: 'workflow.sleep', opName: 'step' }],\n [SpanType.WORKFLOW_WAIT_EVENT, { opType: 'workflow.wait', opName: 'step' }],\n [SpanType.PROCESSOR_RUN, { opType: 'ai.processor', opName: 'step' }],\n [SpanType.GENERIC, { opType: 'ai.span', opName: 'span' }],\n [SpanType.MODEL_STEP, { opType: 'ai.span', opName: 'step' }],\n [SpanType.MODEL_CHUNK, { opType: 'ai.span', opName: 'step' }],\n [SpanType.SCORER_RUN, { opType: 'workflow.run', opName: 'eval' }],\n [SpanType.SCORER_STEP, { opType: 'workflow.step', opName: 'step' }],\n [SpanType.MEMORY_OPERATION, { opType: 'ai.memory', opName: 'memory' }],\n // Skill and workspace spans come from two places: the tools the model calls,\n // and the processors Mastra derives from agent config. A processor-flavoured\n // op would mislabel the tool calls, so both map to their subsystem the way\n // MEMORY_OPERATION already does. Without an entry they fall back to the\n // catch-all 'ai.span'.\n //\n // Both arrived after this package's oldest supported core, so against an\n // older one they read as `undefined`. `buildSpanTypeConfig` drops those\n // entries rather than keying the map under an `undefined` member.\n [SpanType.WORKSPACE_ACTION, { opType: 'ai.workspace', opName: 'workspace' }],\n [SpanType.SKILL_ACTION, { opType: 'ai.skill', opName: 'skill' }],\n]);\n\nconst ATTRIBUTE_KEYS = {\n SPAN_TYPE: 'ai.span.type',\n ORIGIN: 'sentry.origin',\n TAGS: 'tags',\n INPUT: 'input',\n OUTPUT: 'output',\n GEN_AI_REQUEST_STREAM: 'gen_ai.request.stream',\n GEN_AI_RESPONSE_MODEL: 'gen_ai.response.model',\n GEN_AI_RESPONSE_STREAMING: 'gen_ai.response.streaming',\n GEN_AI_RESPONSE_TOOL_CALLS: 'gen_ai.response.tool_calls',\n GEN_AI_RESPONSE_TEXT: 'gen_ai.response.text',\n GEN_AI_CONVERSATION_ID: 'gen_ai.conversation.id',\n GEN_AI_COMPLETION_START_TIME: 'gen_ai.completion_start_time',\n GEN_AI_TOOL_CALL_ID: 'gen_ai.tool.call.id',\n TOOL_SUCCESS: 'tool.success',\n GEN_AI_PIPELINE_NAME: 'gen_ai.pipeline.name',\n GEN_AI_AGENT_PROMPT: 'gen_ai.agent.prompt',\n WORKFLOW_ID: 'workflow.id',\n WORKFLOW_STATUS: 'workflow.status',\n WORKFLOW_STEP_ID: 'workflow.step.id',\n WORKFLOW_STEP_STATUS: 'workflow.step.status',\n GEN_AI_USAGE_INPUT_TOKENS: 'gen_ai.usage.input_tokens',\n GEN_AI_USAGE_OUTPUT_TOKENS: 'gen_ai.usage.output_tokens',\n GEN_AI_USAGE_TOTAL_TOKENS: 'gen_ai.usage.total_tokens',\n GEN_AI_USAGE_CACHE_READ_TOKENS: 'gen_ai.usage.cache_read.input_tokens',\n GEN_AI_USAGE_CACHE_WRITE_TOKENS: 'gen_ai.usage.cache_creation.input_tokens',\n GEN_AI_USAGE_REASONING_TOKENS: 'gen_ai.usage.reasoning_tokens',\n} as const;\n\nexport interface SentryExporterConfig extends BaseExporterConfig {\n // Sentry SDK options (passed to Sentry.init())\n /** Data Source Name - tells the SDK where to send events */\n dsn?: string;\n /** Deployment environment (enables filtering issues and alerts by environment) */\n environment?: string;\n /** Percentage of transactions sent to Sentry (0.0 = 0%, 1.0 = 100%) */\n tracesSampleRate?: number;\n /** Version of your code deployed (helps identify regressions and track deployments) */\n release?: string;\n /** Additional Sentry SDK options (integrations, beforeSend, etc.) */\n options?: Partial<Sentry.NodeOptions>;\n}\n\n/**\n * Internal span tracking data.\n * generation tracks the single MODEL_GENERATION for AGENT_RUN response attributes.\n * toolCalls tracks child tool calls for MODEL_GENERATION spans.\n */\ntype SpanData = {\n span: Sentry.Span;\n spanType: SpanType;\n generation?: {\n model?: string;\n output?: any;\n usage?: UsageStats;\n };\n toolCalls?: Array<{\n name: string;\n id?: string;\n type?: string;\n }>;\n};\n\n/** Config type with Sentry-specific fields resolved */\ntype ResolvedSentryConfig = Required<\n Pick<SentryExporterConfig, 'dsn' | 'environment' | 'tracesSampleRate' | 'release'>\n>;\n\nexport class SentryExporter extends BaseExporter {\n name = 'sentry';\n private sentryConfig: ResolvedSentryConfig;\n private spanMap = new Map<string, SpanData>();\n private skippedSpans = new Map<string, string>();\n private initialized = false;\n\n constructor(config: SentryExporterConfig = {}) {\n super(config);\n\n this.sentryConfig = {\n dsn: config.dsn ?? process.env.SENTRY_DSN ?? '',\n environment: config.environment ?? process.env.SENTRY_ENVIRONMENT ?? 'production',\n tracesSampleRate: config.tracesSampleRate ?? 1.0,\n release: config.release ?? process.env.SENTRY_RELEASE ?? '',\n };\n\n if (!this.sentryConfig.dsn) {\n const dsnSource = config.dsn ? 'from config' : process.env.SENTRY_DSN ? 'from env' : 'missing';\n this.setDisabled(\n `Missing required DSN (dsn: ${dsnSource}). Set SENTRY_DSN environment variable or pass it in config.`,\n );\n return;\n }\n\n try {\n Sentry.init({\n dsn: this.sentryConfig.dsn,\n environment: this.sentryConfig.environment,\n tracesSampleRate: this.sentryConfig.tracesSampleRate,\n release: this.sentryConfig.release,\n ...config.options,\n });\n this.initialized = true;\n } catch (error) {\n this.setDisabled(`Failed to initialize Sentry: ${error}`);\n }\n }\n\n // ============================================================================\n // Main Event Handlers\n // ============================================================================\n\n protected async _exportTracingEvent(event: TracingEvent): Promise<void> {\n if (!this.initialized) return;\n\n const { type, exportedSpan } = event;\n\n if (exportedSpan.isEvent) {\n this.handleEventSpan(exportedSpan);\n return;\n }\n\n // Skip MODEL_CHUNK and MODEL_STEP spans to simplify trace hierarchy.\n // We store them in skippedSpans to preserve parent-child relationships:\n // when a child span references a skipped span as parent, resolveParentSpanId()\n // walks up the chain to find the first non-skipped ancestor.\n if (exportedSpan.type === SpanType.MODEL_CHUNK || exportedSpan.type === SpanType.MODEL_STEP) {\n if (type === TracingEventType.SPAN_STARTED) {\n this.skippedSpans.set(exportedSpan.id, exportedSpan.parentSpanId || '');\n } else if (type === TracingEventType.SPAN_ENDED) {\n this.skippedSpans.delete(exportedSpan.id);\n }\n return;\n }\n\n switch (type) {\n case TracingEventType.SPAN_STARTED:\n await this.handleSpanStarted(exportedSpan);\n break;\n case TracingEventType.SPAN_UPDATED:\n await this.handleSpanUpdated(exportedSpan);\n break;\n case TracingEventType.SPAN_ENDED:\n await this.handleSpanEnded(exportedSpan);\n break;\n }\n }\n\n private handleEventSpan(span: AnyExportedSpan): void {\n Sentry.addBreadcrumb({\n type: 'default',\n category: span.type,\n message: span.name,\n level: span.errorInfo ? 'error' : 'info',\n data: {\n spanId: span.id,\n traceId: span.traceId,\n ...(span.input && { input: this.serializeValue(span.input) }),\n ...(span.output && { output: this.serializeValue(span.output) }),\n ...(span.metadata && { metadata: span.metadata }),\n ...(span.attributes && { attributes: span.attributes }),\n },\n timestamp: span.startTime.getTime() / 1000,\n });\n }\n\n private async handleSpanStarted(span: AnyExportedSpan): Promise<void> {\n const resolvedParentId = this.resolveParentSpanId(span.parentSpanId);\n\n const sentrySpan = Sentry.startInactiveSpan({\n op: this.getOperationType(span),\n name: getGenAISpanName(span),\n startTime: span.startTime.getTime(),\n forceTransaction: span.isRootSpan,\n parentSpan: resolvedParentId ? this.spanMap.get(resolvedParentId)?.span : undefined,\n });\n\n sentrySpan.setAttributes(this.buildSpanAttributes(span));\n\n this.spanMap.set(span.id, {\n span: sentrySpan,\n spanType: span.type,\n });\n\n // Track tool calls as children of MODEL_GENERATION spans for gen_ai.response.tool_calls attribute\n if ((span.type === SpanType.TOOL_CALL || span.type === SpanType.PROVIDER_TOOL_CALL) && resolvedParentId) {\n this.trackToolCallForParent(span, resolvedParentId);\n }\n }\n\n private async handleSpanUpdated(span: AnyExportedSpan): Promise<void> {\n const spanData = this.spanMap.get(span.id);\n if (!spanData) {\n this.logMissingSpan(span, 'span update');\n return;\n }\n // Attributes are set on SPAN_STARTED and finalized on SPAN_ENDED.\n // If dynamic updates become necessary, add spanData.span.setAttributes() here.\n }\n\n private async handleSpanEnded(span: AnyExportedSpan): Promise<void> {\n const spanData = this.spanMap.get(span.id);\n if (!spanData) {\n this.logMissingSpan(span, 'span end');\n return;\n }\n\n const { span: sentrySpan } = spanData;\n\n sentrySpan.setAttributes(this.buildSpanAttributes(span));\n\n if (span.type === SpanType.MODEL_GENERATION) {\n // Set gen_ai.response.tool_calls if this generation had tool calls\n this.applyToolCallsAttribute(spanData);\n\n const resolvedParentId = this.resolveParentSpanId(span.parentSpanId);\n if (resolvedParentId) {\n const parentData = this.spanMap.get(resolvedParentId);\n if (parentData?.spanType === SpanType.AGENT_RUN) {\n const modelAttr = span.attributes as ModelGenerationAttributes;\n parentData.generation = {\n model: modelAttr.model,\n output: span.output,\n usage: modelAttr.usage,\n };\n }\n }\n }\n\n if (span.type === SpanType.AGENT_RUN) {\n // Apply token usage from the single child MODEL_GENERATION span\n // (there is only ever one MODEL_GENERATION span per AGENT_RUN)\n this.applyUsageFromGeneration(spanData);\n\n this.setGenerationResponseAttributes(spanData);\n }\n\n if (span.errorInfo) {\n sentrySpan.setStatus({\n code: 2,\n message: span.errorInfo.message,\n });\n\n // Build an Error instance so Sentry can use the real stack trace captured\n // by observability rather than synthesizing one from this exporter's call site.\n // Passing a string to Sentry.captureException produces a stack that points to\n // handleSpanEnded, hiding the real error origin.\n const error = new Error(span.errorInfo.message);\n if (span.errorInfo.name) {\n error.name = span.errorInfo.name;\n }\n if (span.errorInfo.stack) {\n error.stack = span.errorInfo.stack;\n }\n\n Sentry.captureException(error, {\n contexts: {\n trace: { trace_id: span.traceId, span_id: span.id },\n span_info: {\n name: span.name,\n type: span.type,\n error_id: span.errorInfo.id,\n error_category: span.errorInfo.category,\n },\n },\n });\n }\n\n const endTime = span.endTime ? span.endTime.getTime() : undefined;\n sentrySpan.end(endTime);\n this.spanMap.delete(span.id);\n }\n\n // ============================================================================\n // Span Creation Helpers\n // ============================================================================\n\n private resolveParentSpanId(parentSpanId: string | undefined): string | undefined {\n if (!parentSpanId) return undefined;\n\n let currentParentId: string | undefined = parentSpanId;\n while (currentParentId && this.skippedSpans.has(currentParentId)) {\n currentParentId = this.skippedSpans.get(currentParentId);\n if (!currentParentId) break;\n }\n\n return currentParentId;\n }\n\n private getOperationType(span: AnyExportedSpan): string {\n const config = SPAN_TYPE_CONFIG[span.type];\n return config ? config.opType : 'ai.span';\n }\n\n private buildSpanAttributes(span: AnyExportedSpan): Record<string, any> {\n const attributes = getGenAIAttributes(span) as Record<string, any>;\n\n attributes[ATTRIBUTE_KEYS.SPAN_TYPE] = span.type;\n attributes[ATTRIBUTE_KEYS.ORIGIN] = 'auto.ai.mastra';\n\n if (span.metadata) {\n Object.entries(span.metadata).forEach(([key, value]) => {\n if (value !== undefined && value !== null && key !== 'langfuse') {\n attributes[`metadata.${key}`] = this.serializeValue(value);\n }\n });\n }\n\n this.setAttributeIfDefined(attributes, ATTRIBUTE_KEYS.TAGS, span.tags?.join(','));\n this.setAttributeIfDefined(attributes, ATTRIBUTE_KEYS.GEN_AI_CONVERSATION_ID, span.metadata?.threadId);\n\n this.addInputOutputAttributes(attributes, span);\n\n if (span.type === SpanType.MODEL_GENERATION) {\n this.addModelGenerationAttributes(attributes, span);\n }\n\n if (span.type === SpanType.TOOL_CALL || span.type === SpanType.PROVIDER_TOOL_CALL) {\n this.addToolCallAttributes(attributes, span);\n }\n\n if (span.type === SpanType.AGENT_RUN) {\n this.addAgentRunAttributes(attributes, span);\n }\n\n if (span.type === SpanType.WORKFLOW_RUN) {\n const workflowAttr = span.attributes as WorkflowRunAttributes;\n this.setAttributeIfDefined(attributes, ATTRIBUTE_KEYS.WORKFLOW_ID, this.getEntityName(span));\n this.setAttributeIfDefined(attributes, ATTRIBUTE_KEYS.WORKFLOW_STATUS, workflowAttr.status);\n }\n\n if (span.type === SpanType.WORKFLOW_STEP) {\n const stepAttr = span.attributes as WorkflowStepAttributes;\n this.setAttributeIfDefined(attributes, ATTRIBUTE_KEYS.WORKFLOW_STEP_ID, this.getEntityName(span));\n this.setAttributeIfDefined(attributes, ATTRIBUTE_KEYS.WORKFLOW_STEP_STATUS, stepAttr.status);\n }\n\n return attributes;\n }\n\n // ============================================================================\n // Sentry-Specific Attribute Formatters\n // ============================================================================\n\n /**\n * Adds Sentry-specific input/output attributes that complement GenAI semantic conventions.\n * Adds 'input' and 'output' keys for Sentry UI compatibility.\n */\n private addInputOutputAttributes(attributes: Record<string, any>, span: AnyExportedSpan): void {\n if (span.input !== undefined) {\n attributes[ATTRIBUTE_KEYS.INPUT] = this.serializeValue(span.input);\n }\n\n if (span.output !== undefined) {\n attributes[ATTRIBUTE_KEYS.OUTPUT] = this.serializeValue(span.output);\n\n // Extract text for MODEL_GENERATION spans\n if (span.type === SpanType.MODEL_GENERATION) {\n const outputText = this.extractOutputText(span.output);\n if (outputText) {\n attributes[ATTRIBUTE_KEYS.GEN_AI_RESPONSE_TEXT] = outputText;\n }\n }\n }\n }\n\n /**\n * Adds Sentry-specific MODEL_GENERATION attributes that complement GenAI semantic conventions.\n */\n private addModelGenerationAttributes(attributes: Record<string, any>, span: AnyExportedSpan): void {\n const modelAttr = span.attributes as ModelGenerationAttributes;\n\n if (modelAttr.streaming !== undefined) {\n attributes[ATTRIBUTE_KEYS.GEN_AI_REQUEST_STREAM] = modelAttr.streaming;\n attributes[ATTRIBUTE_KEYS.GEN_AI_RESPONSE_STREAMING] = modelAttr.streaming;\n }\n\n this.setAttributeIfDefined(\n attributes,\n ATTRIBUTE_KEYS.GEN_AI_COMPLETION_START_TIME,\n modelAttr.completionStartTime?.toISOString(),\n );\n\n if (modelAttr.usage) {\n const totalTokens = (modelAttr.usage.inputTokens || 0) + (modelAttr.usage.outputTokens || 0);\n if (totalTokens > 0) {\n attributes[ATTRIBUTE_KEYS.GEN_AI_USAGE_TOTAL_TOKENS] = totalTokens;\n }\n }\n }\n\n /**\n * Adds Sentry-specific TOOL_CALL attributes that complement GenAI semantic conventions.\n */\n private addToolCallAttributes(attributes: Record<string, any>, span: AnyExportedSpan): void {\n const toolAttr = span.attributes as ToolCallAttributes;\n\n this.setAttributeIfDefined(attributes, ATTRIBUTE_KEYS.TOOL_SUCCESS, toolAttr.success);\n this.setAttributeIfDefined(\n attributes,\n ATTRIBUTE_KEYS.GEN_AI_TOOL_CALL_ID,\n toolAttr.toolCallId ?? span.metadata?.toolCallId,\n );\n }\n\n /**\n * Adds Sentry-specific AGENT_RUN attributes that complement GenAI semantic conventions.\n */\n private addAgentRunAttributes(attributes: Record<string, any>, span: AnyExportedSpan): void {\n const agentAttr = span.attributes as AgentRunAttributes;\n\n const agentName = this.getEntityName(span);\n if (agentName) {\n attributes[ATTRIBUTE_KEYS.GEN_AI_PIPELINE_NAME] = agentName;\n }\n\n this.setAttributeIfDefined(attributes, ATTRIBUTE_KEYS.GEN_AI_AGENT_PROMPT, agentAttr.prompt);\n }\n\n // ============================================================================\n // Token Usage Management\n // ============================================================================\n\n /**\n * Applies token usage from the MODEL_GENERATION span to the AGENT_RUN span attributes.\n * Reads usage directly from the generation field.\n * Called when AGENT_RUN spans end to set gen_ai.usage.* attributes.\n */\n private applyUsageFromGeneration(spanData: SpanData): void {\n const usage = spanData.generation?.usage;\n if (!usage) return;\n\n const inputTokens = usage.inputTokens || 0;\n const outputTokens = usage.outputTokens || 0;\n\n if (inputTokens > 0) {\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_USAGE_INPUT_TOKENS, inputTokens);\n }\n if (outputTokens > 0) {\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_USAGE_OUTPUT_TOKENS, outputTokens);\n }\n\n const totalTokens = inputTokens + outputTokens;\n if (totalTokens > 0) {\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_USAGE_TOTAL_TOKENS, totalTokens);\n }\n\n const cacheReadTokens = usage.inputDetails?.cacheRead || 0;\n const cacheWriteTokens = usage.inputDetails?.cacheWrite || 0;\n const reasoningTokens = usage.outputDetails?.reasoning || 0;\n\n if (cacheReadTokens > 0) {\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_USAGE_CACHE_READ_TOKENS, cacheReadTokens);\n }\n if (cacheWriteTokens > 0) {\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_USAGE_CACHE_WRITE_TOKENS, cacheWriteTokens);\n }\n if (reasoningTokens > 0) {\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_USAGE_REASONING_TOKENS, reasoningTokens);\n }\n }\n\n /**\n * Sets gen_ai.response.model and gen_ai.response.text from the MODEL_GENERATION.\n * Only applies to AGENT_RUN spans.\n */\n private setGenerationResponseAttributes(spanData: SpanData): void {\n if (!spanData.generation) return;\n\n if (spanData.generation.model) {\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_RESPONSE_MODEL, spanData.generation.model);\n }\n\n if (spanData.generation.output) {\n const outputText = this.extractOutputText(spanData.generation.output);\n if (outputText) {\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_RESPONSE_TEXT, outputText);\n }\n }\n }\n\n /**\n * Tracks a TOOL_CALL span as a child of its parent MODEL_GENERATION span.\n * This builds the tool_calls array for gen_ai.response.tool_calls attribute.\n */\n private trackToolCallForParent(span: AnyExportedSpan, parentId: string): void {\n const parentSpanData = this.spanMap.get(parentId);\n if (!parentSpanData || parentSpanData.spanType !== SpanType.MODEL_GENERATION) {\n return;\n }\n\n const toolAttr = span.attributes as ToolCallAttributes;\n if (!parentSpanData.toolCalls) {\n parentSpanData.toolCalls = [];\n }\n\n parentSpanData.toolCalls.push({\n name: this.getEntityName(span),\n id: toolAttr.toolCallId ?? span.metadata?.toolCallId,\n type: toolAttr.toolType || 'function',\n });\n }\n\n /**\n * Applies the gen_ai.response.tool_calls attribute to MODEL_GENERATION spans.\n * Called when MODEL_GENERATION spans end if they have child tool calls.\n */\n private applyToolCallsAttribute(spanData: SpanData): void {\n if (!spanData.toolCalls || spanData.toolCalls.length === 0) {\n return;\n }\n\n spanData.span.setAttribute(ATTRIBUTE_KEYS.GEN_AI_RESPONSE_TOOL_CALLS, JSON.stringify(spanData.toolCalls));\n }\n\n // ============================================================================\n // Utility Helpers\n // ============================================================================\n\n private logMissingSpan(span: AnyExportedSpan, operation: string): void {\n this.logger.warn(`Sentry exporter: No Sentry span found for ${operation}`, {\n traceId: span.traceId,\n spanId: span.id,\n spanName: span.name,\n });\n }\n\n private getEntityName(span: AnyExportedSpan): string {\n return span.entityName || span.entityId || 'unknown';\n }\n\n private extractOutputText(output: any): string | undefined {\n if (!output) return undefined;\n if (typeof output === 'string') return output;\n if (output.text && typeof output.text === 'string') return output.text;\n if (output.content && typeof output.content === 'string') return output.content;\n if (output.message?.content && typeof output.message.content === 'string') return output.message.content;\n return undefined;\n }\n\n private serializeValue(value: any): any {\n if (value === null || value === undefined) return value;\n if (typeof value === 'object') {\n try {\n return JSON.stringify(value);\n } catch {\n return String(value);\n }\n }\n return value;\n }\n\n private setAttributeIfDefined(attributes: Record<string, any>, key: string, value: any): void {\n if (value !== undefined && value !== null) {\n attributes[key] = value;\n }\n }\n\n // ============================================================================\n // Flush and Shutdown\n // ============================================================================\n\n /**\n * Force flush any buffered spans without shutting down the exporter.\n * This is useful in serverless environments where you need to ensure spans\n * are exported before the runtime instance is terminated.\n */\n async flush(): Promise<void> {\n if (!this.initialized) return;\n\n try {\n // Sentry.flush() sends any pending events to Sentry\n // The timeout is in milliseconds\n await Sentry.flush(2000);\n this.logger.debug('Sentry exporter: Flushed pending events');\n } catch (error) {\n this.logger.error('Sentry exporter: Error flushing events', { error });\n }\n }\n\n async shutdown(): Promise<void> {\n for (const [spanId, spanData] of this.spanMap.entries()) {\n try {\n spanData.span.end();\n } catch (error) {\n this.logger.error('Sentry exporter: Error ending span during shutdown', { spanId, error });\n }\n }\n\n this.spanMap.clear();\n this.skippedSpans.clear();\n\n if (this.initialized) {\n await Sentry.close(2000);\n }\n\n await super.shutdown();\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAuCA,SAAgB,oBACd,SACyC;CACzC,OAAO,OAAO,YACZ,QAAQ,QAAQ,UAA6C,MAAM,OAAO,KAAA,CAAS,CACrF;AACF;AAEA,MAAM,mBAA4D,oBAAoB;CACpF,CAAC,SAAS,WAAW;EAAE,QAAQ;EAAuB,QAAQ;CAAe,CAAC;CAC9E,CAAC,SAAS,kBAAkB;EAAE,QAAQ;EAAe,QAAQ;CAAO,CAAC;CACrE,CAAC,SAAS,WAAW;EAAE,QAAQ;EAAuB,QAAQ;CAAe,CAAC;CAC9E,CAAC,SAAS,eAAe;EAAE,QAAQ;EAAuB,QAAQ;CAAe,CAAC;CAClF,CAAC,SAAS,oBAAoB;EAAE,QAAQ;EAAuB,QAAQ;CAAe,CAAC;CACvF,CAAC,SAAS,cAAc;EAAE,QAAQ;EAAgB,QAAQ;CAAW,CAAC;CACtE,CAAC,SAAS,eAAe;EAAE,QAAQ;EAAiB,QAAQ;CAAO,CAAC;CACpE,CAAC,SAAS,sBAAsB;EAAE,QAAQ;EAAwB,QAAQ;CAAO,CAAC;CAClF,CAAC,SAAS,2BAA2B;EAAE,QAAQ;EAAwB,QAAQ;CAAO,CAAC;CACvF,CAAC,SAAS,mBAAmB;EAAE,QAAQ;EAAqB,QAAQ;CAAO,CAAC;CAC5E,CAAC,SAAS,eAAe;EAAE,QAAQ;EAAiB,QAAQ;CAAO,CAAC;CACpE,CAAC,SAAS,gBAAgB;EAAE,QAAQ;EAAkB,QAAQ;CAAO,CAAC;CACtE,CAAC,SAAS,qBAAqB;EAAE,QAAQ;EAAiB,QAAQ;CAAO,CAA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ACnB,KAAK,aAAa,MAAM;EAExB,IAAI,KAAK,aACP,MAAM,OAAO,MAAM,GAAI;EAGzB,MAAM,MAAM,SAAS;CACvB;AACF"}
package/dist/tracing.d.ts CHANGED
@@ -8,9 +8,26 @@
8
8
  * MODEL_STEP and MODEL_CHUNK spans are skipped to simplify the trace hierarchy.
9
9
  */
10
10
  import type { TracingEvent } from '@mastra/core/observability';
11
+ import { SpanType } from '@mastra/core/observability';
11
12
  import type { BaseExporterConfig } from '@mastra/observability';
12
13
  import { BaseExporter } from '@mastra/observability';
13
14
  import * as Sentry from '@sentry/node';
15
+ type SentrySpanOp = {
16
+ opType: string;
17
+ opName: string;
18
+ };
19
+ /**
20
+ * Builds the span-type map, dropping any entry whose span type does not exist
21
+ * in the paired `@mastra/core`.
22
+ *
23
+ * The peer range admits a core older than the one that introduced a given
24
+ * `SpanType` member, where `SpanType.X` is `undefined` at runtime. As a plain
25
+ * object literal that lands in the map under a literal `"undefined"` key, which
26
+ * then matches any span whose type is undefined and mislabels it.
27
+ *
28
+ * @internal Exported for tests.
29
+ */
30
+ export declare function buildSpanTypeConfig(entries: Array<[SpanType | undefined, SentrySpanOp]>): Partial<Record<SpanType, SentrySpanOp>>;
14
31
  export interface SentryExporterConfig extends BaseExporterConfig {
15
32
  /** Data Source Name - tells the SDK where to send events */
16
33
  dsn?: string;
@@ -89,4 +106,5 @@ export declare class SentryExporter extends BaseExporter {
89
106
  flush(): Promise<void>;
90
107
  shutdown(): Promise<void>;
91
108
  }
109
+ export {};
92
110
  //# sourceMappingURL=tracing.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"tracing.d.ts","sourceRoot":"","sources":["../src/tracing.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EACV,YAAY,EAQb,MAAM,4BAA4B,CAAC;AAEpC,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAChE,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB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OAAO,CAAC,YAAY,CAAuB;IAC3C,OAAO,CAAC,OAAO,CAA+B;IAC9C,OAAO,CAAC,YAAY,CAA6B;IACjD,OAAO,CAAC,WAAW,CAAS;IAE5B,YAAY,MAAM,GAAE,oBAAyB,EA8B5C;IAMD,UAAgB,mBAAmB,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAkCtE;IAED,OAAO,CAAC,eAAe;YAkBT,iBAAiB;YAwBjB,iBAAiB;YAUjB,eAAe;IA6E7B,OAAO,CAAC,mBAAmB;IAY3B,OAAO,CAAC,gBAAgB;IAKxB,OAAO,CAAC,mBAAmB;IAkD3B;;;OAGG;IACH,OAAO,CAAC,wBAAwB;IAkBhC;;OAEG;IACH,OAAO,CAAC,4BAA4B;IAsBpC;;OAEG;IACH,OAAO,CAAC,qBAAqB;IAW7B;;OAEG;IACH,OAAO,CAAC,qBAAqB;IAe7B;;;;OAIG;IACH,OAAO,CAAC,wBAAwB;IAkChC;;;OAGG;IACH,OAAO,CAAC,+BAA+B;IAevC;;;OAGG;IACH,OAAO,CAAC,sBAAsB;IAkB9B;;;OAGG;IACH,OAAO,CAAC,uBAAuB;IAY/B,OAAO,CAAC,cAAc;IAQtB,OAAO,CAAC,aAAa;IAIrB,OAAO,CAAC,iBAAiB;IASzB,OAAO,CAAC,cAAc;IAYtB,OAAO,CAAC,qBAAqB;IAU7B;;;;OAIG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAW3B;IAEK,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAiB9B;CACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/sentry",
3
- "version": "1.2.15-alpha.2",
3
+ "version": "1.2.16-alpha.0",
4
4
  "description": "Sentry AI observability exporter for Mastra",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -24,8 +24,8 @@
24
24
  "license": "Apache-2.0",
25
25
  "dependencies": {
26
26
  "@sentry/node": "^10.68.0",
27
- "@mastra/observability": "1.17.5-alpha.2",
28
- "@mastra/otel-exporter": "1.3.13-alpha.2"
27
+ "@mastra/observability": "1.17.6-alpha.0",
28
+ "@mastra/otel-exporter": "1.3.14-alpha.0"
29
29
  },
30
30
  "devDependencies": {
31
31
  "@types/node": "22.20.1",
@@ -35,9 +35,9 @@
35
35
  "tsdown": "0.22.9",
36
36
  "typescript": "^7.0.2",
37
37
  "vitest": "4.1.10",
38
- "@internal/lint": "0.0.129",
39
- "@mastra/core": "1.64.0-alpha.8",
40
- "@internal/types-builder": "0.0.104"
38
+ "@internal/types-builder": "0.0.105",
39
+ "@internal/lint": "0.0.130",
40
+ "@mastra/core": "1.65.0-alpha.1"
41
41
  },
42
42
  "peerDependencies": {
43
43
  "@mastra/core": ">=1.16.0-0 <2.0.0-0"