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