-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Go async delivery and goroutines hub (#1085)
* docs: Create a note regarding default async events delivery in Go * docs: Create documentation for scopes usage inside Goroutines * Apply suggestions from code review Co-Authored-By: Anton Ovchinnikov <[email protected]>
- Loading branch information
1 parent
068ba2f
commit 0bf42ca
Showing
4 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
--- | ||
title: Goroutines | ||
sidebar_order: 4 | ||
--- | ||
|
||
A goroutine is a lightweight thread managed by the Go runtime. Goroutines can run concurrently, and because of this, every goroutine has to keep track of its own Sentry-related data locally. Otherwise, there is a chance that you will override your data stored in the Scope. More on this in [Scopes and Hubs]({%- link _documentation/enriching-error-data/scopes.md -%}?platform={{ include.platform }}) section. | ||
|
||
The easiest way to handle this, is to create a new `Hub` for every goroutine you start, however this would require you to rebind the current `Client` and handle `Scope` yourself. That is why we provide a helper method called `Clone`. It takes care of creating a `Hub`, cloning existing `Scope` and reassigning it alongside `Client` to newly create instance. | ||
|
||
Once cloned, `Hub` is completly isolated and can be used safely inside concurrent call. However, instead of using globally exposed methods, they should be called directly on the `Hub`. | ||
|
||
Here are two examples: one that is non-deterministic, would leak information between threads, and could trigger a concurrent-write panic; and one that is totally safe, and should be used instead. | ||
|
||
```go | ||
// Example of __INCORRECT__ use of scopes inside a Goroutines - DON'T USE IT! | ||
|
||
go func() { | ||
sentry.ConfigureScope(func(scope *sentry.Scope) { | ||
scope.SetTag("secretTag", "go#1") | ||
}) | ||
sentry.CaptureMessage("Hello from Goroutine! #1") | ||
}() | ||
|
||
go func() { | ||
sentry.ConfigureScope(func(scope *sentry.Scope) { | ||
scope.SetTag("secretTag", "go#2") | ||
}) | ||
sentry.CaptureMessage("Hello from Goroutine! #2") | ||
}() | ||
|
||
// at this point both events can have either `go#1` tag or `go#2` tag or it can panic with concurrent writes. We'll never know. | ||
``` | ||
|
||
```go | ||
// Example of __CORRECT__ use of scopes inside a Goroutines | ||
|
||
go func(localHub *sentry.Hub) { | ||
// as goroutine argument | ||
localHub.ConfigureScope(func(scope *sentry.Scope) { | ||
scope.SetTag("secretTag", "go#1") | ||
}) | ||
localHub.CaptureMessage("Hello from Goroutine! #1") | ||
}(sentry.CurrentHub().Clone()) | ||
|
||
go func() { | ||
// or created locally | ||
localHub := sentry.CurrentHub().Clone() | ||
localHub.ConfigureScope(func(scope *sentry.Scope) { | ||
scope.SetTag("secretTag", "go#2") | ||
}) | ||
localHub.CaptureMessage("Hello from Goroutine! #2") | ||
}() | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters