feat(notifications): send product notifications with a rights check right before each send

This commit is contained in:
shira committed 2026-10-09 20:38:39 +03:00
1 parent a856066316
commit 623d41b969
59 files changed
+4125 -1126

No files matched your search

@@ -36,6 +36,10 @@ type mailMetrics struct {
// per process, not database-wide, so dashboards and alerts aggregate it
// with max across instances.
senderRunning client.Gauge
// notificationSenderRunning is 1 while this process's product notification
// loop runs. It is separate from senderRunning, so the service loop's alert
// keeps its meaning.
notificationSenderRunning client.Gauge
// senderCancelled counts deliveries that the sending worker ended without
// sending, by cancel reason, a closed set from the domain.
senderCancelled *client.CounterVec
@@ -100,6 +104,10 @@ func newMailMetrics() mailMetrics {
Name: "lincodex_mail_sender_running",
Help: "1 while the mail sending loop of this process runs, 0 while it does not, for example with EMAIL_PROVIDER=disabled or after shutdown began.",
}),
notificationSenderRunning: client.NewGauge(client.GaugeOpts{
Name: "lincodex_notification_sender_running",
Help: "1 while the product notification sending loop of this process runs, 0 while it does not, for example with EMAIL_PROVIDER=disabled or after shutdown began.",
}),
}
// Every reason exists at zero, so an alert on an increase sees the first
// cancellation instead of a new series.
@@ -132,7 +140,7 @@ func (mail mailMetrics) collectors() []client.Collector {
return []client.Collector{
mail.deliveries, mail.oldestDuePendingAge, mail.lastSuccess,
mail.quotaUsed, mail.quotaLimit, mail.quotaProviderExhausted,
mail.senderRunning, mail.senderCancelled, mail.identityRateLimited,
mail.senderRunning, mail.notificationSenderRunning, mail.senderCancelled, mail.identityRateLimited,
}
}
@@ -182,6 +190,16 @@ func (metrics *Metrics) SetMailSenderRunning(running bool) {
metrics.mail.senderRunning.Set(value)
}
// SetNotificationSenderRunning reports whether this process's product
// notification loop runs: 1 from its start until its stop, 0 otherwise.
func (metrics *Metrics) SetNotificationSenderRunning(running bool) {
value := 0.0
if running {
value = 1
}
metrics.mail.notificationSenderRunning.Set(value)
}
// PrepareMailMetricsReads exposes the last success time at 0 when the
// sampler starts, so an alert sees a sampler that never reads successfully.
func (metrics *Metrics) PrepareMailMetricsReads() {
@@ -18,6 +18,19 @@ func TestMetricsHandlerExposesWhetherTheMailSenderRuns(t *testing.T) {
requireSeries(t, scrape(t, metrics), "lincodex_mail_sender_running 0")
}
func TestMetricsHandlerExposesWhetherTheNotificationSenderRuns(t *testing.T) {
metrics := New()
// The product loop has its own series, so the service loop's gauge keeps
// its meaning when only the product loop runs.
requireSeries(t, scrape(t, metrics), "lincodex_notification_sender_running 0")
metrics.SetNotificationSenderRunning(true)
requireSeries(t, scrape(t, metrics), "lincodex_notification_sender_running 1", "lincodex_mail_sender_running 0")
metrics.SetNotificationSenderRunning(false)
requireSeries(t, scrape(t, metrics), "lincodex_notification_sender_running 0")
}
func TestMetricsHandlerCountsCancelledMailByABoundedReason(t *testing.T) {
metrics := New()
// Every reason exists at zero before the first cancellation.
@@ -26,6 +39,9 @@ func TestMetricsHandlerCountsCancelledMailByABoundedReason(t *testing.T) {
`lincodex_mail_sender_cancelled_total{reason="content_invalid"} 0`,
`lincodex_mail_sender_cancelled_total{reason="processing_failed"} 0`,
`lincodex_mail_sender_cancelled_total{reason="token_revoked"} 0`,
`lincodex_mail_sender_cancelled_total{reason="rights_revoked"} 0`,
`lincodex_mail_sender_cancelled_total{reason="recipient_ineligible"} 0`,
`lincodex_mail_sender_cancelled_total{reason="source_closed"} 0`,
)
metrics.ObserveMailCancelled("content_invalid")
metrics.ObserveMailCancelled("content_invalid")