This repository has been archived by the owner on Sep 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy patherrors.go
163 lines (142 loc) · 4.39 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadog.com/).
// Copyright 2018 Datadog, Inc.
package datadog
import (
"errors"
"fmt"
"log"
"strings"
"sync"
"time"
)
const (
// defaultErrorLimit specifies the maximum number of occurrences that will
// be recorded for an error of a certain type.
defaultErrorLimit = 50
// defaultErrorFreq specifies the default frequency at which errors will
// be reported.
defaultErrorFreq = 5 * time.Second
)
// errorType specifies the error type.
type errorType int
const (
// errorTypeEncoding specifies that an encoding error has occurred.
errorTypeEncoding errorType = iota
// errorTypeOverflow specifies that the in channel capacity has been reached.
errorTypeOverflow
// errorTypeTransport specifies that an error occurred while trying
// to upload spans to the agent.
errorTypeTransport
// errorTypeUnknown specifies that an unknown error type was reported.
errorTypeUnknown
)
// errorTypeStrings maps error types to their human-readable description.
var errorTypeStrings = map[errorType]string{
errorTypeEncoding: "encoding error",
errorTypeOverflow: "span buffer overflow",
errorTypeTransport: "transport error",
errorTypeUnknown: "error",
}
// String implements fmt.Stringer.
func (et errorType) String() string { return errorTypeStrings[et] }
// errorAmortizer amortizes high frequency errors and condenses them into
// periodical reports to avoid flooding.
type errorAmortizer struct {
interval time.Duration // frequency of report
callback func(error) // error handler; defaults to log.Println
mu sync.RWMutex // guards below fields
pausing bool
errs map[errorType]*aggregateError
}
// newErrorAmortizer creates a new errorAmortizer which calls the provided function
// at the given interval, passing it a detailed error report if one has occurred.
func newErrorAmortizer(interval time.Duration, cb func(error)) *errorAmortizer {
if cb == nil {
cb = func(err error) {
log.Println(err)
}
}
return &errorAmortizer{
interval: interval,
callback: cb,
errs: make(map[errorType]*aggregateError),
}
}
// flush flushes any aggregated errors and resets the amortizer.
func (e *errorAmortizer) flush() {
e.mu.Lock()
defer e.mu.Unlock()
n := len(e.errs)
if n == 0 {
return
}
var str strings.Builder
str.WriteString("Datadog Exporter error: ")
for _, err := range e.errs {
if n > 1 {
str.WriteString("\n\t")
}
str.WriteString(err.Error())
}
e.callback(errors.New(str.String()))
e.errs = make(map[errorType]*aggregateError)
e.pausing = false
}
// limitReached returns true if the defaultErrorLimit has been reached
// for the given error type.
func (e *errorAmortizer) limitReached(typ errorType) bool {
e.mu.RLock()
defer e.mu.RUnlock()
return e.errs[typ] != nil && e.errs[typ].num > defaultErrorLimit-1
}
// log logs an error of the given type, having the given message. err
// is optional and can be nil.
func (e *errorAmortizer) log(typ errorType, err error) {
if e.limitReached(typ) {
// avoid too much lock contention
return
}
e.mu.Lock()
defer e.mu.Unlock()
if _, ok := e.errs[typ]; !ok {
e.errs[typ] = newError(typ, err)
} else {
e.errs[typ].num++
}
if !e.pausing {
e.pausing = true
time.AfterFunc(e.interval, e.flush)
}
}
var _ error = (*aggregateError)(nil)
// aggregateError is an error consisting of a type and an optional context
// error. It is used to aggregate errors inside the errorAmortizer.
type aggregateError struct {
typ errorType // error type
err error // error message (optional)
num int // number of occurrences
}
// newError creates a new aggregateError.
func newError(t errorType, err error) *aggregateError {
return &aggregateError{t, err, 1}
}
// Error implements the error interface. If the error occurred more than
// once, it appends the number of occurrences to the error message.
func (e *aggregateError) Error() string {
var str strings.Builder
if e.err == nil {
str.WriteString(e.typ.String())
} else {
// no need to include the type into the message, it will be evident
// from the message itself.
str.WriteString(e.err.Error())
}
if e.num >= defaultErrorLimit {
str.WriteString(fmt.Sprintf(" (x%d+)", defaultErrorLimit))
} else if e.num > 1 {
str.WriteString(fmt.Sprintf(" (x%d)", e.num))
}
return str.String()
}