メインコンテンツへ移動

LangfuseExporter

Observability のために Tracing データを Langfuse へ送信します。

コンストラクター
コンストラクターへの直接リンク

new LangfuseExporter(config: LangfuseExporterConfig)

LangfuseExporterConfig
langfuseexporterconfigへの直接リンク

interface LangfuseExporterConfig extends BaseExporterConfig {
publicKey?: string
secretKey?: string
baseUrl?: string
realtime?: boolean
flushAt?: number
flushInterval?: number
environment?: string
release?: string
}

BaseExporterConfig を拡張し、次の項目を含みます。

  • logger?: IMastraLogger - Logger インスタンス
  • logLevel?: LogLevel | 'debug' | 'info' | 'warn' | 'error' - ログレベル(デフォルト:INFO)

プロパティ
プロパティへの直接リンク

client
clientへの直接リンク

get client(): LangfuseClient | undefined

Langfuse の高度な機能で使用する LangfuseClient インスタンスです。exporter が無効な場合(認証情報がない場合)は undefined を返します。プロンプト管理、評価、データセット、API への直接アクセスに使用します。

メソッド
メソッドへの直接リンク

flush
flushへの直接リンク

async flush(): Promise<void>

シャットダウンせず、バッファー内のすべての Span とスコアを Langfuse へ強制的にフラッシュします。ランタイム終了前にデータが確実にエクスポートされるようにする必要がある、サーバーレス環境で役立ちます。

shutdown
shutdownへの直接リンク

async shutdown(): Promise<void>

保留中のデータをフラッシュし、Span processor とクライアントの両方をシャットダウンします。

使用方法
使用方法への直接リンク

ゼロコンフィグ(環境変数を使用)
ゼロコンフィグ(環境変数を使用)への直接リンク

import { LangfuseExporter } from '@mastra/langfuse'

// Reads from LANGFUSE_PUBLIC_KEY, LANGFUSE_SECRET_KEY, LANGFUSE_BASE_URL
const exporter = new LangfuseExporter()

明示的な設定
明示的な設定への直接リンク

import { LangfuseExporter } from '@mastra/langfuse'

const exporter = new LangfuseExporter({
publicKey: process.env.LANGFUSE_PUBLIC_KEY,
secretKey: process.env.LANGFUSE_SECRET_KEY,
baseUrl: 'https://cloud.langfuse.com',
realtime: true,
})

Span のマッピング
Span のマッピングへの直接リンク

Mastra の Span は OTel 形式に変換され、LangfuseSpanProcessor 経由で Langfuse へ送信されます。Langfuse は gen_ai.* 属性に基づいて Span を自動的にマッピングします。

  • model 属性を持つ Span → Langfuse generation
  • その他すべての Span → Langfuse Span
  • 親子関係は OTel の Trace/Span ID によって維持されます

プロンプトのリンク
プロンプトのリンクへの直接リンク

withLangfusePrompt helper を使用し、LLM generation を Langfuse Prompt Management にリンクします。

import { buildTracingOptions } from '@mastra/observability'
import { LangfuseExporter, withLangfusePrompt } from '@mastra/langfuse'

const exporter = new LangfuseExporter()

// Fetch prompt via the Langfuse client
const prompt = await exporter.client.prompt.get('customer-support', { type: 'text' })

const agent = new Agent({
id: 'support-agent',
name: 'support-agent',
instructions: prompt.compile(),
model: 'openai/gpt-5.6-sol',
defaultGenerateOptions: {
tracingOptions: buildTracingOptions(
withLangfusePrompt({ name: prompt.name, version: prompt.version }),
),
},
})

Helper 関数
Helper 関数への直接リンク

withLangfusePrompt(prompt)
withlangfusepromptpromptへの直接リンク

Tracing オプションに Langfuse のプロンプトメタデータを追加します。

// Link by name and version (required for Langfuse v5)
withLangfusePrompt({ name: 'my-prompt', version: 1 })

プロンプトメタデータは Span 属性として渡され、Langfuse は generation を対応するプロンプトにリンクします。