Statistics per view: toggle views on the Statistics page

Answers, result codes, record types, zones, blocking, rate limiting and the top lists are also counted per view, from named's response log. The Statistics page has a toggle per view with All and None, remembered per browser; jarl stats --view and ?views= on the API. named's own counters stay for the whole server, and the page says so.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
adamandClaude Opus 5.5 committed 2026-10-12 19:29:33 +01:00
1 parent ead749dd3d
commit 12820b17bb
13 files changed
+526 -61

No files matched your search

+76 -19
View File
@@ -33,6 +33,13 @@ const (
KindRateLimitedName = "ratelimited_name"
)
// ViewKey is the key of a counter for one view: per-view copies of the
// counters that come from named's response log (docs/stats.md).
func ViewKey(view, key string) string { return "@" + view + "/" + key }
// ViewKind is the kind of a top list for one view.
func ViewKind(kind, view string) string { return kind + "@" + view }
// Kinds lists the top-list kinds.
var Kinds = []string{KindName, KindClient, KindNXDomain, KindServFail, KindRefused, KindBlocked, KindBlockedClient, KindRateLimited, KindRateLimitedName}
@@ -115,6 +122,9 @@ type Collector struct {
// rewritten holds queries named blocked (client#port|name) until their
// response line arrives.
rewritten map[string]bool
// viewsSince is set once per-view counters exist (meta views_since).
viewsSince bool
viewsSinceAt int64
// proc is the last sample of named's process, server the latest server
// statistics (server.go).
proc *namedProc
@@ -212,6 +222,9 @@ func (c *Collector) start(ctx context.Context) {
}
}
}
if v, _ := c.store.Meta(ctx, "views_since"); v != "" {
c.viewsSince = true
}
if v, _ := c.store.Meta(ctx, "first_data"); v != "" {
if n, err := strconv.ParseInt(v, 10, 64); err == nil {
t := time.Unix(n, 0)
@@ -227,7 +240,8 @@ func (c *Collector) start(ctx context.Context) {
if last, err := strconv.ParseInt(merged, 10, 64); err == nil {
from := max(last+3600, cur-48*3600)
for h := from; h < cur; h += 3600 {
for _, k := range Kinds {
kinds, _ := c.store.KindsAt(ctx, ResHour, h)
for _, k := range kinds {
if es, err := c.store.TopsAt(ctx, ResHour, h, k); err == nil && len(es) > 0 {
b.AddTops(ResDay, c.dayOf(time.Unix(h, 0)), k, es, 3*c.opts.TopN)
}
@@ -235,7 +249,8 @@ func (c *Collector) start(ctx context.Context) {
}
}
b.SetMeta("merged_through", strconv.FormatInt(cur-3600, 10))
for _, k := range Kinds {
curKinds, _ := c.store.KindsAt(ctx, ResHour, cur)
for _, k := range curKinds {
es, err := c.store.TopsAt(ctx, ResHour, cur, k)
if err != nil || len(es) == 0 {
continue
@@ -294,7 +309,7 @@ func (c *Collector) count(minute int64, key string, n int64) {
c.minutes[minute] = m
}
m[key] += n
if !strings.HasPrefix(key, "g:") && !strings.HasPrefix(key, "gn:") {
if !strings.HasPrefix(key, "g:") && !strings.HasPrefix(key, "gn:") && !strings.HasPrefix(key, "@") {
c.totals[key] += n
}
}
@@ -365,33 +380,55 @@ func (c *Collector) handleLine(line string) {
if view == "" {
view = "_default"
}
zone := c.zoneOf(ev.Name, ev.View)
c.count(minute, "responses", 1)
c.count(minute, "view:"+view, 1)
c.count(minute, "zone:"+c.zoneOf(ev.Name, ev.View), 1)
c.count(minute, "zone:"+zone, 1)
// The same, per view, for the statistics of chosen views.
c.countView(minute, view, "responses", 1)
c.countView(minute, view, "zone:"+zone, 1)
c.countView(minute, view, "rcode:"+ev.RCode, 1)
c.countView(minute, view, "qtype:"+ev.Type, 1)
client := clientKey(ev.Client, c.opts.Anonymize)
hours := c.hours
if hour < c.liveHour {
hours = c.late
}
hours[hour] = c.hourMaps(hours, hour)
tops := hours[hour]
tops[KindName][ev.Name]++
tops[KindClient][client]++
switch {
case blocked:
// Counted by handleRewrite; not a name that doesn't exist.
case ev.RCode == "NXDOMAIN":
tops[KindNXDomain][ev.Name]++
case ev.RCode == "SERVFAIL":
tops[KindServFail][ev.Name]++
case ev.RCode == "REFUSED":
tops[KindRefused][client]++
}
for _, m := range tops {
add := func(kind, key string) {
hours[hour][kind][key]++
m := c.topsMap(hours, hour, ViewKind(kind, view))
m[key]++
if len(m) > maxKeys {
shrink(m)
}
}
add(KindName, ev.Name)
add(KindClient, client)
switch {
case blocked:
// Counted by handleRewrite; not a name that doesn't exist.
case ev.RCode == "NXDOMAIN":
add(KindNXDomain, ev.Name)
case ev.RCode == "SERVFAIL":
add(KindServFail, ev.Name)
case ev.RCode == "REFUSED":
add(KindRefused, client)
}
for _, k := range Kinds {
if m := hours[hour][k]; len(m) > maxKeys {
shrink(m)
}
}
}
// countView counts a per-view copy of a counter.
func (c *Collector) countView(minute int64, view, key string, n int64) {
if !c.viewsSince {
c.viewsSince = true
c.viewsSinceAt = minute
}
c.count(minute, ViewKey(view, key), n)
}
// handleRewrite counts a query that blocking answered: "blocked", and
@@ -412,15 +449,24 @@ func (c *Collector) handleRewrite(rw Rewrite) {
}
c.rewritten[rw.Client.String()+"#"+rw.Port+"|"+rw.Name] = true
minute, hour := c.minuteOf(rw.Time), c.hourOf(rw.Time)
view := rw.View
if view == "" {
view = "_default"
}
c.count(minute, "blocked", 1)
c.count(minute, "blocklist:"+policySource(rw.Via), 1)
c.countView(minute, view, "blocked", 1)
c.countView(minute, view, "blocklist:"+policySource(rw.Via), 1)
hours := c.hours
if hour < c.liveHour {
hours = c.late
}
hours[hour] = c.hourMaps(hours, hour)
client := clientKey(rw.Client, c.opts.Anonymize)
hours[hour][KindBlocked][rw.Name]++
hours[hour][KindBlockedClient][clientKey(rw.Client, c.opts.Anonymize)]++
hours[hour][KindBlockedClient][client]++
c.topsMap(hours, hour, ViewKind(KindBlocked, view))[rw.Name]++
c.topsMap(hours, hour, ViewKind(KindBlockedClient, view))[client]++
}
// handleRateLimit counts a response rate limiting limited, or would have:
@@ -437,7 +483,12 @@ func (c *Collector) handleRateLimit(rl RateLimit) {
key = "rrl:would-" + rl.Action
}
minute, hour := c.minuteOf(rl.Time), c.hourOf(rl.Time)
view := rl.View
if view == "" {
view = "_default"
}
c.count(minute, key, 1)
c.countView(minute, view, key, 1)
hours := c.hours
if hour < c.liveHour {
hours = c.late
@@ -445,6 +496,8 @@ func (c *Collector) handleRateLimit(rl RateLimit) {
hours[hour] = c.hourMaps(hours, hour)
hours[hour][KindRateLimited][rl.Prefix]++
hours[hour][KindRateLimitedName][rl.Name+" "+rl.Type]++
c.topsMap(hours, hour, ViewKind(KindRateLimited, view))[rl.Prefix]++
c.topsMap(hours, hour, ViewKind(KindRateLimitedName, view))[rl.Name+" "+rl.Type]++
}
// policySource names what a rewrite came from: a blocklist's name, "rules"
@@ -547,6 +600,10 @@ func (c *Collector) flush(ctx context.Context, final bool) {
b.AddCounters(ResHour, c.hourOf(t), vals)
b.AddCounters(ResDay, c.dayOf(t), vals)
delete(c.minutes, m)
if c.viewsSinceAt != 0 {
b.SetMeta("views_since", strconv.FormatInt(c.viewsSinceAt, 10))
c.viewsSinceAt = 0
}
if first && len(vals) > 0 {
ft := t
c.status.FirstData = &ft