-
Notifications
You must be signed in to change notification settings - Fork 0
/
append_handler.go
222 lines (197 loc) · 7.62 KB
/
append_handler.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
package slogdedup
import (
"context"
"log/slog"
"slices"
"modernc.org/b/v2"
)
// AppendHandlerOptions are options for a AppendHandler
type AppendHandlerOptions struct {
// Comparison function to determine if two keys are equal
KeyCompare func(a, b string) int
// Function that will be called on each attribute and group, to determine
// the key to use. Returns the new key value to use, and true to keep the
// attribute or false to drop it. Can be used to drop, keep, or rename any
// attributes matching the builtin attributes.
//
// The first argument is a list of currently open groups that contain the
// Attr. It must not be retained or modified.
//
// ResolveKey will not be called for the built-in fields on slog.Record
// (ie: time, level, msg, and source).
ResolveKey func(groups []string, key string, _ int) (string, bool)
}
// AppendHandler is a slog.Handler middleware that will deduplicate all attributes and
// groups by creating a slice/array whenever there is more than one attribute with the same key.
// It passes the final record and attributes off to the next handler when finished.
type AppendHandler struct {
next slog.Handler
goa *groupOrAttrs
keyCompare func(a, b string) int
resolveKey func(groups []string, key string, _ int) (string, bool)
}
var _ slog.Handler = &AppendHandler{} // Assert conformance with interface
// NewAppendMiddleware creates an AppendHandler slog.Handler middleware
// that conforms to [github.com/samber/slog-multi.Middleware] interface.
// It can be used with slogmulti methods such as Pipe to easily setup a pipeline of slog handlers:
//
// slog.SetDefault(slog.New(slogmulti.
// Pipe(slogcontext.NewMiddleware(&slogcontext.HandlerOptions{})).
// Pipe(slogdedup.NewAppendMiddleware(&slogdedup.AppendHandlerOptions{})).
// Handler(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{})),
// ))
func NewAppendMiddleware(options *AppendHandlerOptions) func(slog.Handler) slog.Handler {
return func(next slog.Handler) slog.Handler {
return NewAppendHandler(
next,
options,
)
}
}
// NewAppendHandler creates a AppendHandler slog.Handler middleware that will deduplicate all attributes and
// groups by creating a slice/array whenever there is more than one attribute with the same key.
// It passes the final record and attributes off to the next handler when finished.
// If opts is nil, the default options are used.
func NewAppendHandler(next slog.Handler, opts *AppendHandlerOptions) *AppendHandler {
if opts == nil {
opts = &AppendHandlerOptions{}
}
if opts.KeyCompare == nil {
opts.KeyCompare = CaseSensitiveCmp
}
if opts.ResolveKey == nil {
opts.ResolveKey = IncrementIfBuiltinKeyConflict
}
return &AppendHandler{
next: next,
keyCompare: opts.KeyCompare,
resolveKey: opts.ResolveKey,
}
}
// Enabled reports whether the next handler handles records at the given level.
// The handler ignores records whose level is lower.
func (h *AppendHandler) Enabled(ctx context.Context, level slog.Level) bool {
return h.next.Enabled(ctx, level)
}
// Handle de-duplicates all attributes and groups, then passes the new set of attributes to the next handler.
func (h *AppendHandler) Handle(ctx context.Context, r slog.Record) error {
// The final set of attributes on the record, is basically the same as a final With-Attributes groupOrAttrs.
// So collect all final attributes and turn them into a groupOrAttrs so that it can be handled the same.
finalAttrs := make([]slog.Attr, 0, r.NumAttrs())
r.Attrs(func(a slog.Attr) bool {
finalAttrs = append(finalAttrs, a)
return true
})
goas := collectGroupOrAttrs(h.goa, &groupOrAttrs{attrs: finalAttrs})
// Resolve groups and with-attributes
uniq := b.TreeNew[string, any](h.keyCompare)
h.createAttrTree(uniq, goas, nil)
// Add all attributes to new record (because old record has all the old attributes)
newR := &slog.Record{
Time: r.Time,
Level: r.Level,
Message: r.Message,
PC: r.PC,
}
// Add deduplicated attributes back in
newR.AddAttrs(buildAttrs(uniq)...)
return h.next.Handle(ctx, *newR)
}
// WithGroup returns a new AppendHandler that still has h's attributes,
// but any future attributes added will be namespaced.
func (h *AppendHandler) WithGroup(name string) slog.Handler {
h2 := *h
h2.goa = h2.goa.WithGroup(name)
return &h2
}
// WithAttrs returns a new AppendHandler whose attributes consists of h's attributes followed by attrs.
func (h *AppendHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
h2 := *h
h2.goa = h2.goa.WithAttrs(attrs)
return &h2
}
// createAttrTree recursively goes through all groupOrAttrs, resolving their attributes and creating subtrees as
// necessary, adding the results to the map
func (h *AppendHandler) createAttrTree(uniq *b.Tree[string, any], goas []*groupOrAttrs, groups []string) {
if len(goas) == 0 {
return
}
// If a group is encountered, create a subtree for that group and all groupOrAttrs after it
if goas[0].group != "" {
if key, keep := h.resolveKey(groups, goas[0].group, 0); keep {
uniqGroup := b.TreeNew[string, any](h.keyCompare)
h.createAttrTree(uniqGroup, goas[1:], append(slices.Clip(groups), key))
// Ignore empty groups, otherwise put subtree into the map
if uniqGroup.Len() > 0 {
// Put calls func(oldValue, true) if key already exists, or func(oldValue, false) if it doesn't.
// Then expects us to return (newValue, true) if replacing the oldValue, or (whatever, false) if not.
uniq.Put(key, func(oldValue any, exists bool) (any, bool) {
if !exists {
return uniqGroup, true
}
if slice, ok := oldValue.(appended); ok {
slice = append(slice, uniqGroup)
return slice, true
}
return appended{oldValue, uniqGroup}, true
})
}
return
}
}
// Otherwise, set all attributes for this groupOrAttrs, and then call again for remaining groupOrAttrs's
h.resolveValues(uniq, goas[0].attrs, groups)
h.createAttrTree(uniq, goas[1:], groups)
}
// resolveValues iterates through the attributes, resolving them and putting them into the map.
// If a group is encountered (as an attribute), it will be separately resolved and added as a subtree.
// Since attributes are ordered from oldest to newest, it creates a slice whenever it detects the key already exists,
// appending the new attribute, then overwriting the key with that slice.
func (h *AppendHandler) resolveValues(uniq *b.Tree[string, any], attrs []slog.Attr, groups []string) {
var keep bool
for _, a := range attrs {
a.Value = a.Value.Resolve()
if a.Equal(slog.Attr{}) {
continue // Ignore empty attributes, and keep iterating
}
// Default situation: resolve the key and put it into the map
a.Key, keep = h.resolveKey(groups, a.Key, 0)
if !keep {
continue
}
if a.Value.Kind() != slog.KindGroup {
uniq.Put(a.Key, func(oldValue any, exists bool) (any, bool) {
if !exists {
return a, true
}
if slice, ok := oldValue.(appended); ok {
slice = append(slice, a)
return slice, true
}
return appended{oldValue, a}, true
})
continue
}
// Groups with empty keys are inlined
if a.Key == "" {
h.resolveValues(uniq, a.Value.Group(), groups)
continue
}
// Create a subtree for this group
uniqGroup := b.TreeNew[string, any](h.keyCompare)
h.resolveValues(uniqGroup, a.Value.Group(), append(slices.Clip(groups), a.Key))
// Ignore empty groups, otherwise put subtree into the map
if uniqGroup.Len() > 0 {
uniq.Put(a.Key, func(oldValue any, exists bool) (any, bool) {
if !exists {
return uniqGroup, true
}
if slice, ok := oldValue.(appended); ok {
slice = append(slice, uniqGroup)
return slice, true
}
return appended{oldValue, uniqGroup}, true
})
}
}
}