consolidate errors that happen a lot and we know about

This commit is contained in:
Alex Grintsvayg 2018-08-29 09:04:09 -04:00
parent d7a4b34d8c
commit 473e7e3b07
2 changed files with 17 additions and 4 deletions

View file

@ -155,8 +155,10 @@ func (s *Server) handleConn(conn net.Conn) {
}
func (s *Server) doError(conn net.Conn, err error) error {
log.Errorln(errors.FullTrace(err))
s.stats.AddError(err)
shouldLog := s.stats.AddError(err)
if shouldLog {
log.Errorln(errors.FullTrace(err))
}
if e2, ok := err.(*json.SyntaxError); ok {
log.Errorf("syntax error at byte offset %d", e2.Offset)
}

View file

@ -2,6 +2,7 @@ package reflector
import (
"fmt"
"strings"
"sync"
"time"
@ -54,14 +55,24 @@ func (s *stats) AddStream() {
defer s.mu.Unlock()
s.streams += 1
}
func (s *stats) AddError(e error) {
func (s *stats) AddError(e error) (shouldLog bool) { // shouldLog is a hack, but whataever
if e == nil {
return
}
err := errors.Wrap(e, 0)
name := err.TypeName()
if strings.Contains(err.Error(), "i/o timeout") {
name = "i/o timeout"
} else if strings.Contains(err.Error(), "read: connection reset by peer") {
name = "read conn reset"
}
shouldLog = true
s.mu.Lock()
defer s.mu.Unlock()
s.errors[err.TypeName()] += 1
s.errors[name] += 1
return
}
func (s *stats) runSlackLogger() {