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

+3
View File
@@ -2,6 +2,9 @@ global:
scrape_interval: 30s
evaluation_interval: 30s
rule_files:
- /etc/prometheus/rules/*.yml
scrape_configs:
- job_name: prometheus
static_configs:
+129
View File
@@ -0,0 +1,129 @@
# Alert rules for the Lincodex backend product metrics. Prometheus evaluates
# them and shows their state on its /alerts page and to Grafana; no
# Alertmanager is configured, so nothing is sent to a person yet.
#
# Gauges that describe database-wide state are reported by every backend
# instance with the same value, so the rules aggregate them with max. Labels
# stay bounded: route templates, status codes, problem codes, consumer and job
# kind names, delivery kind and status, provider, and quota window.
groups:
- name: lincodex-api
rules:
- alert: LincodexBackendDown
expr: up{job="backend"} == 0
for: 2m
labels:
severity: critical
annotations:
summary: Prometheus cannot scrape the backend metrics endpoint.
description: The backend /metrics listener has not answered for 2 minutes; the API may be down as well.
- alert: LincodexApiHighServerErrorRate
# The second condition needs at least 15 requests in 5 minutes, so a
# single failure on a quiet stand does not page anyone.
expr: |
(
sum(rate(lincodex_http_requests_total{job="backend",status=~"5.."}[5m]))
/ sum(rate(lincodex_http_requests_total{job="backend"}[5m]))
> 0.05
)
and
sum(rate(lincodex_http_requests_total{job="backend"}[5m])) > 0.05
for: 10m
labels:
severity: warning
annotations:
summary: More than 5% of API requests fail with a server error.
description: Check lincodex_http_problems_total by route and code and the backend logs in Loki.
- name: lincodex-events
rules:
- alert: LincodexOutboxRelayLagging
expr: max(lincodex_outbox_relay_oldest_pending_age_seconds{job="backend"}) > 300
for: 10m
labels:
severity: warning
annotations:
summary: The earliest unpublished outbox event is older than 5 minutes.
description: The relay does not keep up or cannot publish to Kafka; compare with the published rate of lincodex_outbox_relay_events_total and read the relay warnings.
- alert: LincodexEventConsumerDeadLetter
expr: sum by (consumer) (increase(lincodex_event_consumer_records_total{job="backend",outcome="dead_lettered"}[15m])) > 0
labels:
severity: warning
annotations:
summary: Consumer {{ $labels.consumer }} dead-lettered an event.
description: The record needs an operator; its event ID is in the consumer error log.
- alert: LincodexEventConsumerStalled
expr: sum by (consumer, stage) (increase(lincodex_event_consumer_failures_total{job="backend",stage=~"poll|commit"}[5m])) > 0
for: 10m
labels:
severity: warning
annotations:
summary: Consumer {{ $labels.consumer }} keeps failing at {{ $labels.stage }}.
description: The consumer cannot read Kafka or store its offsets; records wait or will be delivered again.
- alert: LincodexScheduledJobDead
expr: sum by (kind) (increase(lincodex_scheduled_jobs_total{job="backend",outcome="dead"}[15m])) > 0
labels:
severity: warning
annotations:
summary: A scheduled job of kind {{ $labels.kind }} is dead.
description: The job used up its attempts and needs an operator.
- alert: LincodexScheduledJobsLagging
expr: max(lincodex_scheduled_jobs_oldest_due_age_seconds{job="backend"}) > 600
for: 15m
labels:
severity: warning
annotations:
summary: A due scheduled job has waited for more than 10 minutes.
description: No instance claims due jobs; the worker may be disabled, failing, or missing the handler of their kind.
- name: lincodex-mail
rules:
- alert: LincodexMailDeliveryUnknownOutcome
expr: sum(max by (kind) (lincodex_mail_deliveries{job="backend",status="unknown"})) > 0
for: 5m
labels:
severity: warning
annotations:
summary: Mail deliveries have an unknown outcome.
description: An attempt may have reached the provider without a conclusive answer; a retry needs the same idempotency key, and after the idempotency window an operator decides.
# Inert until the sending worker exists: TASK-016 must export
# lincodex_mail_sender_running (1 while its loop runs) as part of its
# definition of done. Without that series the rule matches nothing.
- alert: LincodexMailDeliveriesOverdue
expr: |
max(lincodex_mail_delivery_oldest_due_pending_age_seconds{job="backend"}) > 900
and on ()
max(lincodex_mail_sender_running{job="backend"}) == 1
for: 15m
labels:
severity: warning
annotations:
summary: A pending mail delivery has been due for more than 15 minutes.
description: The sending worker runs but does not pick up due deliveries; check its logs and the quota gauges.
- alert: LincodexMailMetricsStale
expr: time() - max(lincodex_mail_metrics_last_success_timestamp_seconds{job="backend"}) > 300
for: 5m
labels:
severity: warning
annotations:
summary: The delivery and quota gauges have not been read for more than 5 minutes.
description: The delivery metrics sampler cannot read PostgreSQL; the delivery and quota gauges show old values. Read its warnings in the backend logs.
- alert: LincodexMailQuotaUsedUp
expr: |
max by (provider, window) (lincodex_mail_quota_used_recipients{job="backend"})
>= max by (provider, window) (lincodex_mail_quota_limit_recipients{job="backend"})
or
max by (provider, window) (lincodex_mail_quota_provider_exhausted{job="backend"}) == 1
labels:
severity: info
annotations:
summary: The {{ $labels.window }} mail quota of {{ $labels.provider }} is used up.
description: New deliveries wait for the UTC window reset in status waiting_quota; nothing switches the plan or the transport automatically.
@@ -0,0 +1,243 @@
# Unit tests for rules/lincodex.yml; run with `make -C observability check`.
# Every alert has a firing case; guarded alerts also have a silent one.
rule_files:
- ../rules/lincodex.yml
evaluation_interval: 1m
tests:
- interval: 1m
input_series:
- series: lincodex_outbox_relay_oldest_pending_age_seconds{job="backend",instance="a"}
values: 400x20
- series: lincodex_outbox_relay_oldest_pending_age_seconds{job="backend",instance="b"}
values: '0x20'
alert_rule_test:
- eval_time: 5m
alertname: LincodexOutboxRelayLagging
exp_alerts: []
- eval_time: 15m
alertname: LincodexOutboxRelayLagging
exp_alerts:
- exp_labels:
severity: warning
exp_annotations:
summary: The earliest unpublished outbox event is older than 5 minutes.
description: The relay does not keep up or cannot publish to Kafka; compare with the published rate of lincodex_outbox_relay_events_total and read the relay warnings.
- interval: 1m
input_series:
- series: up{job="backend",instance="backend:9090"}
values: 1 1 0 0 0 0 0
alert_rule_test:
- eval_time: 3m
alertname: LincodexBackendDown
exp_alerts: []
- eval_time: 5m
alertname: LincodexBackendDown
exp_alerts:
- exp_labels:
severity: critical
instance: backend:9090
job: backend
exp_annotations:
summary: Prometheus cannot scrape the backend metrics endpoint.
description: The backend /metrics listener has not answered for 2 minutes; the API may be down as well.
- interval: 1m
input_series:
- series: lincodex_event_consumer_records_total{job="backend",consumer="mail_delivery",outcome="dead_lettered"}
values: 0 0 0 1 1 1
- series: lincodex_event_consumer_failures_total{job="backend",consumer="mail_delivery",stage="poll"}
values: 0+1x20
- series: lincodex_event_consumer_failures_total{job="backend",consumer="mail_delivery",stage="handler"}
values: 0+1x20
alert_rule_test:
- eval_time: 2m
alertname: LincodexEventConsumerDeadLetter
exp_alerts: []
- eval_time: 5m
alertname: LincodexEventConsumerDeadLetter
exp_alerts:
- exp_labels:
severity: warning
consumer: mail_delivery
exp_annotations:
summary: Consumer mail_delivery dead-lettered an event.
description: The record needs an operator; its event ID is in the consumer error log.
- eval_time: 5m
alertname: LincodexEventConsumerStalled
exp_alerts: []
- eval_time: 16m
alertname: LincodexEventConsumerStalled
exp_alerts:
- exp_labels:
severity: warning
consumer: mail_delivery
stage: poll
exp_annotations:
summary: Consumer mail_delivery keeps failing at poll.
description: The consumer cannot read Kafka or store its offsets; records wait or will be delivered again.
- interval: 1m
input_series:
- series: lincodex_scheduled_jobs_total{job="backend",kind="invitation.response_deadline",outcome="dead"}
values: 0 0 1 1 1
- series: lincodex_scheduled_jobs_oldest_due_age_seconds{job="backend",instance="a"}
values: 700x30
alert_rule_test:
- eval_time: 1m
alertname: LincodexScheduledJobDead
exp_alerts: []
- eval_time: 4m
alertname: LincodexScheduledJobDead
exp_alerts:
- exp_labels:
severity: warning
kind: invitation.response_deadline
exp_annotations:
summary: A scheduled job of kind invitation.response_deadline is dead.
description: The job used up its attempts and needs an operator.
- eval_time: 10m
alertname: LincodexScheduledJobsLagging
exp_alerts: []
- eval_time: 20m
alertname: LincodexScheduledJobsLagging
exp_alerts:
- exp_labels:
severity: warning
exp_annotations:
summary: A due scheduled job has waited for more than 10 minutes.
description: No instance claims due jobs; the worker may be disabled, failing, or missing the handler of their kind.
- interval: 1m
input_series:
- series: lincodex_http_requests_total{job="backend",method="GET",route="/api/v1/healthz",status="200"}
values: 0+10x20
- series: lincodex_http_requests_total{job="backend",method="GET",route="/api/v1/healthz",status="500"}
values: 0+5x20
alert_rule_test:
- eval_time: 20m
alertname: LincodexApiHighServerErrorRate
exp_alerts:
- exp_labels:
severity: warning
exp_annotations:
summary: More than 5% of API requests fail with a server error.
description: Check lincodex_http_problems_total by route and code and the backend logs in Loki.
- interval: 1m
input_series:
- series: lincodex_http_requests_total{job="backend",method="GET",route="/api/v1/healthz",status="200"}
values: '0x20'
- series: lincodex_http_requests_total{job="backend",method="GET",route="/api/v1/healthz",status="500"}
values: 0+1x20
alert_rule_test:
- eval_time: 8m
alertname: LincodexApiHighServerErrorRate
exp_alerts: []
- eval_time: 15m
alertname: LincodexApiHighServerErrorRate
exp_alerts: []
- interval: 1m
input_series:
- series: lincodex_mail_deliveries{job="backend",instance="a",kind="verification",status="unknown"}
values: 1x10
- series: lincodex_mail_deliveries{job="backend",instance="b",kind="verification",status="unknown"}
values: 1x10
- series: lincodex_mail_quota_used_recipients{job="backend",instance="a",provider="resend",window="day"}
values: 100x10
- series: lincodex_mail_quota_limit_recipients{job="backend",instance="a",provider="resend",window="day"}
values: 100x10
- series: lincodex_mail_quota_provider_exhausted{job="backend",instance="a",provider="resend",window="day"}
values: '0x10'
- series: lincodex_mail_quota_used_recipients{job="backend",instance="a",provider="resend",window="month"}
values: 120x10
- series: lincodex_mail_quota_limit_recipients{job="backend",instance="a",provider="resend",window="month"}
values: 3000x10
- series: lincodex_mail_quota_provider_exhausted{job="backend",instance="a",provider="resend",window="month"}
values: '0x10'
alert_rule_test:
- eval_time: 10m
alertname: LincodexMailDeliveryUnknownOutcome
exp_alerts:
- exp_labels:
severity: warning
exp_annotations:
summary: Mail deliveries have an unknown outcome.
description: An attempt may have reached the provider without a conclusive answer; a retry needs the same idempotency key, and after the idempotency window an operator decides.
- eval_time: 10m
alertname: LincodexMailQuotaUsedUp
exp_alerts:
- exp_labels:
severity: info
provider: resend
window: day
exp_annotations:
summary: The day mail quota of resend is used up.
description: New deliveries wait for the UTC window reset in status waiting_quota; nothing switches the plan or the transport automatically.
- interval: 1m
input_series:
- series: lincodex_mail_quota_used_recipients{job="backend",instance="a",provider="resend",window="month"}
values: 120x10
- series: lincodex_mail_quota_limit_recipients{job="backend",instance="a",provider="resend",window="month"}
values: 3000x10
- series: lincodex_mail_quota_provider_exhausted{job="backend",instance="a",provider="resend",window="month"}
values: 0 0 0 0 0 1 1 1 1 1
alert_rule_test:
- eval_time: 3m
alertname: LincodexMailQuotaUsedUp
exp_alerts: []
- eval_time: 7m
alertname: LincodexMailQuotaUsedUp
exp_alerts:
- exp_labels:
severity: info
provider: resend
window: month
exp_annotations:
summary: The month mail quota of resend is used up.
description: New deliveries wait for the UTC window reset in status waiting_quota; nothing switches the plan or the transport automatically.
- interval: 1m
input_series:
- series: lincodex_mail_delivery_oldest_due_pending_age_seconds{job="backend",instance="a"}
values: 2000x40
alert_rule_test:
- eval_time: 30m
alertname: LincodexMailDeliveriesOverdue
exp_alerts: []
- interval: 1m
input_series:
- series: lincodex_mail_delivery_oldest_due_pending_age_seconds{job="backend",instance="a"}
values: 2000x40
- series: lincodex_mail_sender_running{job="backend",instance="a"}
values: 1x40
alert_rule_test:
- eval_time: 10m
alertname: LincodexMailDeliveriesOverdue
exp_alerts: []
- eval_time: 20m
alertname: LincodexMailDeliveriesOverdue
exp_alerts:
- exp_labels:
severity: warning
exp_annotations:
summary: A pending mail delivery has been due for more than 15 minutes.
description: The sending worker runs but does not pick up due deliveries; check its logs and the quota gauges.
- interval: 1m
input_series:
- series: lincodex_mail_metrics_last_success_timestamp_seconds{job="backend",instance="a"}
values: '0x20'
alert_rule_test:
- eval_time: 5m
alertname: LincodexMailMetricsStale
exp_alerts: []
- eval_time: 12m
alertname: LincodexMailMetricsStale
exp_alerts:
- exp_labels:
severity: warning
exp_annotations:
summary: The delivery and quota gauges have not been read for more than 5 minutes.
description: The delivery metrics sampler cannot read PostgreSQL; the delivery and quota gauges show old values. Read its warnings in the backend logs.
- interval: 1m
input_series:
- series: lincodex_mail_metrics_last_success_timestamp_seconds{job="backend",instance="a"}
values: 0+60x20
alert_rule_test:
- eval_time: 15m
alertname: LincodexMailMetricsStale
exp_alerts: []