@@ -117,3 +117,41 @@ def test_remove_attribute(sentry_init, capture_envelopes):
117117 (metric ,) = metrics
118118
119119 assert "some.attribute" not in metric ["attributes" ]
120+
121+
122+ def test_scope_attributes_preserialized (sentry_init , capture_envelopes ):
123+ def before_send_metric (metric , _ ):
124+ # Scope attrs show up serialized in before_send
125+ assert isinstance (metric ["attributes" ]["instance" ], str )
126+ assert isinstance (metric ["attributes" ]["dictionary" ], str )
127+
128+ return metric
129+
130+ sentry_init (before_send_metric = before_send_metric )
131+
132+ envelopes = capture_envelopes ()
133+
134+ class Cat :
135+ pass
136+
137+ instance = Cat ()
138+ dictionary = {"color" : "tortoiseshell" }
139+
140+ with sentry_sdk .new_scope () as scope :
141+ scope .set_attribute ("instance" , instance )
142+ scope .set_attribute ("dictionary" , dictionary )
143+
144+ # Scope attrs are stored preserialized
145+ assert isinstance (scope ._attributes ["instance" ], str )
146+ assert isinstance (scope ._attributes ["dictionary" ], str )
147+
148+ sentry_sdk .metrics .count ("test" , 1 )
149+
150+ sentry_sdk .get_client ().flush ()
151+
152+ metrics = envelopes_to_metrics (envelopes )
153+ (metric ,) = metrics
154+
155+ # Attrs originally from the scope are serialized when sent
156+ assert isinstance (metric ["attributes" ]["instance" ], str )
157+ assert isinstance (metric ["attributes" ]["dictionary" ], str )
0 commit comments