|
| 1 | +# 遥测与远程配置下发系统审计(除 Sentry 外) |
| 2 | + |
| 3 | +## 1. Datadog 日志 |
| 4 | + |
| 5 | +**文件**: `src/services/analytics/datadog.ts` |
| 6 | + |
| 7 | +- **端点**: `https://http-intake.logs.us5.datadoghq.com/api/v2/logs` |
| 8 | +- **客户端 token**: `pubbbf48e6d78dae54bceaa4acf463299bf` |
| 9 | +- **行为**: 批量发送日志(15s flush 间隔,100 条上限),仅限 1P(直连 Anthropic API)用户 |
| 10 | +- **事件白名单**: `tengu_*` 系列事件(启动、错误、OAuth、工具调用等 ~35 种) |
| 11 | +- **基线数据**: 收集 model、platform、arch、version、userBucket(用户 hash 到 30 个桶)等 |
| 12 | +- **仅限**: `NODE_ENV === 'production'` |
| 13 | + |
| 14 | +## 2. 1P 事件日志(BigQuery) |
| 15 | + |
| 16 | +**文件**: `src/services/analytics/firstPartyEventLogger.ts` + `firstPartyEventLoggingExporter.ts` |
| 17 | + |
| 18 | +- **端点**: `https://api.anthropic.com/api/event_logging/batch`(staging 可切换) |
| 19 | +- **行为**: 使用 OpenTelemetry SDK 的 `BatchLogRecordProcessor`,批量导出到 Anthropic 自有的 BQ 管道 |
| 20 | +- **数据**: 完整事件 metadata(session、model、env context、用户数据、subscription type 等) |
| 21 | +- **弹性**: 本地磁盘持久化失败事件(JSONL),二次退避重试,最多 8 次尝试 |
| 22 | +- **Proto schema**: 事件序列化为 `ClaudeCodeInternalEvent` / `GrowthbookExperimentEvent` protobuf 格式 |
| 23 | +- **Auth fallback**: 401 时自动去掉 auth header 重试 |
| 24 | + |
| 25 | +## 3. GrowthBook 远程 Feature Flags / 动态配置 |
| 26 | + |
| 27 | +**文件**: `src/services/analytics/growthbook.ts` |
| 28 | + |
| 29 | +- **服务端**: `https://api.anthropic.com/`(remote eval 模式) |
| 30 | +- **行为**: 启动时拉取全量 feature flags,每 6h(外部用户)/ 20min(ant)定时刷新 |
| 31 | +- **磁盘缓存**: feature values 写入 `~/.claude.json` 的 `cachedGrowthBookFeatures` |
| 32 | +- **用途**: |
| 33 | + - 控制 Datadog 开关(`tengu_log_datadog_events`) |
| 34 | + - 控制事件采样率(`tengu_event_sampling_config`) |
| 35 | + - 控制 sink killswitch(`tengu_frond_boric`) |
| 36 | + - 控制 BQ batch 配置(`tengu_1p_event_batch_config`) |
| 37 | + - 控制版本上限/自动更新 kill switch |
| 38 | + - 控制远程管理设置的安全检查 gate |
| 39 | +- **用户属性**: 发送 deviceId, sessionId, organizationUUID, accountUUID, email, subscriptionType 等 |
| 40 | + |
| 41 | +## 4. Remote Managed Settings(企业远程配置下发) |
| 42 | + |
| 43 | +**文件**: `src/services/remoteManagedSettings/index.ts` |
| 44 | + |
| 45 | +- **端点**: `{BASE_API_URL}/api/claude_code/settings` |
| 46 | +- **行为**: 企业用户配置下发,支持 ETag/304 缓存,每小时后台轮询 |
| 47 | +- **安全**: 变更包含"危险设置"时弹窗让用户确认 |
| 48 | +- **适用**: API key 用户全部可拉取;OAuth 用户仅 Enterprise/C4E/Team |
| 49 | +- **Fail-open**: 请求失败时使用本地缓存,无缓存则跳过 |
| 50 | + |
| 51 | +## 5. Settings Sync(设置同步) |
| 52 | + |
| 53 | +**文件**: `src/services/settingsSync/index.ts` |
| 54 | + |
| 55 | +- **端点**: `{BASE_API_URL}/api/claude_code/user_settings` |
| 56 | +- **行为**: CLI 上传本地设置/memory 到远程;CCR 模式从远程下载 |
| 57 | +- **同步内容**: userSettings、userMemory、projectSettings、projectMemory |
| 58 | +- **Feature gate**: `UPLOAD_USER_SETTINGS` / `DOWNLOAD_USER_SETTINGS` |
| 59 | +- **文件大小限制**: 500KB/文件 |
| 60 | + |
| 61 | +## 6. OpenTelemetry 三方遥测 |
| 62 | + |
| 63 | +**文件**: `src/utils/telemetry/instrumentation.ts` |
| 64 | + |
| 65 | +- **行为**: 完整的 OTEL SDK 初始化,支持 metrics / logs / traces 三种信号 |
| 66 | +- **协议**: gRPC / http-json / http-protobuf(通过 `OTEL_EXPORTER_OTLP_PROTOCOL` 选择) |
| 67 | +- **exporter**: console / otlp / prometheus |
| 68 | +- **触发**: `CLAUDE_CODE_ENABLE_TELEMETRY=1` 环境变量 |
| 69 | +- **增强 trace**: `feature('ENHANCED_TELEMETRY_BETA')` + GrowthBook gate `enhanced_telemetry_beta` |
| 70 | + |
| 71 | +## 7. BigQuery Metrics Exporter(内部指标) |
| 72 | + |
| 73 | +**文件**: `src/utils/telemetry/bigqueryExporter.ts` |
| 74 | + |
| 75 | +- **端点**: `https://api.anthropic.com/api/claude_code/metrics` |
| 76 | +- **行为**: 定期(5min 间隔)导出 OTel metrics 到内部 BQ |
| 77 | +- **适用**: API 客户、C4E/Team 订阅者 |
| 78 | +- **组织级 opt-out**: 通过 `checkMetricsEnabled()` API 查询(见下方第 8 项) |
| 79 | + |
| 80 | +## 8. 组织级 Metrics Opt-out 查询 |
| 81 | + |
| 82 | +**文件**: `src/services/api/metricsOptOut.ts` |
| 83 | + |
| 84 | +- **端点**: `https://api.anthropic.com/api/claude_code/organizations/metrics_enabled` |
| 85 | +- **行为**: 查询组织是否启用了 metrics,二级缓存(内存 1h + 磁盘 24h) |
| 86 | +- **作用**: 控制 BigQuery metrics exporter 是否导出 |
| 87 | + |
| 88 | +## 9. Startup Profiling |
| 89 | + |
| 90 | +**文件**: `src/utils/startupProfiler.ts` |
| 91 | + |
| 92 | +- **行为**: 采样启动性能数据(100% ant / 0.5% 外部),通过 `logEvent('tengu_startup_perf')` 上报 |
| 93 | +- **详细模式**: `CLAUDE_CODE_PROFILE_STARTUP=1` 输出完整性能报告到文件 |
| 94 | + |
| 95 | +## 10. Beta Session Tracing |
| 96 | + |
| 97 | +**文件**: `src/utils/telemetry/betaSessionTracing.ts` |
| 98 | + |
| 99 | +- **行为**: 详细调试 trace,发送 system prompt、model output、tool schema 等 |
| 100 | +- **触发**: `ENABLE_BETA_TRACING_DETAILED=1` + `BETA_TRACING_ENDPOINT` |
| 101 | +- **外部用户**: SDK/headless 模式自动启用,交互模式需要 GrowthBook gate `tengu_trace_lantern` |
| 102 | + |
| 103 | +## 11. Bridge Poll Config(远程轮询间隔配置) |
| 104 | + |
| 105 | +**文件**: `src/bridge/pollConfig.ts` |
| 106 | + |
| 107 | +- **行为**: 从 GrowthBook 拉取 bridge 轮询间隔配置(`tengu_bridge_poll_interval_config`) |
| 108 | +- **控制**: 单会话和多会话的各种 poll interval |
| 109 | + |
| 110 | +## 12. Plugin/MCP 遥测 |
| 111 | + |
| 112 | +**文件**: `src/utils/plugins/fetchTelemetry.ts` |
| 113 | + |
| 114 | +- **行为**: 记录 plugin/marketplace 的网络请求(安装计数、marketplace clone/pull 等) |
| 115 | +- **事件**: `tengu_plugin_remote_fetch`,包含 host(已脱敏)、outcome、duration |
| 116 | + |
| 117 | +--- |
| 118 | + |
| 119 | +## 全局禁用方式 |
| 120 | + |
| 121 | +```bash |
| 122 | +# 禁用所有遥测(Datadog + 1P + 调查问卷) |
| 123 | +DISABLE_TELEMETRY=1 |
| 124 | + |
| 125 | +# 更激进:禁用所有非必要网络(包括自动更新、grove、release notes 等) |
| 126 | +CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1 |
| 127 | + |
| 128 | +# 3P 提供商自动禁用 |
| 129 | +CLAUDE_CODE_USE_BEDROCK=1 # 或 VERTEX/FOUNDRY |
| 130 | +``` |
| 131 | + |
| 132 | +`src/utils/privacyLevel.ts` 是集中控制点,三个级别:`default < no-telemetry < essential-traffic`。 |
| 133 | + |
| 134 | +--- |
| 135 | + |
| 136 | +## 数据流架构 |
| 137 | + |
| 138 | +``` |
| 139 | +用户操作 → logEvent() |
| 140 | + ↓ |
| 141 | + sink.ts (路由层) |
| 142 | + ↙ ↘ |
| 143 | + trackDatadogEvent() logEventTo1P() |
| 144 | + ↓ ↓ |
| 145 | + Datadog HTTP API OTel BatchLogRecordProcessor |
| 146 | + (us5.datadoghq.com) ↓ |
| 147 | + FirstPartyEventLoggingExporter |
| 148 | + ↓ |
| 149 | + api.anthropic.com/api/event_logging/batch |
| 150 | + ↓ |
| 151 | + BigQuery (ClaudeCodeInternalEvent proto) |
| 152 | +``` |
| 153 | + |
| 154 | +GrowthBook 作为独立通道,同时驱动上述两个 sink 的开关和配置。 |
0 commit comments