> Discover all available pages from the documentation index: https://mastra.zisheng.pro/ja/llms.txt # PostHog exporter [PostHog](https://posthog.com/) は、LLM アプリケーションを監視するための AI Observability 機能を備えた分析プラットフォームです。PostHog exporter は Trace を構造化イベントとして PostHog へ送信し、token 使用量、コスト、レイテンシ、会話フローに関するインサイトを提供します。 ## インストール **npm**: ```bash npm install @mastra/posthog@latest ``` **pnpm**: ```bash pnpm add @mastra/posthog@latest ``` **Yarn**: ```bash yarn add @mastra/posthog@latest ``` **Bun**: ```bash bun add @mastra/posthog@latest ``` ## 設定 ### 前提条件 1. **PostHog アカウント**:[posthog.com](https://posthog.com/) で登録します 2. **プロジェクト API キー**:PostHog の Settings → Project API Key からプロジェクト API キーを取得します 3. **環境変数**:認証情報を設定します ```bash # Required POSTHOG_API_KEY=phc_xxxxxxxxxxxxxxxx # Optional POSTHOG_HOST=https://us.i.posthog.com # or eu.i.posthog.com for EU region ``` ### ゼロコンフィグ設定 環境変数を設定したら、設定なしで exporter を使用できます。 ```typescript import { Mastra } from '@mastra/core' import { Observability } from '@mastra/observability' import { PosthogExporter } from '@mastra/posthog' export const mastra = new Mastra({ observability: new Observability({ configs: { posthog: { serviceName: 'my-service', exporters: [new PosthogExporter()], }, }, }), }) ``` ### 明示的な設定 認証情報を直接渡すこともできます(環境変数より優先されます)。 ```typescript import { Mastra } from '@mastra/core' import { Observability } from '@mastra/observability' import { PosthogExporter } from '@mastra/posthog' export const mastra = new Mastra({ observability: new Observability({ configs: { posthog: { serviceName: 'my-service', exporters: [ new PosthogExporter({ apiKey: process.env.POSTHOG_API_KEY, }), ], }, }, }), }) ``` ## 設定オプション ### 完全な設定 ```typescript new PosthogExporter({ // Required credentials apiKey: process.env.POSTHOG_API_KEY!, // Optional settings host: 'https://us.i.posthog.com', // Default: US region // or "https://eu.i.posthog.com" for EU region // or your self-hosted URL // Batching configuration flushAt: 20, // Batch size (default: 20) flushInterval: 10000, // Flush interval in ms (default: 10000) serverless: false, // Serverless mode: flushAt=10, flushInterval=2000 // User identification defaultDistinctId: 'anonymous', // Fallback if no userId in metadata // Privacy settings enablePrivacyMode: false, // Excludes input/output from generation events // Diagnostic logging logLevel: 'info', // debug | info | warn | error }) ``` ### サーバーレスモード サーバーレス環境向けに最適化されたバッチ処理です。 ```typescript new PosthogExporter({ apiKey: process.env.POSTHOG_API_KEY!, serverless: true, // Configures smaller batches for faster flushing }) ``` ### プライバシーモード token メトリクスを維持したまま、generation イベントから input/output データを除外します。 ```typescript new PosthogExporter({ apiKey: process.env.POSTHOG_API_KEY!, enablePrivacyMode: true, // Removes $ai_input and $ai_output_choices }) ``` ### グループ分析 イベントを [PostHog グループ](https://posthog.com/docs/product-analytics/group-analytics)に関連付けるには、`$groups` オブジェクトを `tracingOptions.metadata` に追加します。exporter はこれらの値を PostHog の capture 呼び出しのトップレベル `groups` フィールドとして送信するため、グループ別の LLM コストなどの分析を切り分けられます。 ```ts await agent.generate(input, { tracingOptions: { metadata: { $groups: { publication: 'publication-1', }, }, }, }) ``` ## 関連情報 - [Tracing の概要](https://mastra.zisheng.pro/ja/docs/observability/tracing/overview) - [PostHog ドキュメント](https://posthog.com/docs)