Skip to content

feat(analytics): sample Analytics Engine writes to cut per-write billing - #291

Draft
alukach wants to merge 1 commit into
source-cooperative:mainfrom
alukach:worktree-sample-analytics-engine
Draft

alukach wants to merge 1 commit into
source-cooperative:mainfrom
alukach:worktree-sample-analytics-engine

Conversation

@alukach

@alukach alukach commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

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_BURST requests in each minute are written in full. Past that, one in ANALYTICS_SAMPLE_EVERY is 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:

  • every error except 404
  • every signed read (GET or HEAD)

Sampled like anything else:

  • writes (PUT, DELETE, POST), which are signed but are the volume this bounds
  • 404s, which are routine for some products

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_count checks 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 have double4 = 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 sampleInterval weighting.

In the monitors' SQL dialect, SUM(x) is already weighted by sampleInterval, 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:

Old New
M2 errors / requests (3 h) 39 / 1,453,495 39 / 1,453,495
M3 403 share 0.0082% 0.0082%
M7 p99 1,031 ms 1,031 ms
M8 requests (1 h) 536,114 536,004 (window moved)

greatest() and toUInt64() aren't available in this dialect. The queries use if() and toUInt32().

Rollout

monitoring/monitors.json is applied on release, alongside the Worker deploy. The new queries work whether or not double4 exists, so the order doesn't matter.

Not measured yet

  • How many writes this removes. The state is per isolate, so the saving depends on how many isolates serve each product. Cutting writes to about 10–25% looks realistic, but I can't confirm it before deploy.
  • After deploy: SELECT SUM(_sample_interval) FROM source_data_proxy_production WHERE timestamp > NOW() - INTERVAL '1' DAY in 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, lower ANALYTICS_SAMPLE_BURST (a smaller allowance), or raise ANALYTICS_SAMPLE_EVERY.
  • M2's minimum_threshold of 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

  • 12 new unit tests in 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, and monitoring/test_apply.py pass.
  • ADR-008 and ADR-015 updated.

🤖 Generated with Claude Code

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

1 failed deployment
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