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

Commit 3f53baa

Browse files
committed
add breadcrumbs support
1 parent 9a0b4be commit 3f53baa

File tree

1 file changed

+50
-14
lines changed

1 file changed

+50
-14
lines changed

writer.go

+50-14
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,31 @@ var now = time.Now
3030
type Writer struct {
3131
hub *sentry.Hub
3232

33-
levels map[zerolog.Level]struct{}
34-
flushTimeout time.Duration
33+
levels map[zerolog.Level]struct{}
34+
flushTimeout time.Duration
35+
withBreadcrumbs bool
36+
}
37+
38+
// addBreadcrumb adds event as a breadcrumb
39+
func (w *Writer) addBreadcrumb(event *sentry.Event) {
40+
if !w.withBreadcrumbs {
41+
return
42+
}
43+
44+
// category is totally optional, but it's nice to have
45+
var category string
46+
if _, ok := event.Extra["category"]; ok {
47+
if v, ok := event.Extra["category"].(string); ok {
48+
category = v
49+
}
50+
}
51+
52+
w.hub.AddBreadcrumb(&sentry.Breadcrumb{
53+
Category: category,
54+
Message: event.Message,
55+
Level: event.Level,
56+
Data: event.Extra,
57+
}, nil)
3558
}
3659

3760
// Write handles zerolog's json and sends events to sentry.
@@ -43,19 +66,23 @@ func (w *Writer) Write(data []byte) (n int, err error) {
4366
return n, nil
4467
}
4568

46-
if _, enabled := w.levels[lvl]; !enabled {
69+
event, ok := w.parseLogEvent(data)
70+
event.Level = levelsMapping[lvl]
71+
72+
if !ok {
4773
return
4874
}
4975

50-
event, ok := w.parseLogEvent(data)
51-
event.Level = levelsMapping[lvl]
76+
if _, enabled := w.levels[lvl]; !enabled {
77+
// if the level is not enabled, add event as a breadcrumb
78+
w.addBreadcrumb(event)
79+
return
80+
}
5281

53-
if ok {
54-
w.hub.CaptureEvent(event)
55-
// should flush before os.Exit
56-
if event.Level == sentry.LevelFatal {
57-
w.hub.Flush(w.flushTimeout)
58-
}
82+
w.hub.CaptureEvent(event)
83+
// should flush before os.Exit
84+
if event.Level == sentry.LevelFatal {
85+
w.hub.Flush(w.flushTimeout)
5986
}
6087

6188
return
@@ -188,6 +215,7 @@ type config struct {
188215
environment string
189216
serverName string
190217
ignoreErrors []string
218+
breadcrumbs bool
191219
debug bool
192220
tracing bool
193221
debugWriter io.Writer
@@ -245,6 +273,13 @@ func WithIgnoreErrors(reList []string) WriterOption {
245273
})
246274
}
247275

276+
// WithBreadcrumbs enables sentry client breadcrumbs.
277+
func WithBreadcrumbs() WriterOption {
278+
return optionFunc(func(cfg *config) {
279+
cfg.breadcrumbs = true
280+
})
281+
}
282+
248283
// WithDebug enables sentry client debug logs.
249284
func WithDebug() WriterOption {
250285
return optionFunc(func(cfg *config) {
@@ -350,9 +385,10 @@ func New(dsn string, opts ...WriterOption) (*Writer, error) {
350385
}
351386

352387
return &Writer{
353-
hub: sentry.CurrentHub(),
354-
levels: levels,
355-
flushTimeout: cfg.flushTimeout,
388+
hub: sentry.CurrentHub(),
389+
levels: levels,
390+
flushTimeout: cfg.flushTimeout,
391+
withBreadcrumbs: cfg.breadcrumbs,
356392
}, nil
357393
}
358394

0 commit comments

Comments
 (0)