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時間
- マージ済み PR(30日)
- 86
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
prometheus/client_java のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
prometheus/client_java#2416 · コメント 1 件 ·
-
Switch Micrometer compatibility workflow to upstream once typed-descriptor path becomes default オープン
難易度 1/5 1時間未満 初心者へのやさしさ 86/100
prometheus/client_java#2182 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
prometheus/client_java#2306 · コメント 9 件 · リアクション 4 件 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 32/100
prometheus/client_java#2084 · コメント 3 件 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
prometheus/client_java#2075 · コメント 10 件 · リアクション 1 件 ·
prometheus/client_java の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
-
bug needs triage
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
-
難易度 1/5 1時間未満 初心者へのやさしさ 94/100
objectionary/hone-maven-plugin#1061 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
spring-projects/spring-modulith#1895 ·