Skip to content

Commit 2ec682b

Browse files
giortzisgbitsandfoxes
authored andcommitted
feat(go): Update transaction & span instrumentation docs (#13262)
1 parent 8a7c56d commit 2ec682b

File tree

1 file changed

+91
-0
lines changed
  • docs/platforms/go/common/tracing/instrumentation/custom-instrumentation

1 file changed

+91
-0
lines changed

docs/platforms/go/common/tracing/instrumentation/custom-instrumentation/index.mdx

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,94 @@ To capture transactions and spans customized to your organization's needs, you m
1515
<PlatformContent includePath="performance/add-spans-example" />
1616

1717
<PlatformContent includePath="performance/retrieve-transaction" />
18+
19+
## Adding Span & Transaction Data Attributes
20+
21+
You can capture data attributes along with your spans and transactions. You can specify data attributes when starting a span or transaction:
22+
23+
```go
24+
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
25+
ctx := r.Context()
26+
27+
options := []sentry.SpanOption{
28+
// Set the OP based on values from https://develop.sentry.dev/sdk/performance/span-operations/
29+
sentry.WithOpName("http.server"),
30+
sentry.ContinueFromRequest(r),
31+
sentry.WithTransactionSource(sentry.SourceURL),
32+
}
33+
34+
// Create a transaction and assign data attributes
35+
transaction := sentry.StartTransaction(ctx,
36+
fmt.Sprintf("%s %s", r.Method, r.URL.Path),
37+
options...,
38+
)
39+
transaction.SetData("dataAttr1", 42)
40+
transaction.SetData("dataAttr2", true)
41+
// omitted code ...
42+
transaction.Finish()
43+
44+
// ... or create a span and assign data attributes
45+
span := sentry.StartSpan(ctx, "span1")
46+
span.SetData("dataAttr1", 42)
47+
span.SetData("dataAttr2", true)
48+
// omitted code ...
49+
span.Finish()
50+
})
51+
```
52+
53+
Or you can add data attributes to an existing transaction or span:
54+
55+
```go
56+
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
57+
ctx := r.Context()
58+
transaction := sentry.TransactionFromContext(ctx)
59+
60+
if transaction != nil {
61+
transaction.SetData("dataAttr1", 42)
62+
transaction.SetData("dataAttr2", true)
63+
}
64+
65+
span := sentry.SpanFromContext(ctx)
66+
span.SetData("dataAttr1", 42)
67+
span.SetData("dataAttr2", true)
68+
})
69+
```
70+
71+
Or you can update existing transaction and span data by:
72+
73+
```go
74+
if d, found := transaction.Data["dataAttr1"]; found {
75+
if dataAttr1, ok := d.(int); ok {
76+
transaction.SetData("dataAttr1", dataAttr1.(int)+42)
77+
}
78+
}
79+
80+
if d, found := span.Data["dataAttr1"]; found {
81+
if dataAttr1, ok := d.(int); ok {
82+
span.SetData("dataAttr1", dataAttr1.(int)+42)
83+
}
84+
}
85+
})
86+
```
87+
88+
To attach data attributes to the transaction and all its spans, you can use `BeforeSendTransaction`:
89+
90+
```go
91+
sentry.Init(sentry.ClientOptions{
92+
Dsn: "___PUBLIC_DSN___",
93+
BeforeSendTransaction: func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
94+
for _, sp := range event.Spans {
95+
sp.SetData("dataAttr1", 42)
96+
sp.SetData("dataAttr2", true)
97+
}
98+
99+
dataCtx, ok := event.Contexts["trace"]["data"].(map[string]any)
100+
if !ok {
101+
dataCtx = make(map[string]any)
102+
event.Contexts["trace"]["data"] = dataCtx
103+
}
104+
dataCtx["num"] = 42
105+
return event
106+
},
107+
})
108+
```

0 commit comments

Comments
 (0)