Repository navigation
Size the usage collector's log read by counting, and fix quiet days - #1883
Merged
Merged
Conversation
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]>
Contributor
There was a problem hiding this comment.
🟡 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
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]>
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.

The usage collector's first run in staging showed four problems. This fixes them. Only
cmd/usage-collectorandinternal/usagechange; 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:
MAX_RECORDSthe day holds.MAX_RECORDSrecords. A day that holds fewer is read whole.MAX_RECORDSis stopped. The next fraction is halfway between the largest known to fit and the smallest known not to.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-IDwhen it sends one, so requests that share an ID are all in a sample or all out of it.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_RECORDSrecords 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_MINUTEdefaults 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_RECORDSrecords is not skipped, because not all of them are looked at.Two cases still fail, on purpose:
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: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: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.
subsampledscenario's expected output changed, because its fraction is now measured.quietscenario has hours that kept no fast listing record.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.
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/usagepasses, also with-race; coverage 90.5%.golangci-lintv2.14.0 reports 0 issues.make validatepasses.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
--no-store.Days already stored by the first run keep their figures until they are collected again by naming them with
--day.