PostHog 导出器
🌐 PostHog Exporter
PostHog 是一个具有 AI 可观测性功能的分析平台,用于监控大型语言模型(LLM)应用。PostHog 导出器会将你的追踪数据作为结构化事件发送到 PostHog,从而提供有关令牌使用情况、成本、延迟和对话流程的洞察。
安装Direct link to 安装
🌐 Installation
- npm
- pnpm
- Yarn
- Bun
npm install @mastra/posthog@latest
pnpm add @mastra/posthog@latest
yarn add @mastra/posthog@latest
bun add @mastra/posthog@latest
配置Direct link to 配置
🌐 Configuration
先决条件Direct link to 先决条件
🌐 Prerequisites
- PostHog 账户:在 posthog.com 注册
- 项目 API 密钥:从 PostHog 设置 → 项目 API 密钥 获取你的项目 API 密钥
- 环境变量:设置你的凭据
.env
# Required
POSTHOG_API_KEY=phc_xxxxxxxxxxxxxxxx
# Optional
POSTHOG_HOST=https://us.i.posthog.com # or eu.i.posthog.com for EU region
零配置设置Direct link to 零配置设置
🌐 Zero-Config Setup
设置环境变量后,使用无配置的导出器:
🌐 With environment variables set, use the exporter with no configuration:
src/mastra/index.ts
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()],
},
},
}),
});
显式配置Direct link to 显式配置
🌐 Explicit Configuration
你也可以直接传递凭据(优先于环境变量):
🌐 You can also pass credentials directly (takes precedence over environment variables):
src/mastra/index.ts
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,
}),
],
},
},
}),
});
配置选项Direct link to 配置选项
🌐 Configuration Options
完成配置Direct link to 完成配置
🌐 Complete Configuration
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
});
无服务器模式Direct link to 无服务器模式
🌐 Serverless Mode
针对无服务器环境的优化批处理:
🌐 Optimized batching for serverless environments:
new PosthogExporter({
apiKey: process.env.POSTHOG_API_KEY!,
serverless: true, // Configures smaller batches for faster flushing
});
隐私模式Direct link to 隐私模式
🌐 Privacy Mode
在保留令牌指标的同时,从生成事件中排除输入/输出数据:
🌐 Exclude input/output data from generation events while preserving token metrics:
new PosthogExporter({
apiKey: process.env.POSTHOG_API_KEY!,
enablePrivacyMode: true, // Removes $ai_input and $ai_output_choices
});
相关Direct link to 相关
🌐 Related