From db4ba41c168bfe768ca3ee6cfbf626e9f132e219 Mon Sep 17 00:00:00 2001 From: Konrad Dysput Date: Wed, 4 Sep 2024 15:15:03 +0200 Subject: [PATCH 1/5] Add basic default attributes --- backtracepython/__init__.py | 32 ++++++++++++++++++++++++-------- tests/test_report_attributes.py | 25 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 tests/test_report_attributes.py diff --git a/backtracepython/__init__.py b/backtracepython/__init__.py index 06575bb..c4a8028 100644 --- a/backtracepython/__init__.py +++ b/backtracepython/__init__.py @@ -1,4 +1,3 @@ -import simplejson as json import os import platform import socket @@ -8,6 +7,8 @@ import time import uuid +import simplejson as json + __all__ = ["BacktraceReport", "initialize", "finalize", "terminate", "version", "version_string", "send_last_exception", "send_report"] class version: @@ -24,7 +25,14 @@ class globs: debug_backtrace = False timeout = None tab_width = None - attributes = None + attributes = { + 'backtrace.agent': 'backtrace-python', + 'backtrace.version': version_string, + 'application.session': str(uuid.uuid4()), + 'uname.sysname': platform.system(), + 'uname.version': platform.version(), + 'uname.release': platform.release() + } context_line_count = None worker = None next_source_code_id = 0 @@ -106,16 +114,17 @@ def __init__(self): self.fault_thread = threading.current_thread() self.source_code = {} self.source_path_dict = {} - + entry_source_code_id = None import __main__ - entry_path = os.path.abspath(__main__.__file__) cwd_path = os.path.abspath(os.getcwd()) entry_thread = get_main_thread() - entry_source_code_id = add_source_code(entry_path, self.source_code, self.source_path_dict, 1) + if hasattr(__main__, '__file__'): + entry_source_code_id = add_source_code(__main__.__file__, self.source_code, self.source_path_dict, 1) if hasattr(__main__, '__file__') else None init_attrs = { 'hostname': socket.gethostname(), 'process.age': get_process_age(), + 'error.type': 'Exception' } init_attrs.update(globs.attributes) @@ -130,13 +139,14 @@ def __init__(self): 'agentVersion': version_string, 'mainThread': str(self.fault_thread.ident), 'entryThread': str(entry_thread.ident), - 'entrySourceCode': entry_source_code_id, 'cwd': cwd_path, 'attributes': init_attrs, 'annotations': { 'Environment Variables': dict(os.environ), }, } + if entry_source_code_id is not None: + self.report['entrySourceCode'] = entry_source_code_id def set_exception(self, garbage, ex_value, ex_traceback): self.report['classifiers'] = [ex_value.__class__.__name__] @@ -168,6 +178,9 @@ def set_dict_attributes(self, target_dict): def set_annotation(self, key, value): self.report['annotations'][key] = value + + def get_attributes(self): + return self.report['attributes'] def set_dict_annotations(self, target_dict): self.report['annotations'].update(target_dict) @@ -196,6 +209,7 @@ def send(self): def create_and_send_report(ex_type, ex_value, ex_traceback): report = BacktraceReport() report.set_exception(ex_type, ex_value, ex_traceback) + report.set_attribute('error.type', 'Unhandled exception') report.send() def bt_except_hook(ex_type, ex_value, ex_traceback): @@ -225,7 +239,7 @@ def initialize(**kwargs): globs.debug_backtrace = kwargs.get('debug_backtrace', False) globs.timeout = kwargs.get('timeout', 4) globs.tab_width = kwargs.get('tab_width', 8) - globs.attributes = kwargs.get('attributes', {}) + globs.attributes.update(kwargs.get('attributes', {})) globs.context_line_count = kwargs.get('context_line_count', 200) stdio_value = None if globs.debug_backtrace else subprocess.PIPE @@ -249,6 +263,7 @@ def send_last_exception(**kwargs): report.capture_last_exception() report.set_dict_attributes(kwargs.get('attributes', {})) report.set_dict_annotations(kwargs.get('annotations', {})) + report.set_attribute('error.type', 'Exception') report.send() @@ -263,5 +278,6 @@ def send_report(msg, **kwargs): report.set_exception(*make_an_exception()) report.set_dict_attributes(kwargs.get('attributes', {})) report.set_dict_annotations(kwargs.get('annotations', {})) - report.report['attributes']['error.message'] = msg + report.set_attribute('error.message', msg) + report.set_attribute('error.type', 'Message') report.send() diff --git a/tests/test_report_attributes.py b/tests/test_report_attributes.py new file mode 100644 index 0000000..528dd73 --- /dev/null +++ b/tests/test_report_attributes.py @@ -0,0 +1,25 @@ +from backtracepython import BacktraceReport + +report = BacktraceReport() + +def test_report_session_attribute_is_defined(): + assert report.get_attributes()['application.session'] is not None + +def test_report_session_attribute_doesnt_change(): + compare_report = BacktraceReport() + + assert report.get_attributes()['application.session'] == compare_report.get_attributes()['application.session'] + +def test_report_backtrace_reporter_attributes(): + attributes = report.get_attributes() + assert attributes['backtrace.agent'] == 'backtrace-python' + assert attributes['backtrace.version'] is not None + +def test_report_attribute_override(): + override_report = BacktraceReport() + expected_value = "test" + # attribute name that is defined + override_attribute_name = "hostname" + override_report.set_attribute(override_attribute_name, expected_value) + + assert override_report.get_attributes()[override_attribute_name] == expected_value \ No newline at end of file From f700f65682218e8bbffa803843a954793e84f589 Mon Sep 17 00:00:00 2001 From: Konrad Dysput Date: Wed, 4 Sep 2024 15:22:09 +0200 Subject: [PATCH 2/5] Add pytest.ini for project paths --- pytest.ini | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 pytest.ini diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..03f586d --- /dev/null +++ b/pytest.ini @@ -0,0 +1,2 @@ +[pytest] +pythonpath = . \ No newline at end of file From 29fa5b74dffbe95c48037341fb0af1976c6bb99a Mon Sep 17 00:00:00 2001 From: Konrad Dysput Date: Wed, 4 Sep 2024 15:24:19 +0200 Subject: [PATCH 3/5] Set python sdk inside the container --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 7421f06..c45e5a9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,4 +8,4 @@ RUN pip install --upgrade pip \ COPY . /sdk -CMD ["pytest"] +CMD ["PYTHONPATH=/sdk " "pytest"] From 1e02c0cf55216f7d821eb650f25fb1888d0bec74 Mon Sep 17 00:00:00 2001 From: Konrad Dysput Date: Wed, 4 Sep 2024 15:27:46 +0200 Subject: [PATCH 4/5] Set env var in docker file --- Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index c45e5a9..879f6bd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,5 +7,6 @@ RUN pip install --upgrade pip \ && pip install pytest -r requirements.txt COPY . /sdk +ENV PYTHONPATH=/sdk -CMD ["PYTHONPATH=/sdk " "pytest"] +CMD ["pytest"] From d26f96cadd15056910e9f1dc808aec3e2219c522 Mon Sep 17 00:00:00 2001 From: Konrad Dysput Date: Wed, 4 Sep 2024 17:43:54 +0200 Subject: [PATCH 5/5] New line --- pytest.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytest.ini b/pytest.ini index 03f586d..a635c5c 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,2 +1,2 @@ [pytest] -pythonpath = . \ No newline at end of file +pythonpath = .