metrics.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package tunneldns
  2. import (
  3. "context"
  4. "github.com/coredns/coredns/plugin"
  5. "github.com/coredns/coredns/plugin/metrics"
  6. "github.com/coredns/coredns/plugin/metrics/vars"
  7. "github.com/coredns/coredns/plugin/pkg/dnstest"
  8. "github.com/coredns/coredns/plugin/pkg/rcode"
  9. "github.com/coredns/coredns/request"
  10. "github.com/miekg/dns"
  11. )
  12. const (
  13. pluginName = "cloudflared"
  14. )
  15. // MetricsPlugin is an adapter for CoreDNS and built-in metrics
  16. type MetricsPlugin struct {
  17. Next plugin.Handler
  18. }
  19. // NewMetricsPlugin creates a plugin with configured metrics
  20. func NewMetricsPlugin(next plugin.Handler) *MetricsPlugin {
  21. return &MetricsPlugin{Next: next}
  22. }
  23. // ServeDNS implements the CoreDNS plugin interface
  24. func (p MetricsPlugin) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
  25. state := request.Request{W: w, Req: r}
  26. rw := dnstest.NewRecorder(w)
  27. status, err := plugin.NextOrFailure(p.Name(), p.Next, ctx, rw, r)
  28. // Update built-in metrics
  29. server := metrics.WithServer(ctx)
  30. vars.Report(server, state, ".", "", rcode.ToString(rw.Rcode), pluginName, rw.Len, rw.Start)
  31. return status, err
  32. }
  33. // Name implements the CoreDNS plugin interface
  34. func (p MetricsPlugin) Name() string { return "metrics" }