Skip to content

Commit

Permalink
MaxBreadcrumbs setting & fix README (#33)
Browse files Browse the repository at this point in the history
* Support logger name. Update dependencies

* Rename NameKey to LoggerNameKey. Add docs

* Reorder config fields

* Add indirect dependencies required since Go 1.17

* Fix breadcrumbs usage instruction

* Fix breadcrumbs usage instruction

* Fix breadcrumbs usage instruction

* Fix breadcrumbs usage instruction

* Fix breadcrumbs usage instruction

* Add MaxBreadcrumbs setting

Co-authored-by: Maxim Shipko <[email protected]>
  • Loading branch information
SladeThe and Maxim Shipko authored Oct 26, 2022
1 parent 1159232 commit 6cab783
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ func modifyToSentryLogger(log *zap.Logger, client *sentry.Client) *zap.Logger {
}
core, err := zapsentry.NewCore(cfg, zapsentry.NewSentryClientFromClient(client))

// to use breadcrumbs feature - create new scope explicitly
log = log.With(zapsentry.NewScope())

//in case of err it will return noop core. so we can safely attach it
if err != nil {
log.Warn("failed to init zap", zap.Error(err))
}
return zapsentry.AttachCoreToLogger(core, log)

log = zapsentry.AttachCoreToLogger(core, log)

// to use breadcrumbs feature - create new scope explicitly
// and attach after attaching the core
return log.With(zapsentry.NewScope())
}
```

Expand Down
5 changes: 5 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ type Configuration struct {
// The field is ignored, if EnableBreadcrumbs is not set.
BreadcrumbLevel zapcore.Level

// MaxBreadcrumbs is the maximum number of breadcrumb events to keep.
// Leave it zero or set to negative for a reasonable default value.
// The field is ignored, if EnableBreadcrumbs is not set.
MaxBreadcrumbs int

// FlushTimeout is the timeout for flushing events to Sentry.
FlushTimeout time.Duration

Expand Down
10 changes: 7 additions & 3 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
)

const (
maxBreadcrumbs = 1000
maxErrorDepth = 10
defaultMaxBreadcrumbs = 100
maxErrorDepth = 10

zapSentryScopeKey = "_zapsentry_scope_"
)
Expand All @@ -40,6 +40,10 @@ func NewCore(cfg Configuration, factory SentryClientFactory) (zapcore.Core, erro
return zapcore.NewNopCore(), errors.New("breadcrumb level must be lower than or equal to error level")
}

if cfg.MaxBreadcrumbs <= 0 {
cfg.MaxBreadcrumbs = defaultMaxBreadcrumbs
}

core := core{
client: client,
cfg: &cfg,
Expand Down Expand Up @@ -85,7 +89,7 @@ func (c *core) Write(ent zapcore.Entry, fs []zapcore.Field) error {
Timestamp: ent.Time,
}

c.sentryScope.AddBreadcrumb(&breadcrumb, maxBreadcrumbs)
c.sentryScope.AddBreadcrumb(&breadcrumb, c.cfg.MaxBreadcrumbs)
}

if c.cfg.Level.Enabled(ent.Level) {
Expand Down

0 comments on commit 6cab783

Please sign in to comment.