Repository navigation
feat: implement issue #767 - per-API-key usage metering service - #953
Merged
Merged
Conversation
Implement comprehensive API key usage metering service with hourly aggregation,
fire-and-forget middleware, and versioned developer API endpoints.
ARCHITECTURE:
- Model (APIKeyUsage.model.ts): hourly aggregation buckets with compound unique index
- Service (apiKeyUsage.service.ts): atomic upsert for concurrent safety, aggregation pipeline
- Middleware (metering.middleware.ts): res.on('finish') hook, fire-and-forget async recording
- Controller & Routes: GET /api/v1/developer/api-keys/:keyId/usage with ownership verification
- Environment: API_USAGE_RETENTION_DAYS, API_USAGE_MAX_QUERY_WINDOW_DAYS, API_USAGE_DEFAULT_RANGE_DAYS
FEATURES:
- Records endpoint, method, status code per API key, aggregated hourly into MongoDB
- Atomic upsert ensures concurrent requests never lose or duplicate counts
- Per-key time-range queries with totals, per-endpoint, per-status, and hourly breakdowns
- Date range validation, pagination, endpoint filtering
- Fire-and-forget metering: failures logged, never block or alter responses
- Secrets and request payloads never stored
- 90-day TTL index for automatic cleanup
TESTS:
- 12 unit tests: bucket creation, concurrency, hour boundaries, aggregation
- 9 integration tests: auth, ownership, date validation, filtering, pagination
- Comprehensive error handling and edge cases
FILES:
- backend/src/models/APIKeyUsage.model.ts (new)
- backend/src/services/apiKeyUsage.service.ts (new)
- backend/src/middlewares/metering.middleware.ts (new)
- backend/src/middlewares/apiKey.middleware.ts (enhanced)
- backend/src/controllers/developer.controller.ts (extended)
- backend/src/routes/developer.routes.ts (extended)
- backend/src/app.ts (integrated middleware)
- backend/.env.example (added config)
- backend/src/config/env.ts (added config parsing)
- backend/src/__tests__/services/apiKeyUsage.service.test.ts (new)
- backend/src/__tests__/integration/apiKeyUsage.integration.test.ts (new)
Owner
|
Thanks for the tasks implemented. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #765
Closes #766
Closes #767
Closes #768
What was done
Added integration test at
backend/src/services/__tests__/attestationMint.integration.test.tscovering the fullverify_attestation→buildMint→submit→confirmpipeline usingmongodb-memory-serverand injected soroban/SPV mocks.10 tests covering:
Added missing types to
backend/src/types/soroban.types.ts:TransactionFailureDiagnosticsSuccessfulTransactionStatus(required by
verificationWorker.tsand the existing unit test — both imported these types but the definitions were absent)Added missing oracle/worker env fields to
backend/src/config/env.ts:STELLAR_ORACLE_SECRET_KEYSTELLAR_PROVENANCE_CONTRACT_IDORACLE_CODE_MEASUREMENT_HASHSTELLAR_TX_CONFIRMATION_TIMEOUT_MSSTELLAR_TX_POLL_INTERVAL_MSSTELLAR_TX_MAX_CONSECUTIVE_RPC_ERRORSSPV_FETCH_TIMEOUT_MSSPV_MAX_MEDIA_BYTESSPV_MAX_MANIFEST_BYTESVERIFICATION_WORKER_POLL_INTERVAL_MSVERIFICATION_WORKER_BATCH_SIZEVERIFICATION_WORKER_MAX_ATTEMPTSVERIFICATION_WORKER_RETRY_BASE_MSVERIFICATION_WORKER_LEASE_MS(all referenced by
oracle.tsviaPick<typeof env, ...>but missing from theenvobject)Added
buildMintTransaction,submitTransaction, andgetTransactionWithConfirmationmethods toSorobanService(backend/src/services/soroban.service.ts), which were declared in the worker deps asPick<SorobanService, ...>but previously unimplemented on the class.Test results
All 10 integration tests pass. The existing 30
verificationWorkerunit tests also pass.