> Discover all available pages from the documentation index: https://mastra.zisheng.pro/llms.txt # PostHog exporter [PostHog](https://posthog.com/) 是一个带有 AI 可观测性功能的分析平台,用于监控 LLM 应用。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 key**:从 PostHog Settings → Project API Key 获取 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 }) ``` ### Serverless 模式 针对 serverless 环境优化批处理: ```typescript new PosthogExporter({ apiKey: process.env.POSTHOG_API_KEY!, serverless: true, // Configures smaller batches for faster flushing }) ``` ### 隐私模式 从生成事件中排除输入/输出数据,同时保留 token 指标: ```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),请在 `tracingOptions.metadata` 中添加 `$groups` 对象。Exporter 会将这些值作为 PostHog capture 调用的顶层 `groups` 字段发送,以便你按群组细分 LLM 成本等分析结果。 ```ts await agent.generate(input, { tracingOptions: { metadata: { $groups: { publication: 'publication-1', }, }, }, }) ``` ## 相关内容 - [Tracing 概览](https://mastra.zisheng.pro/docs/observability/tracing/overview) - [PostHog 文档](https://posthog.com/docs)