Repository navigation
Add API key usage tracking (SEL-346) - #196
PetrefiedThunder wants to merge 5 commits into
Conversation
Port of archived agentmiddleware/agent-middleware-api#840, stacked on the #841 port. Migration 051 renumbered to 046 (down_revision 045_api_key_usage_events). Signed-off-by: Christopher <131207452+PetrefiedThunder@users.noreply.github.com>
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. 🗂️ Base branches to auto review: 1
Please check the settings in the CodeRabbit UI or the
|
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 96aa5cf1b6
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Signed-off-by: Christopher <131207452+PetrefiedThunder@users.noreply.github.com>
Signed-off-by: Christopher <131207452+PetrefiedThunder@users.noreply.github.com>
Signed-off-by: Christopher <131207452+PetrefiedThunder@users.noreply.github.com>
PetrefiedThunder
left a comment
There was a problem hiding this comment.
REVIEW: PASS amw-qa 36d30a3
QA PASS at 36d30a3. Port of agent-middleware-api#840, stacked on #195. 046_api_key_usage_reporting is a documented no-op revising 045; adds admin GET /v1/api-keys/usage/daily. Tested on this head (includes #195): gateway 5435 passed, 87 skipped; SDK 392 passed; migration test covers 046->045->044. Also applies cleanly onto a simulated main+#195 merge. Required CI green on this exact head (CI result, License result, gitleaks; Postgres proofs/concurrency pass). Note: api#840's request-tracking middleware is not ported (#195's auth-path recording covers it). Merge after #195 and retarget to main.
PetrefiedThunder
left a comment
There was a problem hiding this comment.
REVIEW: PASS grok 36d30a3
Stacked PR: base is port/api-841 (SEL-346, #195), not main; merge after #195. Adds admin-only GET /v1/api-keys/usage/daily reusing a refactored _read_api_key_usage_daily with a bound LIMIT (Query ge=1 le=1000, parameterized), mapping to PR #840 field names. require_bootstrap_admin enforced; tests cover 401 unauthenticated, 403 demo and normal keys, and key_id filter. Route is declared before /{key_id} handlers so no shadowing. Migration 046 is an intentional no-op; note it still bumps the required DB head for production boot, so deploy needs the 046-compatible image as the rollout doc states. Nit: two routes now serve the same view (admin and /v1/api-keys); fine but duplicated surface. CI 16 pass.
Posted with C.Lee's approval (Oct 23, 2026, 11:10 PM PT).
Signed-off-by: Christopher <131207452+PetrefiedThunder@users.noreply.github.com>
|
REVIEW: PASS deepseek 848bde3 Findings:
|
|
Closed as a duplicate of #195 (both port agent-middleware-api #840/#841), per C.Lee. |
Port of archived agentmiddleware/agent-middleware-api#840 (agentmiddleware/agent-middleware-api#840)
Stacked on #195 (port of agent-middleware-api#841, same ticket SEL-346). Base is
port/api-841; GitHub retargets this tomainwhen #195 merges. Merge #195 first.Changes
#840 and #841 were two implementations of SEL-346. With #841's table, model, middleware and writer already in place, this ports only what #840 adds:
GET /v1/api-keys/usage/daily(bootstrap admin;key_idfilter,limit1..1000) ingateway/app/routers/api_keys.py, reading #841's daily view.gateway/docs/openapi.json(also exports #841's admin path), updateddocs/api-key-usage.md.gateway/tests/test_api_key_usage_tracking.py, with the PR's own test values.Skipped as duplicates of #841 (conflicting schema): #840's event model/table,
api_key_usage_trackingmiddleware,services/api_key_usage.pywriter, auth context var, conftest entry. Skipped (no counterpart here): railway manifest example, test_demo_key_funnel / test_dispatch_reconcile_migration head bumps.Migration renumbering
051_api_key_usage_events->046_api_key_usage_reporting(down_revision045_api_key_usage_events). It is a no-op revision because 045 already owns the table, index and view. Rollout doc:gateway/docs/schema-046-rollout.md.test_api_key_usage_migration.pynow asserts head 046 and exercises 045 <-> 046.Tests run
pytest tests/test_api_key_usage_tracking.py tests/test_api_key_usage.py tests/test_api_key_usage_migration.py tests/test_migration_rollout_notes.py: 24 passedalembic heads: single head046_api_key_usage_reportingscripts/export_openapi.py --check: matches; ruff check/format clean. No live PostgreSQL run.Note
Low Risk
Read-only reporting behind bootstrap-admin auth, sharing the same SQL view as the existing admin route with no schema changes.
Overview
Adds
GET /v1/api-keys/usage/dailyfor bootstrap admins, reusing the existingapi_key_usage_dailyview with optionalkey_idandlimit(default 100, max 1000). Daily reads are centralized in_read_api_key_usage_daily, which the existing/v1/admin/api-keys/usage/dailyhandler now calls as well.The new route returns a flat list with archived PR #840 field names (
count_4xx,count_5xx,first_call,last_call) instead of the admin response model. OpenAPI andapi-key-usage.mddocument both read paths.Alembic head moves to
046_api_key_usage_reportingas a no-op revision (schema unchanged from 045);schema-046-rollout.mddescribes rollout/rollback. Migration and HTTP tests cover the new endpoint and auth (401/403).Reviewed by Cursor Bugbot for commit 848bde3. Bugbot is set up for automated code reviews on this repo. Configure here.