Skip to main content

Backtrace.io error reporting tool for Python

Project description

backtrace-python

Backtrace error reporting tool for Python.

Documentation

https://docs.saucelabs.com/error-reporting/language-integrations/python/

Installation

Requirements

This module supports Python 2, Python 3, and PyPy.

python -m pip install backtracepython

Basic Usage

import backtracepython as bt
bt.initialize(
    endpoint="https://submit.backtrace.io/{universe}/{token}/json"
)

Sending Reports from Unhandled Exceptions

By default, the backtracepython module automatically captures unhandled exceptions and creates and sends error reports from them. This behavior can be adjusted with the disable_global_handler option in bt.initialize (see below).

Sending Reports Manually

You can also send error reports manually in your code. However, to get a correct callstack and source code context, you must send an error with a Python exception context. To do this, you can raise a Python exception and then immediately send a report using the send_last_exception call. Here's an example:

try:
    raise Exception("This report was sent manually.")
except:
    bt.send_last_exception()

Documentation

bt.initialize

Arguments

  • endpoint  - Required. Example: https://yourcompany.sp.backtrace.io:6098 or https://submit.backtrace.io/{universe}/{token}/json.  Sets the HTTP/HTTPS endpoint that error reports will be sent to. If submit.backtrace.io url is provided, the token argument is not required. token  -  Required only if endpoint is not set to submit.backtrace.io. Example: 51cc8e69c5b62fa8c72dc963e730f1e8eacbd243aeafc35d08d05ded9a024121  Sets the token that will be used for authentication when sending an error report.
  • attributes  - Dictionary that contains additional attributes to be sent along with every error report. These can be overridden on an individual report with report.set_attribute  Example: { 'application': "ApplicationName", 'serverId': "foo" }. Attributes values should be set to a primitive value such as boolean, integer or string.
  • attachments - A list of file paths that will be sent with each report.
  • ignore_ssl_certificate - Defaults to False. If True, ssl verification will be ignored during HTTP submission.
  • timeout  - Defaults to 4. Maximum amount of seconds to wait for error report processing and sending before concluding it failed.
  • debug_backtrace - Defaults to False . Set to True to have an error during collecting the report raise an exception, and to print some debugging information to stderr.
  • disable_global_handler - Defaults to False. If this is False  this module will insert itself in the sys.excepthook chain and report those errors automatically before re-raising the exception. Set to True  to disable this. Note that in this case the only way error reports will be reported is if you manually create and send them.
  • context_line_count  - Defaults to 200 . When an error is reported, this many lines above and below each stack function are included in the report.
  • tab_width  - Defaults to 8.  If there are any hard tabs in the source code, it is unclear how many spaces they should be indented to correctly display the source code. Therefore the error report can override this number to specify how many spaces a hard tab should be represented by when viewing source code.
  • collect_source_code - Default to True. By default Backtrace client collects corresponded source code and send it with the report. If set to False, the source code will not be collected.
  • exit_timeout - Default to 4. Backtrace sends data asynchronously in the background thread. The exit_timeout describes how many time the thread has to finish working before application exit.

bt.BacktraceReport

Create a report object that you can later choose whether or not to send. This may be useful to track something like a request.

report.set_attribute(key, value) Adds an attribute to a specific report. Valid types for value are str, float, int, and bool. Attributes are indexed and searchable. See also addAnnotation

report.set_dict_attributes(dict)

Adds all key-value pairs of dict into the report recursively.

report.get_attributes()

Returns all report attributes.

report.set_annotation(key, value)

Adds an annotation to a specific report. Annotations, unlike attributes, are not indexed and searchable. However, they are available for inspection when you view a specific report.

key - String which is the name of the annotation. value - Any type which is JSON-serializable.

report.set_dict_annotations(dict)

Adds all key-value pairs of dict into the report.

report.add_attachment(attachment_path)

Adds an attachment to the report.

report.get_attachments()

Returns a list of attachment paths.

report.set_exception(ExceptionType, exception, traceback)

error  is an Error object. Backtrace will extract information from this object such as the error message and stack trace and send this information along with the report.

report.capture_last_exception()

This is the same as report.set_exception(*sys.exc_info())

report.log(line)

Adds a timestamped log message to the report. Log output is available when you view a report.

report.send()

Sends the error report to the endpoint specified in initialize.

bt.send_last_exception(**kwargs)

  • attributes  - dictionary of attributes to add to the report. See report.set_dict_attributes
  • annotations  - dictionary of annotations to add to the report. See report.set_dict_annotations

Contributing

To run the test suite:

pytest

Since all of these implementations of Python are supported, be sure to run the test suite with all of them:

  • Python 2
  • Python 3
  • PyPy

Publishing to PyPI

  1. Make sure all tests pass (see above).
  2. Update version number in backtracepython module.
  3. Tag the version in git.
python3 setup.py bdist_wheel --universal
twine upload dist/*

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

backtracepython-0.4.1.tar.gz (18.4 kB view details)

Uploaded Source

Built Distribution

backtracepython-0.4.1-py3-none-any.whl (22.4 kB view details)

Uploaded Python 3

File details

Details for the file backtracepython-0.4.1.tar.gz.

File metadata

  • Download URL: backtracepython-0.4.1.tar.gz
  • Upload date:
  • Size: 18.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for backtracepython-0.4.1.tar.gz
Algorithm Hash digest
SHA256 d7e9899f6b19b6d309d59226a7191b58c86919f0408dabc99ab159a97a51480c
MD5 f4c81283bd8f3f41ad8229530fdad886
BLAKE2b-256 4fe72e27e4eb1fd3a3d59003f87c51f023568080bc0e155ca418ba6e1dbea31c

See more details on using hashes here.

File details

Details for the file backtracepython-0.4.1-py3-none-any.whl.

File metadata

File hashes

Hashes for backtracepython-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 809137bbeb348070019510d3933b1fb87986d1b57c30ad11b1ee60b564d4e744
MD5 b5785b6d313598525b0dbbaf0fd6c7ec
BLAKE2b-256 db6017256604849c32c00418939354677294a2875fda1e34c94264d4326509b7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page