Skip to content

feat(tracing): Add __repr__ to Baggage #4043

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

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading