55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
package dnsstat
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/coredns/caddy"
|
|
"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/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
|
|
}
|
|
|
|
func init() { plugin.Register("dnsstat", setup) }
|
|
|
|
func setup(c *caddy.Controller) error {
|
|
for c.Next() {
|
|
if c.NextArg() {
|
|
return plugin.Error("dnsstat", c.ArgErr())
|
|
}
|
|
}
|
|
|
|
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
|
|
return DNSStat{Next: next}
|
|
})
|
|
|
|
return nil
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
// Name implements the Handler interface.
|
|
func (d DNSStat) Name() string { return "dnsstat" }
|