This commit is contained in:
pegasko 2023-10-29 20:44:47 +03:00
parent f7ed1accfc
commit 53a0036b59
2 changed files with 20 additions and 17 deletions

View file

@ -7,15 +7,11 @@ 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/coredns/coredns/plugin/metrics"
clog "github.com/coredns/coredns/plugin/pkg/log" "github.com/coredns/coredns/request"
"github.com/miekg/dns" "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. // Dump implement the plugin interface.
type DNSStat struct { type DNSStat struct {
Next plugin.Handler Next plugin.Handler
@ -39,16 +35,16 @@ func setup(c *caddy.Controller) error {
// ServeDNS implements the plugin.Handler interface. // ServeDNS implements the plugin.Handler interface.
func (d DNSStat) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { func (d DNSStat) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
state := request.Request{W: w, Req: r}
// Debug log that we've have seen the query. This will only be shown when the debug plugin is loaded. requestCount.WithLabelValues(
log.Error("Received response") metrics.WithServer(ctx),
state.Zone,
// Wrap. state.Class(),
pw := NewResponsePrinter(w) state.Type(),
state.Name(),
// Metric increment state.IP(),
requestCount.WithLabelValues(metrics.WithServer(ctx)).Inc() ).Inc()
return plugin.NextOrFailure(d.Name(), d.Next, ctx, pw, r) return plugin.NextOrFailure(d.Name(), d.Next, ctx, w, r)
} }
// Name implements the Handler interface. // Name implements the Handler interface.

View file

@ -9,12 +9,19 @@ import (
"github.com/prometheus/client_golang/prometheus/promauto" "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{ var requestCount = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: plugin.Namespace, Namespace: plugin.Namespace,
Subsystem: "dnsstat", Subsystem: "dnsstat",
Name: "dnsstat_request_count_total", Name: "dnsstat_request_count_total",
Help: "Counter of requests made.", Help: "Counter of requests made.",
}, []string{"server"}) }, []string{
"server",
"zone",
"class",
"type",
"name",
"client_ip",
})
var once sync.Once var once sync.Once