Skip to content

Size the usage collector's log read by counting, and fix quiet days - #1883

Merged
rdimitrov merged 2 commits into
mainfrom
rdimitrov-usage-collector-bounded-read
Nov 21, 2026
Merged

rdimitrov merged 2 commits into
mainfrom
rdimitrov-usage-collector-bounded-read

Conversation

@rdimitrov

@rdimitrov rdimitrov commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

The usage collector's first run in staging showed four problems. This fixes them. Only cmd/usage-collector and internal/usage change; there is no schema change, and the dashboards' view and functions are untouched.

What was wrong, and what changes

1. A day's read could be many times larger than intended

The fraction of a day's access records to read was worked out from the request counters, on the assumption that 1 in 10 successful listing records had been stored. That holds only while the storage rule in Cloud Logging applies. The rule thins out fast responses only, and it can be changed or switched off. On a day when listings are slow or the rule is off, almost every record is stored, and the read returned several times MAX_RECORDS, close to the pod's memory limit.

The collector now counts before it reads:

  1. It reads a small sample of the day and keeps none of it: 100 records for every MAX_RECORDS the day holds.
  2. From that count it picks the fraction that returns about MAX_RECORDS records. A day that holds fewer is read whole.
  3. That read is kept if it fits and is about as large as intended. Otherwise another fraction is tried, at most four more times:
    • A read that returns more than twice MAX_RECORDS is stopped. The next fraction is halfway between the largest known to fit and the smallest known not to.
    • A read that returns less than half of MAX_RECORDS, or no registry request, is not settled for. The next fraction is larger, chosen from what that read returned.

The rule behind step 3: what a sample returned only ever chooses the next fraction to try. Nothing is concluded from a sample about the rest of the day. A read that was stopped early is not projected onto the hours it did not reach, and a sample that came back small or empty is not taken to mean the day is small or empty. "No access records" is reported only after reading the whole day.

Step 3 matters only when samples are uneven. Samples are taken on the request ID, and the gateway logs the ID a client sends in X-Request-ID when it sends one, so requests that share an ID are all in a sample or all out of it.

  • If no fraction near the limit works, the largest sample known to fit is used, and the day's caveats say it could not be sampled evenly.
  • The same caveat is recorded when a kept sample is at odds with another read: a fraction returned too many records although, by the kept sample's own count, it would have fit.
  • If the largest sample that fits holds no registry request, the day fails with "could not be sampled". The records exist; the day cannot be collected at this limit.

The counters are no longer used to size the read, so a day without counters is bounded too.

Cost: at the default limit, one extra request for every million records the day holds, and one extra request for a small day. No attempt keeps more than twice MAX_RECORDS records in memory. A day with very uneven samples can take up to six reads.

2. Requests ran into the rate limit

The reader asked for pages faster than the project's limit of 60 read requests a minute and was refused every two minutes. Requests are now spaced evenly, retries included and across days. The wait comes immediately before a request is sent, after anything else that takes time, such as a new token. The new setting USAGE_COLLECTOR_LOG_READS_PER_MINUTE defaults to 50, which leaves room for anyone else reading logs. No deployment change is needed.

3. On quiet days the data estimate was far below the counters

Listing records are scaled to the counters hour by hour. In an hour where every fast listing record was thinned out, no record was left to carry that hour's requests, so they and their large responses dropped out of the estimate. With little traffic most hours are like that.

Such an hour's requests now go to the nearest hour that kept a record (the earlier one on a tie). On a day where every hour kept a record, the arithmetic is unchanged.

A quiet day's data estimate is still noisy, because a handful of records stand for all the listing data. It now scatters around the counters and is no longer consistently low.

4. A day with nothing to collect failed the run

A day is now skipped, without failing the run, when the gateway's request counter has series that day, they show no registry request, and none of the day's access records is a registry request.

The last condition is checked by reading every record of the day, keeping none. A sample without a registry request would not show that there was none. A day with more than twice MAX_RECORDS records is not skipped, because not all of them are looked at.

