Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add metric for tracking cache hits #67

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
}
Loading