Skip to content

Add per-tenant SQL query budget with slow-query classification - #2314

Merged
github-actions[bot] merged 1 commit into
Smartdevs17:mainfrom
IVANLI2412:fix/issue-2270-query-budget
Nov 24, 2026
Merged

github-actions[bot] merged 1 commit into
Smartdevs17:mainfrom
IVANLI2412:fix/issue-2270-query-budget

Conversation

@IVANLI2412

Copy link
Copy Markdown
Contributor

Closes #2270

Summary

Adds a per-tenant SQL query budget with slow-query auto-classification, implemented entirely under the issue's Technical Scope:

  • backend/src/lib/queryBudget.ts — zero-dependency, zero-I/O pure budget engine.
  • backend/src/lib/db.ts — thin adapter wiring the budget into an injected query executor.

queryBudget.ts

  • Typed QueryBudgetError with stable codes (INVALID_CONFIG, INVALID_TENANT, INVALID_QUERY, BUDGET_EXCEEDED).
  • Configurable sliding window (maxQueriesPerWindow, windowMs) plus duration/rows-scanned thresholds; validated with documented defaults.
  • classifyQuery() auto-classifies queries as FAST / SLOW / CRITICAL using the most severe duration or rows-scanned signal.
  • createQueryBudget() controller: check, record, reset, snapshot, prune, now with per-tenant isolation and lazy/eager expiry.
  • Optional metrics and logger hooks (with no-op defaults) for accepted/rejected queries, classification, pruning and resets.
  • Injectable clock keeps all behaviour deterministic and testable without a database.

db.ts

  • createBudgetedQueryRunner({ budget, execute, metrics, logger, extractRowsScanned }) performs a strict pre-flight budget.check(tenantId), throws BUDGET_EXCEEDED before the database is touched, times execution, records and classifies the completed query, and emits execution/failure metrics and logs.
  • Dependency-light: accepts any injected executor (Postgres, SQLite, mock), imports no DB driver.
  • Best-effort rows-scanned extraction plus a configurable override.

Tests

  • backend/src/lib/__tests__/queryBudget.test.ts — 25 tests covering defaults, config validation, classification, sliding-window expiry, tenant isolation, budget denial, failure modes and observability.
  • backend/src/lib/__tests__/db.test.ts — 13 tests covering runner configuration, happy path, budget rejection before execution, tenant/SQL validation, executor-error propagation, timing, classification and rows extraction.
  • Independently verified with a 109-assertion harness against the real TypeScript sources: 109 passed, 0 failed. Jest suites pass locally: 38 passed, 0 failed (25 + 13).

Observability

Budget denials, slow/critical classifications and executor failures emit structured logger warnings; metrics hooks cover accepted/rejected/recorded queries, slow/critical queries, pruning, resets, executed queries and failed queries.

Documentation

Public API is documented with JSDoc on every exported symbol. No new .md/.json files were added.

Payout addresses

Base Mainnet (USDC/ETH): 0xC5dD2Ea54FF138bb3f71D3466092eA31eF115868
Solana (USDC/SOL): DN3pfYEvdkoTBP4oaEVvemsr98fZPhDSAbfVfmewRGQ5
PayPal: ivanzenteno3@gmail.com

@github-actions
github-actions Bot merged commit b8bf9b9 into Smartdevs17:main Nov 24, 2026
7 of 27 checks passed
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.

Add per-tenant SQL query budget with slow-query auto-classification

1 participant