-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontext.go
391 lines (335 loc) · 11.9 KB
/
context.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package spcontext
import (
"context"
"errors"
"fmt"
"reflect"
"strings"
"time"
"github.com/bugsnag/bugsnag-go/v2"
"github.com/go-kit/log"
pkgerrors "github.com/pkg/errors"
)
// FieldsTab is the tab in bugsnag to put metadata fields into.
const FieldsTab = "fields"
// Valuer can be passed to get dynamic values in log fields.
type Valuer log.Valuer
// Notifier models the bugsnag interface.
type Notifier interface {
Notify(error, ...interface{}) error
AutoNotify(...interface{})
}
// Logger models the accepted logger underlying the context.
type Logger interface {
Log(keyvals ...interface{}) error
}
// Context is a drop-in replacement to context.Context. Include logging, error reporting and structured metadata.
type Context struct {
context.Context
fields *Fields
logger Logger
Notifier Notifier
Tracer Tracer
onSpanStartHooks []func(Span, Span)
}
// ContextOption is used to optionally configure the context on creation.
type ContextOption func(ctx *Context)
// WithNotifier adds an optional notifier to the new context.
func WithNotifier(notifier Notifier) ContextOption {
return func(ctx *Context) {
ctx.Notifier = notifier
}
}
// WithTracer adds an optional tracer to the new context.
func WithTracer(tracer Tracer) ContextOption {
return func(ctx *Context) {
ctx.Tracer = tracer
}
}
// OnSpanStart adds an on span start hook to the new context.
func OnSpanStart(hook func(parentSpan, activeSpan Span)) ContextOption {
return func(ctx *Context) {
ctx.onSpanStartHooks = append(ctx.onSpanStartHooks, hook)
}
}
// New creates a new context with the logger and configured using additional options.
func New(logger Logger, opts ...ContextOption) *Context {
ctx := &Context{
Context: context.Background(),
fields: (&Fields{}).With(
"caller", Valuer(log.Caller(4)),
"ts", Valuer(log.Timestamp(time.Now)),
),
logger: logger,
Tracer: &NopTracer{},
}
for _, opt := range opts {
opt(ctx)
}
return ctx
}
// With adds the given alternating keys and values to the context, returning a new child context.
func (ctx *Context) With(kvs ...interface{}) *Context {
return &Context{
Context: ctx.Context,
fields: ctx.fields.With(kvs...),
logger: ctx.logger,
Notifier: ctx.Notifier,
Tracer: ctx.Tracer,
onSpanStartHooks: ctx.onSpanStartHooks,
}
}
type contextKey struct{}
// Value returns the value for the given key in this context stack.
func (ctx *Context) Value(key interface{}) interface{} {
if _, ok := key.(contextKey); ok {
return ctx
}
return ctx.Context.Value(key)
}
// FromStdContext tries to find a spcontext.Context inside the given context.Context and returns a new one based on it.
// If no spcontext.Context is found, a default noop Context is returned.
func FromStdContext(stdCtx context.Context) *Context {
v := stdCtx.Value(contextKey{})
if v != nil {
outCtx := v.(*Context)
return &Context{
Context: stdCtx,
fields: outCtx.fields,
logger: outCtx.logger,
Notifier: outCtx.Notifier,
Tracer: outCtx.Tracer,
onSpanStartHooks: outCtx.onSpanStartHooks,
}
}
return &Context{
Context: stdCtx,
fields: &Fields{},
logger: log.NewNopLogger(),
Notifier: nil,
Tracer: &NopTracer{},
}
}
// WithValue adds a key value to the context. Use instead of context.WithValue
func WithValue(ctx *Context, key, val interface{}) *Context {
return &Context{
Context: context.WithValue(ctx.Context, key, val),
fields: ctx.fields,
logger: ctx.logger,
Notifier: ctx.Notifier,
Tracer: ctx.Tracer,
onSpanStartHooks: ctx.onSpanStartHooks,
}
}
// CancelFunc is a function you can call to cancel the connected context.
type CancelFunc = context.CancelFunc
// WithCancel returns a cancelable context. Use instead of context.WithCancel
func WithCancel(ctx *Context) (*Context, CancelFunc) {
newCtx, cancel := context.WithCancel(ctx.Context)
return &Context{
Context: newCtx,
fields: ctx.fields,
logger: ctx.logger,
Notifier: ctx.Notifier,
Tracer: ctx.Tracer,
onSpanStartHooks: ctx.onSpanStartHooks,
}, cancel
}
// WithTimeout returns a context with a timeout. Use instead of context.WithTimeout.
func WithTimeout(ctx *Context, timeout time.Duration) (*Context, context.CancelFunc) {
newCtx, cancel := context.WithTimeout(ctx.Context, timeout)
return &Context{
Context: newCtx,
fields: ctx.fields,
logger: ctx.logger,
Notifier: ctx.Notifier,
Tracer: ctx.Tracer,
onSpanStartHooks: ctx.onSpanStartHooks,
}, cancel
}
// WithTimeoutCause returns a context with a timeout,
// but also sets the cause of the returned Context when the timeout expires.
// The returned [CancelFunc] does not set the cause. Use instead of context.WithTimeoutCause.
func WithTimeoutCause(ctx *Context, timeout time.Duration, cause error) (*Context, context.CancelFunc) {
newCtx, cancel := context.WithTimeoutCause(ctx.Context, timeout, cause)
return &Context{
Context: newCtx,
fields: ctx.fields,
logger: ctx.logger,
Notifier: ctx.Notifier,
Tracer: ctx.Tracer,
onSpanStartHooks: ctx.onSpanStartHooks,
}, cancel
}
// WithDeadline returns a context with a deadline. Use instead of context.WithDeadline.
func WithDeadline(ctx *Context, d time.Time) (*Context, context.CancelFunc) {
newCtx, cancel := context.WithDeadline(ctx.Context, d)
return &Context{
Context: newCtx,
fields: ctx.fields,
logger: ctx.logger,
Notifier: ctx.Notifier,
Tracer: ctx.Tracer,
onSpanStartHooks: ctx.onSpanStartHooks,
}, cancel
}
// WithDeadlineCause returns a context with a deadline,
// but also sets the cause of the returned Context when the deadline is exceeded.
// The returned [CancelFunc] does not set the cause. Use instead of context.WithDeadlineCause.
func WithDeadlineCause(ctx *Context, d time.Time, cause error) (*Context, context.CancelFunc) {
newCtx, cancel := context.WithDeadlineCause(ctx.Context, d, cause)
return &Context{
Context: newCtx,
fields: ctx.fields,
logger: ctx.logger,
Notifier: ctx.Notifier,
Tracer: ctx.Tracer,
onSpanStartHooks: ctx.onSpanStartHooks,
}, cancel
}
// BackgroundFrom creates a new context.Background() from the given Context.
// This keeps all metadata fields and the logger/notifier configuration.
// Use instead of context.Background().
func BackgroundFrom(ctx *Context) *Context {
return &Context{
Context: context.Background(),
fields: ctx.fields,
logger: ctx.logger,
Notifier: ctx.Notifier,
Tracer: ctx.Tracer,
onSpanStartHooks: ctx.onSpanStartHooks,
}
}
// BackgroundWithValuesFrom creates a new background context from the given Context.
// This keeps all key-values, metadata fields and the logger/notifier configuration.
// Use instead of BackgroundFrom when you want to keep key-value information.
func BackgroundWithValuesFrom(ctx *Context) *Context {
return &Context{
Context: &backgroundWithValuesContext{ctx: ctx},
fields: ctx.fields,
logger: ctx.logger,
Notifier: ctx.Notifier,
Tracer: ctx.Tracer,
onSpanStartHooks: ctx.onSpanStartHooks,
}
}
type backgroundWithValuesContext struct {
ctx context.Context
}
func (f *backgroundWithValuesContext) Deadline() (deadline time.Time, ok bool) { return }
func (f *backgroundWithValuesContext) Done() <-chan struct{} { return nil }
func (f *backgroundWithValuesContext) Err() error { return nil }
func (f *backgroundWithValuesContext) Value(key interface{}) interface{} { return f.ctx.Value(key) }
func (ctx *Context) getEvaluatedFields() []interface{} {
return append(ctx.fields.EvaluateFields(), ctx.Tracer.GetLogFields(ctx)...)
}
func (ctx *Context) log(fields []interface{}, level string, format string, args ...interface{}) {
fields = append(fields,
"level", level,
"msg", fmt.Sprintf(format, args...))
_ = ctx.logger.Log(fields...)
}
// Errorf logs the message with error level.
func (ctx *Context) Errorf(format string, args ...interface{}) {
ctx.log(ctx.getEvaluatedFields(), "error", format, args...)
}
// Warnf logs the message with warning level.
func (ctx *Context) Warnf(format string, args ...interface{}) {
ctx.log(ctx.getEvaluatedFields(), "warning", format, args...)
}
// Infof logs the message with info level.
func (ctx *Context) Infof(format string, args ...interface{}) {
ctx.log(ctx.getEvaluatedFields(), "info", format, args...)
}
// Debugf logs the message with debug level.
func (ctx *Context) Debugf(format string, args ...interface{}) {
ctx.log(ctx.getEvaluatedFields(), "debug", format, args...)
}
// InternalMessage is an internal error message not meant for users.
type InternalMessage error
// SafeMessage is a safe, user-friendly error message.
type SafeMessage error
// DirectError directly notifies about the error, without caring which error is
// user-facing, and which isn't.
func (ctx *Context) DirectError(err error, message string) error {
return ctx.error(ctx.getEvaluatedFields(), err, InternalMessage(errors.New(message)), SafeMessage(errors.New(message)))
}
// Error reports the error to the logger and Bugsnag, while returning an error
// with a user-safe message.
func (ctx *Context) Error(err error, internal InternalMessage, safe SafeMessage) error {
return ctx.error(ctx.getEvaluatedFields(), err, internal, safe)
}
// InternalError reports an error with a generic user-facing message.
func (ctx *Context) InternalError(err error, message string) error {
return ctx.error(ctx.getEvaluatedFields(), err, InternalMessage(errors.New(message)), SafeMessage(errors.New("internal error")))
}
// RawError reports an error wrapped in a message.
func (ctx *Context) RawError(err error, message string) error {
wrapped := pkgerrors.Wrap(err, message)
return ctx.error(ctx.getEvaluatedFields(), err, wrapped, wrapped)
}
// notifiedError is an error which has already been sent to bugsnag.
type notifiedError struct {
error
}
func (ctx *Context) error(fields []interface{}, err error, internal InternalMessage, safe SafeMessage) error {
if err == nil {
return nil
}
if notifiedErr := (notifiedError{}); errors.As(err, ¬ifiedErr) {
// This error has already been notified to bugsnag before.
return notifiedError{error: safe}
}
fieldsMap := make(map[string]interface{})
for i := 0; i < len(fields)/2; i++ {
fieldsMap[fields[2*i].(string)] = fields[2*i+1]
}
if ctx.Notifier != nil && !strings.Contains(err.Error(), context.Canceled.Error()) {
var parentErr = err
var st stackTracer
var errorClass string
curSt, ok := parentErr.(stackTracer)
if ok {
st = curSt
}
var curErr = err
for {
unwrapped := errors.Unwrap(curErr)
if unwrapped == nil {
break
}
curErr = unwrapped
curSt, ok := unwrapped.(stackTracer)
if !ok {
continue
}
st = curSt
}
if st != nil {
curErr = &errorWithStackFrames{err: st}
errorClass = reflect.TypeOf(st).String()
} else {
curErr = parentErr
errorClass = reflect.TypeOf(parentErr).String()
}
fieldsMap["original_error"] = parentErr.Error()
if err := ctx.Notifier.Notify(curErr, bugsnag.MetaData{FieldsTab: fieldsMap}, ctx, bugsnag.ErrorClass{Name: errorClass}); err != nil {
ctx.Errorf("error notifying the exception tracker: %v", err)
}
}
ctx.log(fields, "error", "%s: %v", internal.Error(), err)
return notifiedError{error: safe}
}
// Fields returns the context fields.
func (ctx *Context) Fields() *Fields {
return ctx.fields
}
// EvaluateBugsnagMetadata returns Bugsnag metadata with the evaluated fields.
func (ctx *Context) EvaluateBugsnagMetadata() bugsnag.MetaData {
fields := ctx.getEvaluatedFields()
fieldsMap := make(map[string]interface{})
for i := 0; i < len(fields)/2; i++ {
fieldsMap[fields[2*i].(string)] = fields[2*i+1]
}
return bugsnag.MetaData{FieldsTab: fieldsMap}
}