Merge pull request #5 from coredns/metrics

add metrics in the example
This commit is contained in:
Miek Gieben 2018-04-01 13:57:41 +01:00 committed by GitHub
commit 734a8a4cbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 1 deletions

View File

@ -15,6 +15,12 @@ writing CoreDNS plugins.
example example
~~~ ~~~
## Metrics
If monitoring is enabled (via the *prometheus* directive) the following metric is exported:
* `coredns_example_request_count_total{server}` - query count to the *example* plugin.
## Examples ## Examples
In this configuration, we forward all queries to 9.9.9.9 and print "example" whenever we recieve In this configuration, we forward all queries to 9.9.9.9 and print "example" whenever we recieve
@ -26,3 +32,7 @@ a query.
example example
} }
``` ```
## Also See
See the [manual](https://coredns.io/manual).

View File

@ -1,4 +1,6 @@
// The example plugin prints example to stdout on every packet received. // Package example is a CoreDNS plugin that prints "example" to stdout on every packet received.
//
// It serves as an example CoreDNS plugin with numerous code comments.
package example package example
import ( import (
@ -7,6 +9,7 @@ import (
"os" "os"
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metrics"
"github.com/miekg/dns" "github.com/miekg/dns"
"golang.org/x/net/context" "golang.org/x/net/context"
@ -28,6 +31,9 @@ func (e Example) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
// Wrap. // Wrap.
pw := NewResponsePrinter(w) pw := NewResponsePrinter(w)
// Export metric with the server label set to the current server handling the request.
requestCount.WithLabelValues(metrics.WithServer(ctx)).Add(1)
// Call next plugin (if any). // Call next plugin (if any).
return plugin.NextOrFailure(e.Name(), e.Next, ctx, pw, r) return plugin.NextOrFailure(e.Name(), e.Next, ctx, pw, r)
} }

20
metrics.go Normal file
View File

@ -0,0 +1,20 @@
package example
import (
"sync"
"github.com/coredns/coredns/plugin"
"github.com/prometheus/client_golang/prometheus"
)
// requestCount exports a prometheus metric that is incremented every time
// a query is seen by the example plugin.
var requestCount = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: plugin.Namespace,
Subsystem: "example",
Name: "request_count_total",
Help: "Counter of requests made.",
}, []string{"server"})
var once sync.Once

View File

@ -3,6 +3,7 @@ package example
import ( import (
"github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metrics"
"github.com/mholt/caddy" "github.com/mholt/caddy"
) )
@ -27,6 +28,14 @@ func setup(c *caddy.Controller) error {
return plugin.Error("example", c.ArgErr()) return plugin.Error("example", c.ArgErr())
} }
// Add a startup function that will -- after all plugins have been loaded -- check if the
// prometheus plugin has been used - if so we will export metrics. We can only register
// this metric once, hence the "once.Do".
c.OnStartup(func() error {
once.Do(func() { metrics.MustRegister(c, requestCount) })
return nil
})
// Add the Plugin to CoreDNS, so Servers can use it in their plugin chain. // Add the Plugin to CoreDNS, so Servers can use it in their plugin chain.
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler { dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
return Example{Next: next} return Example{Next: next}