Skip to content

Commit

Permalink
feat(tracing): Add __repr__ to Baggage (#4043)
Browse files Browse the repository at this point in the history
The default `__repr__` does not show what is in the `Baggage`, making it
extremely difficult to debug code involving `Baggage` objects. Add a
`__repr__` which includes the serialized `Baggage` to improve
debuggability.
  • Loading branch information
szokeasaurusrex authored Feb 12, 2025
1 parent c227e11 commit 2f51db7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions sentry_sdk/tracing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,10 @@ def strip_sentry_baggage(header):
)
)

def __repr__(self):
# type: () -> str
return f'<Baggage "{self.serialize(include_third_party=True)}", mutable={self.mutable}>'


def should_propagate_trace(client, url):
# type: (sentry_sdk.client.BaseClient, str) -> bool
Expand Down
31 changes: 31 additions & 0 deletions tests/test_tracing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,34 @@ def test_should_be_included(test_case, expected):
)
def test_strip_sentry_baggage(header, expected):
assert Baggage.strip_sentry_baggage(header) == expected


@pytest.mark.parametrize(
("baggage", "expected_repr"),
(
(Baggage(sentry_items={}), '<Baggage "", mutable=True>'),
(Baggage(sentry_items={}, mutable=False), '<Baggage "", mutable=False>'),
(
Baggage(sentry_items={"foo": "bar"}),
'<Baggage "sentry-foo=bar,", mutable=True>',
),
(
Baggage(sentry_items={"foo": "bar"}, mutable=False),
'<Baggage "sentry-foo=bar,", mutable=False>',
),
(
Baggage(sentry_items={"foo": "bar"}, third_party_items="asdf=1234,"),
'<Baggage "sentry-foo=bar,asdf=1234,", mutable=True>',
),
(
Baggage(
sentry_items={"foo": "bar"},
third_party_items="asdf=1234,",
mutable=False,
),
'<Baggage "sentry-foo=bar,asdf=1234,", mutable=False>',
),
),
)
def test_baggage_repr(baggage, expected_repr):
assert repr(baggage) == expected_repr

0 comments on commit 2f51db7

Please sign in to comment.