mcp-context-forge icon indicating copy to clipboard operation
mcp-context-forge copied to clipboard

[BUG]: OBSERVABILITY_TRACE_RETENTION_DAYS and OBSERVABILITY_MAX_TRACES are documented and shipped but never enforced — observability traces grow unbounded

Open Shyrka973 opened this issue 4 months ago • 2 comments

🐞 Bug Summary

OBSERVABILITY_TRACE_RETENTION_DAYS (default 7) and OBSERVABILITY_MAX_TRACES (default 100000) are declared in mcpgateway/config.py, documented across six pages under docs/, listed in .env.example, and exposed in the official Helm chart — but neither is read anywhere in the codebase.

As a result, observability_traces and observability_spans are never pruned by any background task and grow without bound until the database volume fills up.

This is arguably worse than having no setting at all: a missing knob is noticeable, whereas a documented-but-dead knob gives operators a false assurance that retention is bounded. .env.example states the guarantee explicitly:

# Maximum number of traces to retain (prevents unbounded growth)
# OBSERVABILITY_MAX_TRACES=100000

🧩 Affected Component

  • [x] mcpgateway - API
  • [ ] mcpgateway - UI (admin panel)
  • [ ] mcpgateway.wrapper - stdio wrapper
  • [ ] Federation or Transports
  • [ ] CLI, Makefiles, or shell scripts
  • [ ] Container setup (Docker/Podman/Compose)
  • [x] Other — observability background services, docs, .env.example, and charts/mcp-stack

🔁 Steps to Reproduce

  1. Deploy with OBSERVABILITY_ENABLED=true (all other observability settings at their defaults).
  2. Optionally set OBSERVABILITY_TRACE_RETENTION_DAYS=1 and OBSERVABILITY_MAX_TRACES=1000 to make the effect visible sooner.
  3. Generate traffic for longer than the retention window.
  4. Query the database:
SELECT min(start_time), count(*) FROM observability_traces;

Rows older than the retention window are still present, and the row count exceeds OBSERVABILITY_MAX_TRACES.


🤔 Expected Behavior

Traces older than OBSERVABILITY_TRACE_RETENTION_DAYS should be deleted automatically, and the total number of retained traces should be capped at OBSERVABILITY_MAX_TRACES — as stated in the documentation and in .env.example.

Observed instead: retention is never enforced; the only way to prune is to call the manual endpoint yourself.


📓 Logs / Error Output

No error is produced — that is precisely the problem: the growth is silent.

Both identifiers appear only in configuration and documentation, never in application code:

Location Kind
mcpgateway/config.py declaration
docs/docs/manage/configuration.md, docs/docs/manage/observability.md, docs/docs/manage/observability/internal.md, docs/docs/manage/observability/internal-observability.md, docs/docs/architecture/explorer.html user-facing docs
.env.example sample config
charts/mcp-stack/values.yaml, charts/mcp-stack/README.md official Helm chart

delete_old_traces() exists and works, but its only non-test caller is the manual endpoint DELETE /observability/traces/cleanup (mcpgateway/routers/observability.py:386). Nothing invokes it on a schedule.

MetricsCleanupService does run correctly, but its METRIC_TABLES list (mcpgateway/services/metrics_cleanup_service.py:145) covers only tool_metrics, resource_metrics, prompt_metrics, server_metrics and a2a_agent_metrics — the observability tables are not included, so METRICS_RETENTION_DAYS has no effect on them either.

Sanity check on the search method: searching for metrics_retention_days (a setting that is wired up) returns five source files, so the absence above is a real finding and not a search artifact.


🧠 Environment Info

Key Value
Version or commit v1.0.6 (first observed on v1.0.5; both settings still unreferenced on main)
Runtime Python, official container image ghcr.io/ibm/mcp-context-forge
Platform / OS Kubernetes, Helm chart mcp-stack
Container Docker (OCI image)
Database PostgreSQL (CloudNativePG)

🧩 Additional Context

Where this was supposed to be implemented. EPIC #1401 specified exactly this behaviour under US-9: Platform Admin - Auto-Cleanup Old Traces, and the EPIC was closed with these items checked:

- [x] Implement scheduled cleanup job (APScheduler)
- [x] Delete traces older than retention days
- [x] Log cleanup actions (traces deleted, retention stats)
- [x] Run cleanup hourly or daily
- [x] Retention Policy: Automatic cleanup of old traces based on configuration

The scheduled job described there is not present in the codebase. Notably, the only unchecked item in that list is:

- [ ] Unit tests: Cleanup logic (retention, max traces) (3+ tests)

which plausibly explains why the gap went unnoticed for so long: nothing asserts that retention is actually applied.

Impact measured on a self-hosted deployment (single tenant, moderate traffic):

  • ~18,000 spans/day ≈ 26 MB/day
  • After 34 days: 485,182 traces / 486,722 spans, i.e. 903 MB of a 924 MB database
  • That is 4.85× the OBSERVABILITY_MAX_TRACES=100000 cap the configuration advertises
  • The volume reached 98% and the database replica went down

Purging a large backlog is itself hazardous, which makes early enforcement more valuable than a documented cap: the DELETE and the subsequent VACUUM both generate WAL, and if a replica falls behind, its now-inactive replication slot pins WAL and prevents recycling — a self-reinforcing loop.

Workaround. A scheduled job calling the existing endpoint, or a direct delete. One statement suffices, since spans cascade at the database level (fk_observability_spans_trace_id ... ON DELETE CASCADE):

DELETE FROM observability_traces WHERE start_time < now() - make_interval(days => 7);

Suggested fix — any of these closes the gap; the last one is worth doing regardless:

  1. Wire the settings up: add an ObservabilityCleanupService (mirroring MetricsCleanupService) honouring both OBSERVABILITY_TRACE_RETENTION_DAYS and OBSERVABILITY_MAX_TRACES, started from the same lifespan hook as the existing cleanup/rollup services (mcpgateway/main.py:1720). Deleting from observability_traces alone is sufficient thanks to the FK cascade.
  2. Or extend MetricsCleanupService to cover the observability tables.
  3. Or, if enforcement is intentionally out of scope, remove the two settings from config.py, .env.example, the Helm chart and the docs, and state plainly that trace retention is the operator's responsibility — so nobody relies on a guarantee that isn't there.

Adding the unit tests left unchecked in #1401 would prevent this class of regression: a test asserting that rows older than the retention window are gone would have failed from day one.

Shyrka973 avatar Jul 21 '26 23:07 Shyrka973

Superseded by #6387.

The architectural direction is to remove the unbounded observability_traces / observability_spans tables from the primary database entirely and replace them with a small, finite, pre-aggregated store for UX-card data. When that work lands, both OBSERVABILITY_TRACE_RETENTION_DAYS and OBSERVABILITY_MAX_TRACES will be deleted from config.py (along with all docs and chart references) rather than being wired up — making the dead-knob problem disappear rather than be fixed.

No work is needed on this issue independently; it will be closed when #6387 is complete.

jonpspri avatar Aug 24 '26 12:08 jonpspri

Closing as superseded by #6387. The architectural direction in #6387 removes the unbounded observability tables entirely rather than enforcing retention via OBSERVABILITY_TRACE_RETENTION_DAYS / OBSERVABILITY_MAX_TRACES. Once #6387 lands the underlying problem is resolved at the structural level.

jonpspri avatar Sep 07 '26 17:09 jonpspri