-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtypes.go
121 lines (105 loc) · 3.36 KB
/
types.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
package periskop
import (
"fmt"
"hash/fnv"
"strings"
"time"
"github.com/google/uuid"
)
// Severity is the definition of different severities
type Severity string
const (
SeverityInfo Severity = "info"
SeverityWarning Severity = "warning"
SeverityError Severity = "error"
MaxTraces int = 4
MaxErrors int = 10
)
type payload struct {
AggregatedErrors []aggregatedError `json:"aggregated_errors"`
TargetUUID uuid.UUID `json:"target_uuid"`
}
type aggregatedError struct {
AggregationKey string `json:"aggregation_key"`
TotalCount int `json:"total_count"`
Severity Severity `json:"severity"`
LatestErrors []ErrorWithContext `json:"latest_errors"`
CreatedAt time.Time `json:"created_at"`
}
func newAggregatedError(aggregationKey string, severity Severity) aggregatedError {
return aggregatedError{
AggregationKey: aggregationKey,
TotalCount: 0,
Severity: severity,
CreatedAt: time.Now().UTC(),
}
}
func (e *aggregatedError) addError(errWithContext ErrorWithContext) {
if len(e.LatestErrors) >= MaxErrors {
// dequeue
e.LatestErrors = e.LatestErrors[1:]
}
e.LatestErrors = append(e.LatestErrors, errWithContext)
e.TotalCount++
}
// HTTPContext holds info of the HTTP context when an error is produced
type HTTPContext struct {
RequestMethod string `json:"request_method"`
RequestURL string `json:"request_url"`
RequestHeaders map[string]string `json:"request_headers"`
RequestBody *string `json:"request_body"`
}
type ErrorWithContext struct {
Error ErrorInstance `json:"error"`
UUID uuid.UUID `json:"uuid"`
Timestamp time.Time `json:"timestamp"`
Severity Severity `json:"severity"`
HTTPContext *HTTPContext `json:"http_context"`
}
func NewErrorWithContext(errInstance ErrorInstance, severity Severity, httpCtx *HTTPContext) ErrorWithContext {
return ErrorWithContext{
Error: errInstance,
UUID: uuid.New(),
Timestamp: time.Now().UTC(),
Severity: severity,
HTTPContext: httpCtx,
}
}
type ErrorInstance struct {
Class string `json:"class"`
Message string `json:"message"`
Stacktrace []string `json:"stacktrace"`
Cause *ErrorInstance `json:"cause"`
}
func newErrorInstance(err error, errType string, stacktrace []string) ErrorInstance {
return ErrorInstance{
Message: err.Error(),
Class: errType,
Stacktrace: stacktrace,
}
}
// NewCustomErrorInstance allows to create a custom error instance without specifying a Go error
func NewCustomErrorInstance(errMsg string, errType string, stacktrace []string) ErrorInstance {
return ErrorInstance{
Message: errMsg,
Class: errType,
Stacktrace: stacktrace,
}
}
// aggregationKey generates a hash for errorWithContext using the last MaxTraces
func (e *ErrorWithContext) aggregationKey() string {
stacktraceHead := e.Error.Stacktrace
if len(stacktraceHead) > MaxTraces {
stacktraceHead = stacktraceHead[:MaxTraces]
}
stacktraceHeadHash := hash(e.Error.Message + strings.Join(stacktraceHead, ""))
return fmt.Sprintf("%s@%s", e.Error.Class, stacktraceHeadHash)
}
func hash(s string) string {
h := fnv.New32a()
_, err := h.Write([]byte(s))
if err != nil {
fmt.Printf("error hashing string '%s': %s\n", s, err)
}
return fmt.Sprintf("%x", h.Sum32())
}