-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
153 lines (114 loc) · 3.67 KB
/
main.go
File metadata and controls
153 lines (114 loc) · 3.67 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
package main
import (
"fmt"
"os"
"time"
"github.com/willibrandon/mtlog"
"github.com/willibrandon/mtlog/selflog"
)
func main() {
// Enable selflog to see debug output
selflog.Enable(os.Stdout)
defer selflog.Disable()
fmt.Println("=== Sampling Debug Example ===")
// Example 1: Debug counter-based sampling
debugCounterSampling()
// Example 2: Debug rate-based sampling
debugRateSampling()
// Example 3: Debug group sampling
debugGroupSampling()
// Example 4: Debug backoff sampling
debugBackoffSampling()
// Example 5: Check debug status
checkDebugStatus()
}
func debugCounterSampling() {
fmt.Println("--- Counter-Based Sampling Debug ---")
// Enable sampling debug
mtlog.EnableSamplingDebug()
defer mtlog.DisableSamplingDebug()
// Create logger with counter sampling
logger := mtlog.New(mtlog.WithConsole()).Sample(3) // Every 3rd message
// Log messages - debug output will show decisions
for i := 1; i <= 6; i++ {
logger.Info("Counter message {Number}", i)
}
// Show stats
sampled, skipped := logger.GetSamplingStats()
fmt.Printf("Stats: Sampled=%d, Skipped=%d\n\n", sampled, skipped)
}
func debugRateSampling() {
fmt.Println("--- Rate-Based Sampling Debug ---")
// Enable sampling debug
mtlog.EnableSamplingDebug()
defer mtlog.DisableSamplingDebug()
// Create logger with rate sampling
logger := mtlog.New(mtlog.WithConsole()).SampleRate(0.5) // 50% sampling
// Log messages - debug output will show decisions
for i := 1; i <= 10; i++ {
logger.Info("Rate message {Number}", i)
}
// Show stats
sampled, skipped := logger.GetSamplingStats()
fmt.Printf("Stats: Sampled=%d, Skipped=%d (approximately 50%%)\n\n", sampled, skipped)
}
func debugGroupSampling() {
fmt.Println("--- Group Sampling Debug ---")
// Enable sampling debug
mtlog.EnableSamplingDebug()
defer mtlog.DisableSamplingDebug()
logger := mtlog.New(mtlog.WithConsole())
// Create multiple group samplers
apiLogger := logger.SampleGroup("api-endpoint", 2)
dbLogger := logger.SampleGroup("database-query", 3)
// Log to different groups
for i := 1; i <= 4; i++ {
apiLogger.Info("API request {RequestID}", i)
dbLogger.Info("DB query {QueryID}", i)
}
fmt.Println()
}
func debugBackoffSampling() {
fmt.Println("--- Backoff Sampling Debug ---")
// Enable sampling debug
mtlog.EnableSamplingDebug()
defer mtlog.DisableSamplingDebug()
logger := mtlog.New(mtlog.WithConsole())
// Simulate errors with backoff
for i := 1; i <= 8; i++ {
// Use backoff sampling for error messages
logger.SampleBackoff("error-type-1", 2.0).
Error("Error occurred: attempt {Attempt}", i)
// Small delay to show time-based behavior
time.Sleep(100 * time.Millisecond)
}
fmt.Println()
}
func checkDebugStatus() {
fmt.Println("--- Debug Status Check ---")
// Check initial status
fmt.Printf("Debug enabled initially: %v\n", mtlog.IsSamplingDebugEnabled())
// Enable and check
mtlog.EnableSamplingDebug()
fmt.Printf("Debug enabled after enable: %v\n", mtlog.IsSamplingDebugEnabled())
// Disable and check
mtlog.DisableSamplingDebug()
fmt.Printf("Debug enabled after disable: %v\n", mtlog.IsSamplingDebugEnabled())
// Demonstrate conditional debug
logger := mtlog.New(mtlog.WithConsole()).Sample(2)
// Log without debug
fmt.Println("\nLogging without debug:")
for i := 1; i <= 3; i++ {
logger.Info("No debug message {N}", i)
}
// Log with debug
fmt.Println("\nLogging with debug:")
mtlog.EnableSamplingDebug()
for i := 4; i <= 6; i++ {
logger.Info("With debug message {N}", i)
}
mtlog.DisableSamplingDebug()
// Get final stats
sampled, skipped := logger.GetSamplingStats()
fmt.Printf("\nFinal stats: Sampled=%d, Skipped=%d\n", sampled, skipped)
}