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,
@@ -99,6 +99,12 @@ type Querier interface {
// after it.
GetCurrentConsentText(ctx context.Context, arg GetCurrentConsentTextParams) (ConsentText, error)
GetDeliveryByID(ctx context.Context, id pgtype.UUID) (Delivery, error)
// 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.
GetDeliveryStatistics(ctx context.Context) ([]GetDeliveryStatisticsRow, error)
// Among the versions of a family already effective at a time, the newest
// publication wins. A version scheduled for later therefore never undoes a
// version published after it. The unique (policy_key, version) index serves