Span filtering
Span filtering controls which spans are exported from an observability config. Use it to reduce noise, lower per-span costs, or keep only the spans that matter for a specific exporter or environment.
For a shorter overview of tracing configuration, see Tracing.
Usage exampleDirect link to Usage example
The following example demonstrates how to combine excludeSpanTypes and spanFilter in one observability config:
import { Mastra } from '@mastra/core'
import { SpanType } from '@mastra/core/observability'
import { Observability, DefaultExporter } from '@mastra/observability'
import { LangfuseExporter } from '@mastra/langfuse'
export const mastra = new Mastra({
observability: new Observability({
configs: {
default: {
serviceName: 'my-app',
exporters: [new DefaultExporter(), new LangfuseExporter()],
excludeSpanTypes: [SpanType.MODEL_CHUNK, SpanType.MODEL_STEP],
spanFilter: span => {
if (span.type === SpanType.TOOL_CALL && span.attributes?.success) {
return false
}
return true
},
},
},
}),
})
Configuration optionsDirect link to Configuration options
excludeSpanTypes?:
spanFilter?:
excludeSpanTypesDirect link to excludespantypes
Use excludeSpanTypes when you want to suppress whole categories of spans with minimal configuration.
For the current set of supported values, see the SpanType enum.
import { SpanType } from '@mastra/core/observability'
excludeSpanTypes: [SpanType.MODEL_CHUNK, SpanType.MODEL_STEP, SpanType.WORKFLOW_SLEEP]
spanFilterDirect link to spanfilter
When filtering depends on the exported span contents, use spanFilter.
import { SpanType } from '@mastra/core/observability'
spanFilter: span => {
if (span.type === SpanType.MODEL_CHUNK) return false
if (span.type === SpanType.TOOL_CALL && span.attributes?.success) return false
return true
}
Additional examples:
// Only export spans from production
spanFilter: span => span.metadata?.environment === 'production'
// Exclude spans from a specific entity
spanFilter: span => span.entityId !== 'noisy-agent'
Processing orderDirect link to Processing order
Span filtering happens at export time in this order:
- Internal spans are dropped unless
includeInternalSpansistrue. excludeSpanTypesremoves matching span types.spanOutputProcessorstransform the remaining spans.spanFilterdecides whether to keep the final exported span.
If spanFilter throws an error, Mastra keeps the span and logs the error to avoid silent data loss.
RelatedDirect link to Related
- Tracing overview for setup and concepts
- Configuration API for
ObservabilityInstanceConfig - Sensitive data filter for redacting span data