Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion error_map.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,41 @@
package main

import (
"regexp"
"sort"
"strconv"
"sync"
"sync/atomic"
)

var (
// capture 1 should capture the canonicalised error message
errMsgPatterns = []string{
// read tcp 10.10.0.62:60682->63.35.24.107:443: read: connection reset by peer
`read tcp [\d.]+:\d+->[\d.]+:\d+: read: (.*)`,
}

errMsgRegexes []*regexp.Regexp
)

func init() {
for _, pattern := range errMsgPatterns {
errMsgRegexes = append(errMsgRegexes, regexp.MustCompile(pattern))
}
}

func canonicalizeError(err error) string {
msg := err.Error()
for _, errMsgRegex := range errMsgRegexes {
matches := errMsgRegex.FindStringSubmatch(msg)
if matches != nil {
return matches[1]
}
}

return msg
}

type errorMap struct {
mu sync.RWMutex
m map[string]*uint64
Expand All @@ -19,7 +48,7 @@ func newErrorMap() *errorMap {
}

func (e *errorMap) add(err error) {
s := err.Error()
s := canonicalizeError(err)
e.mu.RLock()
c, ok := e.m[s]
e.mu.RUnlock()
Expand Down
19 changes: 19 additions & 0 deletions error_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"errors"
"fmt"
"reflect"
"testing"
)
Expand All @@ -23,6 +24,24 @@ func TestErrorMapGet(t *testing.T) {
}
}

func TestCanonicalisedError(t *testing.T) {
m := newErrorMap()
for i := 0; i < 10; i++ {
m.add(fmt.Errorf("read tcp 10.10.0.62:%d->63.35.24.107:%d: read: connection reset by peer", 1000+i, 2000+i))
}
m.add(errors.New("tls timeout"))

e := errorsByFrequency{
{"connection reset by peer", 10},
{"tls timeout", 1},
}
if a := m.byFrequency(); !reflect.DeepEqual(a, e) {
t.Logf("Expected: %+v", e)
t.Logf("Got: %+v", a)
t.Fail()
}
}

func TestByFrequency(t *testing.T) {
m := newErrorMap()
a := errors.New("A")
Expand Down