Repository navigation
Description
internal/metrics/detailed.go has two defects in its latency reporting. They are filed together
because they are the same feature — the histogram exists to compute the percentiles — and neither
can be fixed alone in a way that makes sense.
Neither is live today: NewDetailedPerformanceMetrics has no caller outside this package's own
tests, so nothing a mount does reaches any of it (see the Not yet wired up table in
docs/index.md). Both would go live the moment it is wired up, which is why they are recorded in
the package doc as well as here.
1. P50Latency, P95Latency, and P99Latency are declared and never assigned
detailed.go:52-54:
P50Latency time.Duration `json:"p50_latency"`
P95Latency time.Duration `json:"p95_latency"`
P99Latency time.Duration `json:"p99_latency"`grep -rn 'P50Latency\|P95Latency\|P99Latency' internal/metrics/ returns the three declarations
and nothing else. No assignment, in any file, including the tests.
They are always zero, and the consequence is worse than a missing field: they are exported with
JSON tags, and GetOperationMetrics (:342) hands the whole struct to a caller. Zero-valued
percentiles read as a very fast filesystem, not as an unimplemented field — a dashboard showing
p99_latency: 0 alongside a populated average_latency looks like a system with no tail latency
at all, which is the most flattering possible wrong answer.
2. LatencyHistogram is indexed by a modulo, so distant latencies collide
detailed.go:204, against a 100-element slice allocated at :182:
bucketIndex := int(latency.Milliseconds()) % len(om.LatencyHistogram)
om.LatencyHistogram[bucketIndex]++1 ms, 101 ms, 201 ms, and 1001 ms all increment bucket 1. This is not a bucketing — it is a hash of
the latency into 100 slots with no ordering, so the array cannot answer "what fraction of
operations were under N ms" for any N. The percentiles in (1) could not be computed from it even if
something tried, which is presumably why nothing does.
The comment on both lines says "for percentile calculation", so the intent is clear and the
arithmetic simply does not implement it. Also note Milliseconds() truncates: every operation
faster than 1 ms lands in bucket 0 along with everything at exactly 100 ms, 200 ms, and so on — and
for a cache hit, sub-millisecond is the expected case.
Fix
Do (2) first; (1) is then a small function over a correct histogram.
- Replace the modulo with real bucket boundaries. Exponential buckets are the standard choice for
latency and the range here spans a cache hit (tens of microseconds) to a cold multipart GET
(seconds), which is five or six orders of magnitude — so linear-in-milliseconds cannot work at
either end. Microsecond resolution at the bottom, with a final overflow bucket, and record the
boundaries in the struct so a reader of the JSON knows what the counts mean. - Compute the three percentiles from the histogram in
updateOverallMetrics(or on read in
GetOperationMetrics), interpolating within the covering bucket. - Consider deleting all four instead.
internal/metrics's Prometheus surface already exposes
latency, and Prometheus computes quantiles from its own histograms — so if this collector is ever
wired up through the Prometheus collector rather than as its own surface, these fields are a
second, worse implementation of something already solved. No issue tracks that wiring-up decision
yet;docs/index.mdlists the collector in its Not yet wired up table and that is all. That decision belongs with whoever wires the
subsystem up; what must not happen is shipping the fields as zeros.
Either way: a percentile field that cannot be computed should not be exported. Zeros published as
measurements is the defect, and deleting the fields fixes it as completely as implementing them.
Verification
- A test recording known latencies (say 1 ms ×95 and 500 ms ×5) and asserting
P99Latencylands in
the 500 ms band, not at zero and not at 1 ms. - A test asserting 1 ms and 101 ms land in different buckets — the direct regression test for
the modulo, and one that fails on today's code. - If the fields are deleted instead, a test asserting the Prometheus collector reports a non-zero
p99 for the same input, so the capability is not lost with them.
Context
Found while deleting docs/performance-metrics.md under #182, which documented both behaviors as
working. That document was 618 lines about this one file and had nine defects; seven were
documentation errors, and these two are the ones that turned out to be in the code. Recorded in
internal/metrics/doc.go so that whoever wires the collector up sees them before trusting the
output rather than after.
Activity
docs: three mechanical gates, and the 90 claims they found false (#225)