Skip to content

Expose native histograms on Scope - #280

Merged
rluong003 merged 1 commit into
masterfrom
richard.luong/tally-native-histogram-scope
Sep 14, 2026
Merged

rluong003 merged 1 commit into
masterfrom
richard.luong/tally-native-histogram-scope

Conversation

@rluong003

@rluong003 rluong003 commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

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.

NativeValueHistogram(name string) NativeValueHistogram
NativeDurationHistogram(name string) NativeDurationHistogram

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") and Gauge("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 Histogram step for step: lookup under RLock, take the write lock, double-check, allocate the cached handle when a CachedStatsReporter is present, store. The new nhm mutex joins the existing lock chain in report, cachedReport, clearMetrics and Snapshot.

Snapshot gains NativeHistograms() map[string]NativeHistogramSnapshot, exposing name, tags and sample count. Both variants land in the one map, the way Histograms() 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 nhm block in report() makes TestScopeNativeHistogram fail, so the test is actually exercising the path.

go build ./... && go test ./... && gofmt -l . clean on this commit alone.

@CLAassistant

CLAassistant commented Sep 11, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Comment thread types.go Outdated
// 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah totally makes sense to me, ill just default to 160

@adamjeanlaurent adamjeanlaurent left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maxBuckets comment

@rluong003
rluong003 force-pushed the richard.luong/tally-native-histogram-type branch from 6162b00 to a687433 Compare September 14, 2026 19:56
@rluong003
rluong003 force-pushed the richard.luong/tally-native-histogram-scope branch 2 times, most recently from 83883ac to b5dabd1 Compare September 14, 2026 20:08
@rluong003
rluong003 marked this pull request as ready for review September 14, 2026 20:40
@rluong003
rluong003 force-pushed the richard.luong/tally-native-histogram-scope branch from b5dabd1 to ca13a53 Compare September 14, 2026 21:43
@rluong003
rluong003 force-pushed the richard.luong/tally-native-histogram-type branch from a687433 to 60e04ce Compare September 14, 2026 21:43
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
rluong003 force-pushed the richard.luong/tally-native-histogram-scope branch from ca13a53 to 6f0bdd7 Compare September 14, 2026 21:45
@rluong003
rluong003 changed the base branch from richard.luong/tally-native-histogram-type to master September 14, 2026 21:45
@rluong003
rluong003 merged commit c825199 into master Sep 14, 2026
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants