Skip to content

Commit

Permalink
Improve Go manual instrumentation examples (#7825)
Browse files Browse the repository at this point in the history
Co-authored-by: Anton Ovchinnikov <anton@tonyo.info>
  • Loading branch information
cleptric and tonyo authored Sep 20, 2023
1 parent 06da41f commit fee5c1a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
15 changes: 9 additions & 6 deletions src/platform-includes/performance/add-spans-example/go.mdx
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
## Add More Spans to the Transaction

The next example contains the implementation of the hypothetical `doWork` function called from the code snippet in the previous section. Our SDK can determine if there is currently an open transaction in the current context and add all newly created spans as child operations to that transaction. Keep in mind that each individual span also needs to be manually finished; otherwise, spans will not show up in the transaction.

You can choose the value of `operation` and `description`.
The next example contains the implementation of the hypothetical `doWork` function called from the code snippet in the previous section. Our SDK can determine if there is currently an open transaction in the current context and add all newly created spans as child operations to that transaction. Keep in mind that each individual span also needs to be manually finished. Otherwise, spans will not show up on the transaction.

```go
func doWork(ctx context.Context, item Item) {
span := sentry.StartSpan(ctx, "suboperation1")
func doWork(ctx context.Context) {
// Set the OP based on values from https://develop.sentry.dev/sdk/performance/span-operations/
span := sentry.StartSpan(ctx, "function")
span.Description = "suboperation1"
// omitted code ...
span.Finish()
span := sentry.StartSpan(ctx, "suboperation2")

// Set the OP based on values from https://develop.sentry.dev/sdk/performance/span-operations/
span := sentry.StartSpan(ctx, "function")
span.Description = "suboperation2"
// omitted code ...
span.Finish()
}
Expand Down
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());
})
```

1 comment on commit fee5c1a

@vercel
Copy link

@vercel vercel bot commented on fee5c1a Sep 20, 2023

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

Please sign in to comment.