@@ -23,13 +23,15 @@ var _ = io.WriteCloser(new(Writer))
23
23
24
24
var now = time .Now
25
25
26
+ // Writer is a sentry events writer with std io.Writer iface.
26
27
type Writer struct {
27
28
client * sentry.Client
28
29
29
30
levels map [zerolog.Level ]struct {}
30
31
flushTimeout time.Duration
31
32
}
32
33
34
+ // Write handles zerolog's json and sends events to sentry.
33
35
func (w * Writer ) Write (data []byte ) (int , error ) {
34
36
event , ok := w .parseLogEvent (data )
35
37
if ok {
@@ -43,6 +45,8 @@ func (w *Writer) Write(data []byte) (int, error) {
43
45
return len (data ), nil
44
46
}
45
47
48
+ // Close forces client to flush all pending events.
49
+ // Can be useful before application exits.
46
50
func (w * Writer ) Close () error {
47
51
w .client .Flush (w .flushTimeout )
48
52
return nil
@@ -136,6 +140,7 @@ func bytesToStrUnsafe(data []byte) string {
136
140
return * (* string )(unsafe .Pointer (& data ))
137
141
}
138
142
143
+ // WriterOption configures sentry events writer.
139
144
type WriterOption interface {
140
145
apply (* config )
141
146
}
@@ -150,63 +155,77 @@ type config struct {
150
155
release string
151
156
environment string
152
157
serverName string
158
+ ignoreErrors []string
153
159
debug bool
154
160
flushTimeout time.Duration
155
161
}
156
162
157
- // WithLevels configures zerolog levels that have to be sent to Sentry. Default levels are error, fatal, panic
163
+ // WithLevels configures zerolog levels that have to be sent to Sentry.
164
+ // Default levels are: error, fatal, panic.
158
165
func WithLevels (levels ... zerolog.Level ) WriterOption {
159
166
return optionFunc (func (cfg * config ) {
160
167
cfg .levels = levels
161
168
})
162
169
}
163
170
164
- // WithSampleRate configures the sample rate as a percentage of events to be sent in the range of 0.0 to 1.0
171
+ // WithSampleRate configures the sample rate as a percentage of events to be sent in the range of 0.0 to 1.0.
165
172
func WithSampleRate (rate float64 ) WriterOption {
166
173
return optionFunc (func (cfg * config ) {
167
174
cfg .sampleRate = rate
168
175
})
169
176
}
170
177
178
+ // WithRelease configures the release to be sent with events.
171
179
func WithRelease (release string ) WriterOption {
172
180
return optionFunc (func (cfg * config ) {
173
181
cfg .release = release
174
182
})
175
183
}
176
184
185
+ // WithEnvironment configures the environment to be sent with events.
177
186
func WithEnvironment (environment string ) WriterOption {
178
187
return optionFunc (func (cfg * config ) {
179
188
cfg .environment = environment
180
189
})
181
190
}
182
191
183
- // WithServerName configures the server name field for events. Default value is OS hostname
192
+ // WithServerName configures the server name field for events. Default value is OS hostname.
184
193
func WithServerName (serverName string ) WriterOption {
185
194
return optionFunc (func (cfg * config ) {
186
195
cfg .serverName = serverName
187
196
})
188
197
}
189
198
190
- // WithDebug enables sentry client debug logs
199
+ // WithIgnoreErrors configures the list of regexp strings that will be used to match against event's message
200
+ // and if applicable, caught errors type and value. If the match is found, then a whole event will be dropped.
201
+ func WithIgnoreErrors (reList []string ) WriterOption {
202
+ return optionFunc (func (cfg * config ) {
203
+ cfg .ignoreErrors = reList
204
+ })
205
+ }
206
+
207
+ // WithDebug enables sentry client debug logs.
191
208
func WithDebug () WriterOption {
192
209
return optionFunc (func (cfg * config ) {
193
210
cfg .debug = true
194
211
})
195
212
}
196
213
214
+ // New creates writer with provided DSN and options.
197
215
func New (dsn string , opts ... WriterOption ) (* Writer , error ) {
198
216
cfg := newDefaultConfig ()
199
217
for _ , opt := range opts {
200
218
opt .apply (& cfg )
201
219
}
202
220
203
221
client , err := sentry .NewClient (sentry.ClientOptions {
204
- Dsn : dsn ,
205
- SampleRate : cfg .sampleRate ,
206
- Release : cfg .release ,
207
- Environment : cfg .environment ,
208
- ServerName : cfg .serverName ,
209
- Debug : cfg .debug ,
222
+ Dsn : dsn ,
223
+ SampleRate : cfg .sampleRate ,
224
+ Release : cfg .release ,
225
+ Environment : cfg .environment ,
226
+ ServerName : cfg .serverName ,
227
+ IgnoreErrors : cfg .ignoreErrors ,
228
+ Debug : cfg .debug ,
210
229
})
211
230
212
231
if err != nil {
0 commit comments