Skip to content

histogram: ignore negative amounts and support batch updates in PercentileTimer - #1307

Open
reginaldalfret wants to merge 1 commit into
Netflix:mainfrom
reginaldalfret:fix/percentile-timer-negative-and-batch
Open

reginaldalfret wants to merge 1 commit into
Netflix:mainfrom
reginaldalfret:fix/percentile-timer-negative-and-batch

Conversation

@reginaldalfret

Copy link
Copy Markdown

Summary

In PercentileTimer, negative durations were unconditionally passed to bucket resolution and incremented the bucket count for min (bucket 0 by default), while the underlying Timer dropped them according to the Timer contract. This caused the sum of bucket counts to exceed timer.count() and artificially skewed percentiles toward zero.

Additionally, PercentileTimer did not override Timer.record(long[] amounts, int n, TimeUnit unit), falling back to the default interface loop.

This change brings PercentileTimer in line with PercentileDistributionSummary (see #1303).

Root cause

PercentileTimer.record(long, TimeUnit) called:
java final long nanos = restrict(unit.toNanos(amount)); timer.record(amount, unit); counterFor(PercentileBuckets.indexOf(nanos)).increment();
When amount < 0, timer.record(amount, unit) dropped the value, but restrict(negative) clamped the nanos to min (0ns by default) and incremented bucket 0.

Changes

  1. spectator-api/.../PercentileTimer.java:
    • Guarded bucket counter increment with if (amount >= 0).
    • Implemented record(long[] amounts, int n, TimeUnit unit) for batch updates, matching PercentileDistributionSummary.record(long[], int).
  2. spectator-api/.../PercentileTimerTest.java:
    • Added negativeAmountIgnored test asserting that negative durations do not increment count() or any bucket counters.
    • Added batchRecordMatchesSingleRecord and batchRecordMatchesSingleRecordDefaultRange tests verifying that batched updates match single updates across zero, positive, and negative amounts.

…centileTimer

Signed-off-by: reginaldalfret <reginaldalfret@gmail.com>

This branch has not been deployed

No deployments
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