-
Notifications
You must be signed in to change notification settings - Fork 24
/
sentry.go
147 lines (128 loc) · 4.16 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
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
// +kubebuilder:validation:Optional
package sentry
import (
"context"
"io"
"time"
"github.com/rs/zerolog"
"go.uber.org/fx"
"github.com/getsentry/sentry-go"
"github.com/fluxninja/aperture/pkg/config"
"github.com/fluxninja/aperture/pkg/info"
"github.com/fluxninja/aperture/pkg/log"
"github.com/fluxninja/aperture/pkg/panichandler"
"github.com/fluxninja/aperture/pkg/status"
)
// SentryConfig holds configuration for Sentry.
// swagger:model
// +kubebuilder:object:generate=true
type SentryConfig struct {
// If DSN is not set, the client is effectively disabled
// You can set test project's dsn to send log events.
// oss-aperture project dsn is set as default.
Dsn string `json:"dsn" default:"https://6223f112b0ac4344aa67e94d1631eb85@o574197.ingest.sentry.io/6605877"`
// Environment
Environment string `json:"environment" default:"production"`
// Sample rate for sampling traces i.e. 0.0 to 1.0
TracesSampleRate float64 `json:"traces_sample_rate" default:"0.2"`
// Sample rate for event submission i.e. 0.0 to 1.0
SampleRate float64 `json:"sample_rate" default:"1.0"`
// Debug enables printing of Sentry SDK debug messages
Debug bool `json:"debug" default:"true"`
// Configure to generate and attach stacktraces to capturing message calls
AttachStacktrace bool `json:"attach_stack_trace" default:"true"`
// Sentry crash report disabled
Disabled bool `json:"disabled" default:"false"`
}
// SentryWriterConstructor holds fields to create an annotated instance of Sentry Writer.
type SentryWriterConstructor struct {
// Name of sentry instance
Name string
// Config key
ConfigKey string
// Default Config
DefaultConfig SentryConfig
}
// Annotate creates an annotated instance of SentryWriter.
func (constructor SentryWriterConstructor) Annotate() fx.Option {
var group string
if constructor.Name == "" {
group = config.GroupTag("main-logger")
} else {
group = config.GroupTag(constructor.Name)
}
return fx.Options(
fx.Provide(
fx.Annotate(
constructor.provideSentryWriter,
fx.ResultTags(group),
),
),
fx.Invoke(setStatusRegistry),
)
}
func (constructor SentryWriterConstructor) provideSentryWriter(unmarshaller config.Unmarshaller,
lifecycle fx.Lifecycle,
) (io.Writer, *SentryWriter, error) {
config := constructor.DefaultConfig
if err := unmarshaller.UnmarshalKey(constructor.ConfigKey, &config); err != nil {
log.Panic().Err(err).Msg("Unable to deserialize sentry config")
}
if config.Disabled {
log.Info().Msg("Sentry crash report disabled")
return nil, nil, nil
}
sentryWriter, _ := newSentryWriter(config)
lifecycle.Append(fx.Hook{
OnStart: func(_ context.Context) error {
sentry.CurrentHub().BindClient(sentryWriter.client)
return nil
},
OnStop: func(_ context.Context) error {
duration, _ := time.ParseDuration(SentryFlushWait)
sentry.Flush(duration)
return nil
},
})
return sentryWriter, sentryWriter, nil
}
// setStatusRegistry sets the status registry.
func setStatusRegistry(sentryWriter *SentryWriter, statusRegistry status.Registry) {
sentryWriter.statusRegistry = statusRegistry
}
// newSentryWriter creates a new SentryWriter instance with Sentry Client and registers panic handler.
func newSentryWriter(config SentryConfig) (*SentryWriter, error) {
client, err := sentry.NewClient(sentry.ClientOptions{
Dsn: config.Dsn,
Debug: config.Debug,
Environment: config.Environment,
Release: info.Version,
AttachStacktrace: config.AttachStacktrace,
SampleRate: config.SampleRate,
TracesSampleRate: config.TracesSampleRate,
})
if err != nil {
log.Error().Err(err).Msg("Failed to create sentry client")
return nil, err
}
reportLevels := []zerolog.Level{
zerolog.DebugLevel,
zerolog.InfoLevel,
zerolog.WarnLevel,
zerolog.ErrorLevel,
zerolog.FatalLevel,
zerolog.PanicLevel,
}
levels := make(map[zerolog.Level]struct{}, len(reportLevels))
for _, level := range reportLevels {
levels[level] = struct{}{}
}
crashWriter := NewCrashWriter(logCountLimit)
sentryWriter := &SentryWriter{
client: client,
levels: levels,
crashWriter: crashWriter,
}
panichandler.RegisterPanicHandler(sentryWriter.sentryPanicHandler)
return sentryWriter, nil
}