This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentry.go
66 lines (58 loc) · 1.64 KB
/
sentry.go
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
package logging
import (
"log/slog"
"strings"
"github.com/getsentry/sentry-go"
validation "github.com/go-ozzo/ozzo-validation/v4"
slogsentry "github.com/samber/slog-sentry/v2"
)
type SentryConfig struct {
Level string `yaml:"level" default:"warn"`
DSN string `yaml:"dsn"`
SampleRate float64 `yaml:"sampleRate" default:"1.0"`
IgnoreErrors []string `yaml:"ignoreErrors"`
SendDefaultPII bool `yaml:"sendDefaultPII"`
Env string `yaml:"env"`
Transport sentry.Transport `yaml:"-"`
}
func (c SentryConfig) Validate() error {
return validation.ValidateStruct(&c,
validation.Field(&c.DSN, validation.Required),
)
}
func (c *SentryConfig) SentryOptions(environment string) sentry.ClientOptions {
if c.Env != "" {
environment = c.Env
}
return sentry.ClientOptions{
Dsn: c.DSN,
IgnoreErrors: c.IgnoreErrors,
Environment: environment,
SampleRate: c.SampleRate,
SendDefaultPII: c.SendDefaultPII,
Transport: c.Transport,
}
}
func (c *SentryConfig) getLevel() slog.Level {
switch strings.ToLower(c.Level) {
case "debug":
return slog.LevelDebug
case "info":
return slog.LevelInfo
case "warn", "warning":
return slog.LevelWarn
case "err", "error":
return slog.LevelError
default:
return slog.LevelInfo
}
}
func NewSentryHandler(conf *SentryConfig, environment string) Handle {
if err := sentry.Init(conf.SentryOptions(environment)); err != nil {
slog.Error("Sentry initialize")
}
option := slogsentry.Option{
Level: conf.getLevel(),
}
return NewErrorTracking(NewAsyncHandler(option.NewSentryHandler()))
}