Skip to content

Add update_data to Span. #4666

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

antonpirker
Copy link
Member

@antonpirker antonpirker commented Aug 4, 2025

This allows users to set multiple span.data attributes at once.
In 3.x this then will become set_attributes (plural).

@antonpirker antonpirker requested a review from a team as a code owner August 4, 2025 14:51
cursor[bot]

This comment was marked as outdated.

Copy link
Member

@szokeasaurusrex szokeasaurusrex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This API is a bit confusing, imo, as it is unclear what should happen if a user calls set_data with a dict as the first parameter, and something else as the second parameter.

I would prefer we instead introduce a separate function to enable setting data as a dict, rather than supporting two distinct signatures for set_data.

Although if others are okay with having set_data support both ways, we can go with it – just sharing my two cents here. In that case, though, I would define both APIs with @overload.

Copy link

codecov bot commented Aug 4, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.05%. Comparing base (84adbb7) to head (80fe601).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4666      +/-   ##
==========================================
- Coverage   85.09%   85.05%   -0.04%     
==========================================
  Files         156      156              
  Lines       15797    15799       +2     
  Branches     2671     2671              
==========================================
- Hits        13442    13438       -4     
- Misses       1578     1585       +7     
+ Partials      777      776       -1     
Files with missing lines Coverage Δ
sentry_sdk/tracing.py 86.52% <100.00%> (+0.05%) ⬆️

... and 4 files with indirect coverage changes

@antonpirker antonpirker changed the title Let set_data accept a dict. Add update_data to Span. Aug 4, 2025
@antonpirker
Copy link
Member Author

True @szokeasaurusrex i have now changed it to have a update_data() function instead. Similar to pythons own dict.update().

@@ -602,6 +602,10 @@ def set_data(self, key, value):
# type: (str, Any) -> None
self._data[key] = value

def update_data(self, data):
# type: (Dict[str, Any]) -> None
self._data.update(data)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: NoOpSpan Method Inheritance Issue

The update_data method, newly added to the Span class, is not overridden in NoOpSpan. This allows NoOpSpan.update_data() to inherit the real implementation and modify internal data, breaking its no-operation contract. This is inconsistent with NoOpSpan.set_data() and similar methods, which are correctly overridden as no-ops.

Fix in Cursor Fix in Web

@@ -509,3 +509,34 @@ def test_transaction_not_started_warning(sentry_init):
"The transaction will not be sent to Sentry. To fix, start the transaction by"
"passing it to sentry_sdk.start_transaction."
)


def test_span_set_data(sentry_init, capture_events):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also add update_data to the name of the test. And, perhaps add a test which only checks update_data in isolation

Copy link
Member

@szokeasaurusrex szokeasaurusrex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the Cursor bug bot might have found something. Besides that, looks good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants