@mastra/mcp-docs-server 0.13.24 → 0.13.25-alpha.1
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/.docs/organized/changelogs/%40internal%2Fstorage-test-utils.md +8 -8
- package/.docs/organized/changelogs/%40internal%2Ftypes-builder.md +2 -0
- package/.docs/organized/changelogs/%40mastra%2Fagent-builder.md +24 -1
- package/.docs/organized/changelogs/%40mastra%2Fai-sdk.md +22 -0
- package/.docs/organized/changelogs/%40mastra%2Fastra.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fchroma.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fclickhouse.md +21 -21
- package/.docs/organized/changelogs/%40mastra%2Fclient-js.md +46 -46
- package/.docs/organized/changelogs/%40mastra%2Fcloud.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare-d1.md +21 -21
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare.md +21 -21
- package/.docs/organized/changelogs/%40mastra%2Fcore.md +104 -104
- package/.docs/organized/changelogs/%40mastra%2Fcouchbase.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloud.md +40 -40
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloudflare.md +33 -33
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-netlify.md +34 -34
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-vercel.md +34 -34
- package/.docs/organized/changelogs/%40mastra%2Fdeployer.md +59 -59
- package/.docs/organized/changelogs/%40mastra%2Fdynamodb.md +21 -21
- package/.docs/organized/changelogs/%40mastra%2Fevals.md +24 -24
- package/.docs/organized/changelogs/%40mastra%2Flance.md +21 -21
- package/.docs/organized/changelogs/%40mastra%2Flibsql.md +29 -29
- package/.docs/organized/changelogs/%40mastra%2Floggers.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fmcp-docs-server.md +25 -25
- package/.docs/organized/changelogs/%40mastra%2Fmcp-registry-registry.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fmcp.md +22 -22
- package/.docs/organized/changelogs/%40mastra%2Fmemory.md +27 -27
- package/.docs/organized/changelogs/%40mastra%2Fmongodb.md +21 -21
- package/.docs/organized/changelogs/%40mastra%2Fmssql.md +21 -21
- package/.docs/organized/changelogs/%40mastra%2Fopensearch.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fpg.md +33 -33
- package/.docs/organized/changelogs/%40mastra%2Fpinecone.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fplayground-ui.md +73 -73
- package/.docs/organized/changelogs/%40mastra%2Fqdrant.md +20 -20
- package/.docs/organized/changelogs/%40mastra%2Frag.md +22 -22
- package/.docs/organized/changelogs/%40mastra%2Freact.md +17 -0
- package/.docs/organized/changelogs/%40mastra%2Fs3vectors.md +18 -0
- package/.docs/organized/changelogs/%40mastra%2Fschema-compat.md +6 -0
- package/.docs/organized/changelogs/%40mastra%2Fserver.md +51 -51
- package/.docs/organized/changelogs/%40mastra%2Fturbopuffer.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fupstash.md +24 -24
- package/.docs/organized/changelogs/%40mastra%2Fvectorize.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fvoice-azure.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fvoice-cloudflare.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fvoice-deepgram.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fvoice-elevenlabs.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fvoice-gladia.md +20 -3
- package/.docs/organized/changelogs/%40mastra%2Fvoice-google-gemini-live.md +18 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-google.md +21 -21
- package/.docs/organized/changelogs/%40mastra%2Fvoice-murf.md +20 -20
- package/.docs/organized/changelogs/%40mastra%2Fvoice-openai-realtime.md +20 -20
- package/.docs/organized/changelogs/%40mastra%2Fvoice-openai.md +21 -21
- package/.docs/organized/changelogs/%40mastra%2Fvoice-playai.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fvoice-sarvam.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fvoice-speechify.md +19 -19
- package/.docs/organized/changelogs/create-mastra.md +32 -32
- package/.docs/organized/changelogs/mastra.md +64 -64
- package/.docs/organized/code-examples/agent.md +0 -4
- package/.docs/organized/code-examples/ai-elements.md +47 -0
- package/.docs/organized/code-examples/heads-up-game.md +5 -5
- package/.docs/raw/auth/clerk.mdx +3 -3
- package/.docs/raw/index.mdx +1 -0
- package/.docs/raw/observability/ai-tracing/exporters/braintrust.mdx +81 -0
- package/.docs/raw/observability/ai-tracing/exporters/cloud.mdx +120 -0
- package/.docs/raw/observability/ai-tracing/exporters/default.mdx +168 -0
- package/.docs/raw/observability/ai-tracing/exporters/langfuse.mdx +121 -0
- package/.docs/raw/observability/ai-tracing/exporters/langsmith.mdx +88 -0
- package/.docs/raw/observability/ai-tracing/exporters/otel.mdx +250 -0
- package/.docs/raw/observability/ai-tracing/overview.mdx +565 -0
- package/.docs/raw/observability/ai-tracing/processors/sensitive-data-filter.mdx +274 -0
- package/.docs/raw/observability/{tracing.mdx → otel-tracing.mdx} +2 -2
- package/.docs/raw/observability/overview.mdx +66 -0
- package/.docs/raw/reference/agents/generate.mdx +39 -1
- package/.docs/raw/reference/agents/generateVNext.mdx +34 -2
- package/.docs/raw/reference/agents/network.mdx +35 -3
- package/.docs/raw/reference/auth/clerk.mdx +1 -1
- package/.docs/raw/reference/client-js/agents.mdx +4 -13
- package/.docs/raw/reference/client-js/mastra-client.mdx +10 -0
- package/.docs/raw/reference/client-js/observability.mdx +76 -0
- package/.docs/raw/reference/core/getScorer.mdx +75 -0
- package/.docs/raw/reference/core/getScorerByName.mdx +75 -0
- package/.docs/raw/reference/core/getScorers.mdx +42 -0
- package/.docs/raw/reference/core/mastra-class.mdx +7 -0
- package/.docs/raw/reference/observability/ai-tracing/ai-tracing.mdx +182 -0
- package/.docs/raw/reference/observability/ai-tracing/configuration.mdx +234 -0
- package/.docs/raw/reference/observability/ai-tracing/exporters/braintrust.mdx +112 -0
- package/.docs/raw/reference/observability/ai-tracing/exporters/cloud-exporter.mdx +176 -0
- package/.docs/raw/reference/observability/ai-tracing/exporters/console-exporter.mdx +148 -0
- package/.docs/raw/reference/observability/ai-tracing/exporters/default-exporter.mdx +196 -0
- package/.docs/raw/reference/observability/ai-tracing/exporters/langfuse.mdx +116 -0
- package/.docs/raw/reference/observability/ai-tracing/exporters/langsmith.mdx +112 -0
- package/.docs/raw/reference/observability/ai-tracing/exporters/otel.mdx +355 -0
- package/.docs/raw/reference/observability/ai-tracing/interfaces.mdx +651 -0
- package/.docs/raw/reference/observability/ai-tracing/processors/sensitive-data-filter.mdx +178 -0
- package/.docs/raw/reference/observability/ai-tracing/span.mdx +371 -0
- package/.docs/raw/reference/observability/{logger.mdx → logging/pino-logger.mdx} +1 -1
- package/.docs/raw/reference/observability/{providers → otel-tracing/providers}/index.mdx +6 -4
- package/.docs/raw/reference/scorers/create-scorer.mdx +59 -9
- package/.docs/raw/reference/scorers/mastra-scorer.mdx +6 -0
- package/.docs/raw/reference/scorers/run-experiment.mdx +216 -0
- package/.docs/raw/reference/streaming/ChunkType.mdx +3 -2
- package/.docs/raw/reference/streaming/agents/MastraModelOutput.mdx +1 -1
- package/.docs/raw/reference/streaming/agents/stream.mdx +41 -3
- package/.docs/raw/reference/streaming/agents/streamVNext.mdx +34 -2
- package/.docs/raw/reference/streaming/workflows/resumeStreamVNext.mdx +17 -1
- package/.docs/raw/reference/streaming/workflows/stream.mdx +39 -1
- package/.docs/raw/reference/streaming/workflows/streamVNext.mdx +39 -1
- package/.docs/raw/reference/tools/create-tool.mdx +34 -1
- package/.docs/raw/reference/workflows/run-methods/resume.mdx +38 -0
- package/.docs/raw/reference/workflows/run-methods/start.mdx +38 -0
- package/.docs/raw/scorers/custom-scorers.mdx +16 -1
- package/.docs/raw/scorers/overview.mdx +28 -0
- package/CHANGELOG.md +15 -0
- package/package.json +6 -6
- package/.docs/raw/observability/ai-tracing.mdx +0 -597
- /package/.docs/raw/reference/observability/{otel-config.mdx → otel-tracing/otel-config.mdx} +0 -0
- /package/.docs/raw/reference/observability/{providers → otel-tracing/providers}/arize-ax.mdx +0 -0
- /package/.docs/raw/reference/observability/{providers → otel-tracing/providers}/arize-phoenix.mdx +0 -0
- /package/.docs/raw/reference/observability/{providers → otel-tracing/providers}/braintrust.mdx +0 -0
- /package/.docs/raw/reference/observability/{providers → otel-tracing/providers}/dash0.mdx +0 -0
- /package/.docs/raw/reference/observability/{providers → otel-tracing/providers}/keywordsai.mdx +0 -0
- /package/.docs/raw/reference/observability/{providers → otel-tracing/providers}/laminar.mdx +0 -0
- /package/.docs/raw/reference/observability/{providers → otel-tracing/providers}/langfuse.mdx +0 -0
- /package/.docs/raw/reference/observability/{providers → otel-tracing/providers}/langsmith.mdx +0 -0
- /package/.docs/raw/reference/observability/{providers → otel-tracing/providers}/langwatch.mdx +0 -0
- /package/.docs/raw/reference/observability/{providers → otel-tracing/providers}/new-relic.mdx +0 -0
- /package/.docs/raw/reference/observability/{providers → otel-tracing/providers}/signoz.mdx +0 -0
- /package/.docs/raw/reference/observability/{providers → otel-tracing/providers}/traceloop.mdx +0 -0
|
@@ -0,0 +1,565 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "AI Tracing | Observability | Mastra Docs"
|
|
3
|
+
description: "Set up AI tracing for Mastra applications"
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import { Callout } from "nextra/components";
|
|
7
|
+
|
|
8
|
+
# AI Tracing
|
|
9
|
+
|
|
10
|
+
AI Tracing provides specialized monitoring and debugging for the AI-related operations in your application. When enabled, Mastra automatically creates traces for agent runs, LLM generations, tool calls, and workflow steps with AI-specific context and metadata.
|
|
11
|
+
|
|
12
|
+
Unlike traditional application tracing, AI Tracing focuses specifically on understanding your AI pipeline — capturing token usage, model parameters, tool execution details, and conversation flows. This makes it easier to debug issues, optimize performance, and understand how your AI systems behave in production.
|
|
13
|
+
|
|
14
|
+
## How It Works
|
|
15
|
+
|
|
16
|
+
AI Traces are created by:
|
|
17
|
+
|
|
18
|
+
- **Configure exporters** → send trace data to observability platforms
|
|
19
|
+
- **Set sampling strategies** → control which traces are collected
|
|
20
|
+
- **Run agents and workflows** → Mastra auto-instruments them with AI Tracing
|
|
21
|
+
|
|
22
|
+
## Configuration
|
|
23
|
+
|
|
24
|
+
### Basic Config
|
|
25
|
+
|
|
26
|
+
```ts filename="src/mastra/index.ts" showLineNumbers copy
|
|
27
|
+
export const mastra = new Mastra({
|
|
28
|
+
// ... other config
|
|
29
|
+
observability: {
|
|
30
|
+
default: { enabled: true }, // Enables DefaultExporter and CloudExporter
|
|
31
|
+
},
|
|
32
|
+
storage: new LibSQLStore({
|
|
33
|
+
url: "file:./mastra.db", // Storage is required for tracing
|
|
34
|
+
}),
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
When enabled, the default configuration automatically includes:
|
|
39
|
+
- **Service Name**: `"mastra"`
|
|
40
|
+
- **Sampling**: `"always"`- Sample (100% of traces)
|
|
41
|
+
- **Exporters**:
|
|
42
|
+
- `DefaultExporter` - Persists traces to your configured storage
|
|
43
|
+
- `CloudExporter` - Sends traces to Mastra Cloud (requires `MASTRA_CLOUD_ACCESS_TOKEN`)
|
|
44
|
+
- **Processors**: `SensitiveDataFilter` - Automatically redacts sensitive fields
|
|
45
|
+
|
|
46
|
+
### Expanded Basic Config
|
|
47
|
+
|
|
48
|
+
This default configuration is a minimal helper that equates to this more verbose configuration:
|
|
49
|
+
|
|
50
|
+
```ts filename="src/mastra/index.ts" showLineNumbers copy
|
|
51
|
+
import { CloudExporter, DefaultExporter, SensitiveDataFilter } from '@mastra/core/ai-tracing';
|
|
52
|
+
|
|
53
|
+
export const mastra = new Mastra({
|
|
54
|
+
// ... other config
|
|
55
|
+
observability: {
|
|
56
|
+
configs: {
|
|
57
|
+
default: {
|
|
58
|
+
serviceName: "mastra",
|
|
59
|
+
sampling: { type: 'always' },
|
|
60
|
+
processors: [
|
|
61
|
+
new SensitiveDataFilter(),
|
|
62
|
+
],
|
|
63
|
+
exporters: [
|
|
64
|
+
new CloudExporter(),
|
|
65
|
+
new DefaultExporter(),
|
|
66
|
+
],
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
storage: new LibSQLStore({
|
|
71
|
+
url: "file:./mastra.db", // Storage is required for tracing
|
|
72
|
+
}),
|
|
73
|
+
});
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
## Exporters
|
|
78
|
+
|
|
79
|
+
Exporters determine where your AI trace data is sent and how it's stored. Choosing the right exporters allows you to integrate with your existing observability stack, comply with data residency requirements, and optimize for cost and performance. You can use multiple exporters simultaneously to send the same trace data to different destinations — for example, storing detailed traces locally for debugging while sending sampled data to a cloud provider for production monitoring.
|
|
80
|
+
|
|
81
|
+
### Internal Exporters
|
|
82
|
+
|
|
83
|
+
Mastra provides two built-in exporters that work out of the box:
|
|
84
|
+
|
|
85
|
+
- **[Default](/docs/observability/ai-tracing/exporters/default)** - Persists traces to local storage for viewing in the Playground
|
|
86
|
+
- **[Cloud](/docs/observability/ai-tracing/exporters/cloud)** - Sends traces to Mastra Cloud for production monitoring and collaboration
|
|
87
|
+
|
|
88
|
+
### External Exporters
|
|
89
|
+
|
|
90
|
+
In addition to the internal exporters, Mastra supports integration with popular observability platforms. These exporters allow you to leverage your existing monitoring infrastructure and take advantage of platform-specific features like alerting, dashboards, and correlation with other application metrics.
|
|
91
|
+
|
|
92
|
+
- **[Braintrust](/docs/observability/ai-tracing/exporters/braintrust)** - Exports traces to Braintrust's eval and observability platform
|
|
93
|
+
- **[Langfuse](/docs/observability/ai-tracing/exporters/langfuse)** - Sends traces to the Langfuse open-source LLM engineering platform
|
|
94
|
+
- **[LangSmith](/docs/observability/ai-tracing/exporters/langsmith)** - Pushes traces into LangSmith’s observability and evaluation toolkit
|
|
95
|
+
- **[OpenTelemetry](/docs/observability/ai-tracing/exporters/otel)** - Deliver traces to any OpenTelemetry-compatible observability system
|
|
96
|
+
- Supports: Dash0, Laminar, New Relic, SigNoz, Traceloop, Zipkin, and others!
|
|
97
|
+
|
|
98
|
+
- **Arize** - Coming soon!
|
|
99
|
+
|
|
100
|
+
## Sampling Strategies
|
|
101
|
+
|
|
102
|
+
Sampling allows you to control which traces are collected, helping you balance between observability needs and resource costs. In production environments with high traffic, collecting every trace can be expensive and unnecessary. Sampling strategies let you capture a representative subset of traces while ensuring you don't miss critical information about errors or important operations.
|
|
103
|
+
|
|
104
|
+
Mastra supports four sampling strategies:
|
|
105
|
+
|
|
106
|
+
### Always Sample
|
|
107
|
+
Collects 100% of traces. Best for development, debugging, or low-traffic scenarios where you need complete visibility.
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
sampling: { type: 'always' }
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### Never Sample
|
|
114
|
+
Disables tracing entirely. Useful for specific environments where tracing adds no value or when you need to temporarily disable tracing without removing configuration.
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
sampling: { type: 'never' }
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Ratio-Based Sampling
|
|
121
|
+
Randomly samples a percentage of traces. Ideal for production environments where you want statistical insights without the cost of full tracing. The probability value ranges from 0 (no traces) to 1 (all traces).
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
sampling: {
|
|
125
|
+
type: 'ratio',
|
|
126
|
+
probability: 0.1 // Sample 10% of traces
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Custom Sampling
|
|
131
|
+
Implements your own sampling logic based on runtime context, metadata, or business rules. Perfect for complex scenarios like sampling based on user tier, request type, or error conditions.
|
|
132
|
+
|
|
133
|
+
```ts
|
|
134
|
+
sampling: {
|
|
135
|
+
type: 'custom',
|
|
136
|
+
sampler: (options) => {
|
|
137
|
+
// Sample premium users at higher rate
|
|
138
|
+
if (options?.metadata?.userTier === 'premium') {
|
|
139
|
+
return Math.random() < 0.5; // 50% sampling
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Default 1% sampling for others
|
|
143
|
+
return Math.random() < 0.01;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### Complete Example
|
|
149
|
+
|
|
150
|
+
```ts filename="src/mastra/index.ts" showLineNumbers copy
|
|
151
|
+
export const mastra = new Mastra({
|
|
152
|
+
observability: {
|
|
153
|
+
configs: {
|
|
154
|
+
"10_percent": {
|
|
155
|
+
serviceName: 'my-service',
|
|
156
|
+
// Sample 10% of traces
|
|
157
|
+
sampling: {
|
|
158
|
+
type: 'ratio',
|
|
159
|
+
probability: 0.1
|
|
160
|
+
},
|
|
161
|
+
exporters: [new DefaultExporter()],
|
|
162
|
+
}
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
});
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## Multi-Config Setup
|
|
169
|
+
|
|
170
|
+
Complex applications often require different tracing configurations for different scenarios. You might want detailed traces with full sampling during development, sampled traces sent to external providers in production, and specialized configurations for specific features or customer segments. The `configSelector` function enables dynamic configuration selection at runtime, allowing you to route traces based on request context, environment variables, feature flags, or any custom logic.
|
|
171
|
+
|
|
172
|
+
This approach is particularly valuable when:
|
|
173
|
+
- Running A/B tests with different observability requirements
|
|
174
|
+
- Providing enhanced debugging for specific customers or support cases
|
|
175
|
+
- Gradually rolling out new tracing providers without affecting existing monitoring
|
|
176
|
+
- Optimizing costs by using different sampling rates for different request types
|
|
177
|
+
- Maintaining separate trace streams for compliance or data residency requirements
|
|
178
|
+
|
|
179
|
+
<Callout type="info">
|
|
180
|
+
Note that only a single config can be used for a specific execution. But a single config can send data to multiple exporters simultaneously.
|
|
181
|
+
</Callout>
|
|
182
|
+
|
|
183
|
+
### Dynamic Configuration Selection
|
|
184
|
+
|
|
185
|
+
Use `configSelector` to choose the appropriate tracing configuration based on runtime context:
|
|
186
|
+
|
|
187
|
+
```ts filename="src/mastra/index.ts" showLineNumbers copy
|
|
188
|
+
export const mastra = new Mastra({
|
|
189
|
+
observability: {
|
|
190
|
+
default: { enabled: true }, // Provides 'default' instance
|
|
191
|
+
configs: {
|
|
192
|
+
langfuse: {
|
|
193
|
+
serviceName: 'langfuse-service',
|
|
194
|
+
exporters: [langfuseExporter],
|
|
195
|
+
},
|
|
196
|
+
braintrust: {
|
|
197
|
+
serviceName: 'braintrust-service',
|
|
198
|
+
exporters: [braintrustExporter],
|
|
199
|
+
},
|
|
200
|
+
debug: {
|
|
201
|
+
serviceName: 'debug-service',
|
|
202
|
+
sampling: { type: 'always' },
|
|
203
|
+
exporters: [new DefaultExporter()],
|
|
204
|
+
},
|
|
205
|
+
},
|
|
206
|
+
configSelector: (context, availableTracers) => {
|
|
207
|
+
// Use debug config for support requests
|
|
208
|
+
if (context.runtimeContext?.get('supportMode')) {
|
|
209
|
+
return 'debug';
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Route specific customers to different providers
|
|
213
|
+
const customerId = context.runtimeContext?.get('customerId');
|
|
214
|
+
if (customerId && premiumCustomers.includes(customerId)) {
|
|
215
|
+
return 'braintrust';
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// Route specific requests to langfuse
|
|
219
|
+
if (context.runtimeContext?.get('useExternalTracing')) {
|
|
220
|
+
return 'langfuse';
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return 'default';
|
|
224
|
+
},
|
|
225
|
+
},
|
|
226
|
+
});
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
### Environment-Based Configuration
|
|
230
|
+
|
|
231
|
+
A common pattern is to select configurations based on deployment environment:
|
|
232
|
+
|
|
233
|
+
```ts filename="src/mastra/index.ts" showLineNumbers copy
|
|
234
|
+
export const mastra = new Mastra({
|
|
235
|
+
observability: {
|
|
236
|
+
configs: {
|
|
237
|
+
development: {
|
|
238
|
+
serviceName: 'my-service-dev',
|
|
239
|
+
sampling: { type: 'always' },
|
|
240
|
+
exporters: [new DefaultExporter()],
|
|
241
|
+
},
|
|
242
|
+
staging: {
|
|
243
|
+
serviceName: 'my-service-staging',
|
|
244
|
+
sampling: { type: 'ratio', probability: 0.5 },
|
|
245
|
+
exporters: [langfuseExporter],
|
|
246
|
+
},
|
|
247
|
+
production: {
|
|
248
|
+
serviceName: 'my-service-prod',
|
|
249
|
+
sampling: { type: 'ratio', probability: 0.01 },
|
|
250
|
+
exporters: [cloudExporter, langfuseExporter],
|
|
251
|
+
},
|
|
252
|
+
},
|
|
253
|
+
configSelector: (context, availableTracers) => {
|
|
254
|
+
const env = process.env.NODE_ENV || 'development';
|
|
255
|
+
return env;
|
|
256
|
+
},
|
|
257
|
+
},
|
|
258
|
+
});
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
### Common Configuration Patterns & Troubleshooting
|
|
262
|
+
|
|
263
|
+
#### Default Config Takes Priority
|
|
264
|
+
|
|
265
|
+
When you have both the default config enabled and custom configs defined, **the default config will always be used** unless you explicitly select a different config:
|
|
266
|
+
|
|
267
|
+
```ts filename="src/mastra/index.ts" showLineNumbers copy
|
|
268
|
+
export const mastra = new Mastra({
|
|
269
|
+
observability: {
|
|
270
|
+
default: { enabled: true }, // This will always be used!
|
|
271
|
+
configs: {
|
|
272
|
+
langfuse: {
|
|
273
|
+
serviceName: 'my-service',
|
|
274
|
+
exporters: [langfuseExporter], // This won't be reached
|
|
275
|
+
},
|
|
276
|
+
},
|
|
277
|
+
},
|
|
278
|
+
});
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
**Solutions:**
|
|
282
|
+
|
|
283
|
+
1. **Disable the default** and use only custom configs:
|
|
284
|
+
```ts
|
|
285
|
+
observability: {
|
|
286
|
+
// comment out or remove this line to disable the default config
|
|
287
|
+
// default: { enabled: true },
|
|
288
|
+
configs: {
|
|
289
|
+
langfuse: { /* ... */ }
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
2. **Use a configSelector** to choose between configs:
|
|
295
|
+
```ts
|
|
296
|
+
observability: {
|
|
297
|
+
default: { enabled: true },
|
|
298
|
+
configs: {
|
|
299
|
+
langfuse: { /* ... */ }
|
|
300
|
+
},
|
|
301
|
+
configSelector: (context, availableConfigs) => {
|
|
302
|
+
// Logic to choose between 'default' and 'langfuse'
|
|
303
|
+
return useExternalTracing ? 'langfuse' : 'default';
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
#### Maintaining Playground and Cloud Access
|
|
309
|
+
|
|
310
|
+
When creating a custom config with external exporters, you might lose access to Mastra Playground and Cloud. To maintain access while adding external exporters, include the default exporters in your custom config:
|
|
311
|
+
|
|
312
|
+
```ts filename="src/mastra/index.ts" showLineNumbers copy
|
|
313
|
+
import { DefaultExporter, CloudExporter } from '@mastra/core/ai-tracing';
|
|
314
|
+
import { LangfuseExporter } from '@mastra/langfuse';
|
|
315
|
+
|
|
316
|
+
export const mastra = new Mastra({
|
|
317
|
+
observability: {
|
|
318
|
+
default: { enabled: false }, // Disable default to use custom
|
|
319
|
+
configs: {
|
|
320
|
+
production: {
|
|
321
|
+
serviceName: 'my-service',
|
|
322
|
+
exporters: [
|
|
323
|
+
new LangfuseExporter(), // External exporter
|
|
324
|
+
new DefaultExporter(), // Keep Playground access
|
|
325
|
+
new CloudExporter(), // Keep Cloud access
|
|
326
|
+
],
|
|
327
|
+
},
|
|
328
|
+
},
|
|
329
|
+
},
|
|
330
|
+
});
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
This configuration sends traces to all three destinations simultaneously:
|
|
334
|
+
- **Langfuse** for external observability
|
|
335
|
+
- **DefaultExporter** for local Playground access
|
|
336
|
+
- **CloudExporter** for Mastra Cloud dashboard
|
|
337
|
+
|
|
338
|
+
<Callout type="info">
|
|
339
|
+
Remember: A single trace can be sent to multiple exporters. You don't need separate configs for each exporter unless you want different sampling rates or processors.
|
|
340
|
+
</Callout>
|
|
341
|
+
|
|
342
|
+
## Adding Custom Metadata
|
|
343
|
+
|
|
344
|
+
Custom metadata allows you to attach additional context to your traces, making it easier to debug issues and understand system behavior in production. Metadata can include business logic details, performance metrics, user context, or any information that helps you understand what happened during execution.
|
|
345
|
+
|
|
346
|
+
You can add metadata to any span using the tracing context:
|
|
347
|
+
|
|
348
|
+
```ts showLineNumbers copy
|
|
349
|
+
execute: async ({ inputData, tracingContext }) => {
|
|
350
|
+
const startTime = Date.now();
|
|
351
|
+
const response = await fetch(inputData.endpoint);
|
|
352
|
+
|
|
353
|
+
// Add custom metadata to the current span
|
|
354
|
+
tracingContext.currentSpan?.update({
|
|
355
|
+
metadata: {
|
|
356
|
+
apiStatusCode: response.status,
|
|
357
|
+
endpoint: inputData.endpoint,
|
|
358
|
+
responseTimeMs: Date.now() - startTime,
|
|
359
|
+
userTier: inputData.userTier,
|
|
360
|
+
region: process.env.AWS_REGION,
|
|
361
|
+
}
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
return await response.json();
|
|
365
|
+
}
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
Metadata set here will be shown in all configured exporters.
|
|
369
|
+
|
|
370
|
+
## Creating Child Spans
|
|
371
|
+
|
|
372
|
+
Child spans allow you to track fine-grained operations within your workflow steps or tools. They provide visibility into sub-operations like database queries, API calls, file operations, or complex calculations. This hierarchical structure helps you identify performance bottlenecks and understand the exact sequence of operations.
|
|
373
|
+
|
|
374
|
+
Create child spans inside a tool call or workflow step to track specific operations:
|
|
375
|
+
|
|
376
|
+
```ts showLineNumbers copy
|
|
377
|
+
execute: async ({ input, tracingContext }) => {
|
|
378
|
+
// Create another child span for the main database operation
|
|
379
|
+
const querySpan = tracingContext.currentSpan?.createChildSpan({
|
|
380
|
+
type: 'generic',
|
|
381
|
+
name: 'database-query',
|
|
382
|
+
input: { query: input.query },
|
|
383
|
+
metadata: { database: 'production' },
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
try {
|
|
387
|
+
const results = await db.query(input.query);
|
|
388
|
+
querySpan?.end({
|
|
389
|
+
output: results.data,
|
|
390
|
+
metadata: {
|
|
391
|
+
rowsReturned: results.length,
|
|
392
|
+
queryTimeMs: results.executionTime,
|
|
393
|
+
cacheHit: results.fromCache
|
|
394
|
+
}
|
|
395
|
+
});
|
|
396
|
+
return results;
|
|
397
|
+
} catch (error) {
|
|
398
|
+
querySpan?.error({
|
|
399
|
+
error,
|
|
400
|
+
metadata: { retryable: isRetryableError(error) }
|
|
401
|
+
});
|
|
402
|
+
throw error;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
Child spans automatically inherit the trace context from their parent, maintaining the relationship hierarchy in your observability platform.
|
|
408
|
+
|
|
409
|
+
## Span Processors
|
|
410
|
+
|
|
411
|
+
Span processors allow you to transform, filter, or enrich trace data before it's exported. They act as a pipeline between span creation and export, enabling you to modify spans for security, compliance, or debugging purposes. Mastra includes built-in processors and supports custom implementations.
|
|
412
|
+
|
|
413
|
+
### Built-in Processors
|
|
414
|
+
|
|
415
|
+
* [Sensitive Data Filter](/docs/observability/ai-tracing/processors/sensitive-data-filter) redacts sensitive information. It is enabled in the default observability config.
|
|
416
|
+
|
|
417
|
+
### Creating Custom Processors
|
|
418
|
+
|
|
419
|
+
You can create custom span processors by implementing the `AISpanProcessor` interface. Here's a simple example that converts all input text in spans to lowercase:
|
|
420
|
+
|
|
421
|
+
```ts filename="src/processors/lowercase-input-processor.ts" showLineNumbers copy
|
|
422
|
+
import type { AISpanProcessor, AnyAISpan } from '@mastra/core/ai-tracing';
|
|
423
|
+
|
|
424
|
+
export class LowercaseInputProcessor implements AISpanProcessor {
|
|
425
|
+
name = 'lowercase-processor';
|
|
426
|
+
|
|
427
|
+
process(span: AnyAISpan): AnyAISpan {
|
|
428
|
+
span.input = `${span.input}`.toLowerCase()
|
|
429
|
+
return span;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
async shutdown(): Promise<void> {
|
|
433
|
+
// Cleanup if needed
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
// Use the custom processor
|
|
438
|
+
export const mastra = new Mastra({
|
|
439
|
+
observability: {
|
|
440
|
+
configs: {
|
|
441
|
+
development: {
|
|
442
|
+
processors: [
|
|
443
|
+
new LowercaseInputProcessor(),
|
|
444
|
+
new SensitiveDataFilter(),
|
|
445
|
+
],
|
|
446
|
+
exporters: [new DefaultExporter()],
|
|
447
|
+
},
|
|
448
|
+
},
|
|
449
|
+
},
|
|
450
|
+
});
|
|
451
|
+
```
|
|
452
|
+
|
|
453
|
+
Processors are executed in the order they're defined, allowing you to chain multiple transformations. Common use cases for custom processors include:
|
|
454
|
+
- Adding environment-specific metadata
|
|
455
|
+
- Filtering out spans based on criteria
|
|
456
|
+
- Normalizing data formats
|
|
457
|
+
- Sampling high-volume traces
|
|
458
|
+
- Enriching spans with business context
|
|
459
|
+
|
|
460
|
+
## Retrieving Trace IDs
|
|
461
|
+
|
|
462
|
+
When you execute agents or workflows with tracing enabled, the response includes a `traceId` that you can use to look up the full trace in your observability platform. This is useful for debugging, customer support, or correlating traces with other events in your system.
|
|
463
|
+
|
|
464
|
+
### Agent Trace IDs
|
|
465
|
+
|
|
466
|
+
Both `generate` and `stream` methods return the trace ID in their response:
|
|
467
|
+
|
|
468
|
+
```ts showLineNumbers copy
|
|
469
|
+
// Using generate
|
|
470
|
+
const result = await agent.generate({
|
|
471
|
+
messages: [{ role: 'user', content: 'Hello' }]
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
console.log('Trace ID:', result.traceId);
|
|
475
|
+
|
|
476
|
+
// Using stream
|
|
477
|
+
const streamResult = await agent.stream({
|
|
478
|
+
messages: [{ role: 'user', content: 'Tell me a story' }]
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
console.log('Trace ID:', streamResult.traceId);
|
|
482
|
+
```
|
|
483
|
+
|
|
484
|
+
### Workflow Trace IDs
|
|
485
|
+
|
|
486
|
+
Workflow executions also return trace IDs:
|
|
487
|
+
|
|
488
|
+
```ts showLineNumbers copy
|
|
489
|
+
// Create a workflow run
|
|
490
|
+
const run = await mastra.getWorkflow('myWorkflow').createRunAsync();
|
|
491
|
+
|
|
492
|
+
// Start the workflow
|
|
493
|
+
const result = await run.start({
|
|
494
|
+
inputData: { data: 'process this' }
|
|
495
|
+
});
|
|
496
|
+
|
|
497
|
+
console.log('Trace ID:', result.traceId);
|
|
498
|
+
|
|
499
|
+
// Or stream the workflow
|
|
500
|
+
const { stream, getWorkflowState } = run.stream({
|
|
501
|
+
inputData: { data: 'process this' }
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
// Get the final state which includes the trace ID
|
|
505
|
+
const finalState = await getWorkflowState();
|
|
506
|
+
console.log('Trace ID:', finalState.traceId);
|
|
507
|
+
```
|
|
508
|
+
|
|
509
|
+
### Using Trace IDs
|
|
510
|
+
|
|
511
|
+
Once you have a trace ID, you can:
|
|
512
|
+
|
|
513
|
+
1. **Look up traces in Mastra Playground**: Navigate to the traces view and search by ID
|
|
514
|
+
2. **Query traces in external platforms**: Use the ID in Langfuse, Braintrust, or your observability platform
|
|
515
|
+
3. **Correlate with logs**: Include the trace ID in your application logs for cross-referencing
|
|
516
|
+
4. **Share for debugging**: Provide trace IDs to support teams or developers for investigation
|
|
517
|
+
|
|
518
|
+
The trace ID is only available when tracing is enabled. If tracing is disabled or sampling excludes the request, `traceId` will be `undefined`.
|
|
519
|
+
|
|
520
|
+
## What Gets Traced
|
|
521
|
+
|
|
522
|
+
Mastra automatically creates spans for:
|
|
523
|
+
|
|
524
|
+
### Agent Operations
|
|
525
|
+
- **Agent runs** - Complete execution with instructions and tools
|
|
526
|
+
- **LLM calls** - Model interactions with tokens and parameters
|
|
527
|
+
- **Tool executions** - Function calls with inputs and outputs
|
|
528
|
+
- **Memory operations** - Thread and semantic recall
|
|
529
|
+
|
|
530
|
+
### Workflow Operations
|
|
531
|
+
- **Workflow runs** - Full execution from start to finish
|
|
532
|
+
- **Individual steps** - Step processing with inputs/outputs
|
|
533
|
+
- **Control flow** - Conditionals, loops, parallel execution
|
|
534
|
+
- **Wait operations** - Delays and event waiting
|
|
535
|
+
|
|
536
|
+
## Viewing Traces
|
|
537
|
+
|
|
538
|
+
Traces are available in multiple locations:
|
|
539
|
+
|
|
540
|
+
- **Mastra Playground** - Local development environment
|
|
541
|
+
- **Mastra Cloud** - Production monitoring dashboard
|
|
542
|
+
- **Langfuse Dashboard** - When using Langfuse exporter
|
|
543
|
+
- **Braintrust Console** - When using Braintrust exporter
|
|
544
|
+
|
|
545
|
+
## See Also
|
|
546
|
+
|
|
547
|
+
### Examples
|
|
548
|
+
- [Basic AI Tracing Example](/examples/observability/basic-ai-tracing) - Working implementation
|
|
549
|
+
|
|
550
|
+
### Reference Documentation
|
|
551
|
+
- [Configuration API](/reference/observability/ai-tracing/configuration) - ObservabilityConfig details
|
|
552
|
+
- [AITracing Classes](/reference/observability/ai-tracing/ai-tracing) - Core classes and methods
|
|
553
|
+
- [Span Interfaces](/reference/observability/ai-tracing/span) - Span types and lifecycle
|
|
554
|
+
- [Type Definitions](/reference/observability/ai-tracing/interfaces) - Complete interface reference
|
|
555
|
+
|
|
556
|
+
### Exporters
|
|
557
|
+
- [DefaultExporter](/reference/observability/ai-tracing/exporters/default-exporter) - Storage persistence
|
|
558
|
+
- [CloudExporter](/reference/observability/ai-tracing/exporters/cloud-exporter) - Mastra Cloud integration
|
|
559
|
+
- [ConsoleExporter](/reference/observability/ai-tracing/exporters/console-exporter) - Debug output
|
|
560
|
+
- [Langfuse](/reference/observability/ai-tracing/exporters/langfuse) - Langfuse integration
|
|
561
|
+
- [Braintrust](/reference/observability/ai-tracing/exporters/braintrust) - Braintrust integration
|
|
562
|
+
- [OpenTelemetry](/reference/observability/ai-tracing/exporters/otel) - OTEL-compatible platforms
|
|
563
|
+
|
|
564
|
+
### Processors
|
|
565
|
+
- [Sensitive Data Filter](/docs/observability/ai-tracing/processors/sensitive-data-filter) - Data redaction
|