Add ai_trace_sample_rate to sample AI observability events by trace id
@jakesciotto 已经在做这个了。
开始于 2026年9月16日。
评估
这个 Issue 还没有评估数据。
描述
Problem Statement
Teams whose AI observability volume is mostly $ai_* events have no supported way to keep a fraction of traces. The only path today is a hand-written before_send hook that hashes $ai_trace_id. Most teams do not write one. A hook that samples per event splits a trace, so the trace view shows partial trees.
Related: PostHog/posthog-js#4997 for the same option in posthog-node.
Solution Brainstorm
One client option, default off:
Client(api_key, ai_trace_sample_rate=0.1)
Env var POSTHOG_AI_TRACE_SAMPLE_RATE, same precedence rule as POSTHOG_CAPTURE_MODE (kwarg > env > default).
Behavior:
- Applies to events named
$ai_*that carry$ai_trace_id. - FNV-1a 32-bit hash of the trace id. Keep when
hash / 2^32 < rate. Same hash in every SDK, so a mixed Python and Node stack makes one decision per trace. - Runs in
Client._enqueuebeforebefore_send, so a customer hook sees only kept events. - Adds
$ai_trace_sample_rateto kept events, so the server can scale totals later. - Events with no trace id are kept. Rate unset or
1.0means no sampling. Out-of-range values log a warning and are ignored.
flowchart LR
W[AI wrappers and agent processors] --> C[_capture_ai_event]
M[Manual capture of $ai_* events] --> E[Client._enqueue]
C --> E
E --> S{ai_trace_sample_rate}
S -- drop --> X[Dropped]
S -- keep --> B[before_send] --> Q[Queue]
Alternatives considered: a helper that returns a before_send function. Rejected because before_send takes one callable, so the helper does not compose with an existing hook.
Impact
- Gives AI observability users a supported volume lever that keeps traces whole.
- Removes the need for a custom hook that is easy to get wrong.
Blast radius
- Default off. No behavior change for any existing caller.
- Touches
Client.__init__andClient._enqueueonly. Wrappers do not change. - Affects
$ai_*events only. Other events never reach the sampler. - Kept
$ai_*events gain one property when the option is set. - Public API grows by one option and one env var.
references/public_api_snapshot.txtchanges.
Not in this issue
Tail sampling that keeps errored traces. A per-call override. Server-side upweighting of cost dashboards.
- 主要语言
- Python
- 星标
- 61
- 派生
- 82
- 平均合并
- 1 天 15 小时
- 30 天内合并 PR
- 71
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
PostHog/posthog-python 的其他 Issue
-
enhancement
难度 5/5 一周以上 新手友好度 35/100
PostHog/posthog-python#146 · 1 条评论 ·
查看 PostHog/posthog-python 的全部 Issue
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 75/100
xinnan-tech/xiaozhi-fde-talk#263 ·
-
rules
难度 1/5 1 小时以内 新手友好度 90/100
-
难度 2/5 1-3 小时 新手友好度 70/100
huggingface/Repo2RLEnv#163 · 1 条评论 ·
-
难度 1/5 1 小时以内 新手友好度 95/100
huggingface/sentence-transformers#4074 ·
-
comp/dashboard invalid P3
难度 2/5 1-3 小时 新手友好度 70/100
NousResearch/hermes-agent#121143 ·