From 417fe8c8690050c832d50ef6ea4f98605711add8 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Fri, 31 Jan 2025 13:54:11 +0100 Subject: [PATCH] feat(python): Add snippet for adding data to transaction and all its spans --- .../custom-instrumentation/index.mdx | 27 +++++++++++++++---- .../before-send-transaction/python.mdx | 1 - 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/docs/platforms/python/tracing/instrumentation/custom-instrumentation/index.mdx b/docs/platforms/python/tracing/instrumentation/custom-instrumentation/index.mdx index 78b30c376ef70..82fccf276fdc6 100644 --- a/docs/platforms/python/tracing/instrumentation/custom-instrumentation/index.mdx +++ b/docs/platforms/python/tracing/instrumentation/custom-instrumentation/index.mdx @@ -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 `before_send_transaction`: + +```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, +) +``` diff --git a/platform-includes/configuration/before-send-transaction/python.mdx b/platform-includes/configuration/before-send-transaction/python.mdx index 273e245fba367..84a34e2829970 100644 --- a/platform-includes/configuration/before-send-transaction/python.mdx +++ b/platform-includes/configuration/before-send-transaction/python.mdx @@ -9,7 +9,6 @@ def strip_sensitive_data(event, hint): sentry_sdk.init( # ... - before_send_transaction=strip_sensitive_data, ) ```