Mastra | Sentry for Next.js
Source URL: https://docs.sentry.io/platforms/javascript/guides/nextjs/ai-agent-monitoring/mastra
Mastra | Sentry for Next.js
Section titled “Mastra | Sentry for Next.js”This is a server-side exporter for Mastra AI tracing that uses the Node.js Sentry SDK. It requires Node.js or compatible runtimes. Requires @mastra/sentry@beta package.
Mastra is a framework for building AI-powered applications and agents with a modern TypeScript stack. The Mastra Sentry Exporter sends tracing data to Sentry using OpenTelemetry semantic conventions, providing insights into model performance, token usage, and tool executions.
Install the Mastra Sentry exporter package:
npm install @mastra/sentry@betaThe Sentry exporter can automatically read configuration from environment variables:
import { SentryExporter } from "@mastra/sentry";
// Reads from SENTRY_DSN, SENTRY_ENVIRONMENT, SENTRY_RELEASEconst exporter = new SentryExporter();You can also configure the exporter explicitly:
import { SentryExporter } from "@mastra/sentry";
const exporter = new SentryExporter({ dsn: process.env.SENTRY_DSN, environment: "production", tracesSampleRate: 1.0, release: "1.0.0",});Span Mapping
Mastra automatically maps its span types to Sentry operations for proper visualization in Sentry’s AI monitoring dashboards:
| Mastra Span Type | Sentry Operation |
|---|---|
AGENT_RUN | gen_ai.invoke_agent |
MODEL_GENERATION | gen_ai.chat |
TOOL_CALL | gen_ai.execute_tool |
MCP_TOOL_CALL | gen_ai.execute_tool |
WORKFLOW_RUN | workflow.run |
WORKFLOW_STEP | workflow.step |
WORKFLOW_CONDITIONAL | workflow.conditional |
WORKFLOW_PARALLEL | workflow.parallel |
WORKFLOW_LOOP | workflow.loop |
PROCESSOR_RUN | ai.processor |
GENERIC | ai.span |
Note: MODEL_STEP and MODEL_CHUNK spans are automatically skipped to simplify trace hierarchy. Their data is aggregated into parent MODEL_GENERATION spans.
Captured Data
The Sentry exporter captures comprehensive trace data following OpenTelemetry semantic conventions:
sentry.origin:auto.ai.mastra(identifies spans from Mastra)ai.span.type: Mastra span type
gen_ai.operation.name: Operation name (e.g.,chat)gen_ai.system: Model provider (e.g., OpenAI, Anthropic)gen_ai.request.model: Model identifiergen_ai.request.messages: Input messages/prompts (JSON)gen_ai.response.model: Response modelgen_ai.response.text: Output textgen_ai.response.tool_calls: Tool calls made during generationgen_ai.usage.input_tokens: Input token countgen_ai.usage.output_tokens: Output token countgen_ai.usage.total_tokens: Total tokens usedgen_ai.request.stream: Whether streaming was usedgen_ai.request.temperature: Temperature parametergen_ai.completion_start_time: Time to first token
gen_ai.operation.name:execute_toolgen_ai.tool.name: Tool identifiergen_ai.tool.type:functiongen_ai.tool.call.id: Tool call IDgen_ai.tool.input: Tool input parametersgen_ai.tool.output: Tool output resulttool.success: Success flag
gen_ai.operation.name:invoke_agentgen_ai.agent.name: Agent identifiergen_ai.pipeline.name: Agent namegen_ai.agent.instructions: Agent instructions/system promptgen_ai.response.model: Model from child generationgen_ai.response.text: Output from child generationgen_ai.usage.*: Token usage aggregated from child spans
Exports a tracing event to Sentry. Handles SPAN_STARTED, SPAN_UPDATED, and SPAN_ENDED events.
await exporter.exportTracingEvent(event);Force flushes any pending spans to Sentry without shutting down the exporter. Waits up to 2 seconds for pending data to be sent. Useful in serverless environments where you need to ensure spans are exported before the runtime terminates.
await exporter.flush();Ends all active spans, clears internal state, and closes the Sentry connection. Waits up to 2 seconds for pending data to be sent.
await exporter.shutdown();For complete documentation on using Mastra with Sentry, see the Mastra Sentry Exporter documentation.
@mastra/sentry:>=1.0.0-beta.2