-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentry.go
More file actions
78 lines (64 loc) · 1.74 KB
/
sentry.go
File metadata and controls
78 lines (64 loc) · 1.74 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
package main
import (
"fmt"
"os"
"time"
"github.com/getsentry/sentry-go"
)
// InitSentry initializes the Sentry client with the given DSN
func InitSentry(dsn string) error {
// Determine environment based on git branch or default to production
environment := getEnvironment()
err := sentry.Init(sentry.ClientOptions{
Dsn: dsn,
Environment: environment,
TracesSampleRate: 0.1, // 10% sampling for performance monitoring
AttachStacktrace: true,
})
if err != nil {
return fmt.Errorf("sentry initialization failed: %w", err)
}
// Set the user context if possible
if user, err := os.UserCacheDir(); err == nil {
sentry.ConfigureScope(func(scope *sentry.Scope) {
scope.SetUser(sentry.User{
ID: user,
})
})
}
return nil
}
// getEnvironment determines the environment (dev or production)
func getEnvironment() string {
// Check if running in dev mode by looking for git directory or environment variables
if _, err := os.Stat(".git"); err == nil {
return "development"
}
if os.Getenv("TED_ENV") == "dev" {
return "development"
}
return "production"
}
// FlushAndShutdown flushes pending Sentry events and closes the client
func FlushAndShutdown() {
sentry.Flush(5 * time.Second)
}
// CaptureError sends an error to Sentry along with any pending breadcrumbs
func CaptureError(err error) {
if err == nil {
return
}
// Flush pending breadcrumbs before capturing the error
if breadcrumbs != nil {
breadcrumbs.Flush()
}
sentry.CaptureException(err)
}
// CaptureMessage sends a message to Sentry with optional breadcrumbs
func CaptureMessage(message string) {
// Flush pending breadcrumbs before capturing the message
if breadcrumbs != nil {
breadcrumbs.Flush()
}
sentry.CaptureMessage(message)
}