From 4df416ca1da2fff396ff84805518aa8f8b55b80b Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Sun, 1 Apr 2018 13:57:03 +0100 Subject: [PATCH] Metrics (#1579) * plugin/metrics: set server address in context Allow cross server block metrics to co-exist; for this we should label each metric with the server label. Put this information in the context and provide a helper function to get it out. Abstracting with entirely away with difficult as the release client_go (0.8.0) doesn't have the CurryWith functions yet. So current use is like so: define metric, with server label: RcodeCount = prometheus.NewCounterVec(prometheus.CounterOpts{ Namespace: plugin.Namespace, Subsystem: "forward", Name: "response_rcode_count_total", Help: "Counter of requests made per upstream.", }, []string{"server", "rcode", "to"}) And report ith with the helper function metrics.WithServer: RcodeCount.WithLabelValues(metrics.WithServer(ctx), rc, p.addr).Add(1) --- core/dnsserver/server.go | 19 ++++++++++++++----- plugin/metrics/context.go | 24 ++++++++++++++++++++++++ plugin/metrics/metrics.go | 1 + plugin/plugin.go | 6 ++++-- 4 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 plugin/metrics/context.go diff --git a/core/dnsserver/server.go b/core/dnsserver/server.go index 2fa67be05..dbc7bd5ce 100644 --- a/core/dnsserver/server.go +++ b/core/dnsserver/server.go @@ -245,6 +245,11 @@ func (s *Server) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) } if h, ok := s.zones[string(b[:l])]; ok { + + // Set server's address in the context so plugins can reference back to this, + // This will makes those metrics unique. + ctx = context.WithValue(ctx, plugin.ServerCtx{}, s.Addr) + if r.Question[0].Qtype != dns.TypeDS { if h.FilterFunc == nil { rcode, _ := h.pluginChain.ServeDNS(ctx, w, r) @@ -287,6 +292,10 @@ func (s *Server) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) // Wildcard match, if we have found nothing try the root zone as a last resort. if h, ok := s.zones["."]; ok && h.pluginChain != nil { + + // See comment above. + ctx = context.WithValue(ctx, plugin.ServerCtx{}, s.Addr) + rcode, _ := h.pluginChain.ServeDNS(ctx, w, r) if !plugin.ClientWrite(rcode) { DefaultErrorFunc(w, r, rcode) @@ -359,11 +368,11 @@ const ( maxreentries = 10 ) -// Key is the context key for the current server -type Key struct{} - -// loopKey is the context key for counting self loops -type loopKey struct{} +type ( + // Key is the context key for the current server + Key struct{} + loopKey struct{} // loopKey is the context key for counting self loops +) // enableChaos is a map with plugin names for which we should open CH class queries as // we block these by default. diff --git a/plugin/metrics/context.go b/plugin/metrics/context.go new file mode 100644 index 000000000..4cce871f5 --- /dev/null +++ b/plugin/metrics/context.go @@ -0,0 +1,24 @@ +package metrics + +import ( + "github.com/coredns/coredns/plugin" + + "golang.org/x/net/context" +) + +// WithServer returns the current server handling the request. It returns the +// server listening address: ://[]: Normally this is +// something like "dns://:53", but if the bind plugin is used, i.e. "bind +// 127.0.0.53", it will be "dns://127.0.0.53:53", etc. If not address is found +// the empty string is returned. +// +// Basic usage with a 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) +} diff --git a/plugin/metrics/metrics.go b/plugin/metrics/metrics.go index a49984b3d..e16749fc0 100644 --- a/plugin/metrics/metrics.go +++ b/plugin/metrics/metrics.go @@ -12,6 +12,7 @@ import ( "github.com/coredns/coredns/coremain" "github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin/metrics/vars" + "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" ) diff --git a/plugin/plugin.go b/plugin/plugin.go index 2510dbaa6..b3d5cc6ab 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -68,8 +68,7 @@ func (f HandlerFunc) Name() string { return "handlerfunc" } // Error returns err with 'plugin/name: ' prefixed to it. func Error(name string, err error) error { return fmt.Errorf("%s/%s: %s", "plugin", name, err) } -// NextOrFailure calls next.ServeDNS when next is not nill, otherwise it will return, a ServerFailure -// and a nil error. +// NextOrFailure calls next.ServeDNS when next is not nill, otherwise it will return, a ServerFailure and a nil error. func NextOrFailure(name string, next Handler, ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { // nolint: golint if next != nil { if span := ot.SpanFromContext(ctx); span != nil { @@ -107,3 +106,6 @@ var TimeBuckets = prometheus.ExponentialBuckets(0.00025, 2, 16) // from 0.25ms t // ErrOnce is returned when a plugin doesn't support multiple setups per server. var ErrOnce = errors.New("this plugin can only be used once per Server Block") + +// ServerCtx is the context key to pass server address context to the plugins handling the request. +type ServerCtx struct{}