-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.go
More file actions
152 lines (126 loc) · 3.48 KB
/
context.go
File metadata and controls
152 lines (126 loc) · 3.48 KB
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
package logging
import (
"context"
"maps"
"sort"
"sync"
)
type boundFieldsKey struct{}
type summaryAccumulatorKey struct{}
type summaryAccumulator struct {
mu sync.Mutex
fields map[string]LoggingField
}
// Bind attaches structured fields to a plain context.Context.
// Later bindings override earlier values for the same field key.
func Bind(ctx context.Context, fields ...LoggingField) context.Context {
ctx = normalizedContext(ctx)
existing, _ := ctx.Value(boundFieldsKey{}).(map[string]LoggingField)
next := make(map[string]LoggingField, len(existing)+len(fields))
maps.Copy(next, existing)
for _, field := range fields {
next[field.key] = field
}
ctx = context.WithValue(ctx, boundFieldsKey{}, next)
ctx, _ = summaryAccumulatorFromContext(ctx)
return ctx
}
// BindSummary promotes fields into the request summary view without changing the
// ordinary fields visible to WithContext on the returned context.
func BindSummary(ctx context.Context, fields ...LoggingField) context.Context {
ctx = normalizedContext(ctx)
ctx, acc := summaryAccumulatorFromContext(ctx)
acc.set(fields...)
return ctx
}
func fieldsFromContext(ctx context.Context) []LoggingField {
if ctx == nil {
return nil
}
fieldMap, ok := ctx.Value(boundFieldsKey{}).(map[string]LoggingField)
if !ok || len(fieldMap) == 0 {
return nil
}
return sortedFields(fieldMap)
}
func summaryFieldsFromContext(ctx context.Context) []LoggingField {
if ctx == nil {
return nil
}
merged := make(map[string]LoggingField)
fieldMap, ok := ctx.Value(boundFieldsKey{}).(map[string]LoggingField)
if ok {
merged = make(map[string]LoggingField, len(fieldMap))
maps.Copy(merged, fieldMap)
}
acc, ok := ctx.Value(summaryAccumulatorKey{}).(*summaryAccumulator)
if ok && acc != nil {
for _, field := range acc.fieldsSnapshot() {
merged[field.key] = field
}
}
if len(merged) == 0 {
return nil
}
return sortedFields(merged)
}
func sortedFields(fieldMap map[string]LoggingField) []LoggingField {
keys := make([]string, 0, len(fieldMap))
for key := range fieldMap {
keys = append(keys, key)
}
sort.Strings(keys)
fields := make([]LoggingField, 0, len(fieldMap))
for _, key := range keys {
fields = append(fields, fieldMap[key])
}
return fields
}
func (a *summaryAccumulator) set(fields ...LoggingField) {
a.mu.Lock()
defer a.mu.Unlock()
for _, field := range fields {
a.fields[field.key] = field
}
}
func (a *summaryAccumulator) fieldsSnapshot() []LoggingField {
a.mu.Lock()
defer a.mu.Unlock()
if len(a.fields) == 0 {
return nil
}
copied := make(map[string]LoggingField, len(a.fields))
maps.Copy(copied, a.fields)
return sortedFields(copied)
}
func normalizedContext(ctx context.Context) context.Context {
if ctx == nil {
return context.Background()
}
return ctx
}
func summaryAccumulatorFromContext(ctx context.Context) (context.Context, *summaryAccumulator) {
if acc, ok := ctx.Value(summaryAccumulatorKey{}).(*summaryAccumulator); ok && acc != nil {
return ctx, acc
}
acc := &summaryAccumulator{
fields: make(map[string]LoggingField),
}
return context.WithValue(ctx, summaryAccumulatorKey{}, acc), acc
}
// RequestIDFromContext extracts `request_id` if it was previously bound as a string field.
func RequestIDFromContext(ctx context.Context) string {
fieldMap, ok := ctx.Value(boundFieldsKey{}).(map[string]LoggingField)
if !ok {
return ""
}
field, exists := fieldMap[KeyRequestID]
if !exists {
return ""
}
value, ok := field.stringValue()
if !ok {
return ""
}
return value
}