Skip to content

Expose native histograms on Scope - #3

Closed
rluong003 wants to merge 1 commit into
richard.luong/tally-native-histogram-typefrom
richard.luong/tally-native-histogram-scope
Closed

rluong003 wants to merge 1 commit into
richard.luong/tally-native-histogram-typefrom
richard.luong/tally-native-histogram-scope

Conversation

@rluong003

Copy link
Copy Markdown
Owner

Stack 3 of 4. Based on #2. The diff shown here is this commit only.

Wires the type from #2 into Scope. This is the commit that makes the feature reachable.

// NativeHistogram returns the NativeHistogram for the given name and bucket
// budget, creating it if it does not already exist.
NativeHistogram(name string, maxBuckets int) NativeHistogram

Pass maxBuckets < 1 to take the scope's default.

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, per Add the NativeHistogram metric type #2.
  • DefaultNativeHistogramMaxBuckets — defaults to 160 when unset or < 1.

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

NativeHistogram follows 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. 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, which is the same argument as the m3 counter in #1.

Tests

7 scope-level tests: default and explicit bucket budgets, no-factory inertness, subscope inheritance, 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.

Scope.NativeHistogram(name, maxBuckets) returns the native histogram for
a name, holding at most maxBuckets buckets; 0 uses the scope's configured
default. It follows Histogram at every site: the lookup-then-create path,
the report loop, the cached path, subscope creation, clearMetrics, and
the snapshot.

Two new ScopeOptions:

  NativeHistogramFactory            supplies the accumulator behind each
                                    native histogram on this scope and
                                    its subscopes.
  DefaultNativeHistogramMaxBuckets  the budget used when NativeHistogram
                                    is called with 0. Defaults to
                                    DefaultNativeHistogramMaxBuckets.

A nil factory falls back to defaultNativeHistogramFactory, so
Scope.NativeHistogram is always safe to call. Without a real factory the
metric accumulates and is 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(). 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

Copy link
Copy Markdown
Owner Author

Superseded by uber-go#280 — org membership came through, so the stack now lives on the upstream repo. Review there.

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.

1 participant