-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
95 lines (80 loc) · 2.99 KB
/
main.go
File metadata and controls
95 lines (80 loc) · 2.99 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
package main
import (
"errors"
"fmt"
"log"
"time"
"github.com/willibrandon/mtlog"
"github.com/willibrandon/mtlog/adapters/sentry"
"github.com/willibrandon/mtlog/core"
"github.com/willibrandon/mtlog/selflog"
)
func main() {
// Enable self-diagnostics to see retry attempts
selflog.Enable(log.Writer())
defer selflog.Disable()
// Create Sentry sink with retry configuration
dsn := "https://your-key@sentry.io/project-id"
sentrySink, err := sentry.NewSentrySink(dsn,
sentry.WithEnvironment("development"),
sentry.WithRelease("v1.0.0"),
// Configure retry with exponential backoff
sentry.WithRetry(3, 1*time.Second), // 3 retries, starting at 1 second
sentry.WithRetryJitter(0.2), // 20% jitter to prevent thundering herd
// Configure batching for efficiency
sentry.WithBatchSize(10),
sentry.WithBatchTimeout(2*time.Second),
// Enable metrics to track retry behavior
sentry.WithMetrics(true),
sentry.WithMetricsCallback(5*time.Second, func(m sentry.Metrics) {
fmt.Printf("\n=== Sentry Metrics ===\n")
fmt.Printf("Events sent: %d\n", m.EventsSent)
fmt.Printf("Events failed: %d\n", m.EventsFailed)
fmt.Printf("Events retried: %d\n", m.EventsRetried)
fmt.Printf("Retry count: %d\n", m.RetryCount)
fmt.Printf("Network errors: %d\n", m.NetworkErrors)
if m.EventsSent > 0 {
retryRate := float64(m.RetryCount) / float64(m.EventsSent) * 100
fmt.Printf("Retry rate: %.2f%%\n", retryRate)
}
fmt.Printf("===================\n\n")
}),
)
if err != nil {
log.Fatalf("Failed to create Sentry sink: %v", err)
}
defer sentrySink.Close()
// Create logger with Sentry
logger := mtlog.New(
mtlog.WithConsole(),
mtlog.WithSink(sentrySink),
mtlog.WithMinimumLevel(core.InformationLevel),
)
// Simulate various network conditions
fmt.Println("Simulating various error conditions with retry logic...")
// Transient error that would benefit from retry
logger.Error("Database connection failed: {Error}",
errors.New("connection refused"))
// Multiple errors in quick succession (will batch)
for i := 0; i < 5; i++ {
logger.Error("Request {RequestId} failed with status {Status}",
fmt.Sprintf("req-%d", i),
500+i)
time.Sleep(100 * time.Millisecond)
}
// Critical error with context
logger.Fatal("Critical system failure: {Component} is down for {UserId} in {TenantId}",
"payment-gateway", "user-123", "tenant-456")
// Wait for batch to flush
fmt.Println("\nWaiting for events to be sent with retry logic...")
time.Sleep(5 * time.Second)
// Get final metrics
metrics := sentrySink.Metrics()
fmt.Printf("\n=== Final Metrics ===\n")
fmt.Printf("Total events sent: %d\n", metrics.EventsSent)
fmt.Printf("Total events failed: %d\n", metrics.EventsFailed)
fmt.Printf("Average batch size: %.2f\n", metrics.AverageBatchSize)
fmt.Printf("Last flush duration: %v\n", metrics.LastFlushDuration)
fmt.Printf("Total flush time: %v\n", metrics.TotalFlushTime)
fmt.Println("\nRetry example completed. Check your Sentry dashboard for events.")
}