Skip to content

Commit

Permalink
Extract events
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a committed Jan 25, 2021
1 parent 0670d5e commit d80b893
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from tribler_common.sentry_reporter.sentry_tools import (
delete_item,
extract_dict,
get_first_item,
get_value,
parse_os_environ,
Expand Down Expand Up @@ -169,6 +170,9 @@ def send_event(event, post_data=None, sys_info=None):
if event is None:
return event

post_data = post_data or dict()
sys_info = sys_info or dict()

if CONTEXTS not in event:
event[CONTEXTS] = {}

Expand Down Expand Up @@ -201,7 +205,9 @@ def send_event(event, post_data=None, sys_info=None):

reporter[OS_ENVIRON] = parse_os_environ(get_value(sys_info, OS_ENVIRON))
delete_item(sys_info, OS_ENVIRON)
reporter[SYSINFO] = sys_info

reporter['events'] = extract_dict(sys_info, r'^(event|request)')
reporter[SYSINFO] = {key: sys_info[key] for key in sys_info if key not in reporter['events']}

with this_sentry_strategy(SentryStrategy.SEND_ALLOWED):
sentry_sdk.capture_event(event)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ def get_value(d, key, default=None):
return d.get(key, default) if d else default


def extract_dict(d, regex_key_pattern):
if not d or not regex_key_pattern:
return dict()

matched_keys = [key for key in d if re.match(regex_key_pattern, key)]
return {key: d[key] for key in matched_keys}


def modify_value(d, key, function):
if not d or not key or not function:
return d
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ def test_send():
'_stacktrace_long': [],
'comments': None,
OS_ENVIRON: {},
'sysinfo': None,
'sysinfo': {},
'events': {},
},
},
'tags': {'machine': None, 'os': None, 'platform': None, PLATFORM_DETAILS: None, 'version': None},
Expand All @@ -81,31 +82,22 @@ def test_send():
'_stacktrace_long': ['l3', 'l4'],
'comments': 'comment',
'os.environ': {},
'sysinfo': None,
'sysinfo': {},
'events': {},
},
},
'tags': {'machine': 'x86_64', 'os': 'posix', 'platform': None, 'platform.details': None, 'version': '0.0.0'},
}

# test sys_info
post_data = {"sysinfo": 'key\tvalue\nkey1\tvalue1\n'}

assert SentryReporter.send_event({}, post_data, None) == {
'contexts': {
'browser': {'name': 'Tribler', 'version': 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},
sys_info = {
'platform': ['darwin'],
'platform.details': ['details'],
OS_ENVIRON: ['KEY:VALUE', 'KEY1:VALUE1'],
'event_1': [{'type': ''}],
'request_1': [{}],
'event_2': [],
'request_2': [],
}

sys_info = {'platform': ['darwin'], 'platform.details': ['details'], OS_ENVIRON: ['KEY:VALUE', 'KEY1:VALUE1']}
assert SentryReporter.send_event({}, None, sys_info) == {
'contexts': {
'browser': {'name': 'Tribler', 'version': None},
Expand All @@ -114,8 +106,9 @@ def test_send():
'_stacktrace_context': [],
'_stacktrace_long': [],
'comments': None,
'os.environ': {'KEY': 'VALUE', 'KEY1': 'VALUE1'},
'sysinfo': sys_info,
OS_ENVIRON: {'KEY': 'VALUE', 'KEY1': 'VALUE1'},
'sysinfo': {'platform': ['darwin'], 'platform.details': ['details']},
'events': {'event_1': [{'type': ''}], 'request_1': [{}], 'event_2': [], 'request_2': []},
},
},
'tags': {'machine': None, 'os': None, 'platform': 'darwin', 'platform.details': 'details', 'version': None},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from tribler_common.sentry_reporter.sentry_tools import (
delete_item,
distinct_by,
extract_dict,
format_version,
get_first_item,
get_last_item,
Expand Down Expand Up @@ -131,3 +132,10 @@ def test_skip_dev_version():
# experimental versions
assert format_version('7.7.1-exp1-1-abcd ') == '7.7.1-exp1'
assert format_version('7.7.1-someresearchtopic-7-abcd ') == '7.7.1-someresearchtopic'


def test_extract_dict():
assert not extract_dict(None, None)

assert extract_dict({}, '') == {}
assert extract_dict({'k': 'v', 'k1': 'v1'}, r'\w$') == {'k': 'v'}

0 comments on commit d80b893

Please sign in to comment.