Classic-only histogram: consider synchronized block instead of multi-LongAdder for observe() hot path
@zeitlinger 已经在做这个了。
开始于 2026年2月25日。
评估
这个 Issue 还没有评估数据。
描述
Context
While benchmarking the Prometheus shim PoC (bridging Prometheus client API to the OTel SDK), I found that classic-only histograms are 30% faster through the OTel SDK than through native Prometheus.
Benchmark numbers (JMH, single thread)
| Path | observe() latency |
|---|---|
| Native Prometheus (classic-only) | 10.5 ns |
| OTel SDK (explicit bucket histogram) | 7.3 ns |
Root cause
Native Prometheus doObserve() uses 3 separate CAS-based atomics per call:
classicBuckets[i].add(1)—LongAddersum.add(value)—DoubleAddercount.increment()—LongAdder
Plus a buffer.append() CAS attempt and volatile reads for reset/scale-down state.
The OTel SDK uses a single synchronized block with plain +=/++ arithmetic:
synchronized (lock) {
this.sum += value;
this.count++;
this.counts[bucketIndex]++;
// min/max tracking
}
In uncontended (single-thread) benchmarks, HotSpot elides the uncontended lock and optimizes the plain arithmetic freely, beating the multi-CAS approach.
Suggestion
For classic-only histograms (where nativeInitialSchema == CLASSIC_HISTOGRAM), consider an alternative doObserve() implementation that uses a synchronized block with plain fields instead of multiple LongAdder/DoubleAdder instances. The buffer mechanism (needed for native histogram scale-down) could also be bypassed in classic-only mode.
This wouldn't affect native or hybrid histograms, which still need the current design.
Multi-threaded consideration
The LongAdder approach was chosen for multi-threaded scalability (striped cells reduce contention). A synchronized block would serialize threads. However:
- Most real-world
observe()calls happen on different label-value combinations (different data points), so contention on a single data point is rare - Even under contention, the critical section is very short (~5 ns of arithmetic), so lock hold time is minimal
- A benchmark with 4 threads would clarify the actual tradeoff
Not a high priority — 10.5 ns is already excellent. But worth considering if classic histogram performance matters.
- 主要语言
- Java
- 星标
- 2.3k
- 派生
- 833
- 平均合并
- 2 天 16 小时
- 30 天内合并 PR
- 86
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
prometheus/client_java 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 82/100
prometheus/client_java#2416 · 1 条评论 ·
-
难度 1/5 1 小时以内 新手友好度 86/100
prometheus/client_java#2182 ·
-
难度 5/5 一周以上 新手友好度 35/100
prometheus/client_java#2306 · 9 条评论 · 4 个 reaction ·
-
难度 5/5 一周以上 新手友好度 32/100
prometheus/client_java#2084 · 3 条评论 ·
-
难度 5/5 一周以上 新手友好度 35/100
prometheus/client_java#2075 · 10 条评论 · 1 个 reaction ·
查看 prometheus/client_java 的全部 Issue
相似的 Issue
-
Bug Java Platform: Java
难度 2/5 1-3 小时 新手友好度 78/100
getsentry/sentry-java#6138 · 1 条评论 ·
-
bug needs triage p2
难度 2/5 1-3 小时 新手友好度 78/100
GoogleCloudPlatform/DataflowTemplates#4273 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 78/100
-
难度 2/5 1-3 小时 新手友好度 76/100
-
bug needs triage
难度 2/5 1-3 小时 新手友好度 76/100