Skip to content

fix: Ensure tags values are strings #4459

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 13 commits into
base: master
Choose a base branch
from

Conversation

szokeasaurusrex
Copy link
Member

@szokeasaurusrex szokeasaurusrex commented Jun 12, 2025

Ensure tag values are strings before serializing an event or a transaction to an Event dictionary.

Fixes #4391

@szokeasaurusrex szokeasaurusrex self-assigned this Jun 12, 2025
Copy link

codecov bot commented Jun 12, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.60%. Comparing base (0e21fa2) to head (16045df).

✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #4459   +/-   ##
=======================================
  Coverage   80.59%   80.60%           
=======================================
  Files         142      142           
  Lines       16030    16030           
  Branches     2745     2748    +3     
=======================================
+ Hits        12920    12921    +1     
  Misses       2243     2243           
+ Partials      867      866    -1     
Files with missing lines Coverage Δ
sentry_sdk/scope.py 86.08% <100.00%> (ø)
sentry_sdk/tracing.py 78.57% <100.00%> (ø)

... and 1 file with indirect coverage changes

@szokeasaurusrex szokeasaurusrex marked this pull request as ready for review June 12, 2025 12:06
@szokeasaurusrex szokeasaurusrex requested a review from a team as a code owner June 12, 2025 12:06
@szokeasaurusrex
Copy link
Member Author

Looks like tests are failing because the non-string values were being asserted in the test suites

@szokeasaurusrex szokeasaurusrex marked this pull request as draft June 13, 2025 12:47
@szokeasaurusrex szokeasaurusrex marked this pull request as ready for review June 16, 2025 13:45
Copy link
Contributor

@sentrivana sentrivana left a comment

Choose a reason for hiding this comment

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

Some high-level thoughts:

  1. We seem to be stringifying the tags in multiple places. Do we need to do it in all of those? Is there a way to centralize it, e.g. move it to the end of the pipeline, e.g. in the serializer?
  2. Are we sure we're not changing anything user-facing, especially in callbacks? (E.g. if a user is accessing tags in a before_send or similar.)

Comment on lines +964 to +1015
def test_set_tag_handles_conversion_failure():
"""Test that set_tag handles objects that fail to convert to string."""
scope = Scope()

# Create an object that raises an exception when str() is called
class BadObject:
def __str__(self):
raise Exception("Cannot convert to string")

def __repr__(self):
return "BadObject()"

bad_obj = BadObject()

# This should not raise an exception
scope.set_tag("bad_object", bad_obj)

# The tag should be set with the repr value
event = scope.apply_to_event({}, {})
tags = event.get("tags", {})

assert tags["bad_object"] == "BadObject()", "Tag should be set with repr value"


def test_set_tags_handles_conversion_failure():
"""Test that set_tags handles objects that fail to convert to string."""
scope = Scope()

# Create an object that raises an exception when str() is called
class BadObject:
def __str__(self):
raise Exception("Cannot convert to string")

def __repr__(self):
return "BadObject()"

bad_obj = BadObject()

scope.set_tags(
{
"good_tag1": "value1",
"bad_tag": bad_obj,
"good_tag2": 123,
}
)

event = scope.apply_to_event({}, {})
tags = event.get("tags", {})

assert tags["good_tag1"] == "value1"
assert tags["bad_tag"] == "BadObject()", "Tag should be set with repr value"
assert tags["good_tag2"] == "123"
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe consolidate these two test cases into one?

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.

Ensure tag values are strings
3 participants