Two cases still fail, on purpose:

  • Neither source holds anything at all. That cannot be told from a collector pointed at the wrong place.
  • The counters counted requests but the whole day holds no access record of one. The error now says how many requests were counted and points at the log settings.

How it was tested

  • Made-up days (sizing_internal_test.go), each too large to read whole, collected with a limit of 2,000 records:

    Day Records stored Earlier rule would read Now read
    Fast listings thinned to 1 in 10 all day 14,127 2,260 2,041
    Thinned in the first half of the day only 37,099 5,898 1,967
    Nothing thinned out 60,000 9,540 1,999

    In all three the sample still adds up to the counters, to within the tolerance the test allows.

  • Uneven samples (TestUnevenSamples), each a day where many records share one request ID:

    • a burst in the first second that no sample can hold: the day is read below it and says so;
    • a sample that comes back nearly empty: it is read again, and turns out to fit whole;
    • a day that fits whole behind a small first read: the whole day is read;
    • a sizing sample that is known to fit, when everything larger returns too many: it is fallen back on;
    • registry requests that are rare among dashboard records: one larger read, then a "could not be sampled" failure;
    • a day where every record shares one ID: fails as "could not be sampled", never as "no records".
  • Days without registry traffic (TestDayWithoutRegistryTraffic): skipped only after an unsampled read, including a day whose one registry request a sample would have missed.

  • Pacing: a test with a controlled clock asserts when each request is sent, including after a slow token and from one read to the next.

  • Each rule is pinned: removing any one of them makes a test fail.

  • Golden files: the Python prototype this package replaced was run on the fixtures again, with the two corrections that touch its arithmetic applied to it in memory by a second, independent implementation. The Go output matches it byte for byte.

    • Only the subsampled scenario's expected output changed, because its fraction is now measured.
    • A new quiet scenario has hours that kept no fast listing record.
    • Every other scenario's files are unchanged, which shows nothing else moved.
  • Live, read-only, nothing stored: every day of the staging look-back window was collected with the first version of this change, and the two heavy days and one quiet day again with the final one.

    • The two heavy days were read at about the limit; before, they were read at about nine times the limit.
    • No request was refused for rate.
    • Peak memory stayed under 300 MiB.
    • The data estimate on the quiet days went from about half of the counters to scattered around them.
  • One production day, also read-only, with the first version: the report is byte-identical to the corrected prototype's report for the same day.

  • go test ./internal/usage passes, also with -race; coverage 90.5%. golangci-lint v2.14.0 reports 0 issues. make validate passes.

Not changed

The counter queries use increase(). In VictoriaMetrics that function leaves out the first value of a series that first appears with a large value, so a short burst that creates a new series is under-counted. The log-based ratio stored with each day shows when that happens. increase_pure() would count such bursts, but it is specific to VictoriaMetrics and would over-count wherever earlier samples of a series are missing, such as at the edge of the retention period. In production the two gave the same totals on every day checked, so the query is left as it is.

Requests that share a request ID also carry the same weight as any other record in a sample, so a large group that is in the sample is over-counted and one that is out of it is missing. The day's existing caveats (a repeated request ID; a log-based estimate far from the counters) flag that; this change does not try to correct it.

Not verified

  • The paths for uneven samples and for skipped days have run on fixtures only. No real day has needed either.
  • The build has not run in a cluster. The live runs were from a workstation with --no-store.

Days already stored by the first run keep their figures until they are collected again by naming them with --day.

The first run in staging showed four problems.

The read for a day was sized from the request counters on the assumption
that one in ten successful listing records had been stored. The storage
rule only thins out fast responses and can be switched off, so on some
days nearly every record is stored and the read returned many times the
intended number of records, close to the pod's memory limit. The collector
now counts a small sample of the day's records first and picks the
fraction from that count. A read that still returns more than twice the
limit is stopped and repeated at a smaller fraction.

Requests to Cloud Logging ran into the project's rate limit every two
minutes. They are now spaced to stay under it, with a setting for the
rate.

On days with little traffic the data estimate was far below the counters.
Listing records are scaled to the counters hour by hour, and an hour in
which every fast listing record was thinned out had no record to carry
its requests. Those requests now go to the nearest hour that kept a
record.

A day on which the gateway ran but served the registry nothing is no
longer a failure. A day whose counters show requests but that has no
access records still is, with a message that points at the log settings.

Co-authored-by: Copilot App <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Critical sampling flaws can misclassify existing traffic as absent, while request pacing can still allow Cloud Logging requests to exceed the configured quota.

3 open issues
What changed in this PR

Improves the registry’s usage collector by sizing reads from stored log records, pacing Cloud Logging requests, and correcting quiet-day handling. The storage schema and dashboard queries remain unchanged.

Changes:

  • Adds count-based sampling and bounded retries for oversized reads.
  • Introduces configurable request pacing across pages, retries, and days.
  • Redistributes quiet-hour listing counts, skips confirmed traffic-free days, and expands regression coverage.
File Description
internal/​usage/​testdata/​scenarios/​subsampled/​report.md Updates expected output for measured sampling.
internal/​usage/​testdata/​scenarios/​quiet/​scenario.json Defines the quiet-day scenario.
internal/​usage/​testdata/​scenarios/​quiet/​rows.json Adds expected quiet-day storage rows.
internal/​usage/​testdata/​scenarios/​quiet/​report.md Adds the expected quiet-day report.
internal/​usage/​testdata/​README.md Documents fixtures and prototype corrections.
internal/​usage/​testdata/​entries/​quiet.jsonl Adds synthetic low-traffic access records.
internal/​usage/​testdata/​entries/​quiet.counters.json Adds matching daily and hourly counters.
internal/​usage/​sources.go Adds pacing for log requests and retries.
internal/​usage/​sources_internal_test.go Tests request pacing across reads.
internal/​usage/​sizing_internal_test.go Tests sizing, overflow, idle days, and quiet-hour scaling.
internal/​usage/​settings.go Adds and validates the log-read rate setting.
internal/​usage/​run.go Wires pacing and handles traffic-free days.
internal/​usage/​run_internal_test.go Tests skipped days and revised collection behavior.
internal/​usage/​networks_internal_test.go Extends settings validation coverage.
internal/​usage/​golden_internal_test.go Updates expected sizing and sampling filters.
internal/​usage/​fixtures_internal_test.go Adds deterministic sampling to the fake log API.
internal/​usage/​collect.go Implements read sizing, bounded retries, and quiet-day handling.
cmd/​usage-collector/​README.md Documents sampling, pacing, scaling, and skipped days.

Review effort: Balanced

Comment thread internal/usage/collect.go Resolved
Comment thread internal/usage/collect.go Resolved
Comment thread internal/usage/sources.go Resolved
Review of the previous commit found three defects, two of them cases of
a sample being trusted for more than it shows.

A day was skipped as having no registry traffic when a read held no
registry request. That read could be a sample, which proves nothing
about the records it left out. The check now reads every record of the
day, keeping none, and does not skip a day it cannot read whole.

A read that returned too many records was sized again by projecting
where it had stopped onto the rest of the day. A burst in the first
second made the next sample so small that it came back empty, and the
day failed as having no records. Now what a sample returned only ever
chooses the next fraction to try: halfway between the largest fraction
known to fit and the smallest known not to, or larger when a read came
back small or without a registry request. A read is kept on what it
returned itself. "No access records" is reported only after reading the
whole day; a day that cannot be sampled fails as that.

Samples are taken on the request ID, and the gateway logs the ID a
client sends, so requests sharing one are all in a sample or all out of
it. When that keeps any sample near the limit from fitting, the largest
one known to fit is used and the day is marked as unevenly sampled. So
is a day whose kept sample is at odds with another read.

Requests to Cloud Logging were paced before the token was obtained, so
a slow token let two requests go out close together. The wait now comes
immediately before a request is sent.

Co-authored-by: Copilot App <[email protected]>
@rdimitrov
rdimitrov merged commit 3be2fcd into main Nov 21, 2026
14 checks passed
@rdimitrov
rdimitrov deleted the rdimitrov-usage-collector-bounded-read branch November 21, 2026 00:00
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.

2 participants