Skip to content

Commit fee5c1a

Browse files
cleptrictonyo
andauthored
Improve Go manual instrumentation examples (#7825)
Co-authored-by: Anton Ovchinnikov <anton@tonyo.info>
1 parent 06da41f commit fee5c1a

File tree

2 files changed

+34
-15
lines changed
  • src/platform-includes/performance

2 files changed

+34
-15
lines changed

src/platform-includes/performance/add-spans-example/go.mdx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
## Add More Spans to the Transaction
22

3-
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.
4-
5-
You can choose the value of `operation` and `description`.
3+
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.
64

75
```go
8-
func doWork(ctx context.Context, item Item) {
9-
span := sentry.StartSpan(ctx, "suboperation1")
6+
func doWork(ctx context.Context) {
7+
// Set the OP based on values from https://develop.sentry.dev/sdk/performance/span-operations/
8+
span := sentry.StartSpan(ctx, "function")
9+
span.Description = "suboperation1"
1010
// omitted code ...
1111
span.Finish()
12-
span := sentry.StartSpan(ctx, "suboperation2")
12+
13+
// Set the OP based on values from https://develop.sentry.dev/sdk/performance/span-operations/
14+
span := sentry.StartSpan(ctx, "function")
15+
span.Description = "suboperation2"
1316
// omitted code ...
1417
span.Finish()
1518
}
Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
To instrument certain regions of your code, you can create transactions to capture them.
22

3-
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:
3+
The following example creates a transaction based on an incoming request:
44

55
```go
6-
ctx := context.Background()
7-
8-
for item := range ch {
9-
span := sentry.StartSpan(ctx, "doWork",
10-
sentry.WithTransactionName(fmt.Sprintf("doWork: %s", item.Type)))
11-
doWork(span.Context(), item) // doWork may create additional spans
12-
span.Finish()
13-
}
6+
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
7+
ctx := r.Context()
8+
hub := sentry.GetHubFromContext(ctx)
9+
if hub == nil {
10+
// Check the concurrency guide for more details: https://docs.sentry.io/platforms/go/concurrency/
11+
hub = sentry.CurrentHub().Clone()
12+
ctx = sentry.SetHubOnContext(ctx, hub)
13+
}
14+
15+
options := []sentry.SpanOption{
16+
// Set the OP based on values from https://develop.sentry.dev/sdk/performance/span-operations/
17+
sentry.WithOpName("http.server"),
18+
sentry.ContinueFromRequest(r),
19+
sentry.WithTransactionSource(sentry.SourceURL),
20+
}
21+
22+
transaction := sentry.StartTransaction(ctx,
23+
fmt.Sprintf("%s %s", r.Method, r.URL.Path),
24+
options...,
25+
)
26+
defer transaction.Finish()
27+
28+
doWork(transaction.Context());
29+
})
1430
```

0 commit comments

Comments
 (0)