> Discover all available pages from the documentation index: https://mastra.zisheng.pro/llms.txt # LangSmithExporter 将 Tracing 数据发送到 LangSmith,用于可观测性。 ## 构造函数 ```typescript new LangSmithExporter(config: LangSmithExporterConfig) ``` ## `LangSmithExporterConfig` ```typescript interface LangSmithExporterConfig extends ClientConfig, BaseExporterConfig { client?: Client projectName?: string } ``` 同时扩展 `ClientConfig`(来自 LangSmith SDK)和 `BaseExporterConfig`: - 来自 `BaseExporterConfig`:`logger?: IMastraLogger`、`logLevel?: LogLevel | 'debug' | 'info' | 'warn' | 'error'` - 来自 `ClientConfig`:`apiKey`、`apiUrl`、`callerOptions`、`hideInputs`、`hideOutputs` 等 ## 方法 ### `exportTracingEvent` ```typescript async exportTracingEvent(event: TracingEvent): Promise ``` 将 Tracing 事件导出到 LangSmith。 ### flush ```typescript async flush(): Promise ``` 强制将所有待处理的 Span 刷新到 LangSmith,而不关闭 Exporter。适用于需要确保运行时终止前已导出 Span 的 serverless 环境。 ### shutdown ```typescript async shutdown(): Promise ``` 结束所有活跃 Span 并清除 Trace map。 ## 用法 ```typescript import { LangSmithExporter } from '@mastra/langsmith' const exporter = new LangSmithExporter({ apiKey: process.env.LANGSMITH_API_KEY, projectName: 'my-project', // Optional: specify which project to send traces to apiUrl: 'https://api.smith.langchain.com', logLevel: 'info', }) ``` ## 环境变量 | 变量 | 说明 | | -------------------- | ------------------------------------ | | `LANGSMITH_API_KEY` | 你的 LangSmith API key | | `LANGCHAIN_PROJECT` | Trace 的默认项目名称(未指定 `projectName` 时使用) | | `LANGSMITH_BASE_URL` | 自托管实例的 API URL | ## Span 类型映射 | Span 类型 | LangSmith 类型 | | -------------------- | ------------ | | `MODEL_GENERATION` | `llm` | | `MODEL_CHUNK` | `llm` | | `TOOL_CALL` | `tool` | | `MCP_TOOL_CALL` | `tool` | | `PROVIDER_TOOL_CALL` | `tool` | | 所有其他类型 | `chain` | ## `withLangsmithMetadata` 用于向 Tracing 选项添加 LangSmith 专用元数据的 helper 函数。 ```typescript function withLangsmithMetadata(metadata: LangSmithMetadataInput): TracingOptionsUpdater ``` ### `LangSmithMetadataInput` ### 用法 ```typescript import { buildTracingOptions } from '@mastra/observability' import { withLangsmithMetadata } from '@mastra/langsmith' // Route to a specific project const tracingOptions = buildTracingOptions(withLangsmithMetadata({ projectName: 'my-project' })) // Set multiple fields const tracingOptions = buildTracingOptions( withLangsmithMetadata({ projectName: 'my-project', sessionId: 'user-123', }), ) ```