setup.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package example
  2. import (
  3. "github.com/coredns/coredns/core/dnsserver"
  4. "github.com/coredns/coredns/plugin"
  5. "github.com/coredns/coredns/plugin/metrics"
  6. "github.com/caddyserver/caddy"
  7. )
  8. // init registers this plugin.
  9. func init() { plugin.Register("example", setup) }
  10. // setup is the function that gets called when the config parser see the token "example". Setup is responsible
  11. // for parsing any extra options the example plugin may have. The first token this function sees is "example".
  12. func setup(c *caddy.Controller) error {
  13. c.Next() // Ignore "example" and give us the next token.
  14. if c.NextArg() {
  15. // If there was another token, return an error, because we don't have any configuration.
  16. // Any errors returned from this setup function should be wrapped with plugin.Error, so we
  17. // can present a slightly nicer error message to the user.
  18. return plugin.Error("example", c.ArgErr())
  19. }
  20. // Add a startup function that will -- after all plugins have been loaded -- check if the
  21. // prometheus plugin has been used - if so we will export metrics. We can only register
  22. // this metric once, hence the "once.Do".
  23. c.OnStartup(func() error {
  24. once.Do(func() { metrics.MustRegister(c, requestCount) })
  25. return nil
  26. })
  27. // Add the Plugin to CoreDNS, so Servers can use it in their plugin chain.
  28. dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
  29. return Example{Next: next}
  30. })
  31. // All OK, return a nil error.
  32. return nil
  33. }