Repository navigation
feat(metrics): Add thread-local histograms - #311
Conversation
|
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.) |
`ClassicHistogramBounds` now owns fixed-bucket normalization, bucket lookup, and snapshot construction. `Histogram` retains its existing mutex-protected sum, count, and bucket counters while using the shared immutable bounds representation. This separates classic bucket semantics from their synchronization model so other histogram storage strategies can reuse them without changing rendering or public behavior.
|
@TheJokr You convinced me with the NIH free list needed with my way, so let's just use |
`ThreadLocalHistogram` keeps independent per-thread fixed-bucket state and aggregates it into a standard classic histogram during collection. Thread slots are reused after threads exit, while accumulated values remain visible. The `thread_local` crate provides runtime-owned storage, and boxed shard state avoids false sharing between threads. `HistogramBuilder` constructs the metric, making it compatible with metric macros and families. The implementation reuses `ClassicHistogramBounds`, `HistogramSnapshot`, and the existing histogram encoder. The type is available from `foundations-metrics` and its backend-gated `foundations` facade. Exemplar support remains deferred because sharing exemplar storage across thread-local values requires additional ownership and conflict semantics.
ThreadLocalHistogramkeeps independent per-thread fixed-bucket state and aggregates it into one standard classic histogram during collection. Thethread_localcrate provides runtime-owned slots that are reused after threads exit while preserving accumulated observations.The implementation uses boxed per-thread atomic state to avoid false sharing, and it reuses
HistogramSnapshotand the existing histogram encoding path for text and protobuf rendering.HistogramBuilderconstructs the metric, so metrics macros and labeled families can use it directly.The type is exported from
foundations-metricsand the backend-gatedfoundationsfacade.Exemplar support remains deferred because sharing exemplar storage across thread-local values requires additional ownership and conflict semantics.