fix: bound SQLite observability metric scans - #6390
Open
jstar0 wants to merge 2 commits into
Open
Conversation
Signed-off-by: King Star <mcxin.y@gmail.com>
jstar0
requested review from
Lang-Akshay,
brian-hussey,
crivetimihai,
ja8zyjits and
msureshkumar88
as code owners
August 24, 2026 13:40
a-effort
requested review from
gcgoncalves,
marekdano and
vishu-bh
and
a lite review from Copilot
August 25, 2026 15:28
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses unbounded memory growth on SQLite deployments for the public observability metrics endpoints by moving aggregation work into SQLite SQL (instead of Python .all() materialization), adding short-TTL caching keyed by query shape, and explicitly classifying these endpoints under a dedicated rate-limit tier.
Changes:
- Add SQLite-native SQL implementations for metrics timeseries and latency percentiles (window functions + interpolation) to avoid Python-side full-window materialization.
- Reuse
AdminStatsCachefor metrics results with query-specific cache keys (metric + hours + interval) and add test coverage for cache key isolation. - Add an explicit
OBSERVABILITY_METRICSrate-limit tier for/observability/metrics/*and/v1/observability/metrics/*and validate tier selection in middleware tests.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
mcpgateway/services/observability_service.py |
Adds SQLite SQL aggregation paths for timeseries and percentiles; keeps PostgreSQL and generic fallback intact. |
mcpgateway/routers/observability.py |
Adds per-query caching for metrics endpoints using AdminStatsCache keyed by metric/hours/interval. |
mcpgateway/cache/admin_stats_cache.py |
Extends observability stats cache API to accept an optional query-specific cache key. |
mcpgateway/middleware/rate_limit_middleware.py |
Introduces OBSERVABILITY_METRICS tier pattern to explicitly classify metrics endpoints. |
tests/unit/mcpgateway/routers/test_observability_metrics.py |
Adds fixtures to isolate cache across tests and adds a cache-key isolation test. |
tests/unit/mcpgateway/cache/test_admin_stats_cache.py |
Verifies the new optional cache_key parameter works for in-memory cache paths. |
tests/unit/mcpgateway/middleware/test_rate_limit_middleware.py |
Adds coverage ensuring metrics paths map to the new rate-limit tier. |
Suppressed comments (1)
mcpgateway/services/observability_service.py:1977
- Same compatibility concern here:
unixepoch()is not present in older SQLite versions, so this percentile query can error at runtime.strftime('%s', start_time)is widely supported and avoids a hard SQLite version dependency.
CAST(unixepoch(start_time) / :interval_seconds AS INTEGER) * :interval_seconds AS bucket_epoch,
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
This was referenced Aug 25, 2026
Signed-off-by: King Star <mcxin.y@gmail.com>
This was referenced Aug 27, 2026
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.
Summary
The public observability metrics endpoints currently use a Python fallback on SQLite that calls
.all()for every trace in the requested window. A 168-hour request can therefore materialize the full raw window in the API process.This change:
percentile_cont, without loading every duration into Python./observability/metrics/*and/v1/observability/metrics/*to an explicit rate-limit tier instead of the default fallback.The PostgreSQL path and the unsupported-dialect Python fallback remain unchanged.
Closes #6286
Verification
make testwith proxy environment variables cleared: 22533 passed, 870 skipped, 2 xfailed.ruff checkon changed production and focused test files: passed.ruff format --checkon changed files: passed.git diff --check: passed.start_timeindex is used for the bounded time filter.