Skip to content

Add basic default attributes #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ RUN pip install --upgrade pip \
&& pip install pytest -r requirements.txt

COPY . /sdk
ENV PYTHONPATH=/sdk

CMD ["pytest"]
32 changes: 24 additions & 8 deletions backtracepython/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import simplejson as json
import os
import platform
import socket
Expand All @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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__]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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()


Expand All @@ -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()
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
pythonpath = .
25 changes: 25 additions & 0 deletions tests/test_report_attributes.py
Original file line number Diff line number Diff line change
@@ -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
Loading