plugin/template: update metrics to include server (#1731)

Add server label to the metrics and update the README.
This commit is contained in:
Miek Gieben 2018-04-27 19:37:12 +01:00 committed by GitHub
parent 13b1f5469a
commit 2a28efa877
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 19 deletions

View file

@ -64,11 +64,12 @@ The output of the template must be a [RFC 1035](https://tools.ietf.org/html/rfc1
If monitoring is enabled (via the *prometheus* directive) then the following metrics are exported: If monitoring is enabled (via the *prometheus* directive) then the following metrics are exported:
* `coredns_template_matches_total{regex}` the total number of matched requests by regex. * `coredns_template_matches_total{server, regex}` the total number of matched requests by regex.
* `coredns_template_template_failures_total{regex,section,template}` the number of times the Go templating failed. Regex, section and template label values can be used to map the error back to the config file. * `coredns_template_template_failures_total{server, regex,section,template}` the number of times the Go templating failed. Regex, section and template label values can be used to map the error back to the config file.
* `coredns_template_rr_failures_total{regex,section,template}` the number of times the templated resource record was invalid and could not be parsed. Regex, section and template label values can be used to map the error back to the config file. * `coredns_template_rr_failures_total{server, regex,section,template}` the number of times the templated resource record was invalid and could not be parsed. Regex, section and template label values can be used to map the error back to the config file.
Both failure cases indicate a problem with the template configuration. Both failure cases indicate a problem with the template configuration. The `server` label indicates
the server incrementing the metric, see the *metrics* plugin for details.
## Examples ## Examples

View file

@ -10,33 +10,32 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
) )
// Metrics for template.
var ( var (
TemplateMatchesCount = prometheus.NewCounterVec(prometheus.CounterOpts{ templateMatchesCount = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: plugin.Namespace, Namespace: plugin.Namespace,
Subsystem: "template", Subsystem: "template",
Name: "matches_total", Name: "matches_total",
Help: "Counter of template regex matches.", Help: "Counter of template regex matches.",
}, []string{"zone", "class", "type"}) }, []string{"server", "zone", "class", "type"})
TemplateFailureCount = prometheus.NewCounterVec(prometheus.CounterOpts{ templateFailureCount = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: plugin.Namespace, Namespace: plugin.Namespace,
Subsystem: "template", Subsystem: "template",
Name: "template_failures_total", Name: "template_failures_total",
Help: "Counter of go template failures.", Help: "Counter of go template failures.",
}, []string{"zone", "class", "type", "section", "template"}) }, []string{"server", "zone", "class", "type", "section", "template"})
TemplateRRFailureCount = prometheus.NewCounterVec(prometheus.CounterOpts{ templateRRFailureCount = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: plugin.Namespace, Namespace: plugin.Namespace,
Subsystem: "template", Subsystem: "template",
Name: "rr_failures_total", Name: "rr_failures_total",
Help: "Counter of mis-templated RRs.", Help: "Counter of mis-templated RRs.",
}, []string{"zone", "class", "type", "section", "template"}) }, []string{"server", "zone", "class", "type", "section", "template"})
) )
// OnStartupMetrics sets up the metrics on startup. // OnStartupMetrics sets up the metrics on startup.
func setupMetrics(c *caddy.Controller) error { func setupMetrics(c *caddy.Controller) error {
c.OnStartup(func() error { c.OnStartup(func() error {
metricsOnce.Do(func() { metricsOnce.Do(func() {
metrics.MustRegister(c, TemplateMatchesCount, TemplateFailureCount, TemplateRRFailureCount) metrics.MustRegister(c, templateMatchesCount, templateFailureCount, templateRRFailureCount)
}) })
return nil return nil
}) })

View file

@ -8,6 +8,7 @@ import (
gotmpl "text/template" gotmpl "text/template"
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metrics"
"github.com/coredns/coredns/plugin/pkg/fall" "github.com/coredns/coredns/plugin/pkg/fall"
"github.com/coredns/coredns/plugin/pkg/upstream" "github.com/coredns/coredns/plugin/pkg/upstream"
"github.com/coredns/coredns/request" "github.com/coredns/coredns/request"
@ -66,7 +67,7 @@ func (h Handler) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
continue continue
} }
TemplateMatchesCount.WithLabelValues(data.Zone, data.Class, data.Type).Inc() templateMatchesCount.WithLabelValues(metrics.WithServer(ctx), data.Zone, data.Class, data.Type).Inc()
if template.rcode == dns.RcodeServerFailure { if template.rcode == dns.RcodeServerFailure {
return template.rcode, nil return template.rcode, nil
@ -78,7 +79,7 @@ func (h Handler) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
msg.Rcode = template.rcode msg.Rcode = template.rcode
for _, answer := range template.answer { for _, answer := range template.answer {
rr, err := executeRRTemplate("answer", answer, data) rr, err := executeRRTemplate(metrics.WithServer(ctx), "answer", answer, data)
if err != nil { if err != nil {
return dns.RcodeServerFailure, err return dns.RcodeServerFailure, err
} }
@ -89,14 +90,14 @@ func (h Handler) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
} }
} }
for _, additional := range template.additional { for _, additional := range template.additional {
rr, err := executeRRTemplate("additional", additional, data) rr, err := executeRRTemplate(metrics.WithServer(ctx), "additional", additional, data)
if err != nil { if err != nil {
return dns.RcodeServerFailure, err return dns.RcodeServerFailure, err
} }
msg.Extra = append(msg.Extra, rr) msg.Extra = append(msg.Extra, rr)
} }
for _, authority := range template.authority { for _, authority := range template.authority {
rr, err := executeRRTemplate("authority", authority, data) rr, err := executeRRTemplate(metrics.WithServer(ctx), "authority", authority, data)
if err != nil { if err != nil {
return dns.RcodeServerFailure, err return dns.RcodeServerFailure, err
} }
@ -114,16 +115,16 @@ func (h Handler) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
// Name implements the plugin.Handler interface. // Name implements the plugin.Handler interface.
func (h Handler) Name() string { return "template" } func (h Handler) Name() string { return "template" }
func executeRRTemplate(section string, template *gotmpl.Template, data templateData) (dns.RR, error) { func executeRRTemplate(server, section string, template *gotmpl.Template, data templateData) (dns.RR, error) {
buffer := &bytes.Buffer{} buffer := &bytes.Buffer{}
err := template.Execute(buffer, data) err := template.Execute(buffer, data)
if err != nil { if err != nil {
TemplateFailureCount.WithLabelValues(data.Zone, data.Class, data.Type, section, template.Tree.Root.String()).Inc() templateFailureCount.WithLabelValues(server, data.Zone, data.Class, data.Type, section, template.Tree.Root.String()).Inc()
return nil, err return nil, err
} }
rr, err := dns.NewRR(buffer.String()) rr, err := dns.NewRR(buffer.String())
if err != nil { if err != nil {
TemplateRRFailureCount.WithLabelValues(data.Zone, data.Class, data.Type, section, template.Tree.Root.String()).Inc() templateRRFailureCount.WithLabelValues(server, data.Zone, data.Class, data.Type, section, template.Tree.Root.String()).Inc()
return rr, err return rr, err
} }
return rr, nil return rr, nil