-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexample.go
More file actions
103 lines (87 loc) 路 2.15 KB
/
Copy pathexample.go
File metadata and controls
103 lines (87 loc) 路 2.15 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
package main
import (
"context"
"fmt"
"os"
"sync/atomic"
"time"
"log/slog"
slogmulti "github.com/samber/slog-multi"
slogsampling "github.com/samber/slog-sampling"
)
func main() {
var accepted atomic.Int64
var dropped atomic.Int64
option := slogsampling.ThresholdSamplingOption{
Tick: 5 * time.Second,
Threshold: 10,
Rate: 0.1,
Matcher: func(ctx context.Context, record *slog.Record) string {
return record.Level.String()
},
OnAccepted: func(context.Context, slog.Record) {
accepted.Add(1)
},
OnDropped: func(context.Context, slog.Record) {
dropped.Add(1)
},
}
// option := slogsampling.CustomSamplingOption{
// Sampler: func(ctx context.Context, record slog.Record) float64 {
// switch record.Level {
// case slog.LevelError:
// return 0.5
// case slog.LevelWarn:
// return 0.2
// default:
// return 0.01
// }
// },
// OnAccepted: func(context.Context, slog.Record) {
// accepted.Add(1)
// },
// OnDropped: func(context.Context, slog.Record) {
// dropped.Add(1)
// },
// }
// option := slogsampling.UniformSamplingOption{
// Rate: 0.33,
// OnAccepted: func(context.Context, slog.Record) {
// accepted.Add(1)
// },
// OnDropped: func(context.Context, slog.Record) {
// dropped.Add(1)
// },
// }
// option := slogsampling.AbsoluteSamplingOption{
// Tick: 5 * time.Second,
// Max: 10,
// Matcher: func(ctx context.Context, record *slog.Record) string {
// return record.Level.String()
// },
// OnAccepted: func(context.Context, slog.Record) {
// accepted.Add(1)
// },
// OnDropped: func(context.Context, slog.Record) {
// dropped.Add(1)
// },
// }
logger := slog.New(
slogmulti.
Pipe(option.NewMiddleware()).
Handler(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{})),
)
l := logger.
With("email", "samuel@acme.org").
With("environment", "dev").
With("hello", "world")
for i := 0; i < 100; i++ {
l.Error("Message 1")
l.Error("Message 2")
l.Info("Message 1")
time.Sleep(100 * time.Millisecond)
}
fmt.Printf("\n\nResults:\n")
fmt.Printf("Accepted: %d\n", accepted.Load())
fmt.Printf("Dropped: %d\n", dropped.Load())
}