@@ -141,9 +141,8 @@ func (p *Postgres) Collect(ch chan<- prometheus.Metric) {
141141 // Return empty metrics to prevent blocking the endpoint
142142 metrics = & Metrics {}
143143 }
144- } else {
145- cachedMetrics = metrics
146144 }
145+ cachedMetrics = metrics
147146 }
148147
149148 // Use unique keys per metric type to prevent collisions
@@ -244,15 +243,24 @@ func (p *Postgres) Collect(ch chan<- prometheus.Metric) {
244243
245244// collectMetrics gathers essential metrics from the DB
246245func (p * Postgres ) collectMetrics () (* Metrics , error ) {
247- ctx , cancel := context .WithTimeout (context .Background (), time .Duration (metricsConfig .Prometheus .SampleTime )* time .Second )
246+ queryTimeout := time .Duration (metricsConfig .Prometheus .QueryTimeout ) * time .Second
247+ if queryTimeout == 0 {
248+ queryTimeout = 30 * time .Second
249+ }
250+ ctx , cancel := context .WithTimeout (context .Background (), queryTimeout )
248251 defer cancel ()
249252
250253 metrics := & Metrics {}
251254
252- queryEventQueueMetrics := "select distinct project_id, coalesce( source_id, 'http') as source_id, count(*) as total from convoy.events group by project_id, source_id "
255+ queryEventQueueMetrics := "SELECT project_id, source_id, total FROM convoy.event_queue_metrics_mv "
253256 rows , err := p .GetDB ().QueryxContext (ctx , queryEventQueueMetrics )
254257 if err != nil {
255- return nil , fmt .Errorf ("failed to query event queue metrics: %w" , err )
258+ log .Warnf ("materialized view query failed, falling back to direct query: %v" , err )
259+ queryEventQueueMetrics = "SELECT DISTINCT project_id, COALESCE(source_id, 'http') as source_id, COUNT(*) as total FROM convoy.events GROUP BY project_id, source_id"
260+ rows , err = p .GetDB ().QueryxContext (ctx , queryEventQueueMetrics )
261+ if err != nil {
262+ return nil , fmt .Errorf ("failed to query event queue metrics: %w" , err )
263+ }
256264 }
257265 defer closeWithError (rows )
258266 eventQueueMetrics := make ([]EventQueueMetrics , 0 )
@@ -266,7 +274,11 @@ func (p *Postgres) collectMetrics() (*Metrics, error) {
266274 }
267275 metrics .EventQueueMetrics = eventQueueMetrics
268276
269- backlogQM := `WITH a1 AS (
277+ backlogQM := "SELECT project_id, source_id, age_seconds FROM convoy.event_queue_backlog_metrics_mv"
278+ rows1 , err := p .GetDB ().QueryxContext (ctx , backlogQM )
279+ if err != nil {
280+ log .Warnf ("materialized view query failed, falling back to direct query: %v" , err )
281+ backlogQM = `WITH a1 AS (
270282 SELECT ed.project_id,
271283 COALESCE(e.source_id, 'http') AS source_id,
272284 EXTRACT(EPOCH FROM (NOW() - MIN(ed.created_at))) AS age_seconds
@@ -287,9 +299,10 @@ func (p *Postgres) collectMetrics() (*Metrics, error) {
287299 WHERE ed.status = 'Success' AND a1.source_id IS NULL
288300 GROUP BY ed.project_id, e.source_id
289301 LIMIT 1000; -- samples`
290- rows1 , err := p .GetDB ().QueryxContext (ctx , backlogQM )
291- if err != nil {
292- return nil , fmt .Errorf ("failed to query backlog metrics: %w" , err )
302+ rows1 , err = p .GetDB ().QueryxContext (ctx , backlogQM )
303+ if err != nil {
304+ return nil , fmt .Errorf ("failed to query backlog metrics: %w" , err )
305+ }
293306 }
294307 defer closeWithError (rows1 )
295308 eventQueueBacklogMetrics := make ([]EventQueueBacklogMetrics , 0 )
@@ -303,7 +316,21 @@ func (p *Postgres) collectMetrics() (*Metrics, error) {
303316 }
304317 metrics .EventQueueBacklogMetrics = eventQueueBacklogMetrics
305318
306- queryDeliveryQ := `SELECT DISTINCT
319+ queryDeliveryQ := `SELECT
320+ project_id,
321+ project_name,
322+ endpoint_id,
323+ status,
324+ event_type,
325+ source_id,
326+ organisation_id,
327+ organisation_name,
328+ total
329+ FROM convoy.event_delivery_queue_metrics_mv`
330+ rows2 , err := p .GetDB ().QueryxContext (ctx , queryDeliveryQ )
331+ if err != nil {
332+ log .Warnf ("materialized view query failed, falling back to direct query: %v" , err )
333+ queryDeliveryQ = `SELECT DISTINCT
307334 ed.project_id,
308335 COALESCE(p.name, '') as project_name,
309336 ed.endpoint_id,
@@ -319,9 +346,10 @@ func (p *Postgres) collectMetrics() (*Metrics, error) {
319346 LEFT JOIN convoy.organisations o ON p.organisation_id = o.id
320347 WHERE ed.deleted_at IS NULL
321348 GROUP BY ed.project_id, p.name, ed.endpoint_id, ed.status, ed.event_type, e.source_id, p.organisation_id, o.name`
322- rows2 , err := p .GetDB ().QueryxContext (ctx , queryDeliveryQ )
323- if err != nil {
324- return nil , fmt .Errorf ("failed to query delivery queue metrics: %w" , err )
349+ rows2 , err = p .GetDB ().QueryxContext (ctx , queryDeliveryQ )
350+ if err != nil {
351+ return nil , fmt .Errorf ("failed to query delivery queue metrics: %w" , err )
352+ }
325353 }
326354 defer closeWithError (rows2 )
327355 eventDeliveryQueueMetrics := make ([]EventDeliveryQueueMetrics , 0 )
@@ -335,7 +363,11 @@ func (p *Postgres) collectMetrics() (*Metrics, error) {
335363 }
336364 metrics .EventDeliveryQueueMetrics = eventDeliveryQueueMetrics
337365
338- backlogEQM := `WITH a1 AS (
366+ backlogEQM := "SELECT project_id, source_id, endpoint_id, age_seconds FROM convoy.event_endpoint_backlog_metrics_mv"
367+ rows3 , err := p .GetDB ().QueryxContext (ctx , backlogEQM )
368+ if err != nil {
369+ log .Warnf ("materialized view query failed, falling back to direct query: %v" , err )
370+ backlogEQM = `WITH a1 AS (
339371 SELECT ed.project_id,
340372 COALESCE(e.source_id, 'http') AS source_id,
341373 ed.endpoint_id,
@@ -358,9 +390,10 @@ func (p *Postgres) collectMetrics() (*Metrics, error) {
358390 WHERE ed.status = 'Success' AND a1.endpoint_id IS NULL
359391 GROUP BY ed.project_id, e.source_id, ed.endpoint_id
360392 LIMIT 1000; -- samples`
361- rows3 , err := p .GetDB ().QueryxContext (ctx , backlogEQM )
362- if err != nil {
363- return nil , fmt .Errorf ("failed to query endpoint backlog metrics: %w" , err )
393+ rows3 , err = p .GetDB ().QueryxContext (ctx , backlogEQM )
394+ if err != nil {
395+ return nil , fmt .Errorf ("failed to query endpoint backlog metrics: %w" , err )
396+ }
364397 }
365398 defer closeWithError (rows3 )
366399 eventQueueEndpointBacklogMetrics := make ([]EventQueueEndpointBacklogMetrics , 0 )
0 commit comments