This checks if the next middleware to be called is nil, and if so returns ServerFailure and an error. This makes the next calling more robust and saves some lines of code. Also prefix the error with the name of the middleware to aid in debugging.
34 lines
963 B
Go
34 lines
963 B
Go
package metrics
|
|
|
|
import (
|
|
"github.com/miekg/coredns/middleware"
|
|
"github.com/miekg/coredns/middleware/metrics/vars"
|
|
"github.com/miekg/coredns/middleware/pkg/dnsrecorder"
|
|
"github.com/miekg/coredns/middleware/pkg/rcode"
|
|
"github.com/miekg/coredns/request"
|
|
|
|
"github.com/miekg/dns"
|
|
"golang.org/x/net/context"
|
|
)
|
|
|
|
// ServeDNS implements the Handler interface.
|
|
func (m *Metrics) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
|
state := request.Request{W: w, Req: r}
|
|
|
|
qname := state.QName()
|
|
zone := middleware.Zones(m.ZoneNames()).Matches(qname)
|
|
if zone == "" {
|
|
zone = "."
|
|
}
|
|
|
|
// Record response to get status code and size of the reply.
|
|
rw := dnsrecorder.New(w)
|
|
status, err := middleware.NextOrFailure(m.Name(), m.Next, ctx, rw, r)
|
|
|
|
vars.Report(state, zone, rcode.ToString(rw.Rcode), rw.Len, rw.Start)
|
|
|
|
return status, err
|
|
}
|
|
|
|
// Name implements the Handler interface.
|
|
func (m *Metrics) Name() string { return "prometheus" }
|