-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
middleware_absolute.go
81 lines (67 loc) · 2.17 KB
/
middleware_absolute.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
package slogsampling
import (
"context"
"log/slog"
"time"
slogmulti "github.com/samber/slog-multi"
"github.com/samber/slog-sampling/buffer"
)
type AbsoluteSamplingOption struct {
// This will log all entries with the same hash until max is reached,
// in a `Tick` interval as-is. Following that, it will reduce log throughput
// depending on previous interval.
Tick time.Duration
Max uint64
// Group similar logs (default: by level and message)
Matcher Matcher
Buffer func(generator func(string) any) buffer.Buffer[string]
buffer buffer.Buffer[string]
// Optional hooks
OnAccepted func(context.Context, slog.Record)
OnDropped func(context.Context, slog.Record)
}
// NewMiddleware returns a slog-multi middleware.
func (o AbsoluteSamplingOption) NewMiddleware() slogmulti.Middleware {
if o.Max == 0 {
panic("unexpected Max: must be greater than 0")
}
if o.Matcher == nil {
o.Matcher = DefaultMatcher
}
if o.Buffer == nil {
o.Buffer = buffer.NewUnlimitedBuffer[string]()
}
o.buffer = o.Buffer(func(k string) any {
return newCounterWithMemory()
})
return slogmulti.NewInlineMiddleware(
func(ctx context.Context, level slog.Level, next func(context.Context, slog.Level) bool) bool {
return next(ctx, level)
},
func(ctx context.Context, record slog.Record, next func(context.Context, slog.Record) error) error {
key := o.Matcher(ctx, &record)
c, _ := o.buffer.GetOrInsert(key)
n, p := c.(*counterWithMemory).Inc(o.Tick)
random, err := randomPercentage(1000) // 0.001 precision
if err != nil {
return err
}
// 3 cases:
// - current interval is over threshold but not previous -> drop
// - previous interval is over threshold -> apply rate limit
// - none of current and previous intervals are over threshold -> accept
if (n > o.Max && p <= o.Max) || (p > o.Max && random >= float64(o.Max)/float64(p)) {
hook(o.OnDropped, ctx, record)
return nil
}
hook(o.OnAccepted, ctx, record)
return next(ctx, record)
},
func(attrs []slog.Attr, next func([]slog.Attr) slog.Handler) slog.Handler {
return next(attrs)
},
func(name string, next func(string) slog.Handler) slog.Handler {
return next(name)
},
)
}