Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimized prometheus metrics collection from db #2179

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 42 additions & 26 deletions database/postgres/postgres_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,20 +208,27 @@ func (p *Postgres) collectMetrics() (*Metrics, error) {
}
metrics.EventQueueMetrics = eventQueueMetrics

backlogQM := `with a1 as (
select ed.project_id, coalesce(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, source_id limit 1000 --samples
backlogQM := `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
select ed.project_id, coalesce(source_id, 'http'), 0 as age_seconds
from convoy.event_deliveries ed left join convoy.events e on e.id = ed.event_id
where ed.status = 'Success' and source_id not in (select source_id from a1)
group by ed.project_id, source_id
limit 1000 -- samples`
SELECT * FROM a1
UNION ALL
SELECT ed.project_id,
COALESCE(e.source_id, 'http'),
0 AS age_seconds
FROM convoy.event_deliveries ed
LEFT JOIN convoy.events e ON e.id = ed.event_id
LEFT JOIN a1 ON e.source_id = a1.source_id
WHERE ed.status = 'Success' AND a1.source_id IS NULL
GROUP BY ed.project_id, e.source_id
LIMIT 1000; -- samples`
rows1, err := p.GetDB().Queryx(backlogQM)
if err != nil {
return nil, err
Expand Down Expand Up @@ -255,20 +262,29 @@ func (p *Postgres) collectMetrics() (*Metrics, error) {
}
metrics.EventDeliveryQueueMetrics = eventDeliveryQueueMetrics

backlogEQM := `with a1 as (
select ed.project_id, coalesce(source_id, 'http') as source_id, endpoint_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, source_id, endpoint_id limit 1000 --samples
backlogEQM := `WITH a1 AS (
SELECT ed.project_id,
COALESCE(e.source_id, 'http') AS source_id,
ed.endpoint_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, ed.endpoint_id
LIMIT 1000 -- samples
)
select * from a1
union all
select ed.project_id, coalesce(source_id, 'http'), endpoint_id, 0 as age_seconds
from convoy.event_deliveries ed left join convoy.events e on e.id = ed.event_id
where ed.status = 'Success' and endpoint_id not in (select endpoint_id from a1)
group by ed.project_id, source_id, endpoint_id
limit 1000 -- samples`
SELECT * FROM a1
UNION ALL
SELECT ed.project_id,
COALESCE(e.source_id, 'http'),
ed.endpoint_id,
0 AS age_seconds
FROM convoy.event_deliveries ed
LEFT JOIN convoy.events e ON e.id = ed.event_id
LEFT JOIN a1 ON ed.endpoint_id = a1.endpoint_id
WHERE ed.status = 'Success' AND a1.endpoint_id IS NULL
GROUP BY ed.project_id, e.source_id, ed.endpoint_id
LIMIT 1000; -- samples`
rows3, err := p.GetDB().Queryx(backlogEQM)
if err != nil {
return nil, err
Expand Down
15 changes: 15 additions & 0 deletions sql/1729941918.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- +migrate Up
CREATE INDEX IF NOT EXISTS idx_event_deliveries_status ON convoy.event_deliveries (status);
CREATE INDEX IF NOT EXISTS idx_event_deliveries_project_id_endpoint_id_status ON convoy.event_deliveries (project_id, endpoint_id, status);
CREATE INDEX IF NOT EXISTS idx_event_deliveries_project_id_event_id ON convoy.event_deliveries (project_id, event_id);
CREATE INDEX IF NOT EXISTS idx_event_deliveries_project_id_endpoint_id ON convoy.event_deliveries (project_id, endpoint_id);
CREATE INDEX IF NOT EXISTS idx_events_source_id ON convoy.events (source_id);
CREATE INDEX IF NOT EXISTS idx_events_project_id_source_id ON convoy.events (project_id, source_id);

-- +migrate Down
DROP INDEX IF EXISTS idx_event_deliveries_status;
DROP INDEX IF EXISTS idx_event_deliveries_project_id_endpoint_id_status;
DROP INDEX IF EXISTS idx_event_deliveries_project_id_event_id;
DROP INDEX IF EXISTS idx_event_deliveries_project_id_endpoint_id;
DROP INDEX IF EXISTS idx_events_source_id;
DROP INDEX IF EXISTS idx_events_project_id_source_id;
Loading