-
Notifications
You must be signed in to change notification settings - Fork 2
/
error_reporting.go
164 lines (141 loc) · 4.13 KB
/
error_reporting.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
164
package appwrap
import (
"context"
"fmt"
"net/http"
"sync"
"cloud.google.com/go/errorreporting"
)
type ErrorReporter interface {
Close() error
FlushReports()
Report(errReport ErrorReport)
WrapLogger(logging Logging, errorAffectsLabel string) Logging
}
type googleErrorReporter struct {
client googleErrorReportingClient
}
type googleErrorReportingClient interface {
Close() error
Flush()
Report(entry errorreporting.Entry)
}
type ErrorReport struct {
Err error
Req *http.Request
ErrorAffectsKey string
}
/**
* These reporters are meant to be long-lived, ideally one per program instance; Not used liberally
* for individual requests/threads
*/
func NewGoogleErrorReporter(info AppengineInfo, ctx context.Context, log Logging) (*googleErrorReporter, error) {
client, err := errorreporting.NewClient(ctx, info.DataProjectID(), errorreporting.Config{
ServiceName: info.ModuleName(),
ServiceVersion: info.VersionID(),
OnError: func(err error) {
log.Errorf("failed to report error: %s", err.Error())
},
})
if err != nil {
return nil, fmt.Errorf("failed to create error reporter, error was: %s", err.Error())
}
return &googleErrorReporter{client: client}, nil
}
func (g *googleErrorReporter) Report(errReport ErrorReport) {
g.client.Report(errorreporting.Entry{
Error: errReport.Err,
Req: errReport.Req,
User: errReport.ErrorAffectsKey,
})
}
func (g *googleErrorReporter) FlushReports() {
g.client.Flush()
}
/**
* It is important that all loggers returned from WrapLogger be unused or garbage collected before this
* function is called
*/
func (g *googleErrorReporter) Close() error {
return g.client.Close()
}
type errorForwardingLogger struct {
wrappedLogger Logging
errorReporter ErrorReporter
errorAffectsLabel string
labelsLock *sync.RWMutex
labels map[string]string
req *http.Request
}
func (e errorForwardingLogger) Debugf(format string, args ...interface{}) {
e.wrappedLogger.Debugf(format, args...)
}
func (e errorForwardingLogger) Infof(format string, args ...interface{}) {
e.wrappedLogger.Infof(format, args...)
}
func (e errorForwardingLogger) Warningf(format string, args ...interface{}) {
e.wrappedLogger.Warningf(format, args...)
}
func (e *errorForwardingLogger) Request(request, url, format string, args ...interface{}) {
req, err := http.NewRequest(request, url, nil)
if err != nil {
e.wrappedLogger.Warningf("could not parse request for potential error forwarding: %s", err.Error())
} else {
e.req = req
}
e.wrappedLogger.Request(request, url, format, args...)
}
func (e *errorForwardingLogger) AddLabels(labels map[string]string) error {
e.labelsLock.Lock()
for k, v := range labels {
e.labels[k] = v
}
e.labelsLock.Unlock()
return e.wrappedLogger.AddLabels(labels)
}
func (e *errorForwardingLogger) TraceID() string {
return e.wrappedLogger.TraceID()
}
func (e *errorForwardingLogger) Errorf(format string, args ...interface{}) {
e.wrappedLogger.Errorf(format, args...)
}
func (e *errorForwardingLogger) Criticalf(format string, args ...interface{}) {
e.wrappedLogger.Criticalf(format, args...)
e.forwardError(forwardedError{
msg: fmt.Sprintf(format, args...),
})
}
type forwardedError struct {
msg string
}
func (f forwardedError) Error() string {
return f.msg
}
func (e *errorForwardingLogger) forwardError(err error) {
var affects string
if len(e.errorAffectsLabel) > 0 {
e.labelsLock.RLock()
affects = e.labels[e.errorAffectsLabel]
e.labelsLock.RUnlock()
}
e.errorReporter.Report(ErrorReport{
Err: err,
Req: e.req,
ErrorAffectsKey: affects,
})
}
/**
* Unlike NewGoogleErrorReporter this function may be used liberally, and across threads/requests
*/
func (g *googleErrorReporter) WrapLogger(logging Logging, errorAffectsLabel string) Logging {
if _, alreadyWrapped := logging.(*errorForwardingLogger); alreadyWrapped {
panic("bug! this logger is already wrapped!")
}
return &errorForwardingLogger{
wrappedLogger: logging,
errorReporter: g,
errorAffectsLabel: errorAffectsLabel,
labelsLock: &sync.RWMutex{},
labels: make(map[string]string),
}
}