Skip to content

Commit f789126

Browse files
committed
tests: Test preserialization of attributes
1 parent 209eb65 commit f789126

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

tests/test_logs.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,3 +548,32 @@ def record_lost_event(reason, data_category=None, item=None, *, quantity=1):
548548
}
549549
]
550550
}
551+
552+
553+
def test_preserialization(sentry_init, capture_envelopes):
554+
"""We don't store references to objects in attributes."""
555+
sentry_init(enable_logs=True)
556+
557+
envelopes = capture_envelopes()
558+
559+
class Cat:
560+
pass
561+
562+
instance = Cat()
563+
dictionary = {"color": "tortoiseshell"}
564+
565+
sentry_sdk.logger.warning(
566+
"Hello world!",
567+
attributes={
568+
"instance": instance,
569+
"dictionary": dictionary,
570+
},
571+
)
572+
573+
get_client().flush()
574+
575+
logs = envelopes_to_logs(envelopes)
576+
(log,) = logs
577+
578+
assert isinstance(log["attributes"]["instance"], str)
579+
assert isinstance(log["attributes"]["dictionary"], str)

tests/test_metrics.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,33 @@ def record_lost_event(reason, data_category, quantity):
290290
assert len(lost_event_calls) == 5
291291
for lost_event_call in lost_event_calls:
292292
assert lost_event_call == ("queue_overflow", "trace_metric", 1)
293+
294+
295+
def test_preserialization(sentry_init, capture_envelopes):
296+
"""We don't store references to objects in attributes."""
297+
sentry_init()
298+
299+
envelopes = capture_envelopes()
300+
301+
class Cat:
302+
pass
303+
304+
instance = Cat()
305+
dictionary = {"color": "tortoiseshell"}
306+
307+
sentry_sdk.metrics.count(
308+
"test.counter",
309+
1,
310+
attributes={
311+
"instance": instance,
312+
"dictionary": dictionary,
313+
},
314+
)
315+
316+
get_client().flush()
317+
318+
metrics = envelopes_to_metrics(envelopes)
319+
(metric,) = metrics
320+
321+
assert isinstance(metric["attributes"]["instance"], str)
322+
assert isinstance(metric["attributes"]["dictionary"], str)

0 commit comments

Comments
 (0)