sentry-go
sentry-go copied to clipboard
Optionally log captured events
Summary
When events are captured by sentry, I would like a way to also log these events.
Motivation
It is very useful when working locally, because it's inconvenient to see the events only through the sentry dashboard.
Additionally, if it's possible to configure it so it does not send the event to sentry, that'll also be great for local development.
Current workaround
I've implemented a custom integration for this and it works pretty well. It may be useful for others too if it is added to the default integrations.
type LoggingIntegration struct {
SupressErrors bool
Logger interface {
Printf(format string, a ...interface{}) (n int, err error)
}
}
func (sli LoggingIntegration) Name() string {
return "Logging"
}
func (sli LoggingIntegration) SetupOnce(client *sentry.Client) {
client.AddEventProcessor(sli.processor)
}
func (sli LoggingIntegration) processor(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
for _, exception := range event.Exception {
sli.Logger.Printf("\n%s\n\n", exception.Value)
if exception.Stacktrace != nil {
for i := len(exception.Stacktrace.Frames) - 1; i >= 0; i-- {
frame := exception.Stacktrace.Frames[i]
// Print general info about the exception
sli.Logger.Printf("%s:%d:%d %s\n",
frame.AbsPath, frame.Lineno, frame.Colno, frame.Function)
if frame.ContextLine != "" {
// Print the lines before the exception line
for j := 0; j < len(frame.PreContext); j++ {
line := frame.PreContext[j]
sli.Logger.Printf("%04d | "+line+"\n", frame.Lineno-len(frame.PreContext)+j)
}
// Print the exception line
sli.Logger.Printf("%04d > "+frame.ContextLine+"\n", frame.Lineno)
// Print the lines after the exception
for j := 0; j < len(frame.PostContext); j++ {
line := frame.PostContext[j]
sli.Logger.Printf("%04d | "+line+"\n", frame.Lineno+j)
}
}
sli.Logger.Printf("\n")
}
}
}
if sli.SupressErrors {
return nil
}
return event
}
I can then add it like this:
func getSentry(config internal.Config) (*sentry.Hub, error) {
client, err := sentry.NewClient(sentry.ClientOptions{
Dsn: config.SENTRY_DSN,
Debug: true,
Integrations: func(in []sentry.Integration) []sentry.Integration {
return append(in, LoggingIntegration{Logger: sentryLogger{}, SupressErrors: config.TESTING})
},
})
if err != nil {
return nil, fmt.Errorf("could not create sentry client: %w", err)
}
return sentry.NewHub(client, sentry.NewScope()), nil
}
type sentryLogger struct{}
func (sentryLogger) Printf(format string, a ...interface{}) (n int, err error) {
return fmt.Printf(format, a...)
}
Hi @stephenafamo! We saw your tweet, at first thought you needed a hand getting it to work.
Glad that it works well for you!
As for adding this integration to the SDK itself, I think it might be too specific for a standard integration. We provide the building blocks so people can build with them.
For debugging what the SDK is doing, the Debug
flag (and DebugWriter
) in ClientOptions
controls debug logging, and BeforeSend
is a generic hook (similar to Integrations/EventProcessors) which allows inspecting events before they leave the SDK. Some people will want to print stack traces, some people prefer a particular structured logging library, some people may want to inspect other parts of the event like tags, etc.
Yet another advanced way to hook into SDK activity is to provide a custom HTTP transport as discussed in https://github.com/getsentry/sentry-go/issues/270#issuecomment-673532243, see example https://godoc.org/github.com/getsentry/sentry-go#example-package--TransportWithHooks.
Would an example of custom integration or event processor or BeforeSend have been more helpful to you than looking at the source of the default integrations? We do have several examples in https://github.com/getsentry/sentry-go/tree/master/example, and now the custom transport is different as it shows up in godoc
.
Thanks!
This issue has gone three weeks without activity. In another week, I will close it.
But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog
or Status: In Progress
, I will leave it alone ... forever!
"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