|
1 | 1 | To instrument certain regions of your code, you can create transactions to capture them.
|
2 | 2 |
|
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: |
4 | 4 |
|
5 | 5 | ```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 | +}) |
14 | 30 | ```
|
0 commit comments