Make the resolver pure DNS: JSON access log on stdout instead of a log store

The resolver kept its own query-log store (a bbolt ring, a live ring with SSE
fanout, rolling per-domain counters and dashboard time-series) for the console.
In a multi-node deployment each pod held a different slice of that, so every
view disagreed with itself, and the cost sat on the query path. Charts belong
in Grafana and log search in the log collector, so the store is gone.

internal/accesslog writes one JSON line per notable query to stdout. The default
is what was blocked and what failed (blocked, redirected, SERVFAIL, dropped,
refused); log_cached and sample_rate add ordinary answers for a debugging
session. Lines go through a bounded buffer to a single writer goroutine and are
dropped, and counted in fancydns_access_log_dropped_total, when the stream
cannot keep up, so a stalled log pipe can never stall a query. The config key is
access_log (the old qlog block is ignored, like any unknown key).

The metrics gain what the dashboard used to compute: fancydns_group_queries_total
by ACL group and outcome and fancydns_responses_total by RCODE. Removed with the
store: /api/dashboard, /api/logs, /api/logs/stream, /api/logs/clear,
/api/cache/top, the series rings, and the cache's per-name hit table, which took
a mutex and built a string on every cache hit.

Tests that read the old log now read the access-log lines; the contract test
drops the removed endpoints. The web console still calls them and is changed in
the next commit.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
matsandClaude Sonnet 5.5 committed 2026-10-16 09:53:04 +02:00
1 parent dd1117fd91
commit c6c5ca4ff7
31 files changed
+818 -3389

No files matched your search

+77 -19
View File
@@ -1,7 +1,9 @@
package engine
import (
"bytes"
"context"
"encoding/json"
"net"
"net/netip"
"os"
@@ -13,13 +15,13 @@ import (
"github.com/miekg/dns"
"git.k8s-git-k6n.net/mats/fancy-dns/internal/accesslog"
"git.k8s-git-k6n.net/mats/fancy-dns/internal/acl"
"git.k8s-git-k6n.net/mats/fancy-dns/internal/blocklist"
"git.k8s-git-k6n.net/mats/fancy-dns/internal/cache"
"git.k8s-git-k6n.net/mats/fancy-dns/internal/config"
"git.k8s-git-k6n.net/mats/fancy-dns/internal/localzone"
"git.k8s-git-k6n.net/mats/fancy-dns/internal/metrics"
"git.k8s-git-k6n.net/mats/fancy-dns/internal/qlog"
"git.k8s-git-k6n.net/mats/fancy-dns/internal/resolver"
)
@@ -266,7 +268,8 @@ type harness struct {
store *config.Store
pool *resolver.Pool
cache *cache.Manager
log *qlog.Logger
log *accesslog.Logger
lines *lineSink
up *fakeUpstream
clock *testClock
blocks *blocklist.Holder
@@ -353,20 +356,12 @@ func newHarness(t *testing.T, o hopt) *harness {
Now: clock.Now,
}, nil)
log, err := qlog.New(qlog.Options{
LiveBuffer: 64,
SampleRate: 1,
// Every action is logged, so a test can count entries without
// reasoning about the sampling policy.
LogBlocked: true,
LogCached: true,
LogErrors: true,
TopDomains: 16,
TopTTL: time.Minute,
lines := &lineSink{}
// Every action is logged, so a test can count entries without reasoning
// about the sampling policy.
log := accesslog.New(accesslog.Options{
Writer: lines, SampleRate: 1, LogBlocked: true, LogCached: true, LogErrors: true,
})
if err != nil {
t.Fatalf("qlog: %v", err)
}
t.Cleanup(func() { _ = log.Close() })
blocks := blocklist.NewHolder(buildSet(o.rules...))
@@ -387,7 +382,7 @@ func newHarness(t *testing.T, o hopt) *harness {
}
h := &harness{
t: t, eng: eng, cfg: cfg, store: store, cache: cm, log: log,
t: t, eng: eng, cfg: cfg, store: store, cache: cm, log: log, lines: lines,
up: up, clock: clock, blocks: blocks, local: localH, aclH: aclH,
}
if !o.noPool {
@@ -446,9 +441,16 @@ func (h *harness) askTCP(name string, qtype uint16) Response {
return h.askWith(req)
}
func (h *harness) entries() []qlog.Entry { return h.log.Recent(64) }
// entries returns the access-log lines written so far, newest first.
func (h *harness) entries() []accesslog.Entry {
h.log.Flush()
return h.lines.entries()
}
func (h *harness) logged() int { return h.log.Count() }
func (h *harness) logged() int {
h.log.Flush()
return h.lines.count()
}
func (h *harness) totals() metrics.Totals { return metrics.TotalsNow() }
@@ -484,7 +486,7 @@ func requireRcode(t *testing.T, res Response, rcode int) {
}
}
func requireAction(t *testing.T, res Response, want qlog.Action) {
func requireAction(t *testing.T, res Response, want accesslog.Action) {
t.Helper()
if res.Action != want {
t.Fatalf("action = %q, want %q", res.Action, want)
@@ -497,3 +499,59 @@ func requireNoUpstreamHit(t *testing.T, h *harness, name string) {
t.Fatalf("upstream saw %d queries for %s, want 0", n, name)
}
}
// lineSink collects the access log's output so a test can read it back.
type lineSink struct {
mu sync.Mutex
buf []byte
rows []accesslog.Entry
}
func (s *lineSink) Write(p []byte) (int, error) {
s.mu.Lock()
defer s.mu.Unlock()
s.buf = append(s.buf, p...)
for {
i := bytes.IndexByte(s.buf, '\n')
if i < 0 {
break
}
line := s.buf[:i]
s.buf = s.buf[i+1:]
var raw struct {
Client string `json:"client"`
Group string `json:"client_group"`
Name string `json:"name"`
Action string `json:"action"`
Protocol string `json:"protocol"`
List string `json:"list"`
Upstream string `json:"upstream"`
Duration float64 `json:"duration_ms"`
}
if err := json.Unmarshal(line, &raw); err != nil {
panic("access log line is not JSON: " + string(line))
}
s.rows = append(s.rows, accesslog.Entry{
Client: raw.Client, Group: raw.Group, Name: raw.Name,
Action: accesslog.Action(raw.Action), Protocol: raw.Protocol,
List: raw.List, Upstream: raw.Upstream,
})
}
return len(p), nil
}
func (s *lineSink) count() int {
s.mu.Lock()
defer s.mu.Unlock()
return len(s.rows)
}
func (s *lineSink) entries() []accesslog.Entry {
s.mu.Lock()
defer s.mu.Unlock()
out := make([]accesslog.Entry, len(s.rows))
for i, r := range s.rows {
out[len(s.rows)-1-i] = r
}
return out
}