Skip to content

Add the NativeHistogram metric type - #2

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

rluong003 wants to merge 1 commit into
richard.luong/tally-native-histogram-reportersfrom
richard.luong/tally-native-histogram-type

Conversation

@rluong003

Copy link
Copy Markdown
Owner

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

Adds the NativeHistogram metric type. Two files, no wiring — Scope does not expose this yet, that is #3.

The seam

Tally must never depend on a histogram implementation. The only extension point is a factory the application supplies:

type NativeHistogramData interface {
	Update(value float64)
	Count() uint64
	Clear()
	MarshalBinary() ([]byte, error)
}

Tally calls Update and MarshalBinary and ships the bytes. It never inspects them, never learns the encoding, and takes on no new dependency. The seam is the deliverable; a real backend lives in the application.

DefaultNativeHistogramMaxBuckets = 160, matching the OpenTelemetry exponential histogram default max size.

Two behaviours worth reviewing closely

RecordDuration converts to seconds. Not milliseconds. This matches the units DurationBuckets.AsValues() already reports durations in, so a native histogram and a bucketed timer over the same data agree. Reporters needing another unit on the wire convert on the way out, once. Getting this wrong silently misaligns percentiles and nothing downstream catches it, so there is a test asserting equality against buckets.AsValues() directly — change the unit and it fails loudly.

The drain is a delta. Each payload covers one reporting interval, not the distribution's whole history: marshal, then clear. On a marshal error the accumulator is left intact, so samples are retried next cycle rather than dropped.

That retry is also what makes an unconfigured scope harmless. The default NativeHistogramData never marshals successfully — it returns ErrNativeHistogramBackendNotConfigured — so the report loop's skip-this-cycle path absorbs it every time. A scope with no factory configured is inert, not broken, and never emits a partial or bogus payload.

Tests

13 tests covering the seconds conversion (table-driven, including negatives), delta semantics, sample retention across a marshal error, and the unconfigured default. Fakes only; no backend is imported.

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

A native histogram accumulates into a mergeable structure supplied by the
application and reports one serialized payload per interval, rather than
a counter per pre-declared bucket. Because the encoding is the
application's choice, tally owns only the seam:

  NativeHistogramData   the accumulator interface an application
                        implements (Update / Count / Clear /
                        MarshalBinary).
  NativeHistogram       the metric interface callers use.

Two details are load-bearing:

RecordDuration converts to SECONDS, matching the units of
DurationBuckets.AsValues(). Any other unit is a wire concern belonging to
a specific reporter and must be applied on the way out; doing it here
would silently misalign percentiles against tally's own bucketed
histograms, and nothing downstream would catch it.

drain() is a delta. Each payload covers only the interval since the last
report, and a marshal error leaves the accumulator intact so the samples
are retried next cycle instead of being dropped.

That retry behavior is also what makes an unconfigured scope inert rather
than broken. defaultNativeHistogramData counts observations but always
fails to marshal with ErrNativeHistogramBackendNotConfigured, so a scope
with no NativeHistogramFactory skips every cycle: values are counted,
nothing is emitted, and nothing panics.

tally itself never gains a dependency on any histogram implementation.
@rluong003

Copy link
Copy Markdown
Owner Author

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

@rluong003 rluong003 closed this Sep 11, 2026
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