-
-
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.
Improve Go manual instrumentation examples (#7825)
Co-authored-by: Anton Ovchinnikov <anton@tonyo.info>
- Loading branch information
Showing
2 changed files
with
34 additions
and
15 deletions.
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
34 changes: 25 additions & 9 deletions
34
src/platform-includes/performance/enable-manual-instrumentation/go.mdx
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 |
---|---|---|
@@ -1,14 +1,30 @@ | ||
To instrument certain regions of your code, you can create transactions to capture them. | ||
|
||
The following example creates a transaction span to time runs of an expensive operation on items from a channel. Timing for each operation is sent to Sentry and grouped by transaction name: | ||
The following example creates a transaction based on an incoming request: | ||
|
||
```go | ||
ctx := context.Background() | ||
|
||
for item := range ch { | ||
span := sentry.StartSpan(ctx, "doWork", | ||
sentry.WithTransactionName(fmt.Sprintf("doWork: %s", item.Type))) | ||
doWork(span.Context(), item) // doWork may create additional spans | ||
span.Finish() | ||
} | ||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
hub := sentry.GetHubFromContext(ctx) | ||
if hub == nil { | ||
// Check the concurrency guide for more details: https://docs.sentry.io/platforms/go/concurrency/ | ||
hub = sentry.CurrentHub().Clone() | ||
ctx = sentry.SetHubOnContext(ctx, hub) | ||
} | ||
|
||
options := []sentry.SpanOption{ | ||
// Set the OP based on values from https://develop.sentry.dev/sdk/performance/span-operations/ | ||
sentry.WithOpName("http.server"), | ||
sentry.ContinueFromRequest(r), | ||
sentry.WithTransactionSource(sentry.SourceURL), | ||
} | ||
|
||
transaction := sentry.StartTransaction(ctx, | ||
fmt.Sprintf("%s %s", r.Method, r.URL.Path), | ||
options..., | ||
) | ||
defer transaction.Finish() | ||
|
||
doWork(transaction.Context()); | ||
}) | ||
``` |
fee5c1a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
sentry-docs – ./
sentry-docs-git-master.sentry.dev
sentry-docs.sentry.dev
docs.sentry.io