diff --git a/README.md b/README.md index 9a8112d..a46121e 100644 --- a/README.md +++ b/README.md @@ -263,7 +263,7 @@ process registers: | `OpenWaf:Policy:ObservationRetentionDays` | `7` | how long a `source_observations` row survives past its last request. 1–365 whole days; anything else aborts startup naming the key, exactly as `RefreshTtlSeconds` does rather than falling back to 7 — a silently widened window keeps rows longer than the deployment asked for and nothing would say so. Surfaced as `observationRetentionDays` on `/admin-api/state` and `/admin-api/policy`, and as `rows are pruned after Nd` on the console. | | `OpenWaf:Policy:BanRetentionDays` | `7` | how long an expired engine ban and an alert stay readable after they stopped being live. Same 1–365 bound, same startup refusal, surfaced as `banRetentionDays` and `alerts are pruned after Nd`. Two keys rather than one because the two windows answer different questions, and their sharing 7 days was coincidence. Operator-written rows are never pruned by either window. | | `OpenWaf:Policy:SharedStreaks` | `false` | where the streak counters live. `false` is the in-process map: no I/O on the request path, and a threshold counted per instance. `true` moves the same rules onto a row in the database, so N edges mature one streak — the gap measured in `research/2026-10-01-multi-instance/` (four refusals split 2+2 over two edges at a threshold of 3 banned nobody). It costs one round trip per observed response and one optimistic-concurrency retry when two edges touch the same source at once. `/admin-api/state` answers `streakScope`, and `/admin-api/streaks` answers `scope` as `this-process` or `shared-database`, because a count is meaningless without the store it came from. | -| `OpenWaf:Alerts:WebhookUrl` (OPEN-53) | empty | where a raised alert is pushed. Empty is no push: the alert table stays the whole record, which is what it was. Set one URL and every alert the edge raises is POSTed there as JSON — `kind`, `sourceKey`, `count`, `lastStatusCode`, `lastPath`, `createdAt`, `detail` — from a **durable queue** — one row in `alert_pushes` per owed delivery, off the request path, written after the alert row is durable (OPEN-60). A failed push never undoes a row, and it is asked again on the schedule below until it lands or the attempts run out; a restart does not lose what is still owed, and two edges sharing one database push each row once, not twice. `OPENWAF_ALERT_WEBHOOK_URL` is the environment fallback. `/admin-api/state` answers `alertWebhookConfigured`, `alertWebhookHost` (host only: a webhook URL carries its secret in the path), `alertWebhookDelivered`, `alertWebhookFailures`, `alertWebhookLost`, `alertWebhookLastError`, `alertWebhookLastSuccessAt`, and the console prints the same on its alert line. The queue itself answers `alertPushPending`, `alertPushDelivered`, `alertPushGivenUp`, `alertPushOldestPendingAt` and `alertPushLastError` — counts of rows, so they survive a restart and agree between edges. | +| `OpenWaf:Alerts:WebhookUrl` (OPEN-53) | empty | where a raised alert is pushed. Empty is no push: the alert table stays the whole record, which is what it was. Set one URL and every alert the edge raises is POSTed there as JSON — `kind`, `sourceKey`, `count`, `lastStatusCode`, `lastPath`, `createdAt`, `detail` — from a **durable queue** — one row in `alert_pushes` per owed delivery, off the request path, written after the alert row is durable (OPEN-60). A failed push never undoes a row, and it is asked again on the schedule below until it lands or the attempts run out; a restart does not lose what is still owed, and two edges sharing one database push each row once, not twice. `OPENWAF_ALERT_WEBHOOK_URL` is the environment fallback. `/admin-api/state` answers `alertWebhookConfigured`, `alertWebhookHost` (host only: a webhook URL carries its secret in the path), `alertWebhookDelivered`, `alertWebhookFailures`, `alertWebhookLost`, `alertWebhookLastError`, `alertWebhookLastSuccessAt`, and the console prints the same on its alert line. The queue itself answers `alertPushPending`, `alertPushDelivered`, `alertPushGivenUp`, `alertPushOldestPendingAt`, `alertPushAttempts` and `alertPushLastError` — counts of rows, so they survive a restart and agree between edges. | | `OpenWaf:Alerts:WebhookTimeoutSeconds` | `5` | how long one push may take. 1–60 whole seconds; anything else aborts startup naming the key, as the other startup-parsed bounds do — a 0-second timeout drops every push and a 600-second one parks a queue worker on an endpoint that is not answering, and neither is what the operator typed. | | `OpenWaf:Alerts:MaxPushAttempts` (OPEN-60) | `6` | how many attempts one delivery gets before the edge stops asking. 1–20 whole attempts; anything else aborts startup naming the key. Against an endpoint that refuses, the default spends its last attempt a little over three minutes in (30 s, 60 s, 120 s, 240 s, 480 s — each step capped at 30 min) and then records the row as given up: `alertPushGivenUp` counts those rows and `alertPushLastError` says why in the sentence the row carries (`gave up after 6 attempts: the endpoint answered 500`). A row that has given up is never retried, and the alert row behind it is untouched — what is lost is the telling, not the record. | | `OpenWaf:Alerts:PushRetrySeconds` | `30` | the base of the backoff between attempts: attempt *n* waits `base × 2^(n−1)`, capped at 30 minutes per step, because exponential growth from a 30-second base reaches a day in eight steps and a day is not a retry interval for a page meant to fire at 03:00. 1–3600 whole seconds, same startup refusal — a 0-second interval against a dead endpoint is a busy loop. | diff --git a/dotnet/OpenWaf.Edge/Admin/AdminEndpoints.cs b/dotnet/OpenWaf.Edge/Admin/AdminEndpoints.cs index 1c1a7aa..0545946 100644 --- a/dotnet/OpenWaf.Edge/Admin/AdminEndpoints.cs +++ b/dotnet/OpenWaf.Edge/Admin/AdminEndpoints.cs @@ -797,6 +797,7 @@ note = t.Note alertPushGivenUp = pushQueue.GivenUp, alertPushLastError = pushQueue.LastError, alertPushOldestPendingAt = pushQueue.OldestPendingAt, + alertPushAttempts = pushQueue.Attempts, retentionPruneFailures = durability.Get(DurabilityHealth.Retention).Failures, retentionLastError = durability.Get(DurabilityHealth.Retention).LastError, retentionLastPruneAt = durability.Get(DurabilityHealth.Retention).LastSuccessAt, diff --git a/dotnet/OpenWaf.Edge/Admin/MetricsEndpoint.cs b/dotnet/OpenWaf.Edge/Admin/MetricsEndpoint.cs index 6a56fae..d37c6a0 100644 --- a/dotnet/OpenWaf.Edge/Admin/MetricsEndpoint.cs +++ b/dotnet/OpenWaf.Edge/Admin/MetricsEndpoint.cs @@ -187,6 +187,11 @@ internal static class MetricsEndpoint "Deliveries the endpoint accepted, counted as rows rather than as an " + "in-process counter, so the answer survives a restart.") .With(queue.Delivered)); + families.Add(MetricFamily.Counter("openwaf_alert_push_attempts_total", + "HTTP attempts the queue has made against the endpoint, over every row it has " + + "ever held. Attempts per delivery is what separates an endpoint that refuses " + + "everything from one that refuses occasionally.") + .With(queue.Attempts)); families.Add(MetricFamily.Counter("openwaf_alert_push_given_up_total", "Deliveries that ran out of attempts. This is the number that says " + "'nobody was told', as distinct from 'we are still asking'.") diff --git a/dotnet/OpenWaf.Infrastructure/Persistence/AlertNotifier.cs b/dotnet/OpenWaf.Infrastructure/Persistence/AlertNotifier.cs index 982d86d..c81d1c3 100644 --- a/dotnet/OpenWaf.Infrastructure/Persistence/AlertNotifier.cs +++ b/dotnet/OpenWaf.Infrastructure/Persistence/AlertNotifier.cs @@ -94,7 +94,7 @@ public sealed class AlertNotifier : BackgroundService private readonly Channel _wake = Channel.CreateBounded( new BoundedChannelOptions(16) { FullMode = BoundedChannelFullMode.DropWrite, SingleReader = true }); - private volatile PushQueueCounts _counts = new(0, 0, 0, null, null); + private volatile PushQueueCounts _counts = new(0, 0, 0, null, null, 0); private DateTimeOffset _countsAt = DateTimeOffset.MinValue; /// Host only, never the URL: webhook URLs routinely carry the @@ -325,7 +325,7 @@ public sealed class AlertNotifier : BackgroundService /// it, not as this process last cached it. public async Task ReadCountsAsync(CancellationToken ct = default) { - if (_url is null) return new PushQueueCounts(0, 0, 0, null, null); + if (_url is null) return new PushQueueCounts(0, 0, 0, null, null, 0); using var scope = _scopeFactory.CreateScope(); return await PushQueueCounts.QueryAsync(scope.ServiceProvider.GetRequiredService(), ct); } @@ -339,7 +339,8 @@ public sealed class AlertNotifier : BackgroundService /// count of rows rather than an in-process counter, so the answer survives a /// restart and agrees between edges sharing the database. public sealed record PushQueueCounts( - int Pending, int Delivered, int GivenUp, string? LastError, DateTimeOffset? OldestPendingAt) + int Pending, int Delivered, int GivenUp, string? LastError, DateTimeOffset? OldestPendingAt, + int Attempts) { public static async Task QueryAsync(OpenWafDbContext db, CancellationToken ct) { @@ -350,6 +351,10 @@ public sealed record PushQueueCounts( await db.AlertPushes.CountAsync(p => p.GivenUpAt != null, ct), await db.AlertPushes.Where(p => p.LastError != null) .OrderByDescending(p => p.Attempts).Select(p => p.LastError).FirstOrDefaultAsync(ct), - await pending.OrderBy(p => p.CreatedAt).Select(p => p.CreatedAt).FirstOrDefaultAsync(ct)); + await pending.OrderBy(p => p.CreatedAt).Select(p => p.CreatedAt).FirstOrDefaultAsync(ct), + // Every attempt every row has made, including rows that have since landed. It is + // the one number that says how hard the edge has been trying: pending and given-up + // say what the queue holds, this says what it cost. + (int) await db.AlertPushes.SumAsync(p => p.Attempts, ct)); } } diff --git a/dotnet/OpenWaf.Tests.Integration/AlertPushQueueTests.cs b/dotnet/OpenWaf.Tests.Integration/AlertPushQueueTests.cs index f5bf784..faaa118 100644 --- a/dotnet/OpenWaf.Tests.Integration/AlertPushQueueTests.cs +++ b/dotnet/OpenWaf.Tests.Integration/AlertPushQueueTests.cs @@ -230,7 +230,7 @@ public sealed class AlertPushQueueTests // Wait on the queue, not on the bodies: a body arrives a moment before the // row that produced it is marked delivered, and an assertion written // against the wire would read the database mid-write. - PushQueueCounts queueA = new(0, 0, 0, null, null), queueB = new(0, 0, 0, null, null); + PushQueueCounts queueA = new(0, 0, 0, null, null, 0), queueB = new(0, 0, 0, null, null, 0); var deadline = DateTime.UtcNow.AddSeconds(25); while (DateTime.UtcNow < deadline) { @@ -283,6 +283,7 @@ public sealed class AlertPushQueueTests Assert.Matches(@"openwaf_alert_push_pending [1-9]", body); Assert.Contains("openwaf_alert_push_delivered_total 0", body); Assert.Contains("openwaf_alert_push_given_up_total 0", body); + Assert.Matches(@"openwaf_alert_push_attempts_total [1-9]", body); Assert.Contains("openwaf_alert_push_oldest_pending_age_seconds", body); } } diff --git a/scripts/compose-smoke.sh b/scripts/compose-smoke.sh index b97b0ae..4dbc2a0 100755 --- a/scripts/compose-smoke.sh +++ b/scripts/compose-smoke.sh @@ -1406,8 +1406,9 @@ check 'the verdict counter counts the rule refusal this stack just made' 'yes' \ "$([ "$(msample 'openwaf_requests_total{verdict="block_rule"} ')" -gt 0 ] 2>/dev/null && echo yes || echo no)" check 'the schema gauge says nothing the build needs is missing' '0' \ "$(msample 'openwaf_schema_missing_tables ')" -check 'the push queue is published as gauges on the shipped image' '0 0 0' \ - "$(msample 'openwaf_alert_push_pending ') $(msample 'openwaf_alert_push_delivered_total ') $(msample 'openwaf_alert_push_given_up_total ')" +check 'the push queue is published as gauges on the shipped image' '0 0 0 0' \ + "$(msample 'openwaf_alert_push_pending ') $(msample 'openwaf_alert_push_delivered_total ') \ +$(msample 'openwaf_alert_push_given_up_total ') $(msample 'openwaf_alert_push_attempts_total ')" check 'the write counters are published even on a healthy stack' '0' \ "$(msample 'openwaf_write_failures_total{pillar="bans"} ')" check 'the body names no address from the scene above' 'yes' \ diff --git a/scripts/smoke.sh b/scripts/smoke.sh index fc8e6a5..5a4ca0c 100755 --- a/scripts/smoke.sh +++ b/scripts/smoke.sh @@ -149,8 +149,9 @@ check 'the live blocks gauge counts the engine ban' 1 \ # way. No endpoint is configured in this scene, so the honest answer is three # zeros — and a gauge that is missing is a different failure from a gauge that # says 0, which is why msample's empty answer fails the check. -check 'the push queue is published as gauges as well' '0 0 0' \ - "$(msample 'openwaf_alert_push_pending ') $(msample 'openwaf_alert_push_delivered_total ') $(msample 'openwaf_alert_push_given_up_total ')" +check 'the push queue is published as gauges as well' '0 0 0 0' \ + "$(msample 'openwaf_alert_push_pending ') $(msample 'openwaf_alert_push_delivered_total ') \ +$(msample 'openwaf_alert_push_given_up_total ') $(msample 'openwaf_alert_push_attempts_total ')" # The aggregate-only invariant, asserted over the whole body with the markers # from the scene above actually in the database behind it. check 'the body names no address, no path and no operator note' yes \