-
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathrouter.go
More file actions
218 lines (200 loc) · 6.19 KB
/
router.go
File metadata and controls
218 lines (200 loc) · 6.19 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
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
package slogmulti
import (
"context"
"fmt"
"log/slog"
"slices"
"github.com/samber/lo"
slogcommon "github.com/samber/slog-common"
)
type router struct {
handlers []slog.Handler
firstMatch bool
}
// Router creates a new router instance for building conditional log routing.
// This function is the entry point for creating a routing configuration.
//
// Example usage:
//
// r := slogmulti.Router().
// Add(consoleHandler, slogmulti.LevelIs(slog.LevelInfo)).
// Add(fileHandler, slogmulti.LevelIs(slog.LevelError)).
// Handler()
//
// Returns:
//
// A new router instance ready for configuration
func Router() *router {
return &router{
handlers: []slog.Handler{},
firstMatch: false,
}
}
// Add registers a new handler with optional predicates to the router.
// The handler will only process records if all provided predicates return true.
//
// Args:
//
// handler: The slog.Handler to register
// predicates: Optional functions that determine if a record should be routed to this handler
//
// Returns:
//
// The router instance for method chaining
func (h *router) Add(handler slog.Handler, predicates ...func(ctx context.Context, r slog.Record) bool) *router {
return &router{
handlers: append(
h.handlers,
&RoutableHandler{
predicates: predicates,
handler: handler,
groups: []string{},
attrs: []slog.Attr{},
skipPredicates: false,
},
),
firstMatch: h.firstMatch,
}
}
// Handler creates a slog.Handler from the configured router.
// This method finalizes the routing configuration and returns a handler
// that can be used with slog.New().
//
// Returns:
//
// A slog.Handler that implements the routing logic
func (h *router) Handler() slog.Handler {
if h.firstMatch {
return FirstMatch(lo.Map(h.handlers, func(h slog.Handler, _ int) *RoutableHandler {
rh, ok := h.(*RoutableHandler)
if !ok {
panic(fmt.Sprintf("expected *RoutableHandler, got %T", h))
}
return &(*rh)
})...)
} else {
return Fanout(h.handlers...)
}
}
func (h *router) FirstMatch() *router {
return &router{
handlers: h.handlers,
firstMatch: true,
}
}
// Ensure RoutableHandler implements the slog.Handler interface at compile time
var _ slog.Handler = (*RoutableHandler)(nil)
// RoutableHandler wraps a slog.Handler with conditional matching logic.
// It only forwards records to the underlying handler if all predicates return true.
// This enables sophisticated routing scenarios like level-based or attribute-based routing.
//
// @TODO: implement round robin strategy for load balancing across multiple handlers
type RoutableHandler struct {
// predicates contains functions that determine if a record should be processed
predicates []func(ctx context.Context, r slog.Record) bool
// handler is the underlying slog.Handler that processes matching records
handler slog.Handler
// groups tracks the current group hierarchy for proper attribute handling
groups []string
// attrs contains accumulated attributes that should be added to records
attrs []slog.Attr
// skipPredicates indicates the caller MUST call isMatch(ctx, record) and MUST NOT invoke the handler for a given record if isMatch returns false.
skipPredicates bool
}
// Enabled checks if the underlying handler is enabled for the given log level.
// This method implements the slog.Handler interface requirement.
//
// Args:
//
// ctx: The context for the logging operation
// l: The log level to check
//
// Returns:
//
// true if the underlying handler is enabled for the level, false otherwise
func (h *RoutableHandler) Enabled(ctx context.Context, l slog.Level) bool {
return h.handler.Enabled(ctx, l)
}
// Handle processes a log record if all predicates return true.
// This method implements the slog.Handler interface requirement.
//
// Args:
//
// ctx: The context for the logging operation
// r: The log record to process
//
// Returns:
//
// An error if the underlying handler failed to process the record, nil otherwise
func (h *RoutableHandler) Handle(ctx context.Context, r slog.Record) error {
if h.skipPredicates {
return h.handler.Handle(ctx, r)
} else {
_, ok := h.isMatch(ctx, r)
if ok {
return h.handler.Handle(ctx, r)
}
}
return nil
}
func (h *RoutableHandler) isMatch(ctx context.Context, r slog.Record) (slog.Record, bool) {
clone := slog.NewRecord(r.Time, r.Level, r.Message, r.PC)
clone.AddAttrs(
slogcommon.AppendRecordAttrsToAttrs(h.attrs, h.groups, &r)...,
)
for _, predicate := range h.predicates {
if !predicate(ctx, clone) {
return clone, false
}
}
return clone, true
}
// WithAttrs creates a new RoutableHandler with additional attributes.
// This method implements the slog.Handler interface requirement.
//
// The method properly handles attribute accumulation within the current group context,
// ensuring that attributes are correctly applied to records when they are processed.
//
// Args:
//
// attrs: The attributes to add to the handler
//
// Returns:
//
// A new RoutableHandler with the additional attributes
func (h *RoutableHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return &RoutableHandler{
predicates: h.predicates,
handler: h.handler.WithAttrs(attrs),
groups: slices.Clone(h.groups),
attrs: slogcommon.AppendAttrsToGroup(h.groups, h.attrs, attrs...),
skipPredicates: h.skipPredicates,
}
}
// WithGroup creates a new RoutableHandler with a group name.
// This method implements the slog.Handler interface requirement.
//
// The method follows the same pattern as the standard slog implementation:
// - If the group name is empty, returns the original handler unchanged
// - Otherwise, creates a new handler with the group name added to the group hierarchy
//
// Args:
//
// name: The group name to apply to the handler
//
// Returns:
//
// A new RoutableHandler with the group name, or the original handler if the name is empty
func (h *RoutableHandler) WithGroup(name string) slog.Handler {
// https://cs.opensource.google/go/x/exp/+/46b07846:slog/handler.go;l=247
if name == "" {
return h
}
return &RoutableHandler{
predicates: h.predicates,
handler: h.handler.WithGroup(name),
groups: append(slices.Clone(h.groups), name),
attrs: h.attrs,
skipPredicates: h.skipPredicates,
}
}