mirror of
https://github.com/coredns/coredns.git
synced 2025-11-02 10:13:14 -05:00
plugin/errors: remove cLogger and eLogger (#2657)
This commit is contained in:
committed by
Miek Gieben
parent
89a4b9bd45
commit
18d305f4b3
@@ -35,28 +35,19 @@ func (p *pattern) setTimer(t *time.Timer) {
|
|||||||
// errorHandler handles DNS errors (and errors from other plugin).
|
// errorHandler handles DNS errors (and errors from other plugin).
|
||||||
type errorHandler struct {
|
type errorHandler struct {
|
||||||
patterns []*pattern
|
patterns []*pattern
|
||||||
eLogger func(int, string, string, string)
|
|
||||||
cLogger func(uint32, string, time.Duration)
|
|
||||||
stopFlag uint32
|
stopFlag uint32
|
||||||
Next plugin.Handler
|
Next plugin.Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
func newErrorHandler() *errorHandler {
|
func newErrorHandler() *errorHandler {
|
||||||
return &errorHandler{eLogger: errorLogger, cLogger: consLogger}
|
return &errorHandler{}
|
||||||
}
|
|
||||||
|
|
||||||
func errorLogger(code int, qName, qType, err string) {
|
|
||||||
log.Errorf("%d %s %s: %s", code, qName, qType, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func consLogger(cnt uint32, pattern string, p time.Duration) {
|
|
||||||
log.Errorf("%d errors like '%s' occurred in last %s", cnt, pattern, p)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *errorHandler) logPattern(i int) {
|
func (h *errorHandler) logPattern(i int) {
|
||||||
cnt := atomic.SwapUint32(&h.patterns[i].count, 0)
|
cnt := atomic.SwapUint32(&h.patterns[i].count, 0)
|
||||||
if cnt > 0 {
|
if cnt > 0 {
|
||||||
h.cLogger(cnt, h.patterns[i].pattern.String(), h.patterns[i].period)
|
log.Errorf("%d errors like '%s' occurred in last %s",
|
||||||
|
cnt, h.patterns[i].pattern.String(), h.patterns[i].period)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,7 +93,7 @@ func (h *errorHandler) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dn
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
state := request.Request{W: w, Req: r}
|
state := request.Request{W: w, Req: r}
|
||||||
h.eLogger(rcode, state.Name(), state.Type(), strErr)
|
log.Errorf("%d %s %s: %s", rcode, state.Name(), state.Type(), strErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
return rcode, err
|
return rcode, err
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
golog "log"
|
golog "log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
@@ -24,7 +25,7 @@ import (
|
|||||||
func TestErrors(t *testing.T) {
|
func TestErrors(t *testing.T) {
|
||||||
buf := bytes.Buffer{}
|
buf := bytes.Buffer{}
|
||||||
golog.SetOutput(&buf)
|
golog.SetOutput(&buf)
|
||||||
em := errorHandler{eLogger: errorLogger}
|
em := errorHandler{}
|
||||||
|
|
||||||
testErr := errors.New("test error")
|
testErr := errors.New("test error")
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
@@ -72,58 +73,22 @@ func TestErrors(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConsLogger(t *testing.T) {
|
func TestLogPattern(t *testing.T) {
|
||||||
buf := bytes.Buffer{}
|
buf := bytes.Buffer{}
|
||||||
golog.SetOutput(&buf)
|
golog.SetOutput(&buf)
|
||||||
|
|
||||||
consLogger(5, "^Error.*!$", 3*time.Second)
|
|
||||||
|
|
||||||
exp := "[ERROR] plugin/errors: 5 errors like '^Error.*!$' occurred in last 3s"
|
|
||||||
act := buf.String()
|
|
||||||
if !strings.Contains(act, exp) {
|
|
||||||
t.Errorf("Unexpected log message, expected to contain %q, actual %q", exp, act)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLogPattern(t *testing.T) {
|
|
||||||
h := &errorHandler{
|
h := &errorHandler{
|
||||||
patterns: []*pattern{{
|
patterns: []*pattern{{
|
||||||
count: 4,
|
count: 4,
|
||||||
period: 2 * time.Second,
|
period: 2 * time.Second,
|
||||||
pattern: regexp.MustCompile("^Error.*!$"),
|
pattern: regexp.MustCompile("^error.*!$"),
|
||||||
}},
|
}},
|
||||||
cLogger: testConsLogger,
|
|
||||||
}
|
}
|
||||||
loggedData = loggedData[:0]
|
|
||||||
|
|
||||||
h.logPattern(0)
|
h.logPattern(0)
|
||||||
expLen := 1
|
|
||||||
if len(loggedData) != expLen {
|
|
||||||
t.Errorf("Unexpected number of logged messages, expected %d, actual %d",
|
|
||||||
expLen, len(loggedData))
|
|
||||||
}
|
|
||||||
expCnt := uint32(4)
|
|
||||||
if loggedData[0].cnt != expCnt {
|
|
||||||
t.Errorf("Unexpected 'count' in logged message, expected %d, actual %d",
|
|
||||||
expCnt, loggedData[0].cnt)
|
|
||||||
}
|
|
||||||
expPat := "^Error.*!$"
|
|
||||||
actPat := loggedData[0].pat
|
|
||||||
if actPat != expPat {
|
|
||||||
t.Errorf("Unexpected 'pattern' in logged message, expected %s, actual %s",
|
|
||||||
expPat, actPat)
|
|
||||||
}
|
|
||||||
expPer := "2s"
|
|
||||||
actPer := loggedData[0].dur.String()
|
|
||||||
if actPer != expPer {
|
|
||||||
t.Errorf("Unexpected 'period' in logged message, expected %s, actual %s",
|
|
||||||
expPer, actPer)
|
|
||||||
}
|
|
||||||
|
|
||||||
h.logPattern(0)
|
expLog := "4 errors like '^error.*!$' occurred in last 2s"
|
||||||
if len(loggedData) != expLen {
|
if log := buf.String(); !strings.Contains(log, expLog) {
|
||||||
t.Errorf("Unexpected number of logged messages, expected %d, actual %d",
|
t.Errorf("Expected log %q, but got %q", expLog, log)
|
||||||
expLen, len(loggedData))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,9 +97,8 @@ func TestInc(t *testing.T) {
|
|||||||
stopFlag: 1,
|
stopFlag: 1,
|
||||||
patterns: []*pattern{{
|
patterns: []*pattern{{
|
||||||
period: 2 * time.Second,
|
period: 2 * time.Second,
|
||||||
pattern: regexp.MustCompile("^Error.*!$"),
|
pattern: regexp.MustCompile("^error.*!$"),
|
||||||
}},
|
}},
|
||||||
cLogger: testConsLogger,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ret := h.inc(0)
|
ret := h.inc(0)
|
||||||
@@ -186,14 +150,15 @@ func TestInc(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStop(t *testing.T) {
|
func TestStop(t *testing.T) {
|
||||||
|
buf := bytes.Buffer{}
|
||||||
|
golog.SetOutput(&buf)
|
||||||
|
|
||||||
h := &errorHandler{
|
h := &errorHandler{
|
||||||
patterns: []*pattern{{
|
patterns: []*pattern{{
|
||||||
period: 2 * time.Second,
|
period: 2 * time.Second,
|
||||||
pattern: regexp.MustCompile("^Error.*!$"),
|
pattern: regexp.MustCompile("^error.*!$"),
|
||||||
}},
|
}},
|
||||||
cLogger: testConsLogger,
|
|
||||||
}
|
}
|
||||||
loggedData = loggedData[:0]
|
|
||||||
|
|
||||||
h.inc(0)
|
h.inc(0)
|
||||||
h.inc(0)
|
h.inc(0)
|
||||||
@@ -225,54 +190,34 @@ func TestStop(t *testing.T) {
|
|||||||
t.Error("Timer was unexpectedly not stopped before")
|
t.Error("Timer was unexpectedly not stopped before")
|
||||||
}
|
}
|
||||||
|
|
||||||
expLen := 1
|
expLog := "3 errors like '^error.*!$' occurred in last 2s"
|
||||||
if len(loggedData) != expLen {
|
if log := buf.String(); !strings.Contains(log, expLog) {
|
||||||
t.Errorf("Unexpected number of logged messages, expected %d, actual %d",
|
t.Errorf("Expected log %q, but got %q", expLog, log)
|
||||||
expLen, len(loggedData))
|
|
||||||
}
|
|
||||||
expCnt = uint32(3)
|
|
||||||
actCnt = loggedData[0].cnt
|
|
||||||
if actCnt != expCnt {
|
|
||||||
t.Errorf("Unexpected 'count' in logged message, expected %d, actual %d",
|
|
||||||
expCnt, actCnt)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
pattern0 = "failed"
|
||||||
|
pattern1 = "down$"
|
||||||
|
)
|
||||||
|
|
||||||
func TestServeDNSConcurrent(t *testing.T) {
|
func TestServeDNSConcurrent(t *testing.T) {
|
||||||
|
buf := bytes.NewBuffer(nil)
|
||||||
|
golog.SetOutput(buf)
|
||||||
|
|
||||||
eg := newErrorGenerator()
|
eg := newErrorGenerator()
|
||||||
rep := &errorReport{}
|
|
||||||
|
|
||||||
h := &errorHandler{
|
h := &errorHandler{
|
||||||
patterns: []*pattern{
|
patterns: []*pattern{
|
||||||
{
|
{
|
||||||
period: 3 * time.Nanosecond,
|
period: 3 * time.Nanosecond,
|
||||||
pattern: regexp.MustCompile("Failed"),
|
pattern: regexp.MustCompile(pattern0),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
period: 2 * time.Nanosecond,
|
period: 2 * time.Nanosecond,
|
||||||
pattern: regexp.MustCompile("down$"),
|
pattern: regexp.MustCompile(pattern1),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
cLogger: func(cnt uint32, pattern string, p time.Duration) {
|
|
||||||
rep.incLoggerCnt()
|
|
||||||
if pattern == "Failed" {
|
|
||||||
rep.incConsolidated(Err0, cnt)
|
|
||||||
} else if pattern == "down$" {
|
|
||||||
rep.incConsolidated(Err1, cnt)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
eLogger: func(code int, n, t, e string) {
|
|
||||||
switch e {
|
|
||||||
case ErrStr0:
|
|
||||||
rep.incPassed(Err0)
|
|
||||||
case ErrStr1:
|
|
||||||
rep.incPassed(Err1)
|
|
||||||
case ErrStr2:
|
|
||||||
rep.incPassed(Err2)
|
|
||||||
case ErrStr3:
|
|
||||||
rep.incPassed(Err3)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Next: eg,
|
Next: eg,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -284,20 +229,19 @@ func TestServeDNSConcurrent(t *testing.T) {
|
|||||||
runnersCnt := 9
|
runnersCnt := 9
|
||||||
runner := func() {
|
runner := func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
for !eg.done() {
|
for eg.progress() < 100 {
|
||||||
h.ServeDNS(ctx, w, r)
|
h.ServeDNS(ctx, w, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
wg.Add(runnersCnt)
|
wg.Add(runnersCnt)
|
||||||
for ; runnersCnt > 0; runnersCnt-- {
|
for ; runnersCnt > 0; runnersCnt-- {
|
||||||
go runner()
|
go runner()
|
||||||
}
|
}
|
||||||
|
|
||||||
stopCalled := false
|
stopCalled := false
|
||||||
for !eg.done() {
|
for eg.progress() < 100 {
|
||||||
if !stopCalled &&
|
if !stopCalled && eg.progress() > 50 {
|
||||||
atomic.LoadUint32(&rep.s[Err0]) > ErrCnt0/2 &&
|
|
||||||
atomic.LoadUint32(&rep.s[Err1]) > ErrCnt1/2 {
|
|
||||||
h.stop()
|
h.stop()
|
||||||
stopCalled = true
|
stopCalled = true
|
||||||
}
|
}
|
||||||
@@ -310,43 +254,30 @@ func TestServeDNSConcurrent(t *testing.T) {
|
|||||||
wg.Wait()
|
wg.Wait()
|
||||||
time.Sleep(time.Millisecond)
|
time.Sleep(time.Millisecond)
|
||||||
|
|
||||||
if rep.loggerCnt() < 3 {
|
rep := makeReport(t, buf)
|
||||||
t.Errorf("Perhaps logger was never called by timer")
|
|
||||||
}
|
|
||||||
if rep.consolidated(Err0) == 0 {
|
|
||||||
t.Errorf("Err0 was never reported by consLogger")
|
|
||||||
}
|
|
||||||
if rep.consolidated(Err1) == 0 {
|
|
||||||
t.Errorf("Err1 was never reported by consLogger")
|
|
||||||
}
|
|
||||||
if rep.consolidated(Err0)+rep.passed(Err0) != ErrCnt0 {
|
|
||||||
t.Errorf("Unexpected Err0 count, expected %d, reported by consLogger %d and by ServeDNS %d",
|
|
||||||
ErrCnt0, rep.consolidated(Err0), rep.passed(Err0))
|
|
||||||
}
|
|
||||||
if rep.consolidated(Err1)+rep.passed(Err1) != ErrCnt1 {
|
|
||||||
t.Errorf("Unexpected Err1 count, expected %d, reported by consLogger %d and by ServeDNS %d",
|
|
||||||
ErrCnt1, rep.consolidated(Err1), rep.passed(Err1))
|
|
||||||
}
|
|
||||||
if rep.passed(Err2) != ErrCnt2 {
|
|
||||||
t.Errorf("Unexpected Err2 count, expected %d, reported by ServeDNS %d",
|
|
||||||
ErrCnt2, rep.passed(Err2))
|
|
||||||
}
|
|
||||||
if rep.passed(Err3) != ErrCnt3 {
|
|
||||||
t.Errorf("Unexpected Err3 count, expected %d, reported by ServeDNS %d",
|
|
||||||
ErrCnt3, rep.passed(Err3))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type logData struct {
|
if rep.c[Err0] == 0 {
|
||||||
cnt uint32
|
t.Errorf("Err0 was never consolidated")
|
||||||
pat string
|
}
|
||||||
dur time.Duration
|
if rep.c[Err1] == 0 {
|
||||||
}
|
t.Errorf("Err1 was never consolidated")
|
||||||
|
}
|
||||||
var loggedData []logData
|
if rep.c[Err0]+rep.r[Err0] != ErrCnt0 {
|
||||||
|
t.Errorf("Unexpected Err0 count, expected %d, consolidated %d and reported %d",
|
||||||
func testConsLogger(cnt uint32, pattern string, p time.Duration) {
|
ErrCnt0, rep.c[Err0], rep.r[Err0])
|
||||||
loggedData = append(loggedData, logData{cnt, pattern, p})
|
}
|
||||||
|
if rep.c[Err1]+rep.r[Err1] != ErrCnt1 {
|
||||||
|
t.Errorf("Unexpected Err1 count, expected %d, consolidated %d and reported %d",
|
||||||
|
ErrCnt1, rep.c[Err1], rep.r[Err1])
|
||||||
|
}
|
||||||
|
if rep.r[Err2] != ErrCnt2 {
|
||||||
|
t.Errorf("Unexpected Err2 count, expected %d, reported %d",
|
||||||
|
ErrCnt2, rep.r[Err2])
|
||||||
|
}
|
||||||
|
if rep.r[Err3] != ErrCnt3 {
|
||||||
|
t.Errorf("Unexpected Err3 count, expected %d, reported %d",
|
||||||
|
ErrCnt3, rep.r[Err3])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// error generator
|
// error generator
|
||||||
@@ -356,10 +287,10 @@ const (
|
|||||||
Err2
|
Err2
|
||||||
Err3
|
Err3
|
||||||
|
|
||||||
ErrStr0 = "Failed to connect"
|
ErrStr0 = "failed to connect"
|
||||||
ErrStr1 = "Upstream is down"
|
ErrStr1 = "upstream is down"
|
||||||
ErrStr2 = "Access denied"
|
ErrStr2 = "access denied"
|
||||||
ErrStr3 = "Yet another error"
|
ErrStr3 = "yet another error"
|
||||||
|
|
||||||
ErrCnt0 = 120000
|
ErrCnt0 = 120000
|
||||||
ErrCnt1 = 130000
|
ErrCnt1 = 130000
|
||||||
@@ -374,7 +305,8 @@ type errorBunch struct {
|
|||||||
|
|
||||||
type errorGenerator struct {
|
type errorGenerator struct {
|
||||||
errors [4]errorBunch
|
errors [4]errorBunch
|
||||||
doneFlag uint32
|
totalCnt int32
|
||||||
|
totalLim int32
|
||||||
}
|
}
|
||||||
|
|
||||||
func newErrorGenerator() *errorGenerator {
|
func newErrorGenerator() *errorGenerator {
|
||||||
@@ -387,11 +319,13 @@ func newErrorGenerator() *errorGenerator {
|
|||||||
{ErrCnt2, fmt.Errorf(ErrStr2)},
|
{ErrCnt2, fmt.Errorf(ErrStr2)},
|
||||||
{ErrCnt3, fmt.Errorf(ErrStr3)},
|
{ErrCnt3, fmt.Errorf(ErrStr3)},
|
||||||
},
|
},
|
||||||
|
totalLim: ErrCnt0 + ErrCnt1 + ErrCnt2 + ErrCnt3,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sh *errorGenerator) done() bool {
|
// returns progress as a value from [0..100] range
|
||||||
return atomic.LoadUint32(&sh.doneFlag) > 0
|
func (sh *errorGenerator) progress() int32 {
|
||||||
|
return atomic.LoadInt32(&sh.totalCnt) * 100 / sh.totalLim
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sh *errorGenerator) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
func (sh *errorGenerator) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
||||||
@@ -399,11 +333,11 @@ func (sh *errorGenerator) ServeDNS(ctx context.Context, w dns.ResponseWriter, r
|
|||||||
for c := 0; c < 4; c++ {
|
for c := 0; c < 4; c++ {
|
||||||
errInd := (i + c) & 3
|
errInd := (i + c) & 3
|
||||||
if atomic.AddInt32(&sh.errors[errInd].cnt, -1) >= 0 {
|
if atomic.AddInt32(&sh.errors[errInd].cnt, -1) >= 0 {
|
||||||
|
atomic.AddInt32(&sh.totalCnt, 1)
|
||||||
return 0, sh.errors[errInd].err
|
return 0, sh.errors[errInd].err
|
||||||
}
|
}
|
||||||
atomic.AddInt32(&sh.errors[errInd].cnt, 1)
|
atomic.AddInt32(&sh.errors[errInd].cnt, 1)
|
||||||
}
|
}
|
||||||
atomic.StoreUint32(&sh.doneFlag, 1)
|
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -411,33 +345,39 @@ func (sh *errorGenerator) Name() string { return "errorGenerator" }
|
|||||||
|
|
||||||
// error report
|
// error report
|
||||||
type errorReport struct {
|
type errorReport struct {
|
||||||
s [4]uint32
|
c [4]uint32
|
||||||
p [4]uint32
|
r [4]uint32
|
||||||
l uint32
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (er *errorReport) consolidated(i int) uint32 {
|
func makeReport(t *testing.T, b *bytes.Buffer) errorReport {
|
||||||
return atomic.LoadUint32(&er.s[i])
|
logDump := b.String()
|
||||||
}
|
r := errorReport{}
|
||||||
|
|
||||||
func (er *errorReport) incConsolidated(i int, cnt uint32) {
|
re := regexp.MustCompile(`(\d+) errors like '(.*)' occurred`)
|
||||||
atomic.AddUint32(&er.s[i], cnt)
|
m := re.FindAllStringSubmatch(logDump, -1)
|
||||||
}
|
for _, sub := range m {
|
||||||
|
ind := 0
|
||||||
|
switch sub[2] {
|
||||||
|
case pattern0:
|
||||||
|
ind = Err0
|
||||||
|
case pattern1:
|
||||||
|
ind = Err1
|
||||||
|
default:
|
||||||
|
t.Fatalf("Unknown pattern found in log: %s", sub[0])
|
||||||
|
}
|
||||||
|
n, err := strconv.ParseUint(sub[1], 10, 32)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Invalid number found in log: %s", sub[0])
|
||||||
|
}
|
||||||
|
r.c[ind] += uint32(n)
|
||||||
|
}
|
||||||
|
t.Logf("%d consolidated messages found", len(m))
|
||||||
|
|
||||||
func (er *errorReport) passed(i int) uint32 {
|
r.r[Err0] = uint32(strings.Count(logDump, ErrStr0))
|
||||||
return atomic.LoadUint32(&er.p[i])
|
r.r[Err1] = uint32(strings.Count(logDump, ErrStr1))
|
||||||
}
|
r.r[Err2] = uint32(strings.Count(logDump, ErrStr2))
|
||||||
|
r.r[Err3] = uint32(strings.Count(logDump, ErrStr3))
|
||||||
func (er *errorReport) incPassed(i int) {
|
return r
|
||||||
atomic.AddUint32(&er.p[i], 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (er *errorReport) loggerCnt() uint32 {
|
|
||||||
return atomic.LoadUint32(&er.l)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (er *errorReport) incLoggerCnt() {
|
|
||||||
atomic.AddUint32(&er.l, 1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func genErrorHandler(rcode int, err error) plugin.Handler {
|
func genErrorHandler(rcode int, err error) plugin.Handler {
|
||||||
|
|||||||
Reference in New Issue
Block a user