example.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Package example is a CoreDNS plugin that prints "example" to stdout on every packet received.
  2. //
  3. // It serves as an example CoreDNS plugin with numerous code comments.
  4. package example
  5. import (
  6. "context"
  7. "fmt"
  8. "io"
  9. "os"
  10. "github.com/coredns/coredns/plugin"
  11. "github.com/coredns/coredns/plugin/metrics"
  12. clog "github.com/coredns/coredns/plugin/pkg/log"
  13. "github.com/coredns/coredns/request"
  14. "github.com/miekg/dns"
  15. )
  16. // Define log to be a logger with the plugin name in it. This way we can just use log.Info and
  17. // friends to log.
  18. var log = clog.NewWithPlugin("example")
  19. // Example is an example plugin to show how to write a plugin.
  20. type Example struct {
  21. Next plugin.Handler
  22. }
  23. // ServeDNS implements the plugin.Handler interface. This method gets called when example is used
  24. // in a Server.
  25. func (e Example) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
  26. // This function could be simpler. I.e. just fmt.Println("example") here, but we want to show
  27. // a slightly more complex example as to make this more interesting.
  28. // Here we wrap the dns.ResponseWriter in a new ResponseWriter and call the next plugin, when the
  29. // answer comes back, it will print "example".
  30. // Debug log that we've have seen the query. This will only be shown when the debug plugin is loaded.
  31. state := request.Request{W: w, Req: r}
  32. log.Info("Foo %s", state)
  33. log.Info("Received response")
  34. // Wrap.
  35. pw := NewResponsePrinter(w)
  36. // Export metric with the server label set to the current server handling the request.
  37. requestCount.WithLabelValues(metrics.WithServer(ctx)).Inc()
  38. // Call next plugin (if any).
  39. return plugin.NextOrFailure(e.Name(), e.Next, ctx, pw, r)
  40. }
  41. // Name implements the Handler interface.
  42. func (e Example) Name() string { return "example" }
  43. // ResponsePrinter wrap a dns.ResponseWriter and will write example to standard output when WriteMsg is called.
  44. type ResponsePrinter struct {
  45. dns.ResponseWriter
  46. }
  47. // NewResponsePrinter returns ResponseWriter.
  48. func NewResponsePrinter(w dns.ResponseWriter) *ResponsePrinter {
  49. return &ResponsePrinter{ResponseWriter: w}
  50. }
  51. // WriteMsg calls the underlying ResponseWriter's WriteMsg method and prints "example" to standard output.
  52. func (r *ResponsePrinter) WriteMsg(res *dns.Msg) error {
  53. fmt.Fprintln(out, "example")
  54. return r.ResponseWriter.WriteMsg(res)
  55. }
  56. // Make out a reference to os.Stdout so we can easily overwrite it for testing.
  57. var out io.Writer = os.Stdout