feat: Report served-request usage - #19
Conversation
Counts every request whose environment key resolves, aggregated per environment and resource, and flushes them to POST /proxy/usage/ every usage_flush_interval_seconds (default 60). Document fetches now carry X-Proxy-Key so core can exclude the proxy's own polls from API usage. Active only when proxy_key is configured; static config-file mode is unchanged. A final partial window is lost on shutdown (no graceful shutdown hook yet) — usage metering tolerates that.
A failed flush used to retry the identical batch forever, so any permanent rejection — a batch over the server's 1000-row cap, a revoked proxy key — silently ended usage reporting until restart. Flushes are now chunked to the server's cap and a rejected (4xx) chunk is dropped; only network errors and 5xx keep rows for the next flush. Also per review: a statically configured environment keeps its old billing end to end (its document fetch is unmarked and its served requests are not reported); counting moved into resolve_key(resource) so an SDK entry point cannot forget it; UsageCount renamed UsageRow; poll_usage renamed flush_usage_periodically; a zero flush interval now fails config validation instead of silently killing the task.
|
Important Review skippedNo new commits to review since the last review. ⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Plus Run ID: 📒 Files selected for processing (7)
Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review. 📝 WalkthroughWalkthroughThe change adds configurable usage flush intervals and public usage types. Estimated code review effort: 4 (Complex) | ~60 minutes Merge Risk: 🔵 Low · up to Transient failures may cause served-request usage batches to be submitted more than once, which could duplicate metering unless the ingestion endpoint deduplicates retries. The change is otherwise mergeable with explicit owner confirmation of that endpoint behavior. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Companion PRs for Flagsmith/flagsmith-private#256:
Merge/deploy order: #282 and 8407 (with the flagsmith-private pin bump) before any proxy build with #19 is deployed, else document polls double-count during the overlap. |
|
@coderabbitai review |
|
|
|
Changes
Contributes to Flagsmith/flagsmith-private#256
The proxy counts every SDK request it serves — aggregated per environment and resource, keyed by the canonical client key inside
resolve_key, so an entry point can't forget to count and unresolved keys never grow the map — and flushes toPOST {api_url}/proxy/usage/everyusage_flush_interval_seconds(default 60), authenticated by the proxy key.X-Proxy-Keyso core stops counting the proxy's own polls.proxy_keyis set; static config-file mode is byte-identical. The final partial window is lost on shutdown (no graceful-shutdown hook) — usage metering tolerates that.Stacked on #18. Companion PRs: usage ingestion endpoint (flagsmith-private) and the core middleware exclusion (links in the first comment).
How did you test this code?
92 tests (
cargo test), 9 new wiremock contract tests intests/test_usage_tracking.rs: aggregation across client/server keys through the full router; unresolved keys never counted; failed flush merges into the next (nothing lost, nothing doubled); rejected flush drops instead of retrying; 1001 environments chunk as 1000+1; static environment neither counted nor marked; flush inert without a proxy key. clippy + fmt clean.