getsentry / raven-go

Sentry client in Go
https://sentry.io
BSD 3-Clause "New" or "Revised" License
561 stars 148 forks source link

Thread-safe ways to attach context and tags #192

Closed zerkms closed 6 years ago

zerkms commented 6 years ago

At this moment there is no way to safely capture the user context

client.SetUserContext(&raven.User{ID: "123", Email: "matt@example.com"})
client.CaptureError(err, nil)
client.ClearContext()

The suggested example from https://blog.sentry.io/2015/07/07/logging-go-errors would cause a condition race if run in a concurrent environment.

There should be a way to do that safely without creating a new client for every request (unless it's by design)

zerkms commented 6 years ago

I see now - it's possible to pass User as an interface.

zerkms commented 5 years ago

Sorry for not mentioning how I solved it.

My final solution, with irrelevant pieces removed looks like this

interfaces := make([]sentry.Interface, 0, 3)

u := &sentry.User{
    ID:       user.ID,
    Username: user.Username,
    Email:    user.Email,
    IP:       user.IP,
}
interfaces = append(interfaces, u)

packet := sentry.NewPacket(err.Error(), interfaces...)
jacquelineCelia commented 5 years ago

Thank you @zerkms. I had the same question, and this helps me resolve it!