A statistics service aggregates every answered query in memory and writes this node's counters, answer mix, upstream latency and top-N rollups each minute #112

Open
opened 2026-10-31 12:42:08 -04:00 by claude · 0 comments
Owner
  • Draft: PQ-14
  • Priority: high
  • Agent: coder
  • Depends on: #101, #111
  • Decisions: Q-PQ-1, Q-PQ-12, R2-STATS-NODE

Why

This is the writer for #111. It must not touch the database per query, the same rule as DnsQueryLogSubscriber (DnsQueryLogSubscriber.cs:26-31).

What changes

  • New project src/dotnet/src/Ninlil.Dns.Server.Statistics and its tests project src/dotnet/tests/Ninlil.Dns.Server.Statistics.Tests, added to Ninlil.slnx. It holds the DNS accumulator, the writer and the retention pass for every statistics row, DNS and DHCP (#116 adds the DHCP part).
  • ServiceCollectionStatisticsExtension, wired in Program.cs.
  • Node identity: every row carries ClusterOptions.NodeId. Program.cs sets it on every start, from cluster-node-id.txt, whether clustering is on or not (src/dotnet/src/Ninlil.Web/Program.cs:234-245; src/dotnet/src/Ninlil.Dns.Server.Cluster/Options/ClusterOptions.cs:27-33). Program.cs hands it to the statistics project through a small IStatisticsNodeIdentity singleton, so the project does not reference Ninlil.Dns.Server.Cluster.
    • Technical call: not DnsServerEventPublisher.InstanceId, which is a new Guid per process (src/dotnet/src/Ninlil.Dns.Server.Core/Events/DnsServerEventPublisher.cs:90), so every restart would look like a new node.
  • A DnsQueryAnsweredEvent subscriber feeds an in-memory accumulator:
    • Lock-free counters, category dns: queries.total, queries.blocked, queries.cached, queries.nxdomain, queries.servfail, latency.sum.us, latency.count. The console derives "allowed, not cached" as total minus blocked minus cached.
    • Answer mix: qtype.<TYPE>, rcode.<RCODE>, source.<AnswerSource>. A query type outside DnsResourceRecordTypeEnum counts as qtype.OTHER, so the name set stays bounded.
    • Upstream latency: upstream.<name>.latency.sum.us and upstream.<name>.count, from the #101 Upstream field (forwarder names, or recursive). Technical call: sums and counts, not averages or percentiles, because only sums add up correctly across buckets and nodes.
    • Bounded per-key dictionaries per hour for top-N. When a dictionary is full, a key not already present is counted in an "other" bucket, so memory stays bounded.
  • A background writer flushes the accumulator every FlushInterval through #111: each counter goes to its minute row and its hour row. The answer mix is written to hour rows only (the doughnuts cover 24 h or more). It runs retention once an hour.
  • Privacy (Q-PQ-1), read from the query-log privacy level in effect when the query is answered:
    • DomainRedacted or above: no Domain or BlockedDomain top items.
    • ClientAnonymized: Client keys are the subnet the query log stores (last IPv4 octet or last 64 IPv6 bits zeroed).
    • FullyAnonymous: no Client top items.
    • Counters, the answer mix and upstream latency are always kept.
  • Options class StatisticsOptions, section Statistics (it covers DNS and DHCP): Enable (default true), MinuteRetention (default 2 days), HourlyRetention (default 90 days), MaxKeysPerHour (default 10,000), FlushInterval (default 1 minute).
    • Register it in src/dotnet/src/Ninlil.Web/EnvironmentVariables/OptionsEnvironmentVariableRegistry.cs.
    • Add the rows and recount the totals in ENVIRONMENT.md.
  • Cluster: nodes that share one database write rows under their own node ids, and the dashboard shows them combined or one at a time (#117, #121). With separate databases, each node's rows live in its own database, like the query log; the existing cluster dashboard (ClusterQueryStatsAggregator) is unchanged. README says both.

Acceptance

  • Test fails first in the new tests project: 10 events (3 blocked) flush to queries.total = 10, queries.blocked = 3 under the fake node id, and the top blocked domain count is 3.
  • Test: 2 AAAA and 1 MX query flush to qtype.AAAA = 2 and qtype.MX = 1 hour rows.
  • Test: two forwarded answers from upstream "quad" of 1000 and 3000 µs flush to upstream.quad.latency.sum.us = 4000 and upstream.quad.count = 2.
  • Test: with MaxKeysPerHour = 2, a third distinct key goes to the overflow bucket.
  • Test: at DomainRedacted no domain top item is written; at FullyAnonymous no client top item is written; counters are written at every level.
  • src/dotnet/tests/Ninlil.Web.Tests: the registered IStatisticsNodeIdentity equals the host's ClusterOptions.NodeId.
  • Event-bus benchmark (Events/) before and after, in the PR.
  • Option added: registry plus ENVIRONMENT.md.

Source

  • audit-feature-parity.md §2A #1.
  • Owner decision 2026-10-31 (Q-PQ-1): "follow privacy level".
  • Owner decision 2026-10-31 (Q-PQ-12): all four DNS chart sets need these series.
  • Owner decision 2026-10-31, round 2 (R2-STATS-NODE): statistics are stored per node.
- **Draft:** PQ-14 - **Priority:** high - **Agent:** coder - **Depends on:** #101, #111 - **Decisions:** Q-PQ-1, Q-PQ-12, R2-STATS-NODE ### Why This is the writer for #111. It must not touch the database per query, the same rule as `DnsQueryLogSubscriber` (`DnsQueryLogSubscriber.cs:26-31`). ### What changes - New project `src/dotnet/src/Ninlil.Dns.Server.Statistics` and its tests project `src/dotnet/tests/Ninlil.Dns.Server.Statistics.Tests`, added to `Ninlil.slnx`. It holds the DNS accumulator, the writer and the retention pass for every statistics row, DNS and DHCP (#116 adds the DHCP part). - `ServiceCollectionStatisticsExtension`, wired in `Program.cs`. - Node identity: every row carries `ClusterOptions.NodeId`. `Program.cs` sets it on every start, from `cluster-node-id.txt`, whether clustering is on or not (`src/dotnet/src/Ninlil.Web/Program.cs:234-245`; `src/dotnet/src/Ninlil.Dns.Server.Cluster/Options/ClusterOptions.cs:27-33`). `Program.cs` hands it to the statistics project through a small `IStatisticsNodeIdentity` singleton, so the project does not reference `Ninlil.Dns.Server.Cluster`. - **Technical call:** not `DnsServerEventPublisher.InstanceId`, which is a new `Guid` per process (`src/dotnet/src/Ninlil.Dns.Server.Core/Events/DnsServerEventPublisher.cs:90`), so every restart would look like a new node. - A `DnsQueryAnsweredEvent` subscriber feeds an in-memory accumulator: - Lock-free counters, category `dns`: `queries.total`, `queries.blocked`, `queries.cached`, `queries.nxdomain`, `queries.servfail`, `latency.sum.us`, `latency.count`. The console derives "allowed, not cached" as total minus blocked minus cached. - Answer mix: `qtype.<TYPE>`, `rcode.<RCODE>`, `source.<AnswerSource>`. A query type outside `DnsResourceRecordTypeEnum` counts as `qtype.OTHER`, so the name set stays bounded. - Upstream latency: `upstream.<name>.latency.sum.us` and `upstream.<name>.count`, from the #101 `Upstream` field (forwarder names, or `recursive`). **Technical call:** sums and counts, not averages or percentiles, because only sums add up correctly across buckets and nodes. - Bounded per-key dictionaries per hour for top-N. When a dictionary is full, a key not already present is counted in an "other" bucket, so memory stays bounded. - A background writer flushes the accumulator every `FlushInterval` through #111: each counter goes to its minute row and its hour row. The answer mix is written to hour rows only (the doughnuts cover 24 h or more). It runs retention once an hour. - Privacy (Q-PQ-1), read from the query-log privacy level in effect when the query is answered: - `DomainRedacted` or above: no `Domain` or `BlockedDomain` top items. - `ClientAnonymized`: `Client` keys are the subnet the query log stores (last IPv4 octet or last 64 IPv6 bits zeroed). - `FullyAnonymous`: no `Client` top items. - Counters, the answer mix and upstream latency are always kept. - Options class `StatisticsOptions`, section `Statistics` (it covers DNS and DHCP): `Enable` (default true), `MinuteRetention` (default 2 days), `HourlyRetention` (default 90 days), `MaxKeysPerHour` (default 10,000), `FlushInterval` (default 1 minute). - Register it in `src/dotnet/src/Ninlil.Web/EnvironmentVariables/OptionsEnvironmentVariableRegistry.cs`. - Add the rows and recount the totals in ENVIRONMENT.md. - Cluster: nodes that share one database write rows under their own node ids, and the dashboard shows them combined or one at a time (#117, #121). With separate databases, each node's rows live in its own database, like the query log; the existing cluster dashboard (`ClusterQueryStatsAggregator`) is unchanged. README says both. ### Acceptance - [ ] Test fails first in the new tests project: 10 events (3 blocked) flush to `queries.total = 10`, `queries.blocked = 3` under the fake node id, and the top blocked domain count is 3. - [ ] Test: 2 AAAA and 1 MX query flush to `qtype.AAAA = 2` and `qtype.MX = 1` hour rows. - [ ] Test: two forwarded answers from upstream "quad" of 1000 and 3000 µs flush to `upstream.quad.latency.sum.us = 4000` and `upstream.quad.count = 2`. - [ ] Test: with `MaxKeysPerHour = 2`, a third distinct key goes to the overflow bucket. - [ ] Test: at `DomainRedacted` no domain top item is written; at `FullyAnonymous` no client top item is written; counters are written at every level. - [ ] `src/dotnet/tests/Ninlil.Web.Tests`: the registered `IStatisticsNodeIdentity` equals the host's `ClusterOptions.NodeId`. - [ ] Event-bus benchmark (`Events/`) before and after, in the PR. - [ ] Option added: registry plus ENVIRONMENT.md. ### Source - `audit-feature-parity.md` §2A #1. - Owner decision 2026-10-31 (Q-PQ-1): "follow privacy level". - Owner decision 2026-10-31 (Q-PQ-12): all four DNS chart sets need these series. - Owner decision 2026-10-31, round 2 (R2-STATS-NODE): statistics are stored per node.
Sign in to join this conversation.
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
lantea-one/ninlil#112
No description provided.