Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Sentry logger #184

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Split trace configs into own options
Cover Panic functions on the logger
  • Loading branch information
Glib Smaga committed Dec 8, 2016
commit 97cf03d85aff003f58c4d5b03a201eb4a607d955
30 changes: 22 additions & 8 deletions sentry/sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
const (
_platform = "go"
_traceContextLines = 3
_traceSkipFrames = 3
_traceSkipFrames = 2
)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a var _ zap.Logger = &Logger{} here; that way the compiler ensures that sentry.Logger implements zap.Logger.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not strictly necessary, as other places return sentry.Logger for zap.Logger return type, so it's impossible to compile without satisfying zap.Logger.

var _zapToRavenMap = map[zap.Level]raven.Severity{
Expand Down Expand Up @@ -91,30 +91,44 @@ func New(dsn string, options ...Option) (*Logger, error) {
return l, nil
}

// MinLevel provides a minimum level threshold, above which Sentry packets will be triggered
// MinLevel provides a minimum level threshold.
// All log messages above the set level are sent to Sentry.
func MinLevel(level zap.Level) Option {
return func(l *Logger) {
l.minLevel = level
}
}

// DisableTraces allows to turn off Stacktrace for sentry packets
// DisableTraces allows to turn off Stacktrace for sentry packets.
func DisableTraces() Option {
return func(l *Logger) {
l.traceEnabled = false
}
}

// TraceCfg allows to change the number of skipped frames, number of context lines and
// list of go prefixes that are considered "in-app", i.e. "github.com/uber-go/zap".
func TraceCfg(skip, context int, prefixes []string) Option {
// TraceContextLines sets how many lines of code (in on direction) are sent
// with the Sentry packet.
func TraceContextLines(lines int) Option {
return func(l *Logger) {
l.traceContextLines = lines
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the default is to enable tracing, consider making this DisableTraces().


// TraceAppPrefixes sets a list of go import prefixes that are considered "in app".
func TraceAppPrefixes(prefixes []string) Option {
return func(l *Logger) {
l.traceSkipFrames = skip
l.traceContextLines = context
l.traceAppPrefixes = prefixes
}
}

// TraceSkipFrames sets how many stacktrace frames to skip when sending a
// sentry packet. This is very useful when helper functions are involved.
func TraceSkipFrames(skip int) Option {
return func(l *Logger) {
l.traceSkipFrames = skip
}
}

// Extra stores additional information for each Sentry event.
func Extra(extra map[string]interface{}) Option {
return func(l *Logger) {
Expand Down
20 changes: 19 additions & 1 deletion sentry/sentry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,34 @@ func TestWithTraceDisabled(t *testing.T) {
}

func TestTraceCfg(t *testing.T) {
l, err := New("", TraceCfg(1, 7, []string{"github.com/uber-go/unicorns"}))
l, err := New(
"",
TraceSkipFrames(1),
TraceContextLines(7),
TraceAppPrefixes([]string{"github.com/uber-go/unicorns"}),
)
assert.NoError(t, err)
assert.Equal(t, l.traceSkipFrames, 1)
assert.Equal(t, l.traceContextLines, 7)
assert.Equal(t, l.traceAppPrefixes, []string{"github.com/uber-go/unicorns"})
}

func TestPanics(t *testing.T) {
l, _ := New("")
l.Development = true

assert.Panics(t, func() {
l.Panic("ERMYGAWD")
})
assert.Panics(t, func() {
l.DPanic("ERMYGAWD in Development")
})
}

func TestLevels(t *testing.T) {
_, ps := capturePackets(func(l *Logger) {
l.Log(zap.DebugLevel, "direct call at Debug level")
l.Debug("debug")
l.Info("info")
l.Warn("warn")
l.Error("error")
Expand Down