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
|
package log
import (
"context"
"sync"
"github.com/sirupsen/logrus"
)
type requestCustomFieldsKey struct{}
// CustomFields stores custom fields, which will be logged as a part of gRPC logs. The gRPC server is expected to add
// corresponding interceptors. They initialize a CustomFields object and inject it into the context. Callers can pull
// the object out with CustomFieldsFromContext.
type CustomFields struct {
numericFields map[string]int
anyFields map[string]any
sync.Mutex
}
// RecordSum sums up all the values for a given key.
func (fields *CustomFields) RecordSum(key string, value int) {
fields.Lock()
defer fields.Unlock()
if prevValue, ok := fields.numericFields[key]; ok {
value += prevValue
}
fields.numericFields[key] = value
}
// RecordMax will store the max value for a given key.
func (fields *CustomFields) RecordMax(key string, value int) {
fields.Lock()
defer fields.Unlock()
if prevValue, ok := fields.numericFields[key]; ok {
if prevValue > value {
return
}
}
fields.numericFields[key] = value
}
// RecordMetadata records a string metadata for the given key.
func (fields *CustomFields) RecordMetadata(key string, value any) {
fields.Lock()
defer fields.Unlock()
fields.anyFields[key] = value
}
// Fields returns all the fields as logrus.Fields
func (fields *CustomFields) Fields() logrus.Fields {
fields.Lock()
defer fields.Unlock()
f := logrus.Fields{}
for k, v := range fields.numericFields {
f[k] = v
}
for k, v := range fields.anyFields {
f[k] = v
}
return f
}
// CustomFieldsFromContext gets the `CustomFields` from the given context.
func CustomFieldsFromContext(ctx context.Context) *CustomFields {
fields, _ := ctx.Value(requestCustomFieldsKey{}).(*CustomFields)
return fields
}
// InitContextCustomFields returns a new context with `CustomFields` added to the given context.
func InitContextCustomFields(ctx context.Context) context.Context {
return context.WithValue(ctx, requestCustomFieldsKey{}, &CustomFields{
numericFields: make(map[string]int),
anyFields: make(map[string]any),
})
}
|