feat(observability): add product metrics for API, outbox and delivery
This commit is contained in:
1 parent
560724e2aa
commit
94488e2b57
51 files changed
+3655
-68
No files matched your search
@@ -0,0 +1,59 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"lincodex/backend/internal/adapter/secondary/postgres/sqlc"
|
||||
"lincodex/backend/internal/domain"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
// DeliveryStatisticsStore reads the delivery aggregate behind the metrics on
|
||||
// a pool connection, with GetDeliveryStatistics.
|
||||
type DeliveryStatisticsStore struct {
|
||||
queries *sqlc.Queries
|
||||
}
|
||||
|
||||
func NewDeliveryStatisticsStore(pool *pgxpool.Pool) *DeliveryStatisticsStore {
|
||||
return &DeliveryStatisticsStore{queries: sqlc.New(standaloneDB{pool: pool})}
|
||||
}
|
||||
|
||||
var _ outbound.DeliveryStatisticsReader = (*DeliveryStatisticsStore)(nil)
|
||||
|
||||
func (store *DeliveryStatisticsStore) DeliveryStatistics(ctx context.Context) (outbound.DeliveryStatistics, error) {
|
||||
rows, err := store.queries.GetDeliveryStatistics(ctx)
|
||||
if err != nil {
|
||||
return outbound.DeliveryStatistics{}, fmt.Errorf("DeliveryStatisticsStore.DeliveryStatistics - read: %w", sanitizedDatabaseError(err))
|
||||
}
|
||||
statistics := outbound.DeliveryStatistics{Counts: make([]outbound.DeliveryStatusCount, 0, len(rows))}
|
||||
for _, row := range rows {
|
||||
count, err := deliveryStatusCount(row.Kind, row.Status, row.Deliveries)
|
||||
if err != nil {
|
||||
return outbound.DeliveryStatistics{}, fmt.Errorf("DeliveryStatisticsStore.DeliveryStatistics - read row: %w", err)
|
||||
}
|
||||
statistics.Counts = append(statistics.Counts, count)
|
||||
earliest := row.EarliestPendingAttemptAt
|
||||
if earliest.Valid && (statistics.EarliestPendingAttemptAt.IsZero() || earliest.Time.Before(statistics.EarliestPendingAttemptAt)) {
|
||||
statistics.EarliestPendingAttemptAt = earliest.Time
|
||||
}
|
||||
}
|
||||
return statistics, nil
|
||||
}
|
||||
|
||||
// deliveryStatusCount parses the stored kind and status, which the table
|
||||
// constraints already restrict, so a metric label never carries a value
|
||||
// outside the domain sets.
|
||||
func deliveryStatusCount(kind, status string, deliveries int64) (outbound.DeliveryStatusCount, error) {
|
||||
parsedKind, err := domain.ParseMailKind(kind)
|
||||
if err != nil {
|
||||
return outbound.DeliveryStatusCount{}, fmt.Errorf("postgres.deliveryStatusCount - parse kind: %w", err)
|
||||
}
|
||||
parsedStatus, err := domain.ParseDeliveryStatus(status)
|
||||
if err != nil {
|
||||
return outbound.DeliveryStatusCount{}, fmt.Errorf("postgres.deliveryStatusCount - parse status: %w", err)
|
||||
}
|
||||
return outbound.DeliveryStatusCount{Kind: parsedKind, Status: parsedStatus, Deliveries: deliveries}, nil
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user