mirror of
https://github.com/coredns/coredns.git
synced 2025-10-27 08:14:18 -04:00
plugin/metrics: add 'server' label (#1682)
* plugin/metrics: add 'server' label This uses the new WithServer(ctx) to get the current server from the context. First in a larger refactor to make all plugins do this. * compile * compile * lala test * compile and test * typos * Dont duplicate the code
This commit is contained in:
@@ -11,18 +11,21 @@ The default location for the metrics is `localhost:9153`. The metrics path is fi
|
||||
The following metrics are exported:
|
||||
|
||||
* `coredns_build_info{version, revision, goversion}` - info about CoreDNS itself.
|
||||
* `coredns_dns_request_count_total{zone, proto, family}` - total query count.
|
||||
* `coredns_dns_request_duration_seconds{zone}` - duration to process each query.
|
||||
* `coredns_dns_request_size_bytes{zone, proto}` - size of the request in bytes.
|
||||
* `coredns_dns_request_do_count_total{zone}` - queries that have the DO bit set
|
||||
* `coredns_dns_request_type_count_total{zone, type}` - counter of queries per zone and type.
|
||||
* `coredns_dns_response_size_bytes{zone, proto}` - response size in bytes.
|
||||
* `coredns_dns_response_rcode_count_total{zone, rcode}` - response per zone and rcode.
|
||||
* `coredns_dns_request_count_total{server, zone, proto, family}` - total query count.
|
||||
* `coredns_dns_request_duration_seconds{server, zone}` - duration to process each query.
|
||||
* `coredns_dns_request_size_bytes{server, zone, proto}` - size of the request in bytes.
|
||||
* `coredns_dns_request_do_count_total{server, zone}` - queries that have the DO bit set
|
||||
* `coredns_dns_request_type_count_total{server, zone, type}` - counter of queries per zone and type.
|
||||
* `coredns_dns_response_size_bytes{server, zone, proto}` - response size in bytes.
|
||||
* `coredns_dns_response_rcode_count_total{server, zone, rcode}` - response per zone and rcode.
|
||||
|
||||
Each counter has a label `zone` which is the zonename used for the request/response.
|
||||
|
||||
Extra labels used are:
|
||||
|
||||
* `server` is identifying the server responsible for the request. This is a string formatted
|
||||
as the server's listening address: `<scheme>://[<bind>]:<port>`. I.e. for a "normal" DNS server
|
||||
this is `dns://:53`. If you are using the *bind* plugin an IP address is included, e.g.: `dns://127.0.0.53:53`.
|
||||
* `proto` which holds the transport of the response ("udp" or "tcp")
|
||||
* The address family (`family`) of the transport (1 = IP (IP version 4), 2 = IP6 (IP version 6)).
|
||||
* `type` which holds the query type. It holds most common types (A, AAAA, MX, SOA, CNAME, PTR, TXT,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"github.com/coredns/coredns/plugin"
|
||||
"github.com/coredns/coredns/plugin/metrics/vars"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
@@ -15,10 +15,4 @@ import (
|
||||
// Basic usage with a metric:
|
||||
//
|
||||
// <metric>.WithLabelValues(metrics.WithServer(ctx), labels..).Add(1)
|
||||
func WithServer(ctx context.Context) string {
|
||||
srv := ctx.Value(plugin.ServerCtx{})
|
||||
if srv == nil {
|
||||
return ""
|
||||
}
|
||||
return srv.(string)
|
||||
}
|
||||
func WithServer(ctx context.Context) string { return vars.WithServer(ctx) }
|
||||
|
||||
@@ -25,7 +25,7 @@ func (m *Metrics) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg
|
||||
rw := dnstest.NewRecorder(w)
|
||||
status, err := plugin.NextOrFailure(m.Name(), m.Next, ctx, rw, r)
|
||||
|
||||
vars.Report(state, zone, rcode.ToString(rw.Rcode), rw.Len, rw.Start)
|
||||
vars.Report(ctx, state, zone, rcode.ToString(rw.Rcode), rw.Len, rw.Start)
|
||||
|
||||
return status, err
|
||||
}
|
||||
|
||||
@@ -3,13 +3,15 @@ package vars
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/coredns/coredns/plugin"
|
||||
"github.com/coredns/coredns/request"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// Report reports the metrics data associcated with request.
|
||||
func Report(req request.Request, zone, rcode string, size int, start time.Time) {
|
||||
func Report(ctx context.Context, req request.Request, zone, rcode string, size int, start time.Time) {
|
||||
// Proto and Family.
|
||||
net := req.Proto()
|
||||
fam := "1"
|
||||
@@ -17,25 +19,35 @@ func Report(req request.Request, zone, rcode string, size int, start time.Time)
|
||||
fam = "2"
|
||||
}
|
||||
|
||||
typ := req.QType()
|
||||
server := WithServer(ctx)
|
||||
|
||||
RequestCount.WithLabelValues(zone, net, fam).Inc()
|
||||
RequestDuration.WithLabelValues(zone).Observe(time.Since(start).Seconds())
|
||||
typ := req.QType()
|
||||
RequestCount.WithLabelValues(server, zone, net, fam).Inc()
|
||||
RequestDuration.WithLabelValues(server, zone).Observe(time.Since(start).Seconds())
|
||||
|
||||
if req.Do() {
|
||||
RequestDo.WithLabelValues(zone).Inc()
|
||||
RequestDo.WithLabelValues(server, zone).Inc()
|
||||
}
|
||||
|
||||
if _, known := monitorType[typ]; known {
|
||||
RequestType.WithLabelValues(zone, dns.Type(typ).String()).Inc()
|
||||
RequestType.WithLabelValues(server, zone, dns.Type(typ).String()).Inc()
|
||||
} else {
|
||||
RequestType.WithLabelValues(zone, other).Inc()
|
||||
RequestType.WithLabelValues(server, zone, other).Inc()
|
||||
}
|
||||
|
||||
ResponseSize.WithLabelValues(zone, net).Observe(float64(size))
|
||||
RequestSize.WithLabelValues(zone, net).Observe(float64(req.Len()))
|
||||
ResponseSize.WithLabelValues(server, zone, net).Observe(float64(size))
|
||||
RequestSize.WithLabelValues(server, zone, net).Observe(float64(req.Len()))
|
||||
|
||||
ResponseRcode.WithLabelValues(zone, rcode).Inc()
|
||||
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)
|
||||
}
|
||||
|
||||
var monitorType = map[uint16]bool{
|
||||
|
||||
@@ -13,7 +13,7 @@ var (
|
||||
Subsystem: subsystem,
|
||||
Name: "request_count_total",
|
||||
Help: "Counter of DNS requests made per zone, protocol and family.",
|
||||
}, []string{"zone", "proto", "family"})
|
||||
}, []string{"server", "zone", "proto", "family"})
|
||||
|
||||
RequestDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
|
||||
Namespace: plugin.Namespace,
|
||||
@@ -21,7 +21,7 @@ var (
|
||||
Name: "request_duration_seconds",
|
||||
Buckets: plugin.TimeBuckets,
|
||||
Help: "Histogram of the time (in seconds) each request took.",
|
||||
}, []string{"zone"})
|
||||
}, []string{"server", "zone"})
|
||||
|
||||
RequestSize = prometheus.NewHistogramVec(prometheus.HistogramOpts{
|
||||
Namespace: plugin.Namespace,
|
||||
@@ -29,21 +29,21 @@ var (
|
||||
Name: "request_size_bytes",
|
||||
Help: "Size of the EDNS0 UDP buffer in bytes (64K for TCP).",
|
||||
Buckets: []float64{0, 100, 200, 300, 400, 511, 1023, 2047, 4095, 8291, 16e3, 32e3, 48e3, 64e3},
|
||||
}, []string{"zone", "proto"})
|
||||
}, []string{"server", "zone", "proto"})
|
||||
|
||||
RequestDo = prometheus.NewCounterVec(prometheus.CounterOpts{
|
||||
Namespace: plugin.Namespace,
|
||||
Subsystem: subsystem,
|
||||
Name: "request_do_count_total",
|
||||
Help: "Counter of DNS requests with DO bit set per zone.",
|
||||
}, []string{"zone"})
|
||||
}, []string{"server", "zone"})
|
||||
|
||||
RequestType = prometheus.NewCounterVec(prometheus.CounterOpts{
|
||||
Namespace: plugin.Namespace,
|
||||
Subsystem: subsystem,
|
||||
Name: "request_type_count_total",
|
||||
Help: "Counter of DNS requests per type, per zone.",
|
||||
}, []string{"zone", "type"})
|
||||
}, []string{"server", "zone", "type"})
|
||||
|
||||
ResponseSize = prometheus.NewHistogramVec(prometheus.HistogramOpts{
|
||||
Namespace: plugin.Namespace,
|
||||
@@ -51,14 +51,14 @@ var (
|
||||
Name: "response_size_bytes",
|
||||
Help: "Size of the returned response in bytes.",
|
||||
Buckets: []float64{0, 100, 200, 300, 400, 511, 1023, 2047, 4095, 8291, 16e3, 32e3, 48e3, 64e3},
|
||||
}, []string{"zone", "proto"})
|
||||
}, []string{"server", "zone", "proto"})
|
||||
|
||||
ResponseRcode = prometheus.NewCounterVec(prometheus.CounterOpts{
|
||||
Namespace: plugin.Namespace,
|
||||
Subsystem: subsystem,
|
||||
Name: "response_rcode_count_total",
|
||||
Help: "Counter of response status codes.",
|
||||
}, []string{"zone", "rcode"})
|
||||
}, []string{"server", "zone", "rcode"})
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
Reference in New Issue
Block a user