Skip to content

Commit

Permalink
Split stacktrace into parts
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a committed Jan 25, 2021
1 parent 652b376 commit 0670d5e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,11 @@ def send_event(event, post_data=None, sys_info=None):

context['browser'] = {'version': version, 'name': 'Tribler'}

reporter[STACKTRACE] = parse_stacktrace(get_value(post_data, 'stack'))
stacktrace_parts = parse_stacktrace(get_value(post_data, 'stack'))
reporter[STACKTRACE] = next(stacktrace_parts, [])
reporter[f'{STACKTRACE}_long'] = next(stacktrace_parts, [])
reporter[f'{STACKTRACE}_context'] = next(stacktrace_parts, [])

reporter['comments'] = get_value(post_data, 'comments')

reporter[OS_ENVIRON] = parse_os_environ(get_value(sys_info, OS_ENVIRON))
Expand Down
29 changes: 22 additions & 7 deletions src/tribler-common/tribler_common/sentry_reporter/sentry_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,36 @@ def parse_os_environ(array):
return result


def parse_stacktrace(stacktrace):
def parse_stacktrace(stacktrace, delimiters=None):
"""Parse stacktrace field.
Example of stacktrace:
Traceback (most recent call last):,
File "src\run_tribler.py", line 179, in <module>,
RuntimeError: ('\'utf-8\' codec can\'t decode byte 0xcd in position 0: invalid continuation byte,
--LONG TEXT--,
Traceback (most recent call last):,
File "<user>\\asyncio\\events.py", line 81, in _run,
UnicodeDecodeError: \'utf-8\' codec can\'t decode byte 0xcd in position 0: invalid continuation byte,
--CONTEXT--,
{\'message\': "Exception in callback'
Args:
stacktrace: a string with '\n' delimiter.
Example: "line1\nline2"
stacktrace: the string that represents stacktrace.
delimiters: hi-level delimiters of the stacktrace.
['--LONG TEXT--', '--CONTEXT--'] by default.
Returns:
The list of strings made from the given string.
Example: ["line1", "line2"]
The generator of stacktrace parts.
"""
if delimiters is None:
delimiters = ['--LONG TEXT--', '--CONTEXT--']

if not stacktrace:
return []
return None

return [line for line in re.split(r'\\n|\n', stacktrace) if line]
for part in re.split('|'.join(delimiters), stacktrace):
yield [line for line in re.split(r'\\n|\n', part) if line]


def get_first_item(items, default=None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,14 @@ def test_send():
assert SentryReporter.send_event({}, None, None) == {
'contexts': {
'browser': {'name': 'Tribler', 'version': None},
'reporter': {'_stacktrace': [], 'comments': None, OS_ENVIRON: {}, 'sysinfo': None},
'reporter': {
'_stacktrace': [],
'_stacktrace_context': [],
'_stacktrace_long': [],
'comments': None,
OS_ENVIRON: {},
'sysinfo': None,
},
},
'tags': {'machine': None, 'os': None, 'platform': None, PLATFORM_DETAILS: None, 'version': None},
}
Expand All @@ -61,14 +68,21 @@ def test_send():
"timestamp": 42,
"sysinfo": '',
"comments": 'comment',
"stack": 'some\nstack',
"stack": 'l1\nl2--LONG TEXT--l3\nl4',
}

assert SentryReporter.send_event({'a': 'b'}, post_data, None) == {
'a': 'b',
'contexts': {
'browser': {'name': 'Tribler', 'version': '0.0.0'},
'reporter': {'_stacktrace': ['some', 'stack'], 'comments': 'comment', 'os.environ': {}, 'sysinfo': None},
'reporter': {
'_stacktrace': ['l1', 'l2'],
'_stacktrace_context': [],
'_stacktrace_long': ['l3', 'l4'],
'comments': 'comment',
'os.environ': {},
'sysinfo': None,
},
},
'tags': {'machine': 'x86_64', 'os': 'posix', 'platform': None, 'platform.details': None, 'version': '0.0.0'},
}
Expand All @@ -79,7 +93,14 @@ def test_send():
assert SentryReporter.send_event({}, post_data, None) == {
'contexts': {
'browser': {'name': 'Tribler', 'version': None},
'reporter': {'_stacktrace': [], 'comments': None, 'os.environ': {}, 'sysinfo': None},
'reporter': {
'_stacktrace': [],
'_stacktrace_context': [],
'_stacktrace_long': [],
'comments': None,
'os.environ': {},
'sysinfo': None,
},
},
'tags': {'machine': None, 'os': None, 'platform': None, 'platform.details': None, 'version': None},
}
Expand All @@ -90,6 +111,8 @@ def test_send():
'browser': {'name': 'Tribler', 'version': None},
'reporter': {
'_stacktrace': [],
'_stacktrace_context': [],
'_stacktrace_long': [],
'comments': None,
'os.environ': {'KEY': 'VALUE', 'KEY1': 'VALUE1'},
'sysinfo': sys_info,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,24 @@ def test_parse_os_environ():


def test_parse_stacktrace():
assert parse_stacktrace(None) == []
assert parse_stacktrace('') == []
assert parse_stacktrace('\n') == []
assert parse_stacktrace('\n\n') == []
assert parse_stacktrace('line1\n\nline2\\nline3') == ['line1', 'line2', 'line3']
assert list(parse_stacktrace(None)) == []
assert list(parse_stacktrace('')) == []
assert list(parse_stacktrace('\n')) == [[]]
assert list(parse_stacktrace('\n\n')) == [[]]
assert list(parse_stacktrace('line1\n\nline2\\nline3')) == [['line1', 'line2', 'line3']]

# split --LONG TEXT-- and --CONTEXT-- parts
assert list(parse_stacktrace('l1\nl2--LONG TEXT--l3\nl4--CONTEXT--l5\nl6')) == [
['l1', 'l2'],
['l3', 'l4'],
['l5', 'l6'],
]

# split custom parts
assert list(parse_stacktrace('l1\nl2customl3\nl4', delimiters=['custom'])) == [
['l1', 'l2'],
['l3', 'l4'],
]


def test_modify():
Expand Down

0 comments on commit 0670d5e

Please sign in to comment.