Skip to content

Conversation

@olamilekan000
Copy link
Contributor

Problem

In production environments with large datasets, Prometheus metrics collection was timing out due to expensive aggregation queries running directly against large tables. The queries were taking longer than the configured timeout, resulting in failed metric collections and incomplete monitoring data.
This often results into error like

Failed to collect metrics data: failed to query event queue metrics: timeout: context deadline exceeded

Solution

This PR introduces Materialized Views for metrics collection to significantly improve query performance, along with a configurable query timeout.

@olamilekan000 olamilekan000 force-pushed the add-materilized-views-for-prom-aggregatoion-queries branch from 9819daf to 0e488b2 Compare January 6, 2026 08:11
Comment on lines 40 to 71
CREATE MATERIALIZED VIEW IF NOT EXISTS convoy.event_queue_backlog_metrics_mv AS
WITH a1 AS (
SELECT ed.project_id,
COALESCE(e.source_id, 'http') AS source_id,
EXTRACT(EPOCH FROM (NOW() - MIN(ed.created_at))) AS age_seconds
FROM convoy.event_deliveries ed
LEFT JOIN convoy.events e ON e.id = ed.event_id
WHERE ed.status = 'Processing'
GROUP BY ed.project_id, e.source_id
LIMIT 1000 -- samples
)
SELECT * FROM a1
UNION ALL
Copy link
Collaborator

@jirevwe jirevwe Jan 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using UNION ALL here and in other queries might create duplicate entries when processing queue is empty

@jirevwe
Copy link
Collaborator

jirevwe commented Jan 6, 2026

We should run (using EXPLAIN ANALYSE) these queries against the US prod db to verify they use an index on the event deliveries table.

@jirevwe jirevwe force-pushed the add-materilized-views-for-prom-aggregatoion-queries branch from 09d0870 to 0e488b2 Compare January 6, 2026 13:14
@olamilekan000 olamilekan000 force-pushed the add-materilized-views-for-prom-aggregatoion-queries branch from 0e488b2 to c4269a8 Compare January 7, 2026 08:55
@olamilekan000 olamilekan000 requested a review from jirevwe January 7, 2026 08:56
@olamilekan000 olamilekan000 force-pushed the add-materilized-views-for-prom-aggregatoion-queries branch from c4269a8 to 219fa5e Compare January 7, 2026 09:07
@olamilekan000 olamilekan000 force-pushed the add-materilized-views-for-prom-aggregatoion-queries branch from 219fa5e to faa52af Compare January 7, 2026 11:16
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.

3 participants