database-observability

majiayu000's avatarfrom majiayu000

Instrument database queries, connection pools, and detect N+1 queries

⭐0stars🔀0forks📁View on GitHub🕐Updated Jan 5, 2026⬇️1downloads

When & Why to Use This Skill

This Claude skill provides comprehensive database observability by instrumenting SQL queries, monitoring connection pool health, and identifying critical performance bottlenecks. It enables developers and SREs to proactively detect N+1 query patterns, track connection wait times, and optimize slow database operations through standardized tracing and metrics collection.

Use Cases

  • Detecting N+1 query patterns in application code to reduce unnecessary database round-trips and improve overall response times.
  • Monitoring connection pool metrics such as active, idle, and wait counts to prevent service exhaustion and optimize resource allocation during peak traffic.
  • Identifying slow-performing SQL statements by analyzing query duration histograms and implementing necessary indexing or query optimizations.
  • Standardizing database instrumentation across microservices using query wrappers to ensure consistent observability and faster incident response.
namedatabase-observability
description"Instrument database queries, connection pools, and detect N+1 queries"
priority2

Database Observability

Database is often the bottleneck. Track queries, pools, and patterns.

Query Span Attributes

Attribute Example Required
db.system postgresql, mysql Yes
db.operation SELECT, INSERT Yes
db.name orders_db Yes
db.sql.table users Recommended
db.statement SELECT * FROM users WHERE id = ? Parameterized only!

Connection Pool Metrics (USE)

Metric Type Description
db.connections.active Gauge In use
db.connections.idle Gauge Available
db.connections.max Gauge Pool limit
db.connections.wait_count Counter Had to wait
db.connections.wait_duration Histogram Wait time

Issues to Detect

Issue Detection Fix
N+1 queries >10 identical queries per request Use eager loading
Slow queries Duration > p95 threshold Add indexes, optimize
Pool exhaustion wait_count increasing Increase pool, fix leaks

Query Wrapper Pattern

Before: Start span (db.system, db.operation, db.sql.table), start timer
After:  Record duration, set db.rows_affected, record errors, end span

Pool Monitor Pattern

Every 10s: Record active, idle, wait_count, wait_duration from pool stats

Anti-Patterns

  • Full SQL with values → PII risk, use parameterized only
  • No pool metrics → Can't detect saturation
  • Missing slow query alerts → Problems go unnoticed

References

  • references/methodology/use-methodology.md
  • references/platforms/{platform}/database.md