Skip to content

Add API key usage tracking (SEL-346) - #196

Closed
PetrefiedThunder wants to merge 5 commits into
port/api-841from
port/api-840
Closed

PetrefiedThunder wants to merge 5 commits into
port/api-841from
port/api-840

Conversation

@PetrefiedThunder

@PetrefiedThunder PetrefiedThunder commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator

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 to main when #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_id filter, limit 1..1000) in gateway/app/routers/api_keys.py, reading #841's daily view.
  • Regenerated gateway/docs/openapi.json (also exports #841's admin path), updated docs/api-key-usage.md.
  • #840's endpoint tests in 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_tracking middleware, services/api_key_usage.py writer, 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_revision 045_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.py now 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 passed
  • alembic heads: single head 046_api_key_usage_reporting
  • scripts/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/daily for bootstrap admins, reusing the existing api_key_usage_daily view with optional key_id and limit (default 100, max 1000). Daily reads are centralized in _read_api_key_usage_daily, which the existing /v1/admin/api-keys/usage/daily handler 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 and api-key-usage.md document both read paths.

Alembic head moves to 046_api_key_usage_reporting as a no-op revision (schema unchanged from 045); schema-046-rollout.md describes 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.

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

coderabbitai Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

🗂️ Base branches to auto review: 1
  • main

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

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
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-24T02:53:02.482607Z 96aa5cf PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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 PetrefiedThunder left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 PetrefiedThunder left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@PetrefiedThunder

Copy link
Copy Markdown
Collaborator Author

REVIEW: PASS deepseek 848bde3

Findings:

  • No blocking bugs or security issues.
  • The new daily-usage route enforces require_bootstrap_admin, binds key_id/limit as SQL parameters, and is registered before /{wallet_id}, so it isn't shadowed (gateway/app/routers/api_keys.py).
  • Migration 046 is an intentional, documented no-op. The changed tests use gateway fixtures, which this repo allows, and contain no fabricated identities, emails, credentials or metrics.

@PetrefiedThunder

Copy link
Copy Markdown
Collaborator Author

Closed as a duplicate of #195 (both port agent-middleware-api #840/#841), per C.Lee.

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