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/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.

View file

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