feat: alert on slow queries via pg_stat_statements#567
Open
BigJohn-dev wants to merge 1 commit into
Open
Conversation
|
@BigJohn-dev Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
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.
Closes #467
Summary
Adds a background worker that polls PostgreSQL's
pg_stat_statementsview and fires a webhook when any query'smean_exec_timeexceeds a configurable P95 latency threshold.Closes #467
Changes
New Files
src/workers/slowQueryAlerter.tspg_stat_statements, deduplicates by fingerprint, POSTs alertssrc/workers/slowQueryAlerter.test.tsdocs/slow-query-alerts.mdModified Files
src/config/env.tsSLOW_QUERY_ALERT_WEBHOOK_URL,SLOW_QUERY_P95_THRESHOLD_MS,SLOW_QUERY_POLL_INTERVAL_MS,SLOW_QUERY_DEDUP_WINDOW_SECONDSsrc/config/index.tsconfig.slowQueryAlertersectionsrc/index.tssrc/metrics.tsslow_query_alerter_runs_total,slow_query_alerter_alerts_total,slow_query_alerter_queries_above_threshold.env.exampleArchitecture
Acceptance Criteria
SLOW_QUERY_POLL_INTERVAL_MS)SLOW_QUERY_P95_THRESHOLD_MSmd5(query)) with configurable windowDrainableSubsystempatternWebhook Payload
{ "event": "slow_query_alert", "timestamp": "2025-01-01T00:00:00.000Z", "data": { "thresholdMs": 500, "queryCount": 2, "queries": [ { "fingerprint": "abc123def456", "querySample": "SELECT * FROM large_table WHERE ...", "calls": 1500, "meanExecTimeMs": 1234.56, "maxExecTimeMs": 8901.23, "rows": 100 } ] } }Configuration
SLOW_QUERY_ALERT_WEBHOOK_URLSLOW_QUERY_P95_THRESHOLD_MSmean_exec_timeabove this trigger alertSLOW_QUERY_POLL_INTERVAL_MSSLOW_QUERY_DEDUP_WINDOW_SECONDSTesting
npm run test:unit # full unit suite npx jest src/workers/slowQueryAlerter.test.ts --coverageTest Coverage
Prerequisites
The
pg_stat_statementsextension must be enabled on the database: