From 53a0036b596e0d3f5a5ff6e18e5d8fe841de5d17 Mon Sep 17 00:00:00 2001 From: pegasko Date: Sun, 29 Oct 2023 20:44:47 +0300 Subject: [PATCH] poke 2 --- dnsstat.go | 26 +++++++++++--------------- metrics.go | 11 +++++++++-- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/dnsstat.go b/dnsstat.go index 4b888cf..5feef30 100644 --- a/dnsstat.go +++ b/dnsstat.go @@ -7,15 +7,11 @@ import ( "github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin/metrics" - clog "github.com/coredns/coredns/plugin/pkg/log" + "github.com/coredns/coredns/request" "github.com/miekg/dns" ) -// Define log to be a logger with the plugin name in it. This way we can just use log.Info and -// friends to log. -var log = clog.NewWithPlugin("dnsstat") - // Dump implement the plugin interface. type DNSStat struct { Next plugin.Handler @@ -39,16 +35,16 @@ func setup(c *caddy.Controller) error { // ServeDNS implements the plugin.Handler interface. func (d DNSStat) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { - - // Debug log that we've have seen the query. This will only be shown when the debug plugin is loaded. - log.Error("Received response") - - // Wrap. - pw := NewResponsePrinter(w) - - // Metric increment - requestCount.WithLabelValues(metrics.WithServer(ctx)).Inc() - return plugin.NextOrFailure(d.Name(), d.Next, ctx, pw, r) + state := request.Request{W: w, Req: r} + requestCount.WithLabelValues( + metrics.WithServer(ctx), + state.Zone, + state.Class(), + state.Type(), + state.Name(), + state.IP(), + ).Inc() + return plugin.NextOrFailure(d.Name(), d.Next, ctx, w, r) } // Name implements the Handler interface. diff --git a/metrics.go b/metrics.go index 316f164..4b3deb5 100644 --- a/metrics.go +++ b/metrics.go @@ -9,12 +9,19 @@ import ( "github.com/prometheus/client_golang/prometheus/promauto" ) -// requestCount exports a prometheus metric that is incremented every time a query is seen by the example plugin. +// Track total requests made to given domain name + misc var requestCount = promauto.NewCounterVec(prometheus.CounterOpts{ Namespace: plugin.Namespace, Subsystem: "dnsstat", Name: "dnsstat_request_count_total", Help: "Counter of requests made.", -}, []string{"server"}) +}, []string{ + "server", + "zone", + "class", + "type", + "name", + "client_ip", +}) var once sync.Once