Files
jarl/internal/render/stats.go
T
abrown38andClaude Opus 5.5 3b7bee67f8 Add optional query statistics
Off by default (stats in jarl.yaml). When enabled, Jarl adds a loopback
statistics channel and a response log to named's configuration (merged
into existing logging/statistics-channels statements) and switches
response logging with rndc responselog, because the responselog option
only counts when named starts. jarld polls named's counters every minute
and tails the response log (resuming after restarts and across
rotation). It stores aggregates, never individual queries, in a separate
SQLite database: per-minute, hourly and daily counters (queries, result
codes, record types, views, zones, cache hits, upstream latency, DNSSEC)
and hourly/daily top lists (names, clients, NXDOMAIN and SERVFAIL names,
refused clients), each with its own retention. Clients can be anonymized
or excluded.

- Web UI: Statistics page with KPI tiles, queries and failed answers over
  time, a weekday x hour heatmap, top lists and breakdowns; SVG charts
  with hover/keyboard readouts, a table view and light/dark palettes
  validated for colour-vision deficiency.
- CLI: jarl stats status|summary|top|timeline|heatmap|breakdown.
- API: /api/v1/stats/*, behind a new "stats" permission resource.
- jarld brings named's configuration up to date with jarl.yaml at
  startup (engine.SyncConfig), never overwriting hand-edited files.
- resync now refuses to overwrite drifted files without force, as
  documented.
- tmpfiles.d creates /var/log/named (allowed by named's AppArmor profile
  on Debian 13 and Ubuntu).
- The e2e harness runs named with -f like the Debian unit.
- docs/stats.md, plus API, CLI, configuration, permissions and README
  updates.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 20:21:34 +00:00

110 lines
3.7 KiB
Go

package render
import (
"net/netip"
"strconv"
"strings"
"jarl/internal/model"
"jarl/internal/namedconf"
)
// Extras are additions to named's configuration that come from jarld's
// own configuration (jarl.yaml) rather than from the database.
type Extras struct {
// Stats, when set, makes named publish what Jarl's statistics
// collector reads.
Stats *StatsHooks
}
// StatsHooks describe the statistics channel and response log Jarl needs.
type StatsHooks struct {
Channel netip.AddrPort // statistics-channels address (loopback)
LogFile string // response log; "" disables response logging
LogVersions int
LogSize string
}
// StatsChannelName is the logging channel Jarl adds for responses.
const StatsChannelName = "jarl_responses"
const statsComment = "Added by Jarl for query statistics (stats in /etc/jarl/jarl.yaml)."
// statsInet is the statistics-channels clause for the collector.
func (h *StatsHooks) statsInet() *namedconf.Stmt {
addr := h.Channel.Addr().String()
return namedconf.NewStmt("inet", addr, "port", strconv.Itoa(int(h.Channel.Port())),
"allow", namedconf.NewBlock(namedconf.NewStmt(addr)))
}
// logChannel is the logging channel the response log is written to.
func (h *StatsHooks) logChannel() *namedconf.Stmt {
return namedconf.NewStmt("channel", StatsChannelName, namedconf.NewBlock(
namedconf.NewStmt("file", namedconf.Quoted(h.LogFile), "versions", strconv.Itoa(h.LogVersions), "size", h.LogSize),
namedconf.NewStmt("print-time", "yes"),
namedconf.NewStmt("severity", "info"),
))
}
// statsStatement returns the statistics-channels statement, merged into the
// user's own if there is one.
func (h *StatsHooks) statsStatement(user *model.Option) string {
if user != nil {
if st, err := namedconf.ParseOptionValue(user.Name, user.Value); err == nil && st.FirstBlock() != nil {
b := st.FirstBlock()
want := namedconf.CompactStmt(h.statsInet())
for _, s := range b.Stmts {
if namedconf.CompactStmt(s) == want {
return optionText(user, 0)
}
}
b.Stmts = append(b.Stmts, h.statsInet())
return commentLines(user.Comment) + commentBlock("//", statsComment) + namedconf.FormatStmt(st, 0)
}
// Unparsable: validation reports it; leave it alone.
return optionText(user, 0)
}
st := namedconf.NewStmt("statistics-channels", namedconf.NewBlock(h.statsInet()))
return commentBlock("//", statsComment) + namedconf.FormatStmt(st, 0)
}
// loggingStatement returns the logging statement with Jarl's channel and
// the responses category, merged into the user's own if there is one.
func (h *StatsHooks) loggingStatement(user *model.Option) string {
var st *namedconf.Stmt
if user != nil {
var err error
st, err = namedconf.ParseOptionValue(user.Name, user.Value)
if err != nil || st.FirstBlock() == nil {
return optionText(user, 0)
}
} else {
st = namedconf.NewStmt("logging", namedconf.NewBlock())
}
b := st.FirstBlock()
// Channels must be defined before the categories that use them.
b.Stmts = append([]*namedconf.Stmt{h.logChannel()}, b.Stmts...)
merged := false
for _, s := range b.Stmts {
if s.Keyword() == "category" && strings.EqualFold(s.Arg(0), "responses") && s.FirstBlock() != nil {
s.FirstBlock().Stmts = append(s.FirstBlock().Stmts, namedconf.NewStmt(StatsChannelName))
merged = true
}
}
if !merged {
b.Stmts = append(b.Stmts, namedconf.NewStmt("category", "responses", namedconf.NewBlock(namedconf.NewStmt(StatsChannelName))))
}
prefix := ""
if user != nil {
prefix = commentLines(user.Comment)
}
return prefix + commentBlock("//", statsComment) + namedconf.FormatStmt(st, 0)
}
func commentLines(c string) string {
if c == "" {
return ""
}
return commentBlock("//", c)
}