From fee5c1a3351f7e5291998757906837a6569437b7 Mon Sep 17 00:00:00 2001 From: Michi Hoffmann Date: Wed, 20 Sep 2023 13:24:21 +0200 Subject: [PATCH] Improve Go manual instrumentation examples (#7825) Co-authored-by: Anton Ovchinnikov --- .../performance/add-spans-example/go.mdx | 15 ++++---- .../enable-manual-instrumentation/go.mdx | 34 ++++++++++++++----- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/platform-includes/performance/add-spans-example/go.mdx b/src/platform-includes/performance/add-spans-example/go.mdx index 7590a1638cb55..fddf909140ae0 100644 --- a/src/platform-includes/performance/add-spans-example/go.mdx +++ b/src/platform-includes/performance/add-spans-example/go.mdx @@ -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() } diff --git a/src/platform-includes/performance/enable-manual-instrumentation/go.mdx b/src/platform-includes/performance/enable-manual-instrumentation/go.mdx index 8e877730c43bc..33df391622eb7 100644 --- a/src/platform-includes/performance/enable-manual-instrumentation/go.mdx +++ b/src/platform-includes/performance/enable-manual-instrumentation/go.mdx @@ -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()); +}) ```