OPEN-59. Every other admin surface answers what the edge already did, so the only test for a path rule, an exemption or a country rule is a real visitor getting the real refusal. GET /admin-api/decide?ip=&path=&country= runs the same DecisionEngine over the same snapshot the wire reads — there is no second decision to drift — and narrates the order. The trace is the product: a verdict says what would happen, the pillar list says which pillar to change, and it keeps four silences apart (did_not_match, not_reached, not_enforced_in_this_mode, not_evaluated). In Observe mode the engine line says whether a live ban for that source exists, because 'nothing was known about this address' and 'something was known and the mode chose not to act' are the two answers operators read as one. What a preview cannot honestly answer — the live rate counter and a streak that depends on responses the upstream has not produced — is named in the body rather than left out. Three things the run caught. IPAddress.TryParse accepts abbreviated IPv4, so '203.0.113' parses as 203.0.113.0 and a missing octet was answered 'pass' for an address the operator did not mean; the guard now requires a full dotted quad. The preview read state.Current while the guard calls GetOrRefresh(), so it could answer from a snapshot the wire had already replaced — the whole claim of the feature is that the two agree. And a Pass carries no source key, so the Observe note claimed 'no live ban' for a source that had one; the key is now computed for the question rather than read off the answer. Gates on this tree: build 0 warnings / 0 errors, unit 256/256, integration 245/245, smoke 65/0, compose 229/0 against the built image (261 MB), console 90/0.
70 lines
3.1 KiB
C#
70 lines
3.1 KiB
C#
using OpenWaf.Domain.Engine;
|
|
|
|
namespace OpenWaf.Edge.Observability;
|
|
|
|
/// <summary>
|
|
/// What this process has done to the traffic it has seen, one counter per
|
|
/// verdict (OPEN-58).
|
|
///
|
|
/// The guard already names every request it refuses in the log and every
|
|
/// request it passes goes to the origin; what it did not have was a number
|
|
/// anyone could scrape. This is that number, and it is deliberately the only
|
|
/// new state the metrics endpoint needs: the rest of what /metrics reports is
|
|
/// state the edge was already keeping.
|
|
///
|
|
/// Two things are on purpose here.
|
|
///
|
|
/// It counts the FINAL verdict, in the guard, at the one place that decides —
|
|
/// counting in two places is how a request ends up counted twice and a counter
|
|
/// whose total does not add up is worse than no counter.
|
|
///
|
|
/// It is per process, like the rate counter (OPEN-56) and unlike the block
|
|
/// list. A Prometheus counter is normally summed across instances by the query,
|
|
/// which is exactly the right answer for "how many requests did this fleet
|
|
/// refuse", and there is no shared version to choose because a shared counter
|
|
/// would cost a round trip on a path whose measured p95 is ~3 ms.
|
|
/// </summary>
|
|
public sealed class EdgeCounterLedger
|
|
{
|
|
/// <summary>The verdicts are a contiguous enum from 0, so the counter store
|
|
/// is an array indexed by the value rather than a dictionary keyed by a
|
|
/// string: no allocation per request, and no way to count a name that is
|
|
/// not a verdict.</summary>
|
|
private const int VerdictCount = 9;
|
|
|
|
private readonly long[] _counts = new long[VerdictCount];
|
|
|
|
/// <summary>The label the exposition gives this verdict. Lower-case,
|
|
/// snake_case, and spelled the way the log spells it, so the same word
|
|
/// appears in the alert and in the line an operator greps for.</summary>
|
|
public static string LabelFor(EdgeVerdict verdict) => EdgeVerdictLabel.For(verdict);
|
|
|
|
/// <summary>Every verdict the edge can answer with, in label form. The
|
|
/// exposition emits all of them, including the zeros: a counter that is
|
|
/// absent until it fires is a query that returns nothing rather than zero,
|
|
/// and "nothing matched" and "this metric does not exist in this build"
|
|
/// look the same in a graph.</summary>
|
|
public static IReadOnlyList<string> VerdictLabels { get; } =
|
|
Enum.GetValues<EdgeVerdict>().Select(LabelFor).ToArray();
|
|
|
|
public void Record(EdgeVerdict verdict)
|
|
{
|
|
var index = (int)verdict;
|
|
if (index < 0 || index >= VerdictCount) return;
|
|
Interlocked.Increment(ref _counts[index]);
|
|
}
|
|
|
|
/// <summary>The counts as they stood at this instant. A scrape reads a
|
|
/// snapshot rather than the live array, so a burst of traffic in the middle
|
|
/// of a scrape cannot produce a body whose totals disagree with each other
|
|
/// by more than the traffic that arrived while it was being written.</summary>
|
|
public long[] Snapshot()
|
|
{
|
|
var copy = new long[VerdictCount];
|
|
for (var i = 0; i < VerdictCount; i++) copy[i] = Interlocked.Read(ref _counts[i]);
|
|
return copy;
|
|
}
|
|
|
|
public long Total() => Snapshot().Sum();
|
|
}
|