Back to Mastra

Reference: Tracing | Observability

docs/src/content/en/reference/observability/tracing/instances.mdx

2025-12-183.6 KB
Original Source

import PropertiesTable from "@site/src/components/PropertiesTable";

Observability Instances

DefaultObservabilityInstance

Default implementation of the ObservabilityInstance interface.

Constructor

typescript
new DefaultObservabilityInstance(config: ObservabilityInstanceConfig)

Creates a new DefaultObservabilityInstance with the specified configuration.

Properties

Inherits all properties and methods from BaseObservabilityInstance.

BaseObservabilityInstance

Base class for custom ObservabilityInstance implementations.

Methods

getConfig

typescript
getConfig(): Readonly<Required<ObservabilityInstanceConfig>>

Returns the current observability configuration.

getExporters

typescript
getExporters(): readonly ObservabilityExporter[]

Returns all configured exporters.

getSpanOutputProcessors

typescript
getSpanOutputProcessors(): readonly SpanOutputProcessor[]

Returns all configured span output processors.

getLogger

typescript
getLogger(): IMastraLogger

Returns the logger instance for exporters and other components.

startSpan

typescript
startSpan<TType extends SpanType>(
  options: StartSpanOptions<TType>
): Span<TType>

Start a new span of a specific SpanType. Creates the root span of a trace if no parent is provided.

<PropertiesTable props={[ { name: 'type', type: 'SpanType', description: 'Type of span to create', required: true, }, { name: 'name', type: 'string', description: 'Name of the span', required: true, }, { name: 'parent', type: 'AnySpan', description: 'Parent span (if not root)', required: false, }, { name: 'attributes', type: 'SpanTypeMap[TType]', description: 'Type-specific attributes', required: false, }, { name: 'metadata', type: 'Record<string, any>', description: 'User-defined metadata', required: false, }, { name: 'input', type: 'any', description: 'Initial input data', required: false, }, { name: 'customSamplerOptions', type: 'CustomSamplerOptions', description: 'Options for custom sampler', required: false, }, ]} />

shutdown

typescript
async shutdown(): Promise<void>

Shuts down all exporters and processors, cleaning up resources.

Custom implementation

To create a custom ObservabilityInstance implementation, extend BaseObservabilityInstance:

typescript
class CustomObservabilityInstance extends BaseObservabilityInstance {
  constructor(config: ObservabilityInstanceConfig) {
    super(config)
    // Custom initialization
  }

  // Override methods as needed
  startSpan<TType extends SpanType>(options: StartSpanOptions<TType>): Span<TType> {
    // Custom span creation logic
    return super.startSpan(options)
  }
}

See also

Documentation

Exporters