-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgo_example.go
More file actions
534 lines (444 loc) · 14.4 KB
/
go_example.go
File metadata and controls
534 lines (444 loc) · 14.4 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/*
Bugsink/Sentry SDK Integration Example for Go
==============================================
This example demonstrates comprehensive error tracking integration
using the Sentry SDK with a self-hosted Bugsink server.
Requirements:
go get github.com/getsentry/sentry-go
DSN Format:
https://<project-key>@<your-bugsink-host>/<project-id>
*/
package main
import (
"context"
"errors"
"fmt"
"log"
"net/http"
"os"
"runtime"
"strings"
"time"
"github.com/getsentry/sentry-go"
sentryhttp "github.com/getsentry/sentry-go/http"
)
// =============================================================================
// CONFIGURATION
// =============================================================================
func getEnv(key, defaultValue string) string {
if value := os.Getenv(key); value != "" {
return value
}
return defaultValue
}
var (
SentryDSN = getEnv("SENTRY_DSN", "https://your-project-key@errors.observability.app.bauer-group.com/1")
Environment = getEnv("ENVIRONMENT", "development")
Release = getEnv("APP_VERSION", "1.0.0")
ServerName = getEnv("HOSTNAME", "")
)
// =============================================================================
// SENTRY SERVICE
// =============================================================================
// SentryService provides a wrapper around the Sentry SDK
type SentryService struct {
initialized bool
}
// NewSentryService creates a new SentryService instance
func NewSentryService() *SentryService {
return &SentryService{}
}
// Init initializes the Sentry SDK
func (s *SentryService) Init() error {
if s.initialized {
log.Println("Sentry already initialized")
return nil
}
// Determine sample rate based on environment
tracesSampleRate := 1.0
if Environment == "production" {
tracesSampleRate = 0.1
}
err := sentry.Init(sentry.ClientOptions{
Dsn: SentryDSN,
Environment: Environment,
Release: fmt.Sprintf("my-app@%s", Release),
ServerName: ServerName,
Debug: Environment == "development",
AttachStacktrace: true,
// Performance Monitoring
TracesSampleRate: tracesSampleRate,
ProfilesSampleRate: 0.1,
// Error Sampling
SampleRate: 1.0,
// Before Send Hook
BeforeSend: beforeSendHandler,
// Before Breadcrumb Hook
BeforeBreadcrumb: beforeBreadcrumbHandler,
})
if err != nil {
return fmt.Errorf("sentry initialization failed: %w", err)
}
// Set global tags
sentry.ConfigureScope(func(scope *sentry.Scope) {
scope.SetTag("app.component", "backend")
scope.SetTag("app.runtime", "go")
scope.SetTag("app.go_version", runtime.Version())
})
s.initialized = true
log.Printf("Sentry initialized for environment: %s", Environment)
return nil
}
// beforeSendHandler processes events before sending
func beforeSendHandler(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
// Sanitize sensitive headers
if event.Request != nil && event.Request.Headers != nil {
sensitiveHeaders := []string{"Authorization", "Cookie", "X-API-Key"}
for _, header := range sensitiveHeaders {
if _, ok := event.Request.Headers[header]; ok {
event.Request.Headers[header] = "[REDACTED]"
}
}
}
// Filter specific exceptions
if hint.OriginalException != nil {
var expectedErr *ExpectedBusinessError
if errors.As(hint.OriginalException, &expectedErr) {
return nil // Don't send this event
}
}
return event
}
// beforeBreadcrumbHandler processes breadcrumbs before adding
func beforeBreadcrumbHandler(breadcrumb *sentry.Breadcrumb, hint *sentry.BreadcrumbHint) *sentry.Breadcrumb {
// Filter health check requests
if breadcrumb.Category == "http" {
if url, ok := breadcrumb.Data["url"].(string); ok {
if strings.Contains(url, "/health") {
return nil
}
}
}
return breadcrumb
}
// SetUser sets the user context
func (s *SentryService) SetUser(id, email, username, ipAddress string, extra map[string]string) {
sentry.ConfigureScope(func(scope *sentry.Scope) {
user := sentry.User{
ID: id,
Email: email,
Username: username,
IPAddress: ipAddress,
}
if extra != nil {
user.Data = extra
}
scope.SetUser(user)
})
}
// ClearUser clears the user context
func (s *SentryService) ClearUser() {
sentry.ConfigureScope(func(scope *sentry.Scope) {
scope.SetUser(sentry.User{})
})
}
// AddBreadcrumb adds a breadcrumb
func (s *SentryService) AddBreadcrumb(message, category string, level sentry.Level, data map[string]interface{}) {
if category == "" {
category = "custom"
}
if level == "" {
level = sentry.LevelInfo
}
sentry.AddBreadcrumb(&sentry.Breadcrumb{
Message: message,
Category: category,
Level: level,
Data: data,
Timestamp: time.Now(),
})
}
// SetTag sets a tag
func (s *SentryService) SetTag(key, value string) {
sentry.ConfigureScope(func(scope *sentry.Scope) {
scope.SetTag(key, value)
})
}
// SetExtra sets extra context
func (s *SentryService) SetExtra(key string, value interface{}) {
sentry.ConfigureScope(func(scope *sentry.Scope) {
scope.SetExtra(key, value)
})
}
// SetContext sets custom context
func (s *SentryService) SetContext(name string, data map[string]interface{}) {
sentry.ConfigureScope(func(scope *sentry.Scope) {
scope.SetContext(name, data)
})
}
// CaptureException captures an exception
func (s *SentryService) CaptureException(err error) *sentry.EventID {
return sentry.CaptureException(err)
}
// CaptureExceptionWithContext captures an exception with extra context
func (s *SentryService) CaptureExceptionWithContext(err error, extraContext map[string]interface{}) *sentry.EventID {
sentry.WithScope(func(scope *sentry.Scope) {
for key, value := range extraContext {
scope.SetExtra(key, value)
}
sentry.CaptureException(err)
})
return nil
}
// CaptureMessage captures a message
func (s *SentryService) CaptureMessage(message string, level sentry.Level) *sentry.EventID {
return sentry.CaptureMessage(message)
}
// CaptureMessageWithContext captures a message with extra context
func (s *SentryService) CaptureMessageWithContext(message string, level sentry.Level, extraContext map[string]interface{}) *sentry.EventID {
var eventID *sentry.EventID
sentry.WithScope(func(scope *sentry.Scope) {
scope.SetLevel(level)
for key, value := range extraContext {
scope.SetExtra(key, value)
}
eventID = sentry.CaptureMessage(message)
})
return eventID
}
// WithTransaction executes a function within a transaction
func (s *SentryService) WithTransaction(ctx context.Context, name, operation string, fn func(context.Context, *sentry.Span) error) error {
span := sentry.StartSpan(ctx, operation, sentry.WithTransactionName(name))
defer span.Finish()
err := fn(span.Context(), span)
if err != nil {
span.Status = sentry.SpanStatusInternalError
return err
}
span.Status = sentry.SpanStatusOK
return nil
}
// WithSpan executes a function within a child span
func (s *SentryService) WithSpan(ctx context.Context, operation, description string, fn func(context.Context) error) error {
span := sentry.StartSpan(ctx, operation, sentry.WithDescription(description))
defer span.Finish()
err := fn(span.Context())
if err != nil {
span.Status = sentry.SpanStatusInternalError
return err
}
span.Status = sentry.SpanStatusOK
return nil
}
// Flush flushes pending events
func (s *SentryService) Flush(timeout time.Duration) bool {
return sentry.Flush(timeout)
}
// =============================================================================
// CUSTOM ERRORS
// =============================================================================
// ExpectedBusinessError represents an expected business logic error
type ExpectedBusinessError struct {
Message string
}
func (e *ExpectedBusinessError) Error() string {
return e.Message
}
// =============================================================================
// EXAMPLE SERVICE
// =============================================================================
// ExampleService demonstrates Sentry integration patterns
type ExampleService struct {
sentry *SentryService
}
// NewExampleService creates a new ExampleService
func NewExampleService(sentry *SentryService) *ExampleService {
return &ExampleService{sentry: sentry}
}
// FetchData fetches data with error tracking
func (s *ExampleService) FetchData(id string) (string, error) {
s.sentry.AddBreadcrumb(
fmt.Sprintf("Fetching data for %s", id),
"service",
sentry.LevelInfo,
map[string]interface{}{"id": id},
)
if id == "error" {
return "", errors.New("failed to fetch data")
}
return fmt.Sprintf("Data for %s", id), nil
}
// ProcessBatch processes items with transaction tracking
func (s *ExampleService) ProcessBatch(ctx context.Context, items []string) (int, error) {
var processed int
err := s.sentry.WithTransaction(ctx, "process_batch", "task", func(ctx context.Context, span *sentry.Span) error {
for _, item := range items {
err := s.sentry.WithSpan(ctx, "task.item", fmt.Sprintf("process_%s", item), func(ctx context.Context) error {
time.Sleep(50 * time.Millisecond) // Simulate work
processed++
return nil
})
if err != nil {
return err
}
}
return nil
})
return processed, err
}
// =============================================================================
// HTTP HANDLER EXAMPLE
// =============================================================================
// CreateHTTPHandler creates an HTTP handler with Sentry integration
func CreateHTTPHandler(sentryService *SentryService) http.Handler {
mux := http.NewServeMux()
// Create Sentry handler wrapper
sentryHandler := sentryhttp.New(sentryhttp.Options{
Repanic: true,
})
// Routes
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
sentryService.AddBreadcrumb("Homepage visited", "navigation", sentry.LevelInfo, nil)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"status": "ok"}`))
})
mux.HandleFunc("/api/users/", func(w http.ResponseWriter, r *http.Request) {
userID := strings.TrimPrefix(r.URL.Path, "/api/users/")
sentryService.AddBreadcrumb(
fmt.Sprintf("Fetching user %s", userID),
"api",
sentry.LevelInfo,
map[string]interface{}{"userID": userID},
)
if userID == "0" {
panic("Invalid user ID")
}
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(fmt.Sprintf(`{"userID": "%s", "name": "Test User"}`, userID)))
})
mux.HandleFunc("/api/error", func(w http.ResponseWriter, r *http.Request) {
panic("Test error from /api/error endpoint")
})
mux.HandleFunc("/api/message", func(w http.ResponseWriter, r *http.Request) {
sentryService.CaptureMessageWithContext(
"User triggered test message",
sentry.LevelInfo,
map[string]interface{}{
"endpoint": "/api/message",
"test": true,
},
)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"status": "message sent"}`))
})
// Wrap with Sentry handler
return sentryHandler.Handle(mux)
}
// =============================================================================
// MAIN EXAMPLE
// =============================================================================
func main() {
fmt.Println(strings.Repeat("=", 60))
fmt.Println("Bugsink/Sentry Go SDK Integration Example")
fmt.Println(strings.Repeat("=", 60))
// Initialize Sentry
sentryService := NewSentryService()
if err := sentryService.Init(); err != nil {
log.Fatalf("Failed to initialize Sentry: %v", err)
}
defer sentryService.Flush(5 * time.Second)
// Set user context
sentryService.SetUser(
"user-123",
"developer@example.com",
"developer",
"127.0.0.1",
map[string]string{"subscriptionTier": "premium"},
)
// Add breadcrumbs
sentryService.AddBreadcrumb("Application started", "app", sentry.LevelInfo, nil)
sentryService.AddBreadcrumb("User authenticated", "auth", sentry.LevelInfo, nil)
// Example 1: Capture handled exception
fmt.Println("\n1. Capturing handled exception...")
func() {
defer func() {
if r := recover(); r != nil {
err := fmt.Errorf("panic: %v", r)
eventID := sentryService.CaptureExceptionWithContext(err, map[string]interface{}{
"operation": "division",
"numerator": 10,
"denominator": 0,
})
fmt.Printf(" Exception captured: %v\n", eventID)
}
}()
// This will panic
result := 10 / 0
_ = result
}()
// Example 2: Capture message
fmt.Println("\n2. Capturing info message...")
eventID := sentryService.CaptureMessageWithContext(
"User completed onboarding flow",
sentry.LevelInfo,
map[string]interface{}{
"stepsCompleted": 5,
"timeTakenSeconds": 120,
},
)
fmt.Printf(" Message captured: %v\n", eventID)
// Example 3: Use example service
fmt.Println("\n3. Using example service...")
service := NewExampleService(sentryService)
data, err := service.FetchData("123")
if err != nil {
fmt.Println(" Error handled")
} else {
fmt.Printf(" Data fetched: %s\n", data)
}
// Example 4: Transaction with service
fmt.Println("\n4. Processing batch with transaction...")
ctx := context.Background()
processed, err := service.ProcessBatch(ctx, []string{"a", "b", "c"})
if err != nil {
fmt.Printf(" Error: %v\n", err)
} else {
fmt.Printf(" Processed %d items\n", processed)
}
// Example 5: Scoped context
fmt.Println("\n5. Using scoped context...")
sentry.WithScope(func(scope *sentry.Scope) {
scope.SetTag("feature", "new_checkout")
scope.SetExtra("cartItems", 3)
scope.SetExtra("totalAmount", 99.99)
sentry.CaptureMessage("Checkout initiated")
})
fmt.Println(" Scoped message captured")
// Example 6: Manual transaction with spans
fmt.Println("\n6. Creating transaction with spans...")
sentryService.WithTransaction(ctx, "order_processing", "task", func(ctx context.Context, txn *sentry.Span) error {
// Fetch order
fetchSpan := sentry.StartSpan(ctx, "db.query", sentry.WithDescription("Fetch order"))
time.Sleep(50 * time.Millisecond)
fetchSpan.Status = sentry.SpanStatusOK
fetchSpan.Finish()
// Payment API
paymentSpan := sentry.StartSpan(ctx, "http.client", sentry.WithDescription("Payment API"))
time.Sleep(100 * time.Millisecond)
paymentSpan.Status = sentry.SpanStatusOK
paymentSpan.Finish()
// Update order status
updateSpan := sentry.StartSpan(ctx, "db.query", sentry.WithDescription("Update order status"))
time.Sleep(50 * time.Millisecond)
updateSpan.Status = sentry.SpanStatusOK
updateSpan.Finish()
return nil
})
fmt.Println(" Transaction with spans recorded")
// Clean up
sentryService.ClearUser()
fmt.Println("\n" + strings.Repeat("=", 60))
fmt.Println("All examples completed!")
fmt.Println("Check your Bugsink dashboard")
fmt.Println(strings.Repeat("=", 60))
}