From ae99ee155e4960925f22628ff26f35e056cacab8 Mon Sep 17 00:00:00 2001 From: Andrei Andreev Date: Mon, 27 May 2024 14:35:26 +0200 Subject: [PATCH] Enhance breadcrumb formatting in Sentry reporter Added functionality to sort breadcrumbs by timestamp and remove duplicates. This ensures that the event logs are presented in a chronological order, providing a more coherent view of the events. Also added tests to verify this new behavior. In case of incorrect event format, the breadcrumb formatting is skipped to prevent exceptions. --- .../tests/test_sentry_reporter.py | 57 ++++++++++++++++++- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/src/tribler/core/sentry_reporter/tests/test_sentry_reporter.py b/src/tribler/core/sentry_reporter/tests/test_sentry_reporter.py index 322320df18..79a7589f6b 100644 --- a/src/tribler/core/sentry_reporter/tests/test_sentry_reporter.py +++ b/src/tribler/core/sentry_reporter/tests/test_sentry_reporter.py @@ -4,10 +4,10 @@ import pytest from tribler.core.sentry_reporter.sentry_reporter import ( - BROWSER, CONTEXTS, NAME, OS_ENVIRON, + BREADCRUMBS, BROWSER, CONTEXTS, NAME, OS_ENVIRON, REPORTER, STACKTRACE, SentryReporter, SentryStrategy, - TAGS, TRIBLER, VERSION, this_sentry_strategy, + TAGS, TRIBLER, VALUES, VERSION, this_sentry_strategy, ) from tribler.core.sentry_reporter.sentry_scrubber import SentryScrubber from tribler.core.utilities.patch_import import patch_import @@ -281,3 +281,56 @@ def test_sentry_strategy(sentry_reporter): assert sentry_reporter.thread_strategy.get() is None assert sentry_reporter.global_strategy == SentryStrategy.SEND_ALLOWED_WITH_CONFIRMATION + + +def test_format_breadcrumbs(sentry_reporter): + # Test that breadcrumbs are sorted by timestamp and duplicates are removed + event = { + BREADCRUMBS: { + VALUES: [ + {'message': 'message 2', 'timestamp': 'timestamp 3'}, + {'message': 'message 1', 'timestamp': 'timestamp 1'}, # wrong order + {'message': 'message 1', 'timestamp': 'timestamp 1'}, # duplicate + {'message': 'message 3', 'timestamp': 'timestamp 4'}, # wrong order + ] + } + } + expected = { + BREADCRUMBS: { + VALUES: [ + {'message': 'message 1', 'timestamp': 'timestamp 1'}, + {'message': 'message 2', 'timestamp': 'timestamp 3'}, + {'message': 'message 3', 'timestamp': 'timestamp 4'}, + ] + } + } + + sentry_reporter._format_breadcrumbs(event) + + assert event == expected + + +def test_format_breadcrumbs_wrong_fields(sentry_reporter): + # Test that in the case of wrong event format the breadcrumbs formatting will be skipped + event = { + BREADCRUMBS: { + VALUES: [ + {'message': 'message 1', 'timestamp': 'timestamp 1'}, + {'message': 'message 1', 'timestamp': 'timestamp 1'}, + {'message': 'message 2'}, # an exception will be raised, therefore format will be skipped + ] + } + } + expected = { + BREADCRUMBS: { + VALUES: [ + {'message': 'message 1', 'timestamp': 'timestamp 1'}, + {'message': 'message 1', 'timestamp': 'timestamp 1'}, + {'message': 'message 2'}, + ] + } + } + + sentry_reporter._format_breadcrumbs(event) + + assert event == expected