@mastra/mcp-docs-server 0.13.17-alpha.5 → 0.13.17-alpha.6
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/%40mastra%2Fagent-builder.md +10 -0
- package/.docs/organized/changelogs/%40mastra%2Fclient-js.md +8 -8
- package/.docs/organized/changelogs/%40mastra%2Fcloud.md +12 -12
- package/.docs/organized/changelogs/%40mastra%2Fcore.md +28 -28
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloud.md +10 -0
- package/.docs/organized/changelogs/%40mastra%2Fdeployer.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Ffirecrawl.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fgithub.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Flibsql.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fmcp-docs-server.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Fmcp.md +15 -15
- package/.docs/organized/changelogs/%40mastra%2Fmem0.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fmemory.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Fplayground-ui.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Fragie.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fschema-compat.md +6 -0
- package/.docs/organized/changelogs/%40mastra%2Fserver.md +11 -11
- package/.docs/organized/changelogs/create-mastra.md +7 -7
- package/.docs/organized/changelogs/mastra.md +14 -14
- package/.docs/organized/code-examples/assistant-ui.md +1 -1
- package/.docs/organized/code-examples/bird-checker-with-nextjs-and-eval.md +2 -2
- package/.docs/raw/getting-started/installation.mdx +2 -7
- package/.docs/raw/getting-started/templates.mdx +2 -7
- package/.docs/raw/memory/working-memory.mdx +17 -17
- package/.docs/raw/observability/ai-tracing.mdx +438 -0
- package/.docs/raw/reference/scorers/noise-sensitivity.mdx +87 -20
- package/.docs/raw/reference/tools/mcp-server.mdx +26 -27
- package/.docs/raw/tools-mcp/mcp-overview.mdx +6 -5
- package/CHANGELOG.md +10 -0
- package/dist/logger.d.ts +5 -1
- package/dist/logger.d.ts.map +1 -1
- package/dist/stdio.js +34 -4
- package/package.json +5 -5
|
@@ -21,8 +21,8 @@
|
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"@types/node": "^20.17.57",
|
|
24
|
-
"@types/react": "^
|
|
25
|
-
"@types/react-dom": "^
|
|
24
|
+
"@types/react": "^19.1.12",
|
|
25
|
+
"@types/react-dom": "^19.1.9",
|
|
26
26
|
"eslint": "^9.30.1",
|
|
27
27
|
"eslint-config-next": "15.3.5",
|
|
28
28
|
"postcss": "^8.5.3",
|
|
@@ -39,7 +39,7 @@ This helps users choose their preferred package manager while following the same
|
|
|
39
39
|
All commands achieve the same result - creating a new Mastra project with the interactive setup.
|
|
40
40
|
*/}
|
|
41
41
|
|
|
42
|
-
<Tabs items={["
|
|
42
|
+
<Tabs items={["npm", "yarn", "pnpm", "bun"]}>
|
|
43
43
|
<Tab>
|
|
44
44
|
```bash copy
|
|
45
45
|
npx create-mastra@latest
|
|
@@ -47,12 +47,7 @@ All commands achieve the same result - creating a new Mastra project with the in
|
|
|
47
47
|
</Tab>
|
|
48
48
|
<Tab>
|
|
49
49
|
```bash copy
|
|
50
|
-
|
|
51
|
-
```
|
|
52
|
-
</Tab>
|
|
53
|
-
<Tab>
|
|
54
|
-
```bash copy
|
|
55
|
-
yarn create mastra@latest
|
|
50
|
+
yarn dlx create-mastra@latest
|
|
56
51
|
```
|
|
57
52
|
</Tab>
|
|
58
53
|
<Tab>
|
|
@@ -14,7 +14,7 @@ Templates are pre-built Mastra projects that demonstrate specific use cases and
|
|
|
14
14
|
|
|
15
15
|
Install a template using the `create-mastra` command:
|
|
16
16
|
|
|
17
|
-
<Tabs items={["
|
|
17
|
+
<Tabs items={["npm", "yarn", "pnpm", "bun"]}>
|
|
18
18
|
<Tab>
|
|
19
19
|
```bash copy
|
|
20
20
|
npx create-mastra@latest --template template-name
|
|
@@ -22,12 +22,7 @@ Install a template using the `create-mastra` command:
|
|
|
22
22
|
</Tab>
|
|
23
23
|
<Tab>
|
|
24
24
|
```bash copy
|
|
25
|
-
|
|
26
|
-
```
|
|
27
|
-
</Tab>
|
|
28
|
-
<Tab>
|
|
29
|
-
```bash copy
|
|
30
|
-
yarn create mastra@latest --template template-name
|
|
25
|
+
yarn dlx create-mastra@latest --template template-name
|
|
31
26
|
```
|
|
32
27
|
</Tab>
|
|
33
28
|
<Tab>
|
|
@@ -61,9 +61,9 @@ const memory = new Memory({
|
|
|
61
61
|
enabled: true,
|
|
62
62
|
scope: 'thread', // Default - memory is isolated per thread
|
|
63
63
|
template: `# User Profile
|
|
64
|
-
- **Name**:
|
|
65
|
-
- **Interests**:
|
|
66
|
-
- **Current Goal**:
|
|
64
|
+
- **Name**:
|
|
65
|
+
- **Interests**:
|
|
66
|
+
- **Current Goal**:
|
|
67
67
|
`,
|
|
68
68
|
},
|
|
69
69
|
},
|
|
@@ -87,11 +87,11 @@ const memory = new Memory({
|
|
|
87
87
|
enabled: true,
|
|
88
88
|
scope: 'resource', // Memory persists across all user threads
|
|
89
89
|
template: `# User Profile
|
|
90
|
-
- **Name**:
|
|
91
|
-
- **Location**:
|
|
92
|
-
- **Interests**:
|
|
93
|
-
- **Preferences**:
|
|
94
|
-
- **Long-term Goals**:
|
|
90
|
+
- **Name**:
|
|
91
|
+
- **Location**:
|
|
92
|
+
- **Interests**:
|
|
93
|
+
- **Preferences**:
|
|
94
|
+
- **Long-term Goals**:
|
|
95
95
|
`,
|
|
96
96
|
},
|
|
97
97
|
},
|
|
@@ -137,23 +137,23 @@ const memory = new Memory({
|
|
|
137
137
|
enabled: true,
|
|
138
138
|
template: `
|
|
139
139
|
# User Profile
|
|
140
|
-
|
|
140
|
+
|
|
141
141
|
## Personal Info
|
|
142
|
-
|
|
142
|
+
|
|
143
143
|
- Name:
|
|
144
144
|
- Location:
|
|
145
145
|
- Timezone:
|
|
146
|
-
|
|
146
|
+
|
|
147
147
|
## Preferences
|
|
148
|
-
|
|
148
|
+
|
|
149
149
|
- Communication Style: [e.g., Formal, Casual]
|
|
150
150
|
- Project Goal:
|
|
151
151
|
- Key Deadlines:
|
|
152
152
|
- [Deadline 1]: [Date]
|
|
153
153
|
- [Deadline 2]: [Date]
|
|
154
|
-
|
|
154
|
+
|
|
155
155
|
## Session State
|
|
156
|
-
|
|
156
|
+
|
|
157
157
|
- Last Task Discussed:
|
|
158
158
|
- Open Questions:
|
|
159
159
|
- [Question 1]
|
|
@@ -367,7 +367,7 @@ await memory.updateWorkingMemory({
|
|
|
367
367
|
|
|
368
368
|
## Examples
|
|
369
369
|
|
|
370
|
-
- [
|
|
371
|
-
- [
|
|
372
|
-
- [
|
|
370
|
+
- [Basic working memory](/examples/memory/working-memory-basic)
|
|
371
|
+
- [Working memory with template](/examples/memory/working-memory-template)
|
|
372
|
+
- [Working memory with schema](/examples/memory/working-memory-schema)
|
|
373
373
|
- [Per-resource working memory](https://github.com/mastra-ai/mastra/tree/main/examples/memory-per-resource-example) - Complete example showing resource-scoped memory persistence
|
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "AI Tracing | Mastra Observability Documentation"
|
|
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
|
+
You create AI traces by:
|
|
15
|
+
|
|
16
|
+
- **Configuring exporters** to send trace data to observability platforms like Langfuse
|
|
17
|
+
- **Setting sampling strategies** to control which traces are collected
|
|
18
|
+
- **Running agents and workflows** — Mastra automatically instruments them with detailed AI tracing
|
|
19
|
+
|
|
20
|
+
This provides full visibility into your AI operations with minimal setup, helping you build more reliable and observable AI applications.
|
|
21
|
+
|
|
22
|
+
<Callout type="warning">
|
|
23
|
+
**Experimental Feature**
|
|
24
|
+
|
|
25
|
+
AI Tracing is available as of `@mastra/core 0.14.0` and is currently experimental. The API may change in future releases.
|
|
26
|
+
</Callout>
|
|
27
|
+
|
|
28
|
+
## How It Differs from Standard Tracing
|
|
29
|
+
|
|
30
|
+
AI Tracing complements Mastra's existing [OpenTelemetry-based tracing](./tracing.mdx) but serves a different purpose:
|
|
31
|
+
|
|
32
|
+
| Feature | Standard Tracing | AI Tracing |
|
|
33
|
+
|---------|-----------------|------------|
|
|
34
|
+
| **Focus** | Application infrastructure | AI operations only |
|
|
35
|
+
| **Data Format** | OpenTelemetry standard | Provider-native (Langfuse, etc.) |
|
|
36
|
+
| **Timing** | Batch export | Real-time option for debugging |
|
|
37
|
+
| **Metadata** | Generic span attributes | AI-specific (tokens, models, tools) |
|
|
38
|
+
|
|
39
|
+
## Current Status
|
|
40
|
+
|
|
41
|
+
**Supported Exporters:**
|
|
42
|
+
- ✅ [Langfuse](https://langfuse.com/) - Full support with real-time mode
|
|
43
|
+
- 🔄 [Braintrust](https://www.braintrust.dev/home) - Coming soon
|
|
44
|
+
- 🔄 [OpenTelemetry](https://opentelemetry.io/) - Coming soon
|
|
45
|
+
|
|
46
|
+
**Known Limitations:**
|
|
47
|
+
- Mastra playground traces still use the legacy tracing system
|
|
48
|
+
- API is experimental and may change
|
|
49
|
+
|
|
50
|
+
For the latest updates, see [GitHub issue #6773](https://github.com/mastra-ai/mastra/issues/6773).
|
|
51
|
+
|
|
52
|
+
## Basic Configuration
|
|
53
|
+
|
|
54
|
+
Here's a simple example of enabling AI Tracing:
|
|
55
|
+
|
|
56
|
+
```ts filename="src/mastra/index.ts" showLineNumbers copy
|
|
57
|
+
import { LangfuseExporter } from '@mastra/langfuse';
|
|
58
|
+
|
|
59
|
+
export const mastra = new Mastra({
|
|
60
|
+
// ... other config
|
|
61
|
+
observability: {
|
|
62
|
+
instances: {
|
|
63
|
+
langfuse: {
|
|
64
|
+
serviceName: 'my-service',
|
|
65
|
+
exporters: [
|
|
66
|
+
new LangfuseExporter({
|
|
67
|
+
publicKey: process.env.LANGFUSE_PUBLIC_KEY!,
|
|
68
|
+
secretKey: process.env.LANGFUSE_SECRET_KEY!,
|
|
69
|
+
baseUrl: process.env.LANGFUSE_BASE_URL!,
|
|
70
|
+
realtime: true,
|
|
71
|
+
}),
|
|
72
|
+
],
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Configuration Options
|
|
80
|
+
|
|
81
|
+
The AI tracing config accepts these properties:
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
type AITracingConfig = {
|
|
85
|
+
// Map of tracing instance names to their configurations
|
|
86
|
+
instances: Record<string, AITracingInstanceConfig | MastraAITracing>;
|
|
87
|
+
|
|
88
|
+
// Optional function to select which tracing instance to use
|
|
89
|
+
selector?: TracingSelector;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
type AITracingInstanceConfig = {
|
|
93
|
+
// Name to identify your service in traces
|
|
94
|
+
serviceName: string;
|
|
95
|
+
|
|
96
|
+
// Control how many traces are sampled
|
|
97
|
+
sampling?: {
|
|
98
|
+
type: "always" | "never" | "ratio" | "custom";
|
|
99
|
+
probability?: number; // For ratio sampling (0.0 to 1.0)
|
|
100
|
+
sampler?: (context: TraceContext) => boolean; // For custom sampling
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
// Array of exporters to send trace data to
|
|
104
|
+
exporters?: AITracingExporter[];
|
|
105
|
+
|
|
106
|
+
// Array of processors to transform spans before export
|
|
107
|
+
processors?: AISpanProcessor[];
|
|
108
|
+
};
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Sampling Configuration
|
|
112
|
+
|
|
113
|
+
Control which traces are collected and exported:
|
|
114
|
+
|
|
115
|
+
```ts filename="src/mastra/index.ts" showLineNumbers copy
|
|
116
|
+
export const mastra = new Mastra({
|
|
117
|
+
observability: {
|
|
118
|
+
instances: {
|
|
119
|
+
langfuse: {
|
|
120
|
+
serviceName: 'my-service',
|
|
121
|
+
// Sample all traces (default)
|
|
122
|
+
sampling: { type: 'always' },
|
|
123
|
+
exporters: [langfuseExporter],
|
|
124
|
+
},
|
|
125
|
+
|
|
126
|
+
development: {
|
|
127
|
+
serviceName: 'dev-service',
|
|
128
|
+
// Sample 10% of traces
|
|
129
|
+
sampling: {
|
|
130
|
+
type: 'ratio',
|
|
131
|
+
probability: 0.1
|
|
132
|
+
},
|
|
133
|
+
exporters: [langfuseExporter],
|
|
134
|
+
},
|
|
135
|
+
|
|
136
|
+
custom: {
|
|
137
|
+
serviceName: 'custom-service',
|
|
138
|
+
// Custom sampling logic
|
|
139
|
+
sampling: {
|
|
140
|
+
type: 'custom',
|
|
141
|
+
sampler: (context) => {
|
|
142
|
+
// Only trace requests from specific users
|
|
143
|
+
return context.metadata?.userId === 'debug-user';
|
|
144
|
+
}
|
|
145
|
+
},
|
|
146
|
+
exporters: [langfuseExporter],
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
});
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Langfuse Exporter Configuration
|
|
154
|
+
|
|
155
|
+
The Langfuse exporter accepts these options:
|
|
156
|
+
|
|
157
|
+
```ts
|
|
158
|
+
type LangfuseExporterConfig = {
|
|
159
|
+
// Langfuse API credentials
|
|
160
|
+
publicKey: string;
|
|
161
|
+
secretKey: string;
|
|
162
|
+
baseUrl: string;
|
|
163
|
+
|
|
164
|
+
// Enable realtime mode for immediate trace visibility
|
|
165
|
+
realtime?: boolean; // defaults to false
|
|
166
|
+
|
|
167
|
+
// Additional options passed to Langfuse client
|
|
168
|
+
options?: any;
|
|
169
|
+
};
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Example with environment variables:
|
|
173
|
+
|
|
174
|
+
```ts filename="mastra.config.ts" showLineNumbers copy
|
|
175
|
+
import { LangfuseExporter } from '@mastra/langfuse';
|
|
176
|
+
|
|
177
|
+
export const mastra = new Mastra({
|
|
178
|
+
observability: {
|
|
179
|
+
instances: {
|
|
180
|
+
langfuse: {
|
|
181
|
+
serviceName: process.env.SERVICE_NAME || 'mastra-app',
|
|
182
|
+
sampling: { type: 'always' },
|
|
183
|
+
exporters: [
|
|
184
|
+
new LangfuseExporter({
|
|
185
|
+
publicKey: process.env.LANGFUSE_PUBLIC_KEY!,
|
|
186
|
+
secretKey: process.env.LANGFUSE_SECRET_KEY!,
|
|
187
|
+
baseUrl: process.env.LANGFUSE_BASE_URL!,
|
|
188
|
+
realtime: process.env.NODE_ENV === 'development',
|
|
189
|
+
}),
|
|
190
|
+
],
|
|
191
|
+
},
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
});
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
#### Real-time vs Batch Mode
|
|
198
|
+
|
|
199
|
+
The Langfuse exporter supports two modes:
|
|
200
|
+
|
|
201
|
+
**Batch Mode (default)**
|
|
202
|
+
- Traces are buffered and sent periodically
|
|
203
|
+
- Better performance for production
|
|
204
|
+
- Traces may appear with slight delay
|
|
205
|
+
|
|
206
|
+
**Real-time Mode**
|
|
207
|
+
- Each trace event is immediately flushed
|
|
208
|
+
- Ideal for development and debugging
|
|
209
|
+
- Immediate visibility in Langfuse dashboard
|
|
210
|
+
|
|
211
|
+
```ts
|
|
212
|
+
new LangfuseExporter({
|
|
213
|
+
// ... other config
|
|
214
|
+
realtime: process.env.NODE_ENV === 'development',
|
|
215
|
+
})
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
#### Multi-Instance Configuration
|
|
219
|
+
|
|
220
|
+
You can configure multiple tracing instances and use a selector to choose which one to use:
|
|
221
|
+
|
|
222
|
+
```ts filename="mastra.config.ts" showLineNumbers copy
|
|
223
|
+
export const mastra = new Mastra({
|
|
224
|
+
observability: {
|
|
225
|
+
instances: {
|
|
226
|
+
production: {
|
|
227
|
+
serviceName: 'prod-service',
|
|
228
|
+
sampling: { type: 'ratio', probability: 0.1 },
|
|
229
|
+
exporters: [prodLangfuseExporter],
|
|
230
|
+
},
|
|
231
|
+
development: {
|
|
232
|
+
serviceName: 'dev-service',
|
|
233
|
+
sampling: { type: 'always' },
|
|
234
|
+
exporters: [devLangfuseExporter],
|
|
235
|
+
},
|
|
236
|
+
},
|
|
237
|
+
selector: (context, availableTracers) => {
|
|
238
|
+
// Use development tracer for debug sessions
|
|
239
|
+
if (context.runtimeContext?.get('debug') === 'true') {
|
|
240
|
+
return 'development';
|
|
241
|
+
}
|
|
242
|
+
return 'production';
|
|
243
|
+
},
|
|
244
|
+
},
|
|
245
|
+
});
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
## Span Types and Attributes
|
|
249
|
+
|
|
250
|
+
AI Tracing automatically creates spans for different AI operations. Mastra supports the following span types:
|
|
251
|
+
|
|
252
|
+
### Agent Operation Types
|
|
253
|
+
- **`AGENT_RUN`** - Agent execution from start to finish
|
|
254
|
+
- **`LLM_GENERATION`** - Individual model calls with prompts and completions
|
|
255
|
+
- **`TOOL_CALL`** - Function/tool executions with inputs and outputs
|
|
256
|
+
- **`MCP_TOOL_CALL`** - Model Context Protocol tool executions
|
|
257
|
+
- **`GENERIC`** - Custom operations
|
|
258
|
+
|
|
259
|
+
### Workflow Operation Types
|
|
260
|
+
- **`WORKFLOW_RUN`** - Workflow execution from start to finish
|
|
261
|
+
- **`WORKFLOW_STEP`** - Individual step processing
|
|
262
|
+
- **`WORKFLOW_CONDITIONAL`** - Conditional execution blocks
|
|
263
|
+
- **`WORKFLOW_CONDITIONAL_EVAL`** - Individual condition evaluations
|
|
264
|
+
- **`WORKFLOW_PARALLEL`** - Parallel execution blocks
|
|
265
|
+
- **`WORKFLOW_LOOP`** - Loop execution blocks
|
|
266
|
+
- **`WORKFLOW_SLEEP`** - Sleep/delay operations
|
|
267
|
+
- **`WORKFLOW_WAIT_EVENT`** - Event waiting operations
|
|
268
|
+
|
|
269
|
+
### Key Attributes
|
|
270
|
+
Each span type includes relevant attributes:
|
|
271
|
+
- **Agent spans**: Agent ID, instructions, available tools, max steps
|
|
272
|
+
- **LLM spans**: Model name, provider, token usage, parameters, finish reason
|
|
273
|
+
- **Tool spans**: Tool ID, tool type, success status
|
|
274
|
+
- **Workflow spans**: Step/workflow IDs, status information
|
|
275
|
+
|
|
276
|
+
## Adding Custom Metadata to Spans
|
|
277
|
+
|
|
278
|
+
You can add custom metadata to spans using the `tracingContext.currentSpan` available in workflow steps and tool calls. This is useful for tracking additional context like API status codes, user IDs, or performance metrics.
|
|
279
|
+
|
|
280
|
+
```ts showLineNumbers copy
|
|
281
|
+
execute: async ({ inputData, tracingContext }) => {
|
|
282
|
+
const response = await fetch(inputData.endpoint, {
|
|
283
|
+
method: 'POST',
|
|
284
|
+
body: JSON.stringify(inputData.payload),
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
// Add custom metadata to the current span
|
|
288
|
+
tracingContext.currentSpan?.update({
|
|
289
|
+
metadata: {
|
|
290
|
+
apiStatusCode: response.status,
|
|
291
|
+
responseHeaders: Object.fromEntries(response.headers.entries()),
|
|
292
|
+
endpoint: inputData.endpoint,
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
const data = await response.json();
|
|
297
|
+
return { data, statusCode: response.status };
|
|
298
|
+
}
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
## Creating Child Spans
|
|
302
|
+
|
|
303
|
+
You can create child spans to track specific operations within your workflow steps or tools. This provides more granular visibility into what's happening during execution.
|
|
304
|
+
|
|
305
|
+
```ts showLineNumbers copy
|
|
306
|
+
execute: async ({ input, tracingContext }) => {
|
|
307
|
+
// Create a child span for the database query
|
|
308
|
+
const querySpan = tracingContext.currentSpan?.createChildSpan({
|
|
309
|
+
type: 'generic',
|
|
310
|
+
name: 'database-query',
|
|
311
|
+
input: {
|
|
312
|
+
query: input.query,
|
|
313
|
+
params: input.params,
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
try {
|
|
318
|
+
const results = await db.query(input.query, input.params);
|
|
319
|
+
|
|
320
|
+
// Update child span with results and end it
|
|
321
|
+
querySpan?.end({
|
|
322
|
+
output: results.data,
|
|
323
|
+
metadata: {
|
|
324
|
+
rowsReturned: results.length,
|
|
325
|
+
success: true,
|
|
326
|
+
}
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
return { results, rowCount: results.length };
|
|
330
|
+
} catch (error) {
|
|
331
|
+
// Record error on child span
|
|
332
|
+
querySpan?.error({error});
|
|
333
|
+
throw error;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
## Span Processors and Data Filtering
|
|
339
|
+
|
|
340
|
+
Span processors allow you to modify or filter span data before it's exported to observability platforms. This is useful for adding computed fields, redacting sensitive information, or transforming data formats.
|
|
341
|
+
|
|
342
|
+
### Built-in SensitiveDataFilter
|
|
343
|
+
|
|
344
|
+
Mastra includes a `SensitiveDataFilter` processor that automatically redacts sensitive fields from span data. It's enabled by default and scans for common sensitive field names:
|
|
345
|
+
|
|
346
|
+
```ts filename="src/mastra/index.ts" showLineNumbers copy
|
|
347
|
+
import { LangfuseExporter } from '@mastra/langfuse';
|
|
348
|
+
import { SensitiveDataFilter } from '@mastra/core/ai-tracing';
|
|
349
|
+
|
|
350
|
+
export const mastra = new Mastra({
|
|
351
|
+
observability: {
|
|
352
|
+
instances: {
|
|
353
|
+
langfuse: {
|
|
354
|
+
serviceName: 'my-service',
|
|
355
|
+
exporters: [new LangfuseExporter({ /* config */ })],
|
|
356
|
+
// SensitiveDataFilter is included by default, but you can customize it
|
|
357
|
+
processors: [
|
|
358
|
+
new SensitiveDataFilter([
|
|
359
|
+
'password', 'token', 'secret', 'key', 'apiKey',
|
|
360
|
+
'auth', 'authorization', 'bearer', 'jwt',
|
|
361
|
+
'credential', 'sessionId',
|
|
362
|
+
// Add your custom sensitive fields
|
|
363
|
+
'ssn', 'creditCard', 'bankAccount'
|
|
364
|
+
])
|
|
365
|
+
],
|
|
366
|
+
},
|
|
367
|
+
},
|
|
368
|
+
},
|
|
369
|
+
});
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
The `SensitiveDataFilter` automatically redacts matching fields in:
|
|
373
|
+
- Span attributes
|
|
374
|
+
- Span metadata
|
|
375
|
+
- Input/output data
|
|
376
|
+
- Error information
|
|
377
|
+
|
|
378
|
+
Fields are matched case-insensitively, and nested objects are processed recursively.
|
|
379
|
+
|
|
380
|
+
### Custom Processors
|
|
381
|
+
|
|
382
|
+
You can create custom processors to implement your own span transformation logic:
|
|
383
|
+
|
|
384
|
+
```ts showLineNumbers copy
|
|
385
|
+
import type { AISpanProcessor, AnyAISpan } from '@mastra/core/ai-tracing';
|
|
386
|
+
|
|
387
|
+
export class PerformanceEnrichmentProcessor implements AISpanProcessor {
|
|
388
|
+
name = 'performance-enrichment';
|
|
389
|
+
|
|
390
|
+
process(span: AnyAISpan): AnyAISpan | null {
|
|
391
|
+
const modifiedSpan = { ...span };
|
|
392
|
+
|
|
393
|
+
// Add computed performance metrics
|
|
394
|
+
if (span.startTime && span.endTime) {
|
|
395
|
+
const duration = span.endTime.getTime() - span.startTime.getTime();
|
|
396
|
+
|
|
397
|
+
modifiedSpan.metadata = {
|
|
398
|
+
...span.metadata,
|
|
399
|
+
durationMs: duration,
|
|
400
|
+
performanceCategory: duration < 100 ? 'fast' : duration < 1000 ? 'medium' : 'slow',
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// Add environment context
|
|
405
|
+
modifiedSpan.metadata = {
|
|
406
|
+
...modifiedSpan.metadata,
|
|
407
|
+
environment: process.env.NODE_ENV || 'unknown',
|
|
408
|
+
region: process.env.AWS_REGION || 'unknown',
|
|
409
|
+
};
|
|
410
|
+
|
|
411
|
+
return modifiedSpan;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
async shutdown(): Promise<void> {
|
|
415
|
+
// Cleanup if needed
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
// Use in your Mastra configuration
|
|
420
|
+
export const mastra = new Mastra({
|
|
421
|
+
observability: {
|
|
422
|
+
instances: {
|
|
423
|
+
langfuse: {
|
|
424
|
+
serviceName: 'my-service',
|
|
425
|
+
exporters: [new LangfuseExporter({ /* config */ })],
|
|
426
|
+
processors: [
|
|
427
|
+
new SensitiveDataFilter(),
|
|
428
|
+
new PerformanceEnrichmentProcessor(),
|
|
429
|
+
],
|
|
430
|
+
},
|
|
431
|
+
},
|
|
432
|
+
},
|
|
433
|
+
});
|
|
434
|
+
```
|
|
435
|
+
|
|
436
|
+
Processors are executed in the order they're defined, and each processor receives the output of the previous one.
|
|
437
|
+
|
|
438
|
+
|