feat(observability): add product metrics for API, outbox and delivery

This commit is contained in:
shira committed 2026-10-06 19:48:15 +03:00
1 parent 560724e2aa
commit 94488e2b57
51 files changed
+3655 -68

No files matched your search

@@ -80,6 +80,55 @@ func (q *Queries) GetDeliveryByID(ctx context.Context, id pgtype.UUID) (Delivery
return i, err
}
const getDeliveryStatistics = `-- name: GetDeliveryStatistics :many
SELECT
kind,
status,
count(*) AS deliveries,
min(next_attempt_at) FILTER (
WHERE status = 'pending'
)::timestamptz AS earliest_pending_attempt_at
FROM deliveries
GROUP BY kind, status
`
type GetDeliveryStatisticsRow struct {
Kind string
Status string
Deliveries int64
EarliestPendingAttemptAt pgtype.Timestamptz
}
// Aggregates the deliveries by kind and status for the metrics, at most once
// per metrics interval. It reads only kind, status, and next_attempt_at,
// never an address, a body, or an ID. No index covers status, so it scans
// the table; add an index on (status) or keep per-status counters before
// the table holds millions of rows.
func (q *Queries) GetDeliveryStatistics(ctx context.Context) ([]GetDeliveryStatisticsRow, error) {
rows, err := q.db.Query(ctx, getDeliveryStatistics)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetDeliveryStatisticsRow
for rows.Next() {
var i GetDeliveryStatisticsRow
if err := rows.Scan(
&i.Kind,
&i.Status,
&i.Deliveries,
&i.EarliestPendingAttemptAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getLatestDeliveryStatus = `-- name: GetLatestDeliveryStatus :one
SELECT
id,