diff options
author | 2018-10-27 17:37:09 +0300 | |
---|---|---|
committer | 2018-10-27 15:37:09 +0100 | |
commit | 7b25d180198a34b9c7bf2426a7b5dc4e8abfdf92 (patch) | |
tree | b6182ce75589ee296a1a367514e0de58fbd9f17a /plugin/errors/setup.go | |
parent | b0a89452ef863b3995cae7b8a367e0648bc00b5e (diff) | |
download | coredns-7b25d180198a34b9c7bf2426a7b5dc4e8abfdf92.tar.gz coredns-7b25d180198a34b9c7bf2426a7b5dc4e8abfdf92.tar.zst coredns-7b25d180198a34b9c7bf2426a7b5dc4e8abfdf92.zip |
plugin/errors: 'consolidate' option (#2192)
- see more details at https://github.com/infobloxopen/coredns-plugin-errors/pull/3
Diffstat (limited to 'plugin/errors/setup.go')
-rw-r--r-- | plugin/errors/setup.go | 46 |
1 files changed, 40 insertions, 6 deletions
diff --git a/plugin/errors/setup.go b/plugin/errors/setup.go index d90928f74..42a5a40f5 100644 --- a/plugin/errors/setup.go +++ b/plugin/errors/setup.go @@ -1,7 +1,8 @@ package errors import ( - "fmt" + "regexp" + "time" "github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/plugin" @@ -22,6 +23,11 @@ func setup(c *caddy.Controller) error { return plugin.Error("errors", err) } + c.OnShutdown(func() error { + handler.stop() + return nil + }) + dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler { handler.Next = next return handler @@ -30,13 +36,13 @@ func setup(c *caddy.Controller) error { return nil } -func errorsParse(c *caddy.Controller) (errorHandler, error) { - handler := errorHandler{} +func errorsParse(c *caddy.Controller) (*errorHandler, error) { + handler := newErrorHandler() i := 0 for c.Next() { if i > 0 { - return handler, plugin.ErrOnce + return nil, plugin.ErrOnce } i++ @@ -45,11 +51,39 @@ func errorsParse(c *caddy.Controller) (errorHandler, error) { case 0: case 1: if args[0] != "stdout" { - return handler, fmt.Errorf("invalid log file: %s", args[0]) + return nil, c.Errf("invalid log file: %s", args[0]) } default: - return handler, c.ArgErr() + return nil, c.ArgErr() + } + + for c.NextBlock() { + if err := parseBlock(c, handler); err != nil { + return nil, err + } } } return handler, nil } + +func parseBlock(c *caddy.Controller, h *errorHandler) error { + if c.Val() != "consolidate" { + return c.SyntaxErr("consolidate") + } + + args := c.RemainingArgs() + if len(args) != 2 { + return c.ArgErr() + } + p, err := time.ParseDuration(args[0]) + if err != nil { + return c.Err(err.Error()) + } + re, err := regexp.Compile(args[1]) + if err != nil { + return c.Err(err.Error()) + } + h.patterns = append(h.patterns, &pattern{period: p, pattern: re}) + + return nil +} |