Skip to content

Commit

Permalink
Support logger name. Update dependencies (#28)
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

Co-authored-by: Maxim Shipko <[email protected]>
  • Loading branch information
SladeThe and Maxim Shipko authored Mar 4, 2022
1 parent 89ad6ed commit 0eca8bc
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 45 deletions.
36 changes: 30 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,35 @@ import (

// Configuration is a minimal set of parameters for Sentry integration.
type Configuration struct {
Tags map[string]string
// Tags are passed as is to the corresponding sentry.Event field.
Tags map[string]string

// LoggerNameKey is the key for zap logger name.
// If not empty, the name is added to the rest of zapcore.Field(s),
// so that be careful with key duplicates.
// Leave LoggerNameKey empty to disable the feature.
LoggerNameKey string

// DisableStacktrace disables adding stacktrace to sentry.Event, if set.
DisableStacktrace bool
Level zapcore.Level
BreadcrumbLevel zapcore.Level
EnableBreadcrumbs bool // this feature works only when you explicitly passed new scope
FlushTimeout time.Duration
Hub *sentry.Hub

// Level is the minimal level of sentry.Event(s).
Level zapcore.Level

// EnableBreadcrumbs enables use of sentry.Breadcrumb(s).
// This feature works only when you explicitly passed new scope.
EnableBreadcrumbs bool

// BreadcrumbLevel is the minimal level of sentry.Breadcrumb(s).
// Breadcrumb specifies an application event that occurred before a Sentry event.
// NewCore fails if BreadcrumbLevel is greater than Level.
// The field is ignored, if EnableBreadcrumbs is not set.
BreadcrumbLevel zapcore.Level

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

// Hub overrides the sentry.CurrentHub value.
// See sentry.Hub docs for more detail.
Hub *sentry.Hub
}
12 changes: 10 additions & 2 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewCore(cfg Configuration, factory SentryClientFactory) (zapcore.Core, erro
}

if cfg.EnableBreadcrumbs && cfg.BreadcrumbLevel > cfg.Level {
return zapcore.NewNopCore(), errors.New("breadcrumb level must be lower than error level")
return zapcore.NewNopCore(), errors.New("breadcrumb level must be lower than or equal to error level")
}

core := core{
Expand Down Expand Up @@ -70,7 +70,7 @@ func (c *core) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.Check
}

func (c *core) Write(ent zapcore.Entry, fs []zapcore.Field) error {
clone := c.with(fs)
clone := c.with(c.addSpecialFields(ent, fs))

// only when we have local sentryScope to avoid collecting all breadcrumbs ever in a global scope
if c.cfg.EnableBreadcrumbs && c.cfg.BreadcrumbLevel.Enabled(ent.Level) && c.sentryScope != nil {
Expand Down Expand Up @@ -112,6 +112,14 @@ func (c *core) Write(ent zapcore.Entry, fs []zapcore.Field) error {
return nil
}

func (c *core) addSpecialFields(ent zapcore.Entry, fs []zapcore.Field) []zapcore.Field {
if c.cfg.LoggerNameKey != "" && ent.LoggerName != "" {
fs = append(fs, zap.String(c.cfg.LoggerNameKey, ent.LoggerName))
}

return fs
}

func (c *core) createExceptions() []sentry.Exception {
errorsCount := len(c.errs)

Expand Down
12 changes: 9 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
module github.com/TheZeroSlave/zapsentry

go 1.12
go 1.17

require (
github.com/getsentry/sentry-go v0.10.0
go.uber.org/zap v1.16.0
github.com/getsentry/sentry-go v0.12.0
go.uber.org/zap v1.21.0
)

require (
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac // indirect
)
Loading

0 comments on commit 0eca8bc

Please sign in to comment.