@@ -15,3 +15,70 @@ 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+ To attach data attributes to the transaction and all its spans, you can use ` BeforeSendTransaction ` :
72+
73+ ``` go
74+ sentry.Init (sentry.ClientOptions {
75+ Dsn : " ___PUBLIC_DSN___" ,
76+ BeforeSendTransaction : func (event *sentry.Event , hint *sentry.EventHint ) *sentry.Event {
77+ for _ , sp := range event.Spans {
78+ sp.SetData (" dataAttr1" , 42 )
79+ sp.SetData (" dataAttr2" , true )
80+ }
81+ return event
82+ },
83+ })
84+ ```
0 commit comments