Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(python): Add snippet for adding data to transaction and all its spans #12534

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
@@ -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:
@@ -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
@@ -9,7 +9,6 @@ def strip_sensitive_data(event, hint):

sentry_sdk.init(
# ...

before_send_transaction=strip_sensitive_data,
)
```