Skip to content

Commit 9115b96

Browse files
feat(tracing): Add __repr__ to Baggage
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.
1 parent c227e11 commit 9115b96

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

Diff for: sentry_sdk/tracing_utils.py

+4
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,10 @@ def strip_sentry_baggage(header):
638638
)
639639
)
640640

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

642646
def should_propagate_trace(client, url):
643647
# type: (sentry_sdk.client.BaseClient, str) -> bool

Diff for: tests/test_tracing_utils.py

+31
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,34 @@ def test_should_be_included(test_case, expected):
115115
)
116116
def test_strip_sentry_baggage(header, expected):
117117
assert Baggage.strip_sentry_baggage(header) == expected
118+
119+
120+
@pytest.mark.parametrize(
121+
("baggage", "expected_repr"),
122+
(
123+
(Baggage(sentry_items={}), '<Baggage "", mutable=True>'),
124+
(Baggage(sentry_items={}, mutable=False), '<Baggage "", mutable=False>'),
125+
(
126+
Baggage(sentry_items={"foo": "bar"}),
127+
'<Baggage "sentry-foo=bar,", mutable=True>',
128+
),
129+
(
130+
Baggage(sentry_items={"foo": "bar"}, mutable=False),
131+
'<Baggage "sentry-foo=bar,", mutable=False>',
132+
),
133+
(
134+
Baggage(sentry_items={"foo": "bar"}, third_party_items="asdf=1234,"),
135+
'<Baggage "sentry-foo=bar,asdf=1234,", mutable=True>',
136+
),
137+
(
138+
Baggage(
139+
sentry_items={"foo": "bar"},
140+
third_party_items="asdf=1234,",
141+
mutable=False,
142+
),
143+
'<Baggage "sentry-foo=bar,asdf=1234,", mutable=False>',
144+
),
145+
),
146+
)
147+
def test_baggage_repr(baggage, expected_repr):
148+
assert repr(baggage) == expected_repr

0 commit comments

Comments
 (0)