Skip to content

Conversation

@smoreinis
Copy link
Collaborator

Summary

  • Add optional performance logging for all PostgreSQL CRUD operations in PostgresCRUDRepository
  • Track query execution time, detect slow queries, and emit StatsD metrics
  • Support both global configuration via environment variables and per-query opt-in

Changes

  • New utility: src/utils/postgres_perf_logging.py - Performance logging with QueryType enum, QueryMetrics dataclass, and PostgresPerformanceLogger class
  • Environment variables: Added 3 new config options:
    • POSTGRES_PERF_LOGGING_ENABLED (default: false)
    • POSTGRES_SLOW_QUERY_THRESHOLD_MS (default: 100ms)
    • POSTGRES_PERF_LOGGING_SAMPLE_RATE (default: 1.0)
  • CRUD methods: Added optional perf_logging parameter to all methods (create, get, list, update, delete, etc.)
  • StatsD metrics: postgres.query.duration (histogram), postgres.query.count, postgres.query.slow (counters)
  • Unit tests: 17 new tests covering all functionality

Usage

Enable globally:

POSTGRES_PERF_LOGGING_ENABLED=true
POSTGRES_SLOW_QUERY_THRESHOLD_MS=50

Per-query override:

# Force enable
agent = await agent_repo.get(id="123", perf_logging=True)

# Force disable for high-frequency queries
result = await repo.list(filters={...}, perf_logging=False)

Test plan

  • All 17 new unit tests pass
  • All 196 existing unit tests pass
  • Manual testing with POSTGRES_PERF_LOGGING_ENABLED=true

Add optional performance logging for all PostgreSQL CRUD operations with:
- Query execution time tracking
- Slow query detection with configurable threshold
- StatsD metrics emission (histogram for duration, counters for count/slow)
- JSON-formatted logs compatible with Datadog

Configuration via environment variables:
- POSTGRES_PERF_LOGGING_ENABLED (default: false)
- POSTGRES_SLOW_QUERY_THRESHOLD_MS (default: 100ms)
- POSTGRES_PERF_LOGGING_SAMPLE_RATE (default: 1.0)

Per-query opt-in via `perf_logging` parameter on all CRUD methods.
…e repository

Remove environment_variables parameter from all PostgresCRUDRepository
subclasses. The base class now directly accesses GlobalDependencies()
singleton, eliminating boilerplate parameter passing through the
constructor chain.

This reduces code by ~35 lines while maintaining the same functionality.
The base class now handles environment variable resolution via
GlobalDependencies singleton, so repository subclasses don't need changes.
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.

2 participants