Skip to content

feat: Report served-request usage - #19

Open
gagantrivedi wants to merge 5 commits into
feat/proxy-config-discoveryfrom
feat/usage-tracking
Open

feat: Report served-request usage#19
gagantrivedi wants to merge 5 commits into
feat/proxy-config-discoveryfrom
feat/usage-tracking

Conversation

@gagantrivedi

Copy link
Copy Markdown
Member

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 to POST {api_url}/proxy/usage/ every usage_flush_interval_seconds (default 60), authenticated by the proxy key.

  • Flushes are chunked to the server's 1000-row cap. A rejected (4xx) chunk is dropped — retrying can't heal a rejection; a failed (5xx/network) chunk is kept for the next flush.
  • Document fetches carry X-Proxy-Key so core stops counting the proxy's own polls.
  • Statically configured environments keep their old billing end to end: their fetches are unmarked, their served requests unreported.
  • Active only when proxy_key is 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 in tests/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.

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.
@coderabbitai

coderabbitai Bot commented Aug 29, 2026

Copy link
Copy Markdown

Review Change Stack

Important

Review skipped

No new commits to review since the last review.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 3006ff42-747e-41ec-bda6-ec434ca06fe8

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: ad385c8f-0e1e-4a38-8bd0-44bda84104f5

📥 Commits

Reviewing files that changed from the base of the PR and between 90f27dd and 36d7cdd.

📒 Files selected for processing (7)
  • src/config/settings.rs
  • src/environments.rs
  • src/lib.rs
  • src/main.rs
  • src/services/environment.rs
  • src/usage.rs
  • tests/test_usage_tracking.rs

Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

The change adds configurable usage flush intervals and public usage types. EnvironmentService records dynamic-environment requests by client key and resource, excludes static environments, and applies proxy authentication to dynamic document requests. It batches usage submissions, retains retryable failures, drops client-error batches, and flushes periodically. Unit and integration tests cover aggregation, serialisation, request tracking, retries, batching, static environments, and proxy-key headers.

Estimated code review effort: 4 (Complex) | ~60 minutes

Merge Risk: 🔵 Low · up to 36d7c

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@gagantrivedi

Copy link
Copy Markdown
Member Author

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.

@gagantrivedi
gagantrivedi marked this pull request as ready for review August 29, 2026 10:11
@gagantrivedi

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Aug 29, 2026

Copy link
Copy Markdown
⚠️ Action not completed

Already reviewed the last commit. Use @coderabbitai full review to rerun a review of the entire changeset.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai

coderabbitai Bot commented Aug 29, 2026

Copy link
Copy Markdown

@gagantrivedi I will review the changes.

⚠️ Action not completed

Already reviewed the last commit. Use @coderabbitai full review to rerun a review of the entire changeset.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

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