Files
coredns/middleware/metrics/handler.go

80 lines
1.9 KiB
Go
Raw Normal View History

2016-03-18 20:57:35 +00:00
package metrics
import (
"time"
"github.com/miekg/coredns/middleware"
2016-03-18 20:57:35 +00:00
"github.com/miekg/dns"
"golang.org/x/net/context"
2016-03-18 20:57:35 +00:00
)
func (m Metrics) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
state := middleware.State{W: w, Req: r}
2016-06-23 11:21:12 +01:00
qname := state.QName()
2016-03-18 20:57:35 +00:00
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)
2016-03-18 20:57:35 +00:00
2016-06-23 11:21:12 +01:00
Report(state, zone, rw.Rcode(), rw.Size(), rw.Start())
2016-04-06 13:42:56 +01:00
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.
2016-06-23 11:21:12 +01:00
func Report(state middleware.State, zone, rcode string, size int, start time.Time) {
if requestCount == nil {
// no metrics are enabled
return
}
2016-06-23 11:21:12 +01:00
// Proto and Family
net := state.Proto()
fam := "1"
if state.Family() == 2 {
fam = "2"
}
requestCount.WithLabelValues(zone, net, fam).Inc()
requestDuration.WithLabelValues(zone).Observe(float64(time.Since(start) / time.Second))
2016-06-24 08:37:23 +00:00
requestSize.WithLabelValues(zone, net).Observe(float64(state.Size()))
2016-06-23 11:21:12 +01:00
if state.Do() {
requestDo.WithLabelValues(zone).Inc()
}
2016-06-25 18:12:13 +01:00
typ := state.QType()
if _, known := monitorType[typ]; known {
requestType.WithLabelValues(zone, dns.Type(typ).String()).Inc()
2016-06-25 18:12:13 +01:00
} else {
requestType.WithLabelValues(zone, other).Inc()
2016-06-25 18:12:13 +01:00
}
2016-06-23 11:21:12 +01:00
responseSize.WithLabelValues(zone, net).Observe(float64(size))
responseRcode.WithLabelValues(zone, rcode).Inc()
2016-03-18 20:57:35 +00:00
}
2016-06-25 18:12:13 +01:00
var monitorType = map[uint16]bool{
dns.TypeAAAA: true,
dns.TypeA: true,
dns.TypeCNAME: true,
dns.TypeDNSKEY: true,
dns.TypeDS: true,
dns.TypeMX: true,
dns.TypeNSEC3: true,
dns.TypeNSEC: true,
dns.TypeNS: true,
dns.TypePTR: true,
dns.TypeRRSIG: true,
dns.TypeSOA: true,
dns.TypeSRV: true,
dns.TypeTXT: true,
}
const other = "other"