Repository navigation
Conversation
`SplitHistogram` collects explicitly registered `SplitHistogramShard` handles into a standard classic histogram while observations use per-shard relaxed atomics. The parent retains shard ownership, and cloned shard handles share their underlying counters. The implementation reuses `HistogramSnapshot` and the existing histogram encoder, preserving standard text and protobuf rendering. Caller-managed thread-local storage is documented without adding TLS policy to the metrics backend. Both types are available from `foundations-metrics` and its backend-gated `foundations` facade. Exemplar support remains deferred because sharing exemplar storage across explicit shards requires additional ownership and conflict semantics.
|
Why not use the |
|
@gregor-cf I wasn't aware of |
|
Tbh I don't think TLS-backed counters should use the |
It adds an extra 2 (or 3, if you count the boxing we do for ThreadLocalCounter) pointers to chase. All of those are in cache lines that only get written if a new thread's value is initialized, so should rarely be invalidated. And the rest of your code can execute OOO while the CPU is waiting for the cache. I don't think the overhead is noticeable. That being said, I'm not against explicitly-sharded metric types, but they are very different from ThreadLocalCounter. The latter is a 1:1 API replacement for a regular Counter, whereas explicit sharding requires the user to figure out what to do with the shards. (For example, stuffing them into static thread locals as in your example.) |
| .collect(), | ||
| }); | ||
|
|
||
| self.state.shards.write().push(Arc::clone(&state)); |
There was a problem hiding this comment.
Before we allocate a new shard, can we check if we have an unused shard here? (E.g. checking for Arc::strong_count(shard) == 1 or maintaining a free list.) That way, workloads that churn through threads (or just call .shard() too often) don't derange into an endless list.
SplitHistogramcollects explicitly registeredSplitHistogramShardhandles into one standard classic histogram. Each shard records through relaxed atomics without taking a parent-wide observation lock, while the parent retains ownership for collection.The implementation reuses
HistogramSnapshotand the existing histogram encoding path, preserving text and protobuf rendering. Shard placement remains caller-controlled, including the documented option of caller-managed thread-local storage.Both types are exported from
foundations-metricsand the backend-gatedfoundationsfacade.Exemplar support remains deferred because sharing exemplar storage across explicit shards requires additional ownership and conflict semantics.