2018-05-01 23:45:06 +00:00
|
|
|
package tunneldns
|
|
|
|
|
|
|
|
import (
|
2019-01-10 20:55:44 +00:00
|
|
|
"context"
|
|
|
|
|
2018-05-01 23:45:06 +00:00
|
|
|
"github.com/coredns/coredns/plugin"
|
2020-09-09 18:09:42 +00:00
|
|
|
"github.com/coredns/coredns/plugin/metrics"
|
2018-05-01 23:45:06 +00:00
|
|
|
"github.com/coredns/coredns/plugin/metrics/vars"
|
|
|
|
"github.com/coredns/coredns/plugin/pkg/dnstest"
|
|
|
|
"github.com/coredns/coredns/plugin/pkg/rcode"
|
|
|
|
"github.com/coredns/coredns/request"
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
2022-01-25 13:15:24 +00:00
|
|
|
const (
|
|
|
|
pluginName = "cloudflared"
|
|
|
|
)
|
2020-05-01 15:30:50 +00:00
|
|
|
|
2018-05-01 23:45:06 +00:00
|
|
|
// MetricsPlugin is an adapter for CoreDNS and built-in metrics
|
|
|
|
type MetricsPlugin struct {
|
|
|
|
Next plugin.Handler
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMetricsPlugin creates a plugin with configured metrics
|
|
|
|
func NewMetricsPlugin(next plugin.Handler) *MetricsPlugin {
|
|
|
|
return &MetricsPlugin{Next: next}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeDNS implements the CoreDNS plugin interface
|
|
|
|
func (p MetricsPlugin) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
|
|
|
state := request.Request{W: w, Req: r}
|
|
|
|
|
|
|
|
rw := dnstest.NewRecorder(w)
|
|
|
|
status, err := plugin.NextOrFailure(p.Name(), p.Next, ctx, rw, r)
|
|
|
|
|
|
|
|
// Update built-in metrics
|
2020-09-09 18:09:42 +00:00
|
|
|
server := metrics.WithServer(ctx)
|
2023-01-11 00:12:59 +00:00
|
|
|
vars.Report(server, state, ".", "", rcode.ToString(rw.Rcode), pluginName, rw.Len, rw.Start)
|
2018-05-01 23:45:06 +00:00
|
|
|
|
|
|
|
return status, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name implements the CoreDNS plugin interface
|
|
|
|
func (p MetricsPlugin) Name() string { return "metrics" }
|