Skip to content

Commit

Permalink
docs: Create documentation for scopes usage inside Goroutines
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilogorek committed Jul 1, 2019
1 parent a909b6b commit ce7e047
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions __tests__/__snapshots__/documentation.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ Array [
"platforms/go/config/index.html",
"platforms/go/echo/index.html",
"platforms/go/gin/index.html",
"platforms/go/goroutines/index.html",
"platforms/go/http/index.html",
"platforms/go/index.html",
"platforms/go/integrations/index.html",
Expand Down
53 changes: 53 additions & 0 deletions src/collections/_documentation/platforms/go/goroutines.md
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. It is run concurrently with other functions in your code. And because of this, it has to keep track of it's own data locally. Otherwise, there is a chance that you will override your informations 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`, 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")
}()
```
1 change: 1 addition & 0 deletions src/collections/_documentation/platforms/go/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ For more detailed information about how to get the most out of `sentry-go` there
- [Error Reporting]({%- link _documentation/error-reporting/quickstart.md -%}?platform={{ include.platform }})
- [Enriching Error Data]({%- link _documentation/enriching-error-data/context.md -%}?platform={{ include.platform }})
- [Transports]({%- link _documentation/platforms/go/transports.md -%})
- [Goroutines]({%- link _documentation/platforms/go/goroutines.md -%})
- [Integrations]({%- link _documentation/platforms/go/integrations.md -%})
- [net/http]({%- link _documentation/platforms/go/http.md -%})
- [echo]({%- link _documentation/platforms/go/echo.md -%})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Integrations
sidebar_order: 4
sidebar_order: 5
---

The sentry-go package currently comes with an integration for the native `net/http` package, `echo`, `gin`, `iris`, `martini` and `negroni` to make it easy to handle common scenarios.
Expand Down

0 comments on commit ce7e047

Please sign in to comment.