2016-10-26 10:01:52 +01:00
|
|
|
package vars
|
|
|
|
|
|
|
|
|
|
import (
|
2018-04-22 08:34:35 +01:00
|
|
|
"context"
|
2016-10-26 10:01:52 +01:00
|
|
|
"time"
|
|
|
|
|
|
2018-04-18 09:42:20 +01:00
|
|
|
"github.com/coredns/coredns/plugin"
|
2017-02-21 22:51:47 -08:00
|
|
|
"github.com/coredns/coredns/request"
|
2016-10-26 10:01:52 +01:00
|
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
|
)
|
|
|
|
|
|
2018-08-14 17:55:55 +02:00
|
|
|
// Report reports the metrics data associated with request.
|
2018-04-18 09:42:20 +01:00
|
|
|
func Report(ctx context.Context, req request.Request, zone, rcode string, size int, start time.Time) {
|
2016-11-29 11:02:43 +00:00
|
|
|
// Proto and Family.
|
2016-10-26 10:01:52 +01:00
|
|
|
net := req.Proto()
|
|
|
|
|
fam := "1"
|
|
|
|
|
if req.Family() == 2 {
|
|
|
|
|
fam = "2"
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-18 09:42:20 +01:00
|
|
|
server := WithServer(ctx)
|
2016-10-26 10:01:52 +01:00
|
|
|
|
2018-04-18 09:42:20 +01:00
|
|
|
typ := req.QType()
|
|
|
|
|
RequestCount.WithLabelValues(server, zone, net, fam).Inc()
|
|
|
|
|
RequestDuration.WithLabelValues(server, zone).Observe(time.Since(start).Seconds())
|
2016-10-26 10:01:52 +01:00
|
|
|
|
|
|
|
|
if req.Do() {
|
2018-04-18 09:42:20 +01:00
|
|
|
RequestDo.WithLabelValues(server, zone).Inc()
|
2016-10-26 10:01:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if _, known := monitorType[typ]; known {
|
2018-04-18 09:42:20 +01:00
|
|
|
RequestType.WithLabelValues(server, zone, dns.Type(typ).String()).Inc()
|
2016-10-26 10:01:52 +01:00
|
|
|
} else {
|
2018-04-18 09:42:20 +01:00
|
|
|
RequestType.WithLabelValues(server, zone, other).Inc()
|
2016-10-26 10:01:52 +01:00
|
|
|
}
|
|
|
|
|
|
2018-04-18 09:42:20 +01:00
|
|
|
ResponseSize.WithLabelValues(server, zone, net).Observe(float64(size))
|
|
|
|
|
RequestSize.WithLabelValues(server, zone, net).Observe(float64(req.Len()))
|
2016-10-26 10:01:52 +01:00
|
|
|
|
2018-04-18 09:42:20 +01:00
|
|
|
ResponseRcode.WithLabelValues(server, zone, rcode).Inc()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithServer returns the current server handling the request.
|
|
|
|
|
func WithServer(ctx context.Context) string {
|
|
|
|
|
srv := ctx.Value(plugin.ServerCtx{})
|
|
|
|
|
if srv == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
return srv.(string)
|
2016-10-26 10:01:52 +01:00
|
|
|
}
|
|
|
|
|
|
2018-12-10 10:17:15 +00:00
|
|
|
var monitorType = map[uint16]struct{}{
|
|
|
|
|
dns.TypeAAAA: struct{}{},
|
|
|
|
|
dns.TypeA: struct{}{},
|
|
|
|
|
dns.TypeCNAME: struct{}{},
|
|
|
|
|
dns.TypeDNSKEY: struct{}{},
|
|
|
|
|
dns.TypeDS: struct{}{},
|
|
|
|
|
dns.TypeMX: struct{}{},
|
|
|
|
|
dns.TypeNSEC3: struct{}{},
|
|
|
|
|
dns.TypeNSEC: struct{}{},
|
|
|
|
|
dns.TypeNS: struct{}{},
|
|
|
|
|
dns.TypePTR: struct{}{},
|
|
|
|
|
dns.TypeRRSIG: struct{}{},
|
|
|
|
|
dns.TypeSOA: struct{}{},
|
|
|
|
|
dns.TypeSRV: struct{}{},
|
|
|
|
|
dns.TypeTXT: struct{}{},
|
2016-10-26 10:01:52 +01:00
|
|
|
// Meta Qtypes
|
2018-12-10 10:17:15 +00:00
|
|
|
dns.TypeIXFR: struct{}{},
|
|
|
|
|
dns.TypeAXFR: struct{}{},
|
|
|
|
|
dns.TypeANY: struct{}{},
|
2016-10-26 10:01:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const other = "other"
|