Exemplo n.º 1
0
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)
	}
}
Exemplo n.º 2
0
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)
	}
}
Exemplo n.º 3
0
// 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)
	})
}