instrument-repo
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinese/instrument-repo
/instrument-repo
Add production observability to a repository. Detects stack, installs SDKs, writes config, opens PR.
为代码仓库添加生产环境可观测性能力。自动检测技术栈、安装SDK、编写配置文件并创建Pull Request。
Shared Config
通用配置
SENTRY_ORG=misty-step
SENTRY_TEAM=misty-step
POSTHOG_PROJECT_ID=293836
POSTHOG_HOST=https://us.i.posthog.comAll repos share ONE PostHog project (segmented by events). Each repo gets its OWN Sentry project.
SENTRY_ORG=misty-step
SENTRY_TEAM=misty-step
POSTHOG_PROJECT_ID=293836
POSTHOG_HOST=https://us.i.posthog.com所有代码仓库共享同一个PostHog项目(通过事件区分),每个仓库拥有独立的Sentry项目。
Detection
检测逻辑
Before instrumenting, detect what we're working with:
bash
undefined在集成可观测性工具前,先检测当前项目的技术栈:
bash
undefinedLanguage detection
语言检测
[ -f package.json ] && echo "typescript"
[ -f go.mod ] && echo "go"
[ -f pyproject.toml ] || [ -f setup.py ] && echo "python"
[ -f Package.swift ] && echo "swift"
[ -f Cargo.toml ] && echo "rust"
[ -f package.json ] && echo "typescript"
[ -f go.mod ] && echo "go"
[ -f pyproject.toml ] || [ -f setup.py ] && echo "python"
[ -f Package.swift ] && echo "swift"
[ -f Cargo.toml ] && echo "rust"
Framework detection (TypeScript)
框架检测(TypeScript)
grep -q '"next"' package.json 2>/dev/null && echo "nextjs"
grep -q '"hono"' package.json 2>/dev/null && echo "hono"
grep -q '"express"' package.json 2>/dev/null && echo "express"
grep -q '"react-native"' package.json 2>/dev/null && echo "react-native"
grep -q '"next"' package.json 2>/dev/null && echo "nextjs"
grep -q '"hono"' package.json 2>/dev/null && echo "hono"
grep -q '"express"' package.json 2>/dev/null && echo "express"
grep -q '"react-native"' package.json 2>/dev/null && echo "react-native"
LLM usage detection (for Helicone)
LLM使用检测(用于Helicone)
grep -rq '@ai-sdk|openai|anthropic|@google/genai' package.json src/ lib/ app/ 2>/dev/null && echo "has-llm"
grep -rq 'openai|anthropic|langchain' *.go cmd/ internal/ 2>/dev/null && echo "has-llm"
grep -rq 'openai|anthropic|langchain' *.py src/ 2>/dev/null && echo "has-llm"
grep -rq '@ai-sdk|openai|anthropic|@google/genai' package.json src/ lib/ app/ 2>/dev/null && echo "has-llm"
grep -rq 'openai|anthropic|langchain' *.go cmd/ internal/ 2>/dev/null && echo "has-llm"
grep -rq 'openai|anthropic|langchain' *.py src/ 2>/dev/null && echo "has-llm"
Existing instrumentation
已集成的可观测性工具检测
grep -q '@sentry' package.json 2>/dev/null && echo "has-sentry"
grep -q 'posthog' package.json 2>/dev/null && echo "has-posthog"
grep -rq 'helicone' src/ lib/ app/ 2>/dev/null && echo "has-helicone"
undefinedgrep -q '@sentry' package.json 2>/dev/null && echo "has-sentry"
grep -q 'posthog' package.json 2>/dev/null && echo "has-posthog"
grep -rq 'helicone' src/ lib/ app/ 2>/dev/null && echo "has-helicone"
undefinedPer-repo Workflow
单仓库集成流程
1. cd ~/Development/$REPO
2. Detect language, framework, LLM usage, existing instrumentation
3. git fetch origin && git checkout -b infra/observability origin/main (or master)
4. Create Sentry project if needed → mcp__sentry__create_project
5. Install packages (language-specific)
6. Write/update config files from templates
7. Update .env.example
8. Typecheck/build to verify
9. git add <specific files> && git commit
10. git push -u origin infra/observability
11. gh pr create with structured body1. cd ~/Development/$REPO
2. 检测语言、框架、LLM使用情况及已集成的可观测性工具
3. git fetch origin && git checkout -b infra/observability origin/main(或master分支)
4. 按需创建Sentry项目 → 调用mcp__sentry__create_project
5. 安装对应语言的依赖包
6. 从模板生成/更新配置文件
7. 更新.env.example文件
8. 执行类型检查/构建以验证配置
9. git add <指定文件> && git commit
10. git push -u origin infra/observability
11. 使用结构化内容创建GitHub PRWhat to Add (Decision Matrix)
工具集成决策矩阵
| Condition | Sentry | PostHog | Helicone |
|---|---|---|---|
| Any app/service | YES | — | — |
| User-facing web app | YES | YES | — |
| Has LLM SDK imports | YES | maybe | YES |
| CLI tool | YES | NO | maybe |
| GitHub Action / lib | NO | NO | NO |
| 条件 | Sentry | PostHog | Helicone |
|---|---|---|---|
| 任意应用/服务 | 是 | — | — |
| 面向用户的Web应用 | 是 | 是 | — |
| 包含LLM SDK依赖 | 是 | 可选 | 是 |
| CLI工具 | 是 | 否 | 可选 |
| GitHub Action / 类库 | 否 | 否 | 否 |
Sentry Sampling Rates
Sentry采样率配置
Cost-conscious defaults (5k errors/mo free tier):
tracesSampleRate: 0.1 # 10% of transactions
replaysSessionSampleRate: 0 # Don't record sessions by default
replaysOnErrorSampleRate: 1.0 # Always replay on error兼顾成本的默认配置(免费层每月支持5000条错误记录):
tracesSampleRate: 0.1 # 10%的事务会被采样
replaysSessionSampleRate: 0 # 默认不记录会话回放
replaysOnErrorSampleRate: 1.0 # 发生错误时始终记录会话回放Templates
配置模板
Next.js → templates/nextjs/
templates/nextjs/Next.js → templates/nextjs/
templates/nextjs/Files to create:
- — Client-side Sentry init
sentry.client.config.ts - — Server-side Sentry init
sentry.server.config.ts - — Edge runtime Sentry init (same as server)
sentry.edge.config.ts - — Next.js instrumentation hook
instrumentation.ts - — Shared Sentry config factory with PII scrubbing
lib/sentry.ts - — PostHog React provider
components/posthog-provider.tsx - — Route-aware pageview tracking
components/posthog-pageview.tsx - — PostHog client init + helpers
lib/posthog.ts
Install:
pnpm add @sentry/nextjs posthog-js posthog-nodeNext.js config: wrap with in
withSentryConfig()next.config.tsPostHog rewrite in :
/ingestnext.config.tstypescript
async rewrites() {
return [
{ source: "/ingest/static/:path*", destination: "https://us-assets.i.posthog.com/static/:path*" },
{ source: "/ingest/:path*", destination: "https://us.i.posthog.com/:path*" },
{ source: "/ingest/decide", destination: "https://us.i.posthog.com/decide" },
];
},需要创建的文件:
- — 客户端Sentry初始化配置
sentry.client.config.ts - — 服务端Sentry初始化配置
sentry.server.config.ts - — Edge运行时Sentry初始化配置(与服务端一致)
sentry.edge.config.ts - — Next.js instrumentation钩子
instrumentation.ts - — 包含PII脱敏逻辑的Sentry共享配置工厂
lib/sentry.ts - — PostHog React提供者组件
components/posthog-provider.tsx - — 路由感知的页面访问追踪组件
components/posthog-pageview.tsx - — PostHog客户端初始化及工具函数
lib/posthog.ts
安装命令:
pnpm add @sentry/nextjs posthog-js posthog-nodeNext.js配置:在中使用包裹配置
next.config.tswithSentryConfig()PostHog 路径重写配置(在中):
/ingestnext.config.tstypescript
async rewrites() {
return [
{ source: "/ingest/static/:path*", destination: "https://us-assets.i.posthog.com/static/:path*" },
{ source: "/ingest/:path*", destination: "https://us.i.posthog.com/:path*" },
{ source: "/ingest/decide", destination: "https://us.i.posthog.com/decide" },
];
},Node.js (Express/Hono) → templates/node/
templates/node/Node.js (Express/Hono) → templates/node/
templates/node/Files to create:
- — Sentry init for Node
lib/sentry.ts
Install:
pnpm add @sentry/nodeExpress: after all routes
Hono: Import and init at app entry, use hook
Sentry.setupExpressErrorHandler(app)onError需要创建的文件:
- — Node.js环境下的Sentry初始化配置
lib/sentry.ts
安装命令:
pnpm add @sentry/nodeExpress:在所有路由定义后调用
Hono:在应用入口导入并初始化,使用钩子
Sentry.setupExpressErrorHandler(app)onErrorGo → templates/go/
templates/go/Go → templates/go/
templates/go/Files to create:
- — Sentry init + flush helper
internal/observability/sentry.go
Install:
go get github.com/getsentry/sentry-go需要创建的文件:
- — Sentry初始化及刷新工具函数
internal/observability/sentry.go
安装命令:
go get github.com/getsentry/sentry-goPython → templates/python/
templates/python/Python → templates/python/
templates/python/Files to create:
- — Sentry init
observability.py
Install: or add to
pip install sentry-sdkpyproject.toml需要创建的文件:
- — Sentry初始化配置
observability.py
安装方式: 或添加到
pip install sentry-sdkpyproject.tomlSwift → templates/swift/
templates/swift/Swift → templates/swift/
templates/swift/Files to create:
- — Sentry init via SPM
SentrySetup.swift
SPM dependency:
PostHog SPM:
https://github.com/getsentry/sentry-cocoahttps://github.com/PostHog/posthog-ios需要创建的文件:
- — 通过SPM初始化Sentry
SentrySetup.swift
SPM依赖:
PostHog SPM依赖:
https://github.com/getsentry/sentry-cocoahttps://github.com/PostHog/posthog-iosRust → templates/rust/
templates/rust/Rust → templates/rust/
templates/rust/Files to create:
- — Sentry guard init
src/sentry_init.rs
Install: Add to
sentry = "0.35"Cargo.toml需要创建的文件:
- — Sentry guard初始化配置
src/sentry_init.rs
安装方式:在中添加
Cargo.tomlsentry = "0.35"React Native → templates/react-native/
templates/react-native/React Native → templates/react-native/
templates/react-native/Install:
pnpm add @sentry/react-native posthog-react-native安装命令:
pnpm add @sentry/react-native posthog-react-nativeHelicone → templates/helicone/
templates/helicone/Helicone → templates/helicone/
templates/helicone/Files to create:
- — Helicone-proxied AI SDK provider
lib/ai-provider.ts
Pattern: Change on AI SDK provider to Helicone gateway, add auth + property headers.
baseURL需要创建的文件:
- — 经过Helicone代理的AI SDK提供者配置
lib/ai-provider.ts
配置模式:将AI SDK提供者的修改为Helicone网关地址,添加认证及属性头
baseURLCommit Message
提交信息模板
feat: add observability (Sentry [+ PostHog] [+ Helicone])
- Sentry: error tracking with PII-safe defaults
[- PostHog: product analytics with privacy masking]
[- Helicone: LLM cost tracking via gateway proxy]
- Updated .env.example with required variablesfeat: add observability (Sentry [+ PostHog] [+ Helicone])
- Sentry: error tracking with PII-safe defaults
[- PostHog: product analytics with privacy masking]
[- Helicone: LLM cost tracking via gateway proxy]
- Updated .env.example with required variablesPR Body
PR内容模板
markdown
undefinedmarkdown
undefinedSummary
摘要
Add production observability to {repo}.
为{repo}添加生产环境可观测性能力。
Changes
变更内容
- Sentry error tracking (DSN: )
{dsn}- PII scrubbing (emails, IPs, sensitive headers)
- Environment-aware (production/preview/development)
- Traces sample rate: 10% [- PostHog product analytics
- Pageview tracking via proxy rewrite
/ingest - Privacy: , manual pageview capture] [- Helicone LLM cost tracking
respect_dnt: true - Proxied through gateway for cost/latency monitoring
- Tagged with product name and environment]
- Updated with all required variables
.env.example
- Sentry错误追踪(DSN: )
{dsn}- PII脱敏处理(邮箱、IP、敏感请求头)
- 环境感知(生产/预览/开发环境)
- 事务采样率:10% [- PostHog产品数据分析
- 通过代理重写实现页面访问追踪
/ingest - 隐私配置:,手动触发页面访问捕获] [- Helicone LLM成本追踪
respect_dnt: true - 通过网关代理实现成本/延迟监控
- 标记产品名称及环境信息]
- 更新文件,添加所有必填环境变量
.env.example
Required Env Vars
必填环境变量
| Variable | Where | Value |
|---|---|---|
| Vercel | |
| Vercel + GH Actions | org-level token |
| [ | | Vercel |
| [ | | Vercel (server) |
| 变量名 | 配置位置 | 值 |
|---|---|---|
| Vercel | |
| Vercel + GH Actions | 组织级令牌 |
| [ | | Vercel |
| [ | | Vercel (服务端) |
Test Plan
测试计划
- passes
pnpm build - Deploy to preview → check Sentry for test error
- Set env vars on Vercel production [- [ ] Verify PostHog Live Events after deploy] [- [ ] Verify Helicone dashboard shows tagged requests]
undefined- 执行成功
pnpm build - 部署到预览环境 → 在Sentry中验证测试错误是否被捕获
- 在Vercel生产环境配置环境变量 [- [ ] 部署后验证PostHog实时事件] [- [ ] 验证Helicone仪表盘显示标记后的请求]
undefinedEnv Var Reference
环境变量参考
| Variable | Scope | Notes |
|---|---|---|
| Per-project | Unique per Sentry project |
| Per-project (server-only) | Same DSN, no NEXT_PUBLIC prefix |
| Shared | For source map uploads |
| | Hardcode in config |
| Per-project | Matches Sentry project slug |
| Shared | PostHog project API key |
| | Via rewrite proxy |
| Shared (server-only) | Never expose client-side |
| 变量名 | 作用范围 | 说明 |
|---|---|---|
| 单项目 | 每个Sentry项目唯一 |
| 单项目(仅服务端) | 与上述DSN相同,无NEXT_PUBLIC前缀 |
| 共享 | 用于上传source map |
| | 在配置中硬编码 |
| 单项目 | 与Sentry项目别名一致 |
| 共享 | PostHog项目API密钥 |
| | 通过代理重写配置 |
| 共享(仅服务端) | 绝不能在客户端暴露 |
Audit Mode
审计模式
When flag or repo already has instrumentation:
--audit- Check Sentry config against best practices (PII scrubbing, sampling rates, env detection)
- Check PostHog config (respect_dnt, manual pageview, proxy rewrite)
- Check Helicone config (product tag, environment tag, user-id)
- Report findings, fix issues, open PR with fixes
当添加参数或仓库已集成可观测性工具时:
--audit- 检查Sentry配置是否符合最佳实践(PII脱敏、采样率、环境检测)
- 检查PostHog配置(Do Not Track支持、手动页面访问捕获、代理重写)
- 检查Helicone配置(产品标记、环境标记、用户ID)
- 生成审计报告、修复问题并创建包含修复内容的PR
Anti-Patterns
反模式(需避免)
- — exhausts free tier instantly
tracesSampleRate: 1 - Missing PII scrubbing — privacy violation
- — leaks emails/IPs
sendDefaultPii: true - PostHog without rewrite — blocked by ad blockers
/ingest - Helicone API key in client-side code — key exposure
- Hardcoded DSN in code instead of env var — can't change per environment
- Missing on PostHog — privacy violation
respect_dnt: true
- — 瞬间耗尽免费层额度
tracesSampleRate: 1 - 缺失PII脱敏逻辑 — 违反隐私规定
- — 泄露邮箱/IP信息
sendDefaultPii: true - PostHog未配置重写 — 会被广告拦截器阻止
/ingest - Helicone API密钥出现在客户端代码中 — 密钥泄露风险
- 代码中硬编码DSN而非使用环境变量 — 无法按环境切换配置
- PostHog未设置— 违反隐私规定
respect_dnt: true