veza/veza-backend-api/internal/database
senke ccf3e64d9a feat(observability): DB pool monitoring + N+1 detection (v1.0.10 ops item 11)
Two complementary signals : pool-side (do we have enough connections
for the load?) and per-request side (does any single handler quietly
run hundreds of queries?). Both feed Prometheus + Grafana + alert
rules.

Pool stats exporter (internal/database/pool_stats_exporter.go) :
- Background goroutine ticks every 15s and feeds the existing
  veza_db_connections{state} gauges. Before this, the gauges only
  refreshed when /health/deep was hit, so PoolExhaustionImminent
  evaluated against stale data.
- Wired into cmd/api/main.go alongside the ledger sampler with a
  shutdown hook for clean cancellation.

N+1 detector (internal/database/n1_detector.go +
internal/middleware/n1_query_counter.go) :
- Per-request *int64 counter attached to ctx by the gin
  middleware ; GORM after-callback (Query/Create/Update/Delete/
  Row/Raw) atomic-adds.
- Cost : one pointer load + one atomic add per query.
- Cardinality bounded by c.FullPath() (templated route, not URL).
- Threshold default 50, override via VEZA_N1_THRESHOLD.
- Histogram veza_db_request_query_count + counter
  veza_db_n1_suspicions_total.

Alerts in alert_rules.yml veza_db_pool_n1 group :
- PoolExhaustionImminent (in_use ≥ 90% for 5m)
- PoolStatsExporterStuck (gauges frozen for 10m despite traffic)
- N1QuerySpike (> 3% of requests over threshold for 15m)
- SlowQuerySustained (slow query rate > 2/min for 15m on same op+table)

Tests : 8 detector tests + 4 middleware tests, all pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-04 23:53:37 +02:00
..
chat_repository.go adding initial backend API (Go) 2025-12-03 20:29:37 +01:00
database.go v0.9.4 2026-03-05 23:03:43 +01:00
migration_rollback_test.go incus deployement fully implemented, Makefile updated and make fmt ran 2026-01-13 19:47:57 +01:00
migrations.go adding initial backend API (Go) 2025-12-03 20:29:37 +01:00
migrations_password_reset_test.go v0.9.4 2026-03-05 23:03:43 +01:00
migrations_sessions_test.go stabilizing veza-backend-api: phase 1 2025-12-16 11:23:49 -05:00
migrations_test.go refonte: backend-api go first; phase 1 2025-12-12 21:34:34 -05:00
n1_detector.go feat(observability): DB pool monitoring + N+1 detection (v1.0.10 ops item 11) 2026-05-04 23:53:37 +02:00
n1_detector_test.go feat(observability): DB pool monitoring + N+1 detection (v1.0.10 ops item 11) 2026-05-04 23:53:37 +02:00
performance_monitor.go incus deployement fully implemented, Makefile updated and make fmt ran 2026-01-13 19:47:57 +01:00
pool.go [BE-DB-015] be-db: Optimize database connection pooling 2025-12-24 15:53:19 +01:00
pool_stats_exporter.go feat(observability): DB pool monitoring + N+1 detection (v1.0.10 ops item 11) 2026-05-04 23:53:37 +02:00
pool_test.go report generation and future tasks selection 2025-12-08 19:57:54 +01:00
prepared_statements.go adding initial backend API (Go) 2025-12-03 20:29:37 +01:00