Skip to content

Commit

Permalink
feat(python): Add snippet for adding data to transaction and all its …
Browse files Browse the repository at this point in the history
…spans (#12534)
  • Loading branch information
sentrivana authored Feb 3, 2025
1 parent acb1f35 commit 343cc3c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,6 @@ def eat_slice(slice):

## Improving Data on Transactions and Spans

### Adding Data Attributes to Transactions

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:

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

### Adding Data Attributes to Spans

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:
You can add data attributes to your spans the same way, with the same type restrictions as described above.

```python
with sentry_sdk.start_span(name="my-span") as span:
Expand All @@ -267,3 +263,24 @@ with sentry_sdk.start_span(name="my-span") as span:
span.set_data("my-data-attribute-5", [42, 43, 44])
span.set_data("my-data-attribute-6", [True, False, True])
```

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>:

```python
def my_before_send_transaction(event, hint):
# Set the data attribute "foo" to "bar" on every span belonging to this
# transaction event
for span in event["spans"]:
span["data"]["foo"] = "bar"

# Set the data on the transaction itself, too
event["contexts"]["trace"]["data"]["foo"] = "bar"

return event


sentry_sdk.init(
traces_sample_rate=1.0,
before_send_transaction=my_before_send_transaction,
)
```
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ def strip_sensitive_data(event, hint):

sentry_sdk.init(
# ...

before_send_transaction=strip_sensitive_data,
)
```
Expand Down

0 comments on commit 343cc3c

Please sign in to comment.