> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-TW/llms.txt # Span ## `BaseSpan` 所有 span 類型的基礎介面。 ```typescript interface BaseSpan { /** Unique span identifier */ id: string /** OpenTelemetry-compatible trace ID (32 hex chars) */ traceId: string /** Name of the span */ name: string /** Type of the span */ type: TType /** When span started */ startTime: Date /** When span ended */ endTime?: Date /** Type-specific attributes */ attributes?: SpanTypeMap[TType] /** User-defined metadata */ metadata?: Record /** Input passed at the start of the span */ input?: any /** Output generated at the end of the span */ output?: any /** Error information if span failed */ errorInfo?: { message: string id?: string domain?: string category?: string details?: Record } /** Snapshot of the RequestContext */ requestContext?: Record /** Is an event span? (occurs at startTime, has no endTime) */ isEvent: boolean } ``` ## Span 內部 tracing 使用的 Span 介面。擴充 BaseSpan,加入生命週期方法與屬性。 ```typescript interface Span extends BaseSpan { /** Is an internal span? (spans internal to the operation of mastra) */ isInternal: boolean /** Parent span reference (undefined for root spans) */ parent?: AnySpan /** Pointer to the ObservabilityInstance instance */ observabilityInstance: ObservabilityInstance } ``` ### 屬性 ```typescript /** Returns TRUE if the span is the root span of a trace */ get isRootSpan(): boolean /** Returns TRUE if the span is a valid span (not a NO-OP Span) */ get isValid(): boolean /** Get the closest parent spanId that isn't an internal span */ getParentSpanId(includeInternalSpans?: boolean): string | undefined /** Returns a lightweight span ready for export */ exportSpan(includeInternalSpans?: boolean): ExportedSpan | undefined ``` ### 方法 #### end ```typescript end(options?: EndSpanOptions): void ``` 結束 span 並觸發匯出至已設定的 exporter。設定 `endTime`,並可選擇更新 `output`、`metadata` 與 `attributes`。 #### error ```typescript error(options: ErrorSpanOptions): void ``` 在 span 上記錄錯誤。設定 `errorInfo` 欄位,並可選擇結束 span。 #### update ```typescript update(options: UpdateSpanOptions): void ``` 在 span 仍處於作用中時更新其資料。可修改 `input`、`output`、`metadata` 與 `attributes`。 #### `createChildSpan` ```typescript createChildSpan( options: ChildSpanOptions ): Span ``` 在此 span 下建立子 span。子 span 會追蹤子操作並繼承 trace context。 #### `createEventSpan` ```typescript createEventSpan( options: ChildEventOptions ): Span ``` 在此 span 下建立事件 span。事件 span 代表沒有持續時間的時間點事件。 ## `ExportedSpan` 供 tracing exporter 使用的已匯出 Span 介面。這是沒有方法或循環參照的輕量版 Span。 ```typescript interface ExportedSpan extends BaseSpan { /** Parent span id reference (undefined for root spans) */ parentSpanId?: string /** TRUE if the span is the root span of a trace */ isRootSpan: boolean } ``` ## Span 生命週期事件 Span 生命週期期間產生的事件。 ### `TracingEventType` ```typescript enum TracingEventType { /** Emitted when a span is created and started */ SPAN_STARTED = 'span_started', /** Emitted when a span is updated via update() */ SPAN_UPDATED = 'span_updated', /** Emitted when a span is ended via end() or error() */ SPAN_ENDED = 'span_ended', } ``` ### `TracingEvent` ```typescript type TracingEvent = | { type: 'span_started'; exportedSpan: AnyExportedSpan } | { type: 'span_updated'; exportedSpan: AnyExportedSpan } | { type: 'span_ended'; exportedSpan: AnyExportedSpan } ``` Exporter 會接收這些事件,以處理 trace 資料並傳送至可觀測性平台。 ## Union 型別 ### `AnySpan` ```typescript type AnySpan = Span ``` 需要處理任意 span 類型時使用的 union 型別。 ### `AnyExportedSpan` ```typescript type AnyExportedSpan = ExportedSpan ``` 需要處理任意已匯出 span 類型時使用的 union 型別。 ## NO-OP span 停用 tracing(取樣傳回 false)時,會傳回 NO-OP span: ### `NoOpSpan` ```typescript class NoOpSpan extends BaseSpan ``` 不執行任何操作的 span。所有方法都是 no-op: - `id` 傳回 `'no-op'` - `traceId` 傳回 `'no-op-trace'` - `isValid` 傳回 `false` - `end()`、`error()`、`update()` 不執行任何操作 - `createChildSpan()` 傳回另一個 NO-OP span ## 另請參閱 ### 文件 - [Tracing 概覽](https://mastra.zisheng.pro/zh-TW/docs/observability/tracing/overview):概念與使用方式 - [建立子 Span](https://mastra.zisheng.pro/zh-TW/docs/observability/tracing/overview):實用範例 - [擷取 Trace ID](https://mastra.zisheng.pro/zh-TW/docs/observability/tracing/overview):使用 trace ID ### 參考 - [Tracing 類別](https://mastra.zisheng.pro/zh-TW/reference/observability/tracing/instances):核心 tracing 類別 - [介面](https://mastra.zisheng.pro/zh-TW/reference/observability/tracing/interfaces):完整型別參考 - [設定](https://mastra.zisheng.pro/zh-TW/reference/observability/tracing/configuration):設定選項