-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhandler.go
More file actions
330 lines (275 loc) · 7.81 KB
/
Copy pathhandler.go
File metadata and controls
330 lines (275 loc) · 7.81 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package slogdatadog
import (
"context"
"encoding/json"
"fmt"
"strings"
"sync"
"time"
"log/slog"
slogcommon "github.com/samber/slog-common"
"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
)
type Option struct {
// log level (default: debug)
Level slog.Leveler
// datadog endpoint
Client *datadog.APIClient
Context context.Context
Timeout time.Duration // default: 10s
// batching (default: disabled)
Batching bool
BatchDuration time.Duration // default: 5s
MaxBatchSize int // default: 0 (no limit)
// source parameters
Service string
// source (optional): Allows overriding the `source` field sent to DD. defaulted to version.name
Source string
Hostname string
GlobalTags map[string]string
// optional: customize Datadog message builder
Converter Converter
// optional: custom marshaler
Marshaler func(v any) ([]byte, error)
// optional: fetch attributes from context
AttrFromContext []func(ctx context.Context) []slog.Attr
// optional: see slog.HandlerOptions
AddSource bool
ReplaceAttr func(groups []string, a slog.Attr) slog.Attr
}
func (o Option) NewDatadogHandler() slog.Handler {
if o.Level == nil {
o.Level = slog.LevelDebug
}
if o.Client == nil {
panic("missing Datadog client")
}
if o.Context == nil {
o.Context = context.Background()
}
if o.Timeout == 0 {
o.Timeout = 10 * time.Second
}
if o.Converter == nil {
o.Converter = DefaultConverter
}
if o.Marshaler == nil {
o.Marshaler = json.Marshal
}
if o.AttrFromContext == nil {
o.AttrFromContext = []func(ctx context.Context) []slog.Attr{}
}
if o.BatchDuration == 0 {
o.BatchDuration = 5 * time.Second
}
handler := &DatadogHandler{
option: o,
attrs: []slog.Attr{},
groups: []string{},
wg: &sync.WaitGroup{},
}
// Wrap the provided context with a cancelable context
handler.ctx, handler.cancel = context.WithCancel(o.Context)
// Start the buffer processing goroutine if batching is enabled
if o.Batching {
handler.batch = &batchState{
bufferTimer: time.NewTimer(o.BatchDuration),
}
handler.wg.Add(1)
go handler.processBuffer()
}
return handler
}
var _ slog.Handler = (*DatadogHandler)(nil)
type batchState struct {
buffer []string
immediateFlushScheduled bool
bufferMu sync.Mutex
bufferTimer *time.Timer
bufferTimerMu sync.Mutex
}
type DatadogHandler struct {
option Option
attrs []slog.Attr
groups []string
wg *sync.WaitGroup
ctx context.Context
cancel context.CancelFunc
batch *batchState // nil when batching is disabled
}
func (h *DatadogHandler) Enabled(_ context.Context, level slog.Level) bool {
return level >= h.option.Level.Level()
}
func (h *DatadogHandler) Handle(ctx context.Context, record slog.Record) error {
bytes, err := handle(h, ctx, record)
if err != nil {
return err
}
if h.option.Batching {
h.batch.bufferMu.Lock()
defer h.batch.bufferMu.Unlock()
h.batch.buffer = append(h.batch.buffer, string(bytes))
if h.option.MaxBatchSize > 0 &&
len(h.batch.buffer) >= h.option.MaxBatchSize &&
!h.batch.immediateFlushScheduled {
// if the buffer is full, schedule a flush immediately
h.batch.immediateFlushScheduled = true
h.scheduleFlush(0)
}
return nil
}
// non-blocking
h.wg.Add(1)
go func() {
defer h.wg.Done()
_ = h.send([]string{string(bytes)})
}()
return nil
}
func handle(h *DatadogHandler, ctx context.Context, record slog.Record) ([]byte, error) {
fromContext := slogcommon.ContextExtractor(ctx, h.option.AttrFromContext)
log := h.option.Converter(h.option.AddSource, h.option.ReplaceAttr, append(h.attrs, fromContext...), h.groups, &record)
return h.option.Marshaler(log)
}
func (h *DatadogHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return &DatadogHandler{
option: h.option,
attrs: slogcommon.AppendAttrsToGroup(h.groups, h.attrs, attrs...),
groups: h.groups,
wg: h.wg,
ctx: h.ctx,
cancel: h.cancel,
batch: h.batch, // Share batching state with parent handler
}
}
func (h *DatadogHandler) WithGroup(name string) slog.Handler {
// https://cs.opensource.google/go/x/exp/+/46b07846:slog/handler.go;l=247
if name == "" {
return h
}
return &DatadogHandler{
option: h.option,
attrs: h.attrs,
groups: append(h.groups, name),
wg: h.wg,
ctx: h.ctx,
cancel: h.cancel,
batch: h.batch, // Share batching state with parent handler
}
}
func (h *DatadogHandler) Flush(ctx context.Context) error {
if !h.option.Batching {
return nil
}
errChan := make(chan error)
go func() {
errChan <- h.flushBuffer()
}()
select {
case err := <-errChan:
return err
case <-ctx.Done():
return ctx.Err()
}
}
func (h *DatadogHandler) Stop(ctx context.Context) error {
// Cancel the context to stop the buffer processing goroutine
h.cancel()
// Wait for all outstanding goroutines to finish
h.wg.Wait()
// Flush any remaining logs
return h.Flush(ctx)
}
func (h *DatadogHandler) flushBuffer() error {
h.batch.bufferMu.Lock()
messages := h.batch.buffer
h.batch.buffer = nil
h.batch.immediateFlushScheduled = false
h.batch.bufferMu.Unlock()
if len(messages) == 0 {
return nil
}
return h.send(messages)
}
func (h *DatadogHandler) stopBufferTimer() {
h.batch.bufferTimerMu.Lock()
defer h.batch.bufferTimerMu.Unlock()
// Stop the timer if it's running
if !h.batch.bufferTimer.Stop() {
// Drain stale value if timer fired before stopped
<-h.batch.bufferTimer.C
}
}
func (h *DatadogHandler) scheduleFlush(duration time.Duration) {
h.batch.bufferTimerMu.Lock()
defer h.batch.bufferTimerMu.Unlock()
// Stop the timer if it's running
if !h.batch.bufferTimer.Stop() {
// Drain stale value if timer fired before stopped
// This is a non-blocking read in case processBuffer reads the channel here.
select {
case <-h.batch.bufferTimer.C:
default:
}
}
// Reset the timer to the new duration
h.batch.bufferTimer.Reset(duration)
}
func (h *DatadogHandler) processBuffer() {
defer h.wg.Done()
for {
select {
case <-h.ctx.Done():
_ = h.flushBuffer()
h.stopBufferTimer()
return
case <-h.batch.bufferTimer.C:
_ = h.flushBuffer()
// Check if buffer filled up again during the flush
// If immediateFlushScheduled is true, Handle() already scheduled a flush, so don't schedule again
// Otherwise, schedule based on buffer size
h.batch.bufferMu.Lock()
if !h.batch.immediateFlushScheduled {
if h.option.MaxBatchSize > 0 && len(h.batch.buffer) >= h.option.MaxBatchSize {
// Buffer is still full, flush immediately
h.scheduleFlush(0)
h.batch.immediateFlushScheduled = true
} else {
// Buffer is below threshold, use normal periodic duration
h.scheduleFlush(h.option.BatchDuration)
}
}
h.batch.bufferMu.Unlock()
}
}
}
func (h *DatadogHandler) send(messages []string) error {
var tags []string
if h.option.GlobalTags != nil {
for key, value := range h.option.GlobalTags {
tags = append(tags, fmt.Sprintf("%v:%v", key, value))
}
}
source := h.option.Source
if source == "" {
source = name
}
body := make([]datadogV2.HTTPLogItem, len(messages))
for i, message := range messages {
body[i] = datadogV2.HTTPLogItem{
Ddsource: datadog.PtrString(source),
Hostname: datadog.PtrString(h.option.Hostname),
Service: datadog.PtrString(h.option.Service),
Ddtags: datadog.PtrString(strings.Join(tags, ",")),
Message: message,
}
}
// Do not use h.ctx here because it is cancelled when the handler is stopped.
// Use h.option.Context instead, which is the original context passed to the handler.
ctx, cancel := context.WithTimeout(h.option.Context, h.option.Timeout)
defer cancel()
api := datadogV2.NewLogsApi(h.option.Client)
_, _, err := api.SubmitLog(ctx, body, *datadogV2.NewSubmitLogOptionalParameters().WithContentEncoding(datadogV2.CONTENTENCODING_DEFLATE))
return err
}