-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmiddleware_uniform.go
51 lines (42 loc) · 1.24 KB
/
middleware_uniform.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
package slogsampling
import (
"context"
"log/slog"
slogmulti "github.com/samber/slog-multi"
)
type UniformSamplingOption struct {
// The sample rate for sampling traces in the range [0.0, 1.0].
Rate float64
// Optional hooks
OnAccepted func(context.Context, slog.Record)
OnDropped func(context.Context, slog.Record)
}
// NewMiddleware returns a slog-multi middleware.
func (o UniformSamplingOption) NewMiddleware() slogmulti.Middleware {
if o.Rate < 0.0 || o.Rate > 1.0 {
panic("unexpected Rate: must be between 0.0 and 1.0")
}
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 {
random, err := randomPercentage(1000) // 0.001 precision
if err != nil {
return err
}
if random >= o.Rate {
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)
},
)
}