@@ -559,12 +559,16 @@ def before_send(event: "sentry_types.Event", _: "sentry_types.Hint") -> "sentry_
559559 sentry .teardown ()
560560
561561
562+ @pytest .mark .parametrize ("attach_stacktrace" , [False , True ])
562563def test_structlog_stored_exception_reaches_sentry_and_stdout (
563564 minimal_sentry_config : SentryConfig ,
564565 sentry_mock : SentryTestTransport ,
565566 capsys : pytest .CaptureFixture [str ],
567+ * ,
568+ attach_stacktrace : bool ,
566569) -> None :
567- sentry = SentryInstrument (bootstrap_config = minimal_sentry_config )
570+ config = dataclasses .replace (minimal_sentry_config , sentry_attach_stacktrace = attach_stacktrace )
571+ sentry = SentryInstrument (bootstrap_config = config )
568572 logs = LoggingInstrument (bootstrap_config = LoggingConfig ())
569573 sentry .bootstrap ()
570574 logs .bootstrap ()
@@ -574,20 +578,102 @@ def test_structlog_stored_exception_reaches_sentry_and_stdout(
574578 except ValueError as exc :
575579 stored = exc
576580
577- logger .error ("stored exception" , exc_info = stored , component = "worker" )
581+ marker = uuid .uuid4 ().hex
582+ padding = "x" * config .sentry_max_value_length
583+ logger .error ("stored exception" , exc_info = stored , component = "worker" , password = marker , padding = padding )
578584
579585 line = json .loads (capsys .readouterr ().out .splitlines ()[- 1 ])
586+ assert line ["password" ] == marker
587+ assert line ["padding" ] == padding
580588 assert "Traceback (most recent call last)" in line ["exception" ]
581589 assert "ValueError: invalid literal for int() with base 10: 'original stored error'" in line ["exception" ]
582- event = sentry_mock .mock_envelopes [0 ].get_event ()
590+ envelope = sentry_mock .mock_envelopes [0 ]
591+ assert marker .encode () not in envelope .serialize ()
592+ event = envelope .get_event ()
583593 assert event is not None
584594 (exception ,) = event ["exception" ]["values" ]
585595 assert exception ["type" ] == "ValueError"
586596 assert exception ["value" ] == "invalid literal for int() with base 10: 'original stored error'"
587597 assert exception ["stacktrace" ]["frames" ][- 1 ]["function" ] == (
588598 "test_structlog_stored_exception_reaches_sentry_and_stdout"
589599 )
590- assert event ["contexts" ]["structlog" ] == {"component" : "worker" }
600+ assert event ["contexts" ]["structlog" ]["component" ] == "worker"
601+ assert event ["contexts" ]["structlog" ]["password" ] == "[Filtered]" # noqa: S105
602+ finally :
603+ logs .teardown ()
604+ sentry .teardown ()
605+
606+
607+ @pytest .mark .parametrize ("attach_stacktrace" , [False , True ])
608+ @pytest .mark .parametrize ("oversized" , [False , True ])
609+ def test_structlog_envelope_privacy_across_string_limit (
610+ minimal_sentry_config : SentryConfig ,
611+ sentry_mock : SentryTestTransport ,
612+ capsys : pytest .CaptureFixture [str ],
613+ * ,
614+ attach_stacktrace : bool ,
615+ oversized : bool ,
616+ ) -> None :
617+ limit = 1024
618+ marker = uuid .uuid4 ().hex
619+ padding = "x" * (limit + 1 if oversized else limit - 256 )
620+ callback_events : list [sentry_types .Event ] = []
621+
622+ def before_send (event : "sentry_types.Event" , _ : "sentry_types.Hint" ) -> "sentry_types.Event" :
623+ callback_events .append (copy .deepcopy (event ))
624+ return event
625+
626+ config = dataclasses .replace (
627+ minimal_sentry_config ,
628+ sentry_max_value_length = limit ,
629+ sentry_attach_stacktrace = attach_stacktrace ,
630+ sentry_before_send = before_send ,
631+ )
632+ sentry = SentryInstrument (bootstrap_config = config )
633+ logs = LoggingInstrument (bootstrap_config = LoggingConfig ())
634+ sentry .bootstrap ()
635+ logs .bootstrap ()
636+ try :
637+ logger .error ("failure" , password = marker , padding = padding )
638+ logger .error ("suppressed" , password = marker , padding = padding , skip_sentry = True )
639+
640+ lines = capsys .readouterr ().out .splitlines ()
641+ expected_lines = 2
642+ assert len (lines ) == expected_lines
643+ for line in lines :
644+ assert (len (line ) > limit ) == oversized
645+ rendered = json .loads (line )
646+ assert rendered ["password" ] == marker
647+ assert rendered ["padding" ] == padding
648+ assert json .loads (lines [1 ])["skip_sentry" ] is True
649+ (envelope ,) = sentry_mock .mock_envelopes
650+ assert marker .encode () not in envelope .serialize ()
651+ (event ,) = callback_events
652+ assert event ["logentry" ] == {"message" : "failure" , "formatted" : "failure" , "params" : []}
653+ assert event ["contexts" ]["structlog" ]["password" ] == "[Filtered]" # noqa: S105
654+ assert ("threads" in event ) == attach_stacktrace
591655 finally :
592656 logs .teardown ()
593657 sentry .teardown ()
658+
659+
660+ @pytest .mark .parametrize ("template" , ["plain %s" , "{invalid %s" , '{"unrelated":"%s"}' ])
661+ def test_unstructured_log_keeps_sdk_truncation (
662+ minimal_sentry_config : SentryConfig , sentry_mock : SentryTestTransport , template : str
663+ ) -> None :
664+ limit = 256
665+ config = dataclasses .replace (minimal_sentry_config , sentry_max_value_length = limit )
666+ sentry = SentryInstrument (bootstrap_config = config )
667+ sentry .bootstrap ()
668+ try :
669+ std_logger .error (template , "x" * (limit * 2 ))
670+ (envelope ,) = sentry_mock .mock_envelopes
671+ event = envelope .get_event ()
672+ assert event is not None
673+ formatted = event ["logentry" ]["formatted" ]
674+ assert isinstance (formatted , str )
675+ assert len (formatted ) <= limit
676+ assert event ["logentry" ]["message" ] == template
677+ assert "structlog" not in event ["contexts" ]
678+ finally :
679+ sentry .teardown ()
0 commit comments