diff --git a/sentry_sdk/serializer.py b/sentry_sdk/serializer.py index 4dc4bb5177..df6a9053c1 100644 --- a/sentry_sdk/serializer.py +++ b/sentry_sdk/serializer.py @@ -281,6 +281,9 @@ def _serialize_node_impl( else: return obj + elif callable(getattr(obj, "sentry_repr", None)): + return obj.sentry_repr() + elif isinstance(obj, datetime): return ( text_type(format_timestamp(obj)) diff --git a/tests/test_serializer.py b/tests/test_serializer.py index 35cbdfb96b..503bc14fb2 100644 --- a/tests/test_serializer.py +++ b/tests/test_serializer.py @@ -64,3 +64,12 @@ def test_bytes_serialization_repr(message_normalizer): def test_serialize_sets(extra_normalizer): result = extra_normalizer({1, 2, 3}) assert result == [1, 2, 3] + + +def test_serialize_custom_mapping(extra_normalizer): + class CustomReprDict(dict): + def sentry_repr(self): + return "custom!" + + result = extra_normalizer(CustomReprDict(one=1, two=2)) + assert result == "custom!"