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>
68 lines
1.7 KiB
Go
68 lines
1.7 KiB
Go
package app
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/miekg/dns"
|
|
)
|
|
|
|
type lockedBuf struct {
|
|
mu sync.Mutex
|
|
b bytes.Buffer
|
|
}
|
|
|
|
func (l *lockedBuf) Write(p []byte) (int, error) {
|
|
l.mu.Lock()
|
|
defer l.mu.Unlock()
|
|
return l.b.Write(p)
|
|
}
|
|
func (l *lockedBuf) lines() []map[string]any {
|
|
l.mu.Lock()
|
|
defer l.mu.Unlock()
|
|
var out []map[string]any
|
|
for _, ln := range strings.Split(strings.TrimSpace(l.b.String()), "\n") {
|
|
if ln == "" {
|
|
continue
|
|
}
|
|
var m map[string]any
|
|
if err := json.Unmarshal([]byte(ln), &m); err != nil {
|
|
panic("access log line is not JSON: " + ln)
|
|
}
|
|
out = append(out, m)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// TestAccessLogDefaultsToBlockedAndFailed runs the whole resolver with the
|
|
// shipped defaults: a blocked query leaves a line, an ordinary answer and a
|
|
// cache hit leave none, and the line carries what a log search needs.
|
|
func TestAccessLogDefaultsToBlockedAndFailed(t *testing.T) {
|
|
out := &lockedBuf{}
|
|
h := newHarness(t, nil, func(o *Options) { o.AccessLogOut = out })
|
|
|
|
h.query("www.example.test", dns.TypeA) // allowed
|
|
h.query("www.example.test", dns.TypeA) // cached
|
|
if m := h.query("ads.example.com", dns.TypeA); m.Rcode != dns.RcodeNameError {
|
|
t.Fatalf("setup: ads.example.com rcode %d, want a block", m.Rcode)
|
|
}
|
|
h.app.logger.Flush()
|
|
|
|
lines := out.lines()
|
|
if len(lines) != 1 {
|
|
t.Fatalf("%d access-log lines, want exactly the blocked query: %v", len(lines), lines)
|
|
}
|
|
l := lines[0]
|
|
for k, want := range map[string]any{
|
|
"log": "access", "msg": "query", "name": "ads.example.com.", "action": "block",
|
|
"list": "test-hosts", "qtype": "A", "client": "127.0.0.1", "protocol": "udp",
|
|
} {
|
|
if l[k] != want {
|
|
t.Errorf("%s = %v, want %v (line %v)", k, l[k], want, l)
|
|
}
|
|
}
|
|
}
|