-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
126 lines (108 loc) · 3.61 KB
/
main.go
File metadata and controls
126 lines (108 loc) · 3.61 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
package main
import (
"context"
"errors"
"log"
"os"
"time"
sentrygo "github.com/getsentry/sentry-go"
"github.com/willibrandon/mtlog"
"github.com/willibrandon/mtlog/adapters/sentry"
"github.com/willibrandon/mtlog/core"
)
func main() {
// Get DSN from environment
dsn := os.Getenv("SENTRY_DSN")
if dsn == "" {
log.Fatal("Please set SENTRY_DSN environment variable")
}
// Create Sentry sink with context enrichment
sentrySink, err := sentry.WithSentry(dsn,
sentry.WithEnvironment("production"),
sentry.WithRelease("v2.1.0"),
sentry.WithServerName("api-server-01"),
)
if err != nil {
log.Fatalf("Failed to create Sentry sink: %v", err)
}
// Create logger with context enrichment
logger := mtlog.New(
mtlog.WithConsole(),
mtlog.WithSink(sentrySink),
)
defer logger.Close()
// Example 1: User context
ctx := context.Background()
ctx = sentry.WithUser(ctx, sentrygo.User{
ID: "user-789",
Email: "john.doe@example.com",
Username: "johndoe",
})
// Log with user context - if this errors, Sentry will know which user was affected
logger.WithContext(ctx).Information("Processing payment for user")
// Simulate payment error - Sentry will associate this with the user
paymentErr := errors.New("payment gateway timeout")
logger.WithContext(ctx).Error("Payment failed: {Error}", paymentErr)
// Example 2: Request context with tags
requestCtx := context.Background()
requestCtx = sentry.WithTags(requestCtx, map[string]string{
"request.id": "req-12345",
"request.method": "POST",
"request.path": "/api/v1/payments",
"client.ip": "192.168.1.100",
"region": "us-west-2",
})
// Add user to request context
requestCtx = sentry.WithUser(requestCtx, sentrygo.User{
ID: "user-890",
Email: "jane.smith@example.com",
})
// Log with rich context
logger.WithContext(requestCtx).Information("API request received")
// If an error occurs, Sentry will have all the context
apiErr := errors.New("invalid payment method")
logger.WithContext(requestCtx).Error("API request failed: {Error}", apiErr)
// Example 3: Custom context data
deviceCtx := context.Background()
deviceCtx = sentry.WithContext(deviceCtx, "device", map[string]interface{}{
"model": "iPhone 14 Pro",
"os": "iOS 17.1",
"app_version": "3.2.1",
})
deviceCtx = sentry.WithContext(deviceCtx, "location", map[string]interface{}{
"country": "USA",
"city": "Seattle",
"timezone": "PST",
})
// Log with device and location context
logger.WithContext(deviceCtx).Warning("App crash detected on mobile device")
// Example 4: Custom fingerprinting for error grouping
customSink, err := sentry.WithSentry(dsn,
sentry.WithFingerprinter(func(event *core.LogEvent) []string {
// Group errors by template and error type
fingerprint := []string{event.MessageTemplate}
if err, ok := event.Properties["Error"].(error); ok {
if unwrapped := errors.Unwrap(err); unwrapped != nil {
fingerprint = append(fingerprint, unwrapped.Error())
} else {
fingerprint = append(fingerprint, err.Error())
}
}
return fingerprint
}),
)
if err != nil {
log.Fatalf("Failed to create custom Sentry sink: %v", err)
}
customLogger := mtlog.New(
mtlog.WithConsole(),
mtlog.WithSink(customSink),
)
defer customLogger.Close()
// These errors will be grouped together in Sentry
for i := 0; i < 3; i++ {
customLogger.Error("Database query failed: {Error}", errors.New("connection timeout"))
}
// Give time for events to be sent
time.Sleep(2 * time.Second)
}