Repository navigation
fix(metrics): bucket latencies instead of hashing them, and assign the percentiles (#222) - #405
Merged
Merged
Conversation
…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
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.
Closes #222.
The two defects
P50Latency,P95LatencyandP99Latencywere declared with JSON tags and assigned nowhere in the repository. Anything serializingDetailedOperationMetricspublished zeros as percentiles — which reads as a filesystem with no tail latency at all, next to a populatedaverage_latency. Zeros published as measurements is the defect, not the absent feature.They could not have been computed from
LatencyHistogramin any case: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:LatencyBucketBounds()publishes the boundaries. Counts without their intervals are not interpretable, and the boundaries are deliberately not a struct field:LatencyHistogramisjson:"-", 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.histogram_quantiledoes, 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.DetailedPerformanceMetricsserializes these structs directly through its ownoperation_metricstag — computing on read would leave the zeros in place for anything that marshals the collector without callingGetOperationMetrics, 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.GetTopFilesdoes not copyOperationsat 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 arrayRecordOperationincrements. 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:
TestPercentilesReflectTheTail, +3TestPercentilesReflectTheTail, +4<for<=in the bucket searchTestEveryBucketIsReachable,TestSlowOperationsOverflowRatherThanWrappingTestHistogramCountsEveryOperation(index panic)TestASingleOperationGetsPercentilesInItsOwnBucketTestGetOperationMetricsCopiesTheHistogramTestLatencyBucketBoundsCannotBeMutatedByACaller, +1TestDistantLatenciesDoNotShareABucket, +3Two 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) andTestSubMillisecondLatenciesAreResolved. The issue's own criterion — 1ms ×95 and 500ms ×5 with p99 in the 500ms band, not zero and not 1ms — isTestPercentilesReflectTheTail.go test -race -count=1 ./...clean across the repo;go build ./...,gofmt -l internal/,go vet, andgolangci-lint run --new-from-merge-base=origin/mainclean. No Prometheus metric names change, sosdks/testdata/metrics-scrape.txtis 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
DetailedPerformanceMetricsup — 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.