Skip to content

Commit

Permalink
add metric for tracking cache hits (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
cottand authored Oct 9, 2024
1 parent 8779e3d commit 63c22f6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
3 changes: 2 additions & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bufio"
"errors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"net"
"net/http"
Expand Down Expand Up @@ -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)
}
}()
Expand Down
10 changes: 4 additions & 6 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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 {
Expand All @@ -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())
Expand Down
13 changes: 12 additions & 1 deletion internal/metric/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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()
}
}

0 comments on commit 63c22f6

Please sign in to comment.