Parcourir la source

Merge pull request #5 from coredns/metrics

add metrics in the example
Miek Gieben il y a 7 ans
Parent
commit
734a8a4cbb
4 fichiers modifiés avec 46 ajouts et 1 suppressions
  1. 10 0
      README.md
  2. 7 1
      example.go
  3. 20 0
      metrics.go
  4. 9 0
      setup.go

+ 10 - 0
README.md

@@ -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).

+ 7 - 1
example.go

@@ -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 - 0
metrics.go

@@ -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

+ 9 - 0
setup.go

@@ -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}