Skip to content

fix(metrics): bucket latencies instead of hashing them, and assign the percentiles (#222) - #405

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/222-latency-percentiles-and-bucketing
Aug 9, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/222-latency-percentiles-and-bucketing

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #222.

The two defects

P50Latency, P95Latency and P99Latency were declared with JSON tags and assigned nowhere in the repository. Anything serializing DetailedOperationMetrics published zeros as percentiles — which reads as a filesystem with no tail latency at all, next to a populated average_latency. Zeros published as measurements is the defect, not the absent feature.

They could not have been computed from LatencyHistogram in any case:

bucketIndex := int(latency.Milliseconds()) % len(om.LatencyHistogram)  // 100 buckets

That is a hash of the latency into 100 slots with no ordering. 50ms and 250ms shared a bucket; so did 1ms and 1001ms; and Milliseconds() truncates, so every operation under a millisecond — the expected case for a cache hit — landed in bucket 0 along with everything at exactly 100ms. Past the top of the array it wrapped to a fast bucket, which is the failure direction that makes a slow filesystem look fast.

The fix

internal/metrics/latency.go, new:

  • 24 exponential buckets from 25µs, plus an overflow bucket. The range is set by what has to fit in it — an L1 hit is tens of microseconds, a cold multipart GET is seconds — so nothing linear in milliseconds resolves both ends. ~11 buckets sit under a millisecond; the top finite bound is ~209s.
  • LatencyBucketBounds() publishes the boundaries. Counts without their intervals are not interpretable, and the boundaries are deliberately not a struct field: LatencyHistogram is json:"-", so serializing 24 constants beside counts a JSON reader cannot see would describe nothing. Returns a copy — the histogram's meaning depends on those numbers.
  • Percentiles interpolated within the covering bucket, as Prometheus's histogram_quantile does, because the individual latencies are gone. A rank in the overflow bucket saturates at the top bound: "at least this" is the honest answer where there is no upper bound to interpolate toward.
  • Computed on record rather than on read, since DetailedPerformanceMetrics serializes these structs directly through its own operation_metrics tag — computing on read would leave the zeros in place for anything that marshals the collector without calling GetOperationMetrics, which is the defect in smaller form.

Per-file metrics allocate no histogram and are now documented as leaving the percentiles zero; a histogram per (file, operation) pair is 25 int64s × MaxTrackedFiles × 21 operations. GetTopFiles does not copy Operations at all, and a test pins that so nobody publishes those zeros.

One thing found on the way: GetOperationMetrics's comment said it returned a copy to avoid races, but a struct copy copies a slice header, so the caller walked the array RecordOperation increments. Harmless while nothing read the field; a data race now that the percentiles give something a reason to. Fixed, with a test that writes through the returned slice.

Verification

Eight mutations, eight caught:

Mutation Caught by
restore the modulo bucketing TestPercentilesReflectTheTail, +3
delete the percentile assignment TestPercentilesReflectTheTail, +4
< for <= in the bucket search TestEveryBucketIsReachable, TestSlowOperationsOverflowRatherThanWrapping
drop the overflow guard TestHistogramCountsEveryOperation (index panic)
return the lower bound, no interpolation TestASingleOperationGetsPercentilesInItsOwnBucket
drop the histogram copy TestGetOperationMetricsCopiesTheHistogram
return the package's bounds slice TestLatencyBucketBoundsCannotBeMutatedByACaller, +1
linear millisecond buckets TestDistantLatenciesDoNotShareABucket, +3

Two tests exist specifically to tell a bucketing from the modulo, and both fail on the old line: TestDistantLatenciesDoNotShareABucket (50ms/250ms, 1ms/101ms, 1ms/1001ms, 400µs/1ms) and TestSubMillisecondLatenciesAreResolved. The issue's own criterion — 1ms ×95 and 500ms ×5 with p99 in the 500ms band, not zero and not 1ms — is TestPercentilesReflectTheTail.

go test -race -count=1 ./... clean across the repo; go build ./..., gofmt -l internal/, go vet, and golangci-lint run --new-from-merge-base=origin/main clean. No Prometheus metric names change, so sdks/testdata/metrics-scrape.txt is untouched.

What is deliberately not done

The issue floated deleting all four fields instead, on the grounds that Prometheus computes quantiles from its own histograms. That decision belongs with whoever wires DetailedPerformanceMetrics up — it still has no caller outside this package's tests — and nothing tracks that wiring yet. What must not ship is the fields as zeros, which is what this fixes either way.

…e percentiles (#222)

`DetailedOperationMetrics` declared `P50Latency`, `P95Latency` and `P99Latency` with JSON tags and
assigned none of them, so anything serializing the struct published zeros as percentiles — a
filesystem with no tail latency, which reads as a measurement rather than as an unimplemented field.

They could not have been computed from `LatencyHistogram` anyway. It was indexed by
`int(latency.Milliseconds()) % 100`: a hash into 100 slots with no ordering, so 50ms and 250ms shared
a bucket, 1ms and 1001ms shared a bucket, and every sub-millisecond operation — the expected case for
a cache hit — landed in bucket 0 alongside everything at exactly 100ms. An array indexed that way
cannot answer "what fraction of operations were faster than N" for any N, which is presumably why
nothing ever tried.

The histogram is now 24 exponential buckets from 25µs plus an overflow bucket, with the boundaries
exported through `LatencyBucketBounds` — counts without their intervals are not interpretable, and
the histogram itself is `json:"-"`, so putting the bounds in the struct would describe numbers a JSON
reader cannot see. The range is set by what must fit: an L1 hit is tens of microseconds and a cold
multipart GET is seconds. The percentiles are interpolated within the covering bucket, as
Prometheus's `histogram_quantile` does, and a rank in the overflow bucket saturates at the top bound
rather than wrapping to a fast one — the failure direction that would make a slow filesystem look
fast.

`GetOperationMetrics` also stopped handing out the live histogram. Its comment claimed it returned a
copy to avoid races, but a struct copy copies a slice header, so the caller walked the array the
recorder increments. Harmless while nothing read the field; a data race now that the percentiles give
something a reason to.

Eight mutations, eight caught: restoring the modulo, deleting the percentile assignment, `<` for `<=`
in the bucket search, dropping the overflow guard, returning the bucket's lower bound instead of
interpolating, dropping the histogram copy, returning the package's bounds slice to a caller, and
switching to linear millisecond buckets. Two tests exist specifically to distinguish a bucketing from
the modulo — 50ms against 250ms, and four sub-millisecond latencies — and both fail on the old line.

Closes #222
@scttfrdmn
scttfrdmn merged commit ccd2d4a into main Aug 9, 2026
35 checks passed
@scttfrdmn
scttfrdmn deleted the fix/222-latency-percentiles-and-bucketing branch September 20, 2026 05:25
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.

metrics: percentile fields are never assigned and the latency histogram is a modulo, not a bucketing

1 participant