diff options
author | 2016-04-09 16:17:53 +0100 | |
---|---|---|
committer | 2016-04-09 16:17:53 +0100 | |
commit | ad221f4b2afa0cf89810cd9c3b4e953913d602fc (patch) | |
tree | 23bf7561336d29096eb24d821265235f84ac4222 /middleware/metrics/handler.go | |
parent | db3d689a8ac2bec199e5643394ffa779341acde0 (diff) | |
download | coredns-ad221f4b2afa0cf89810cd9c3b4e953913d602fc.tar.gz coredns-ad221f4b2afa0cf89810cd9c3b4e953913d602fc.tar.zst coredns-ad221f4b2afa0cf89810cd9c3b4e953913d602fc.zip |
correct EDNS responses (#96)
Tests updated as well and all the middleware. And Prometheus renamed to
metrics (directive is still prometheus).
Diffstat (limited to 'middleware/metrics/handler.go')
-rw-r--r-- | middleware/metrics/handler.go | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/middleware/metrics/handler.go b/middleware/metrics/handler.go new file mode 100644 index 000000000..b3ef79554 --- /dev/null +++ b/middleware/metrics/handler.go @@ -0,0 +1,42 @@ +package metrics + +import ( + "time" + + "golang.org/x/net/context" + + "github.com/miekg/coredns/middleware" + "github.com/miekg/dns" +) + +func (m Metrics) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { + state := middleware.State{W: w, Req: r} + qname := state.Name() + qtype := state.Type() + zone := middleware.Zones(m.ZoneNames).Matches(qname) + if zone == "" { + zone = "." + } + + // Record response to get status code and size of the reply. + rw := middleware.NewResponseRecorder(w) + status, err := m.Next.ServeDNS(ctx, rw, r) + + Report(zone, qtype, rw.Rcode(), rw.Size(), rw.Start()) + + return status, err +} + +// Report is a plain reporting function that the server can use for REFUSED and other +// queries that are turned down because they don't match any middleware. +func Report(zone, qtype, rcode string, size int, start time.Time) { + if requestCount == nil { + // no metrics are enabled + return + } + + requestCount.WithLabelValues(zone, qtype).Inc() + requestDuration.WithLabelValues(zone, qtype).Observe(float64(time.Since(start) / time.Second)) + responseSize.WithLabelValues(zone, qtype).Observe(float64(size)) + responseRcode.WithLabelValues(zone, rcode, qtype).Inc() +} |