sentry-go icon indicating copy to clipboard operation
sentry-go copied to clipboard

Please clarify docs on Hub usage

Open mbyio opened this issue 6 years ago • 3 comments

Hey, we are trying to use the new API, and we are a little confused about the intended usage of a Client, Hub, and Scope. According to https://docs.sentry.io/platforms/go/goroutines/, we should be cloning a hub for each goroutine. That makes sense - it maps to making a clone for each concurrent transaction. However, there are a few things that aren't clear:

  • Within a transaction, we might use multiple goroutines; they share the same high level information (eg. they are working on the same operation, same user, etc.) but they might report errors independently; can we add information to the Hub concurrently?

  • What is the difference between a Scope and a Hub? We are avoiding global variables so our code is testable, so we are making a new Hub for our client using NewHub. But that function is also asking for a Scope. Do we need to make a new Scope? What information should we add to the Scope vs the Hub?

  • In our integration tests, how can we verify that an error was reported correctly in tests?

  • We have some middleware that can see the HTTP status code of a response, and reports to Sentry whenever it sees a server error. Some handlers also have error stack trace info available, which we'd like to add-on - but we don't want to report an error twice. Any suggestions on how to do that?

Thanks!

mbyio avatar Jul 22 '19 23:07 mbyio

Hey, thanks for very detailed questions @mbyio! I'll update docs Today with all the answers :)

kamilogorek avatar Jul 23 '19 07:07 kamilogorek

Within a transaction, we might use multiple goroutines; they share the same high level information (eg. they are working on the same operation, same user, etc.) but they might report errors independently; can we add information to the Hub concurrently?

Starting v0.2.0 yes - https://github.com/getsentry/sentry-go/releases/tag/v0.2.0 Just be aware, that if you, for example, change the same tag from two various places, they might override, as it's not deterministic.

What is the difference between a Scope and a Hub? We are avoiding global variables so our code is testable, so we are making a new Hub for our client using NewHub. But that function is also asking for a Scope. Do we need to make a new Scope? What information should we add to the Scope vs the Hub?

https://docs.sentry.io/enriching-error-data/scopes/?platform=go

Hub manages stack of scopes (as they are inheriting from each other) and has an access to the SDK client that catches and sends the errors. Scope is a very lightweight container for the user-specific data, it has no functionality other than this one. It's also capable of attaching it's own data to the event internally. And yes, you have to create the scope, otherwise you won't be able to store any information.

Everything should be done through Hub, as it knows which scope it should use to store the data in.

Here's example of manual usage: https://github.com/getsentry/sentry-go/blob/master/example/multiclient/main.go

In our integration tests, how can we verify that an error was reported correctly in tests?

It depends on the setup and what you want to achieve. Do you want to just verify that the event went out and assert it's shape? I assume you don't want to send real events to the Sentry itself right? If so, then I'd recommend either writing your own transport that'd put that event onto channel instead of sending it using http, or use BeforeSend directly.

https://docs.sentry.io/platforms/go/transports/ https://github.com/getsentry/sentry-go/blob/24bba70a9ee3a6f3c46d6dde1bbdf0d7517b6251/example/basic/main.go#L18-L33

We have some middleware that can see the HTTP status code of a response, and reports to Sentry whenever it sees a server error. Some handlers also have error stack trace info available, which we'd like to add-on - but we don't want to report an error twice. Any suggestions on how to do that?

Can you show me some example code of that so I can better understand the need?

kamilogorek avatar Jul 26 '19 10:07 kamilogorek

Hi @mbyio, very good questions about using the Sentry Go SDK. I see that @kamilogorek already gave you very detailed answers. Do you still need any help? Cheers!

rhcarvalho avatar Jan 30 '20 03:01 rhcarvalho