Skip to content
This repository was archived by the owner on Dec 30, 2024. It is now read-only.

Commit c154491

Browse files
committed
add new writer options
1 parent edcc90a commit c154491

File tree

5 files changed

+42
-15
lines changed

5 files changed

+42
-15
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: go
22

33
go:
4-
- '1.14'
4+
- '1.18'
55

66
script:
77
- make coverage

Makefile

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ modules:
66
test:
77
@$(GO) test -v -race -cover
88

9+
lint:
10+
golangci-lint run --deadline=5m -v
11+
912
benchmarks:
1013
@$(GO) test -bench=. -benchmem
1114

1215
coverage:
1316
@$(GO) test -race -covermode=atomic -coverprofile=cover.out
1417

15-
.PHONY: modules test coverage
18+
.PHONY: modules test lint benchmarks coverage

go.mod

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
module github.com/archdx/zerolog-sentry
22

3-
go 1.15
3+
go 1.18
44

55
require (
66
github.com/buger/jsonparser v1.0.0
77
github.com/getsentry/sentry-go v0.12.0
88
github.com/rs/zerolog v1.26.1
99
github.com/stretchr/testify v1.4.0
1010
)
11+
12+
require (
13+
github.com/davecgh/go-spew v1.1.1 // indirect
14+
github.com/pmezard/go-difflib v1.0.0 // indirect
15+
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac // indirect
16+
gopkg.in/yaml.v2 v2.2.4 // indirect
17+
)

go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tL
126126
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
127127
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
128128
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
129-
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
130129
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
131130
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
132131
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
@@ -211,7 +210,6 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
211210
golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo=
212211
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
213212
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
214-
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
215213
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
216214
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
217215
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

writer.go

+29-10
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ var _ = io.WriteCloser(new(Writer))
2323

2424
var now = time.Now
2525

26+
// Writer is a sentry events writer with std io.Writer iface.
2627
type Writer struct {
2728
client *sentry.Client
2829

2930
levels map[zerolog.Level]struct{}
3031
flushTimeout time.Duration
3132
}
3233

34+
// Write handles zerolog's json and sends events to sentry.
3335
func (w *Writer) Write(data []byte) (int, error) {
3436
event, ok := w.parseLogEvent(data)
3537
if ok {
@@ -43,6 +45,8 @@ func (w *Writer) Write(data []byte) (int, error) {
4345
return len(data), nil
4446
}
4547

48+
// Close forces client to flush all pending events.
49+
// Can be useful before application exits.
4650
func (w *Writer) Close() error {
4751
w.client.Flush(w.flushTimeout)
4852
return nil
@@ -136,6 +140,7 @@ func bytesToStrUnsafe(data []byte) string {
136140
return *(*string)(unsafe.Pointer(&data))
137141
}
138142

143+
// WriterOption configures sentry events writer.
139144
type WriterOption interface {
140145
apply(*config)
141146
}
@@ -150,63 +155,77 @@ type config struct {
150155
release string
151156
environment string
152157
serverName string
158+
ignoreErrors []string
153159
debug bool
154160
flushTimeout time.Duration
155161
}
156162

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.
158165
func WithLevels(levels ...zerolog.Level) WriterOption {
159166
return optionFunc(func(cfg *config) {
160167
cfg.levels = levels
161168
})
162169
}
163170

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.
165172
func WithSampleRate(rate float64) WriterOption {
166173
return optionFunc(func(cfg *config) {
167174
cfg.sampleRate = rate
168175
})
169176
}
170177

178+
// WithRelease configures the release to be sent with events.
171179
func WithRelease(release string) WriterOption {
172180
return optionFunc(func(cfg *config) {
173181
cfg.release = release
174182
})
175183
}
176184

185+
// WithEnvironment configures the environment to be sent with events.
177186
func WithEnvironment(environment string) WriterOption {
178187
return optionFunc(func(cfg *config) {
179188
cfg.environment = environment
180189
})
181190
}
182191

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.
184193
func WithServerName(serverName string) WriterOption {
185194
return optionFunc(func(cfg *config) {
186195
cfg.serverName = serverName
187196
})
188197
}
189198

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.
191208
func WithDebug() WriterOption {
192209
return optionFunc(func(cfg *config) {
193210
cfg.debug = true
194211
})
195212
}
196213

214+
// New creates writer with provided DSN and options.
197215
func New(dsn string, opts ...WriterOption) (*Writer, error) {
198216
cfg := newDefaultConfig()
199217
for _, opt := range opts {
200218
opt.apply(&cfg)
201219
}
202220

203221
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,
210229
})
211230

212231
if err != nil {

0 commit comments

Comments
 (0)