plugin/errors: 'consolidate' option (#2192)

- see more details at https://github.com/infobloxopen/coredns-plugin-errors/pull/3
This commit is contained in:
Ruslan Drozhdzh
2018-10-27 17:37:09 +03:00
committed by Miek Gieben
parent b0a89452ef
commit 7b25d18019
5 changed files with 582 additions and 24 deletions

View File

@@ -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
}