Skip to content

Commit 343cc3c

Browse files
authored
feat(python): Add snippet for adding data to transaction and all its spans (#12534)
1 parent acb1f35 commit 343cc3c

File tree

2 files changed

+22
-6
lines changed
  • docs/platforms/python/tracing/instrumentation/custom-instrumentation
  • platform-includes/configuration/before-send-transaction

2 files changed

+22
-6
lines changed

Diff for: docs/platforms/python/tracing/instrumentation/custom-instrumentation/index.mdx

+22-5
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,6 @@ def eat_slice(slice):
238238

239239
## Improving Data on Transactions and Spans
240240

241-
### Adding Data Attributes to Transactions
242-
243241
You can add data attributes to your transactions. This data is visible in the trace explorer in Sentry. Data attributes can be of type `string`, `number` or `boolean`, as well as (non-mixed) arrays of these types:
244242

245243
```python
@@ -253,9 +251,7 @@ with sentry_sdk.start_transaction(name="my-transaction") as transaction:
253251
transaction.set_data("my-data-attribute-6", [True, False, True])
254252
```
255253

256-
### Adding Data Attributes to Spans
257-
258-
You can add data attributes to your spans. This data is visible in the trace explorer in Sentry. Data attributes can be of type `string`, `number` or `boolean`, as well as (non-mixed) arrays of these types:
254+
You can add data attributes to your spans the same way, with the same type restrictions as described above.
259255

260256
```python
261257
with sentry_sdk.start_span(name="my-span") as span:
@@ -267,3 +263,24 @@ with sentry_sdk.start_span(name="my-span") as span:
267263
span.set_data("my-data-attribute-5", [42, 43, 44])
268264
span.set_data("my-data-attribute-6", [True, False, True])
269265
```
266+
267+
To attach data attributes to the transaction and all its spans, you can use <PlatformLink to="/configuration/filtering/#using-before-send-transaction">`before_send_transaction`</PlatformLink>:
268+
269+
```python
270+
def my_before_send_transaction(event, hint):
271+
# Set the data attribute "foo" to "bar" on every span belonging to this
272+
# transaction event
273+
for span in event["spans"]:
274+
span["data"]["foo"] = "bar"
275+
276+
# Set the data on the transaction itself, too
277+
event["contexts"]["trace"]["data"]["foo"] = "bar"
278+
279+
return event
280+
281+
282+
sentry_sdk.init(
283+
traces_sample_rate=1.0,
284+
before_send_transaction=my_before_send_transaction,
285+
)
286+
```

Diff for: platform-includes/configuration/before-send-transaction/python.mdx

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ def strip_sensitive_data(event, hint):
99

1010
sentry_sdk.init(
1111
# ...
12-
1312
before_send_transaction=strip_sensitive_data,
1413
)
1514
```

0 commit comments

Comments
 (0)