From 90873175eed4b2776b8ce32d4ade1b8cf4a69724 Mon Sep 17 00:00:00 2001 From: Cottand Date: Tue, 8 Oct 2024 23:07:50 +0100 Subject: [PATCH] add metric for tracking cache hits --- api.go | 3 ++- handler.go | 10 ++++------ internal/metric/metric.go | 13 ++++++++++++- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/api.go b/api.go index dc20afb..ed6d6bc 100644 --- a/api.go +++ b/api.go @@ -2,6 +2,7 @@ package main import ( "bufio" + "errors" "github.com/prometheus/client_golang/prometheus/promhttp" "net" "net/http" @@ -234,7 +235,7 @@ func StartAPIServer(config *Config, return nil, err } go func() { - if err := server.Serve(listener); err != http.ErrServerClosed { + if err := server.Serve(listener); !errors.Is(err, http.ErrServerClosed) { logger.Fatal(err) } }() diff --git a/handler.go b/handler.go index 59ce753..8519894 100644 --- a/handler.go +++ b/handler.go @@ -147,7 +147,7 @@ func (h *EventLoop) responseFor(Net string, req *dns.Msg, _local net.Addr, _remo msg := *mesg msg.Id = req.Id - defer metric.ReportDNSRespond(remote, &msg, blocked) + defer metric.ReportDNSRespond(remote, &msg, blocked, true) return &msg, true } } @@ -190,7 +190,7 @@ func (h *EventLoop) responseFor(Net string, req *dns.Msg, _local net.Addr, _remo } } - defer metric.ReportDNSRespond(remote, m, true) + defer metric.ReportDNSRespond(remote, m, true, false) logger.Noticef("%s found in blocklist\n", Q.Qname) @@ -252,7 +252,7 @@ func (h *EventLoop) responseFor(Net string, req *dns.Msg, _local net.Addr, _remo } } - defer metric.ReportDNSRespond(remote, mesg, false) + defer metric.ReportDNSRespond(remote, mesg, false, false) if IPQuery > 0 && len(mesg.Answer) > 0 { if !lengActive && blacklisted { @@ -270,9 +270,7 @@ func (h *EventLoop) responseFor(Net string, req *dns.Msg, _local net.Addr, _remo func (h *EventLoop) doRequest(Net string, w dns.ResponseWriter, req *dns.Msg) { defer func(w dns.ResponseWriter) { - err := w.Close() - if err != nil { - } + _ = w.Close() }(w) resp, ok := h.responseFor(Net, req, w.LocalAddr(), w.RemoteAddr()) diff --git a/internal/metric/metric.go b/internal/metric/metric.go index 01089f8..42611a8 100644 --- a/internal/metric/metric.go +++ b/internal/metric/metric.go @@ -36,6 +36,14 @@ var ( Help: "Served DNS replies", }, []string{"q_type", "remote_ip", "q_name", "rcode", "blocked"}) + cachedResponseCounter = prometheus.NewCounter( + prometheus.CounterOpts{ + Namespace: Namespace, + Name: "request_cached", + Help: "Cached DNS replies", + }, + ) + RequestUpstreamResolveCounter = prometheus.NewCounterVec( prometheus.CounterOpts{ Namespace: Namespace, @@ -130,7 +138,7 @@ func ReportDNSResponse(w dns.ResponseWriter, message *dns.Msg, blocked bool) { }).Inc() } -func ReportDNSRespond(remote net.IP, message *dns.Msg, blocked bool) { +func ReportDNSRespond(remote net.IP, message *dns.Msg, blocked bool, cached bool) { question := message.Question[0] var remoteHost string var qName string @@ -148,4 +156,7 @@ func ReportDNSRespond(remote net.IP, message *dns.Msg, blocked bool) { "rcode": dns.RcodeToString[message.Rcode], "blocked": strconv.FormatBool(blocked), }).Inc() + if cached { + cachedResponseCounter.Inc() + } }