-
Notifications
You must be signed in to change notification settings - Fork 127
/
armada_context.go
111 lines (97 loc) · 3.47 KB
/
armada_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
package armadacontext
import (
"context"
"time"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
)
// Context is an extension of Go's context which also includes a logger. This allows us to pass round a contextual logger
// while retaining type-safety
type Context struct {
context.Context
logrus.FieldLogger
}
// Background creates an empty context with a default logger. It is analogous to context.Background()
func Background() *Context {
return &Context{
Context: context.Background(),
FieldLogger: logrus.NewEntry(logrus.StandardLogger()),
}
}
// TODO creates an empty context with a default logger. It is analogous to context.TODO()
func TODO() *Context {
return &Context{
Context: context.TODO(),
FieldLogger: logrus.NewEntry(logrus.StandardLogger()),
}
}
// FromGrpcCtx creates a context where the logger is extracted via ctxlogrus's Extract() method.
// Note that this will result in a no-op logger if a logger hasn't already been inserted into the context via ctxlogrus
func FromGrpcCtx(ctx context.Context) *Context {
armadaCtx, ok := ctx.(*Context)
if ok {
return armadaCtx
}
log := ctxlogrus.Extract(ctx)
return New(ctx, log)
}
// New returns an armada context that encapsulates both a go context and a logger
func New(ctx context.Context, log *logrus.Entry) *Context {
return &Context{
Context: ctx,
FieldLogger: log,
}
}
// WithCancel returns a copy of parent with a new Done channel. It is analogous to context.WithCancel()
func WithCancel(parent *Context) (*Context, context.CancelFunc) {
c, cancel := context.WithCancel(parent.Context)
return &Context{
Context: c,
FieldLogger: parent.FieldLogger,
}, cancel
}
// WithDeadline returns a copy of the parent context with the deadline adjusted to be no later than d.
// It is analogous to context.WithDeadline()
func WithDeadline(parent *Context, d time.Time) (*Context, context.CancelFunc) {
c, cancel := context.WithDeadline(parent.Context, d)
return &Context{
Context: c,
FieldLogger: parent.FieldLogger,
}, cancel
}
// WithTimeout returns WithDeadline(parent, time.Now().Add(timeout)). It is analogous to context.WithTimeout()
func WithTimeout(parent *Context, timeout time.Duration) (*Context, context.CancelFunc) {
return WithDeadline(parent, time.Now().Add(timeout))
}
// WithLogField returns a copy of parent with the supplied key-value added to the logger
func WithLogField(parent *Context, key string, val interface{}) *Context {
return &Context{
Context: parent.Context,
FieldLogger: parent.FieldLogger.WithField(key, val),
}
}
// WithLogFields returns a copy of parent with the supplied key-values added to the logger
func WithLogFields(parent *Context, fields logrus.Fields) *Context {
return &Context{
Context: parent.Context,
FieldLogger: parent.FieldLogger.WithFields(fields),
}
}
// WithValue returns a copy of parent in which the value associated with key is
// val. It is analogous to context.WithValue()
func WithValue(parent *Context, key, val any) *Context {
return &Context{
Context: context.WithValue(parent, key, val),
FieldLogger: parent.FieldLogger,
}
}
// ErrGroup returns a new Error Group and an associated Context derived from ctx.
// It is analogous to errgroup.WithContext(ctx)
func ErrGroup(ctx *Context) (*errgroup.Group, *Context) {
group, goctx := errgroup.WithContext(ctx)
return group, &Context{
Context: goctx,
FieldLogger: ctx.FieldLogger,
}
}