Repository navigation
Expose native histograms on Scope - #280
Merged
Merged
Conversation
This was referenced Sep 11, 2026
| // rather than declared up front, and the distribution is reported as a | ||
| // single serialized payload. Requires ScopeOptions.NativeHistogramFactory | ||
| // to be set; without it the metric accumulates but is never reported. | ||
| NativeHistogram(name string, maxBuckets int) NativeHistogram |
Contributor
There was a problem hiding this comment.
LMK what you think, but my vote would be to not allow users to set max buckets. I think we should just use 160 as the default for everyone. I think the less configuration for users the better, and us controlling this on our side lets us find a good accuracy / performance tradeoff.
Contributor
Author
There was a problem hiding this comment.
yeah totally makes sense to me, ill just default to 160
adamjeanlaurent
requested changes
Sep 14, 2026
adamjeanlaurent
left a comment
Contributor
There was a problem hiding this comment.
maxBuckets comment
rluong003
force-pushed
the
richard.luong/tally-native-histogram-type
branch
from
September 14, 2026 19:56
6162b00 to
a687433
Compare
rluong003
force-pushed
the
richard.luong/tally-native-histogram-scope
branch
2 times, most recently
from
September 14, 2026 20:08
83883ac to
b5dabd1
Compare
rluong003
marked this pull request as ready for review
September 14, 2026 20:40
adamjeanlaurent
approved these changes
Sep 14, 2026
rluong003
force-pushed
the
richard.luong/tally-native-histogram-scope
branch
from
September 14, 2026 21:43
b5dabd1 to
ca13a53
Compare
rluong003
force-pushed
the
richard.luong/tally-native-histogram-type
branch
from
September 14, 2026 21:43
a687433 to
60e04ce
Compare
Scope.NativeValueHistogram(name) and Scope.NativeDurationHistogram(name) return
the native histogram for a name. Both follow Histogram at every site: the
lookup-then-create path, the report loop, the cached path, subscope creation,
clearMetrics, and the snapshot.
Values and durations get separate lookup namespaces rather than separate
mutexes or slices. The two variants are views over one accumulator type, so
everything past the record call -- draining, reporting, snapshotting -- is
identical, and only the create-and-find path needs to know which kind it is
holding. A name registered as both yields two accumulators reporting under it,
exactly as Counter("x") and Gauge("x") already do; tally does not arbitrate
cross-type name collisions and this does not add a new kind of them.
Two new ScopeOptions:
NativeHistogramFactory supplies the accumulator behind each
native histogram on this scope and its
subscopes.
DefaultNativeHistogramMaxBuckets the bucket budget for every native
histogram on this scope. Defaults to
DefaultNativeHistogramMaxBuckets, 160.
The budget is configured on the scope rather than passed per call. It follows
from the encoding and the cost an application has signed up for, which is a
deployment-wide decision, and these objects are memoized: the accumulator is
built on the first call for a name and every later call returns it, so a
per-call budget was silently ignored everywhere except the call that happened
to run first.
A nil factory falls back to defaultNativeHistogramFactory, so both methods are
always safe to call. Without a real factory the metric is counted and never
reported, rather than panicking or emitting an empty payload -- an application
that has not chosen an encoding has nothing meaningful to put on the wire.
Snapshot gains NativeHistograms(), with both variants in the one map the way
Histograms() already carries value and duration histograms together. Only the
sample count is exposed: the distribution lives in an application-supplied
accumulator whose encoding tally does not interpret, so there is nothing
generic to show.
tally still has no dependency on any histogram implementation. The factory is
the entire seam.
rluong003
force-pushed
the
richard.luong/tally-native-histogram-scope
branch
from
September 14, 2026 21:45
ca13a53 to
6f0bdd7
Compare
rluong003
changed the base branch from
richard.luong/tally-native-histogram-type
to
master
September 14, 2026 21:45
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stack 3 of 4. Based on #279. The diff shown here is this commit only.
Wires the types from #279 into
Scope. This is the commit that makes the feature reachable.Values and durations get separate lookup namespaces over one shared accumulator slice. Everything past the record call — draining, reporting, snapshotting — is identical, so only the create-and-find path needs to know which kind it is holding.
A name registered as both yields two distributions reporting under it, exactly as
Counter("x")andGauge("x")already do. Tally does not arbitrate cross-type name collisions and this does not add a new kind of them.ScopeOptions
Two new fields:
NativeHistogramFactory func(maxBuckets int) NativeHistogramData— the only place a backend is ever supplied. Leave it nil and every native histogram on the scope is inert.DefaultNativeHistogramMaxBuckets— defaults to 160 when unset or< 1.The bucket budget is configured on the scope rather than passed per call. It follows from the encoding and the cost an application has signed up for, which is a deployment-wide decision — and these objects are memoized, so a per-call budget was silently ignored on every call except whichever one happened to run first.
Both propagate to subscopes through
scope_registry.go, so a factory set once at the root reaches everything beneath it. There is a test for that specifically, since the registry's sharded subscope construction is easy to get wrong.Mirrors the existing Histogram plumbing
Both methods follow
Histogramstep for step: lookup underRLock, take the write lock, double-check, allocate the cached handle when aCachedStatsReporteris present, store. The newnhmmutex joins the existing lock chain inreport,cachedReport,clearMetricsandSnapshot.SnapshotgainsNativeHistograms() map[string]NativeHistogramSnapshot, exposing name, tags and sample count. Both variants land in the one map, the wayHistograms()already carries value and duration histograms together. Not the payload — the encoding is the application's, so tally has nothing meaningful to hand back.Deliberately not included
No cardinality gauge in
reportInternalMetrics. Adding one would put a permanently-zero series on every scope that never touches this feature, same argument as the m3 counter in #278.Tests
8 scope-level tests: the scope-level bucket budget, no-factory inertness, subscope inheritance, memoization, the two variants as separate metrics, and snapshot contents.
I mutation-tested the report wiring rather than trusting a green run — disabling the
nhmblock inreport()makesTestScopeNativeHistogramfail, so the test is actually exercising the path.go build ./... && go test ./... && gofmt -l .clean on this commit alone.