Repository navigation
Conversation
Workers Analytics Engine bills each data point written, whether or not it is stored, so its own adaptive sampling does not reduce the bill. At ~1.7B requests a month that is ~$425/month once billing starts (it has not yet). Per product and per isolate, the first ANALYTICS_SAMPLE_BURST requests in each minute are written in full; past that, one in ANALYTICS_SAMPLE_EVERY is written, with that number as a new double4 sample weight. Errors other than 404 and signed reads are never sampled. Unset, nothing is sampled (staging and previews); production sets 10 and 5. The monitors that counted rows (M2, M3, M8) now sum the weight, and M7 folds it into its weighted p99, so errors, which are always kept, are not set against a thinned total. Checked against the live dataset: the new forms return the same values as the old before any weight exists. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This branch had an error being deployed
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.
What
The proxy now samples requests before writing them to Analytics Engine, and records each row's weight in a new
double4.Per product and per isolate, the first
ANALYTICS_SAMPLE_BURSTrequests in each minute are written in full. Past that, one inANALYTICS_SAMPLE_EVERYis written, with that number as its weight. Production sets 10 and 5. Unset means no sampling, which is how staging and previews run.Never sampled:
Sampled like anything else:
Why
Analytics Engine bills each data point written, not each one stored, and its own adaptive sampling only thins storage. Writes are about 1.7B a month, which is about $425/month at the published $0.25 per million over 10 million. Billing hasn't started and Cloudflare gives no date, so this avoids a future charge. It doesn't reduce today's bill.
Two products account for most of what's avoidable. One product's benchmark runs made over 100 million PUTs and DELETEs in two days (Nov 15 and 16). Another product's 404s are about 9% of all requests.
How a count stays right
A row written at weight 10 stands for 10 requests, and one written at weight 1 for one, so the weights sum to the true request count in expectation. The test
weights_sum_to_the_true_request_countchecks this over 200,000 requests (within 1%).Because a quiet product never leaves its burst allowance, it's recorded in full, and only hot products are thinned. That keeps the long-tail visibility that ADR-008 rejected request-level sampling for losing.
To count requests, query
SUM(if(double4 > 1, double4, 1.0)). Rows from before this change havedouble4= 0, which reads as 1.Monitors
M2, M3 and M8 counted rows, which would read low once sampled. M2 would also read its error rate about ten times too high, since errors are always kept but the total is thinned. They now sum the weight, and M7's p99 folds it into its existing
sampleIntervalweighting.In the monitors' SQL dialect,
SUM(x)is already weighted bysampleInterval, so the weight is not multiplied by it. Multiplying it in would double-count (1.46M against a true 0.54M for the same hour).I ran the old and new forms against the live dataset (before any weight exists) and they return identical values:
greatest()andtoUInt64()aren't available in this dialect. The queries useif()andtoUInt32().Rollout
monitoring/monitors.jsonis applied on release, alongside the Worker deploy. The new queries work whether or notdouble4exists, so the order doesn't matter.Not measured yet
SELECT SUM(_sample_interval) FROM source_data_proxy_production WHERE timestamp > NOW() - INTERVAL '1' DAYin the Analytics Engine SQL API counts data points written. Compare it with the proxy's request count for the same day. If the saving falls short, lowerANALYTICS_SAMPLE_BURST(a smaller allowance), or raiseANALYTICS_SAMPLE_EVERY.minimum_thresholdof 1000. I haven't confirmed whether it counts weighted requests or stored rows. If rows, a quiet night's 10-minute window should still clear it, but check the first quiet night.Checks
tests/analytics.rs(burst allowance, window renewal, clock stepping back, per-product isolation, always-kept classes, bounded state, config parsing, unbiased weights).cargo fmt, wasm clippy with-D warnings, wasm check,cargo test, andmonitoring/test_apply.pypass.🤖 Generated with Claude Code