func dnsMetricsHandler(next dns.Handler) dns.HandlerFunc { return func(w dns.ResponseWriter, req *dns.Msg) { var ( prot = "unknown" qtype = "unknown" rcode = "unknown" buffer = &cachingWriter{ResponseWriter: w} start = time.Now() ) if _, ok := w.RemoteAddr().(*net.UDPAddr); ok { prot = "udp" } else if _, ok := w.RemoteAddr().(*net.TCPAddr); ok { prot = "tcp" } if len(req.Question) == 1 { qtype = dns.TypeToString[req.Question[0].Qtype] } next.ServeDNS(buffer, req) if buffer.last != nil { rcode = dns.RcodeToString[buffer.last.Rcode] } duration := float64(time.Since(start)) / float64(time.Second) dnsDurations.WithLabelValues(prot, qtype, rcode).Observe(duration) } }
func dnsLoggingHandler(logger *log.Logger, next dns.Handler) dns.HandlerFunc { return func(w dns.ResponseWriter, r *dns.Msg) { next.ServeDNS(&loggingWriter{ ResponseWriter: w, logger: logger, start: time.Now(), }, r) } }
// TODO(alx): Settle on naming for handlers acting as middleware. func protocolHandler(maxAnswers int, next dns.Handler) dns.Handler { return dns.HandlerFunc(func(w dns.ResponseWriter, r *dns.Msg) { next.ServeDNS(&truncatingWriter{w, maxAnswers}, r) }) }