mirror of
https://github.com/coredns/coredns.git
synced 2025-11-01 10:43:17 -04:00
middleware/metrics: cleanup (#355)
* middleware/metrics: add more metrics middleware/cache: Add metrics for number of elements in the cache. Also export the total size. Update README to detail the new metrics. middleware/metrics Move metrics into subpackage called "vars". This breaks the import cycle and is cleaner. This allows vars.Report to be used in the the dnsserver to log refused queries. middleware/metrics: tests Add tests to the metrics framework. The metrics/test subpackage allows scraping of the local server. Do a few test scrape of the metrics that are defined in the metrics middleware. This also allows metrics integration tests to check if the caching and dnssec middleware export their metrics correctly. * update README * typos * fix tests
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"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"
|
||||
@@ -17,7 +16,7 @@ func (m *Metrics) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg
|
||||
state := request.Request{W: w, Req: r}
|
||||
|
||||
qname := state.QName()
|
||||
zone := middleware.Zones(m.ZoneNames).Matches(qname)
|
||||
zone := middleware.Zones(m.ZoneNames()).Matches(qname)
|
||||
if zone == "" {
|
||||
zone = "."
|
||||
}
|
||||
@@ -26,71 +25,9 @@ func (m *Metrics) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg
|
||||
rw := dnsrecorder.New(w)
|
||||
status, err := m.Next.ServeDNS(ctx, rw, r)
|
||||
|
||||
Report(state, zone, rcode.ToString(rw.Rcode), rw.Size, rw.Start)
|
||||
vars.Report(state, zone, rcode.ToString(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(req request.Request, zone, rcode string, size int, start time.Time) {
|
||||
if requestCount == nil {
|
||||
// no metrics are enabled
|
||||
return
|
||||
}
|
||||
|
||||
// Proto and Family
|
||||
net := req.Proto()
|
||||
fam := "1"
|
||||
if req.Family() == 2 {
|
||||
fam = "2"
|
||||
}
|
||||
|
||||
typ := req.QType()
|
||||
|
||||
requestCount.WithLabelValues(zone, net, fam).Inc()
|
||||
requestDuration.WithLabelValues(zone).Observe(float64(time.Since(start) / time.Millisecond))
|
||||
|
||||
if req.Do() {
|
||||
requestDo.WithLabelValues(zone).Inc()
|
||||
}
|
||||
|
||||
if _, known := monitorType[typ]; known {
|
||||
requestType.WithLabelValues(zone, dns.Type(typ).String()).Inc()
|
||||
} else {
|
||||
requestType.WithLabelValues(zone, other).Inc()
|
||||
}
|
||||
|
||||
if typ == dns.TypeIXFR || typ == dns.TypeAXFR {
|
||||
responseTransferSize.WithLabelValues(zone, net).Observe(float64(size))
|
||||
requestTransferSize.WithLabelValues(zone, net).Observe(float64(req.Size()))
|
||||
} else {
|
||||
responseSize.WithLabelValues(zone, net).Observe(float64(size))
|
||||
requestSize.WithLabelValues(zone, net).Observe(float64(req.Size()))
|
||||
}
|
||||
|
||||
responseRcode.WithLabelValues(zone, rcode).Inc()
|
||||
}
|
||||
|
||||
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,
|
||||
// Meta Qtypes
|
||||
dns.TypeIXFR: true,
|
||||
dns.TypeAXFR: true,
|
||||
dns.TypeANY: true,
|
||||
}
|
||||
|
||||
const other = "other"
|
||||
func (m *Metrics) Name() string { return "prometheus" }
|
||||
|
||||
Reference in New Issue
Block a user