cloudflare/cloudflared
Publicmirrored from https://github.com/cloudflare/cloudflaredAvailable
tunneldns/metrics.go
46lines · modecode
| 1 | package tunneldns |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | |
| 6 | "github.com/coredns/coredns/plugin" |
| 7 | "github.com/coredns/coredns/plugin/metrics/vars" |
| 8 | "github.com/coredns/coredns/plugin/pkg/dnstest" |
| 9 | "github.com/coredns/coredns/plugin/pkg/rcode" |
| 10 | "github.com/coredns/coredns/request" |
| 11 | "github.com/miekg/dns" |
| 12 | "github.com/prometheus/client_golang/prometheus" |
| 13 | ) |
| 14 | |
| 15 | // MetricsPlugin is an adapter for CoreDNS and built-in metrics |
| 16 | type MetricsPlugin struct { |
| 17 | Next plugin.Handler |
| 18 | } |
| 19 | |
| 20 | // NewMetricsPlugin creates a plugin with configured metrics |
| 21 | func NewMetricsPlugin(next plugin.Handler) *MetricsPlugin { |
| 22 | prometheus.MustRegister(vars.RequestCount) |
| 23 | prometheus.MustRegister(vars.RequestDuration) |
| 24 | prometheus.MustRegister(vars.RequestSize) |
| 25 | prometheus.MustRegister(vars.RequestDo) |
| 26 | prometheus.MustRegister(vars.RequestType) |
| 27 | prometheus.MustRegister(vars.ResponseSize) |
| 28 | prometheus.MustRegister(vars.ResponseRcode) |
| 29 | return &MetricsPlugin{Next: next} |
| 30 | } |
| 31 | |
| 32 | // ServeDNS implements the CoreDNS plugin interface |
| 33 | func (p MetricsPlugin) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { |
| 34 | state := request.Request{W: w, Req: r} |
| 35 | |
| 36 | rw := dnstest.NewRecorder(w) |
| 37 | status, err := plugin.NextOrFailure(p.Name(), p.Next, ctx, rw, r) |
| 38 | |
| 39 | // Update built-in metrics |
| 40 | vars.Report(ctx, state, ".", rcode.ToString(rw.Rcode), rw.Len, rw.Start) |
| 41 | |
| 42 | return status, err |
| 43 | } |
| 44 | |
| 45 | // Name implements the CoreDNS plugin interface |
| 46 | func (p MetricsPlugin) Name() string { return "metrics" } |
| 47 | |