Code Monkey home page Code Monkey logo

error-tracker's Introduction

Error Tracker

Full featured error tracking module for Python apps supports Flask and Django

image

image

image

image

image

Introduction

ErrorTracker is a batteries-included app and extensions for python app, that can track errors, send notification, mask sensitive data and capture frames data.

It plays nicely with Django and Flask

Simple to use extension that lets you add error recording interfaces to Python applications. It's implemented in such a way that the developer has total control of the resulting application.

Out-of-the-box, Error Tracker plays nicely with various ORM's, including

It also boasts a simple Model management interface.

The biggest feature of ErrorTracker is flexibility. To start off with you can create a very simple application in no time, with exception monitor enabled, but then you can go further and customize different aspects.

ErrorTracker is an active project, well-tested and production ready.

Installation

To install ErrorTracker, simply:

pip install error-tracker

Features

  • Sensitive data( like password, secret ) Masking
  • Record all the frames ( frame data are stored in JSON format so that it can be analyzed later)
  • Unique URL generation
  • Number of times the exception occurred and first/last time of exception
  • Sending notifications with exception details
  • Record different types of exception like 500 or 404 etc
  • Raise or update ticket in Jira/Bugzilla etc by ticketing interface.

Usage

Flask App configuration

...
APP_ERROR_SEND_EMAIL = True
APP_ERROR_RECIPIENT_EMAIL = ('[email protected]',)
APP_ERROR_SUBJECT_PREFIX = "Server Error"
APP_ERROR_EMAIL_SENDER = '[email protected]'

app.py

from flask import Flask
from flask_mail import Mail
import settings
from error_tracker import AppErrorTracker, NotificationMixin
from flask_sqlalchemy import SQLAlchemy
...
app = Flask(__name__)
app.config.from_object(settings)
db = SQLAlchemy(app)
class Notifier(Mail, NotificationMixin):
    def notify(self, request, exception,
               email_subject=None,
               email_body=None,
               from_email=None,
               recipient_list=None):
        message = Message(email_subject, recipient_list, email_body, sender=from_email)
        self.send(message)
mailer = Notifier(app=app)
error_tracker = AppErrorTracker(app=app, db=db, notifier=mailer)

....

....
# Record exception when 404 error code is raised
@app.errorhandler(403)
def error_403(e):
    error_tracker.capture_exception()
    # any custom logic

# Record error using decorator
@app.errorhandler(500)
@error_tracker.track_exception
def error_500(e):
    # some custom logic
....

Django App Usage

We need to update settings.py file as

  • Add app to installed apps list
  • Add Middleware for exception tracking. This should be added at the end so that it can process exception 1st in the middleware call stack.
  • Other configs related to notification

Sample Code

...
APP_ERROR_RECIPIENT_EMAIL = ('[email protected]',)
APP_ERROR_SUBJECT_PREFIX = "Server Error"
APP_ERROR_EMAIL_SENDER = '[email protected]'

INSTALLED_APPS = [
    ...
    'error_tracker.DjangoErrorTracker'
]
MIDDLEWARE = [
    ...
    'error_tracker.django.middleware.ExceptionTrackerMiddleWare'
]

Documentations

This has got extensive document browse at https://error-tracker.readthedocs.io/en/latest/

All docs are in docs/source

And if you want to preview any .rst snippets that you may want to contribute, go to http://rst.ninjs.org/.

Examples

Several usage examples are included in the /tests folder. Please feel free to add your own examples, or improve on some of the existing ones, and then submit them via GitHub as a pull-request.

You can see some of these examples in action at https://github.com/sonus21/error-tracker/tree/master/examples To run the examples on your local environment, one at a time, do something like:

cd error-tracker/examples

Django:

cd error-tracker/examples
cd DjangoSample
python manage.py runserver

Flask:

cd flask-sample
python app.py

Tests

To run the tests, from the project directory, simply:

pip install -r requirements-dev.txt
bash run-tests.sh

You should see output similar to:

.............................................
----------------------------------------------------------------------
Ran 31 tests in 1.144s

OK

Contribution

You're most welcome to raise pull request or fixes.

error-tracker's People

Contributors

alanparente avatar jeduardo211 avatar sonus21 avatar tboulogne avatar xangcastle avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

error-tracker's Issues

Change break migrations

Hello,

the change

dependencies = [
        ('django', '0001_initial'),
    ]
to
 dependencies = [
        ('error_tracker', '0001_initial'),
    ]

made migrations broken.

The error message

django.db.migrations.exceptions.NodeNotFoundError: Migration django.0002_auto_20201018_1311 dependencies reference nonexistent parent node ('error_tracker', '0001_initial')

Regards

Extend Django ErrorModel

Hi, I saw on the docs that I can set a model to replace the default ErrorModel but I need to implement all abstract methods. And if I want to extend the ErrorModel, this would be possible?

UserWarning: APP_ERROR_DB_MODEL is not set using default model

/error_tracker/django/init.py:28: UserWarning: APP_ERROR_DB_MODEL is not set using default model
warnings.warn("APP_ERROR_DB_MODEL is not set using default model")
/error_tracker/libs/utils.py:121: UserWarning: Default Masking module will be used
warnings.warn(message)
/error_tracker/libs/utils.py:121: UserWarning: Default Notification module will be used
warnings.warn(message)
/error_tracker/libs/utils.py:121: UserWarning: Default ContextBuilder module will be used
warnings.warn(message)

All variables in traceback are `None`

I used the 3.0.0 version of error -tracker for Django 4.2.1 for some time. In my admin in all traceback, I see None instead of the actual value of variables.

I searched in the documentation but don't see anything about It. I don't have any masking variables set.

Zrzut ekranu 2023-07-16 o 12 50 48

Reduce dependencies?

Currently, this package depends on Flask, Django and Django-rest-framework, but is that really needed? Typically, when this library is used with django and/or django-rest-framework or Flask, then the application will already depend on those packages and error-tracker could just import them.

Typically an application will not use both Flask and Django at the same time, so this installs a bunch of packages that are never used.

Maybe the dependencies can be made optional using setup.py's "extra" feature, or just removed altogether (with the responsibility for installing these moved to the application). The latter might need some changes in the code (haven't checked) to ensure things are only imported when actually needed and/or to have a graceful fallback if imports are not available.

Record all errors (do not overwrite)

At the moment, errors are overwritten. A better approach would be to simply group similar errors together (Sentry way) and allow to navigate from one to another.

Error on warnings library

To be raised an exception must derive from BaseException

File "/.../lib/python3.8/site-packages/error_tracker/django/__init__.py", line 43, in get_exception_model
    raise warnings.warn(
TypeError: exceptions must derive from BaseException

Simplify error reporting

Take Sentry SDK as a good example of such.

Capture errors

https://docs.sentry.io/platforms/python/#capturing-errors

from sentry_sdk import capture_exception

try:
    a_potentially_failing_function()
except Exception as e:
    # Alternatively the argument can be omitted
    capture_exception(e)

Capture messages

https://docs.sentry.io/error-reporting/capturing/?platform=python#capturing-messages

from sentry_sdk import capture_message

capture_message('Something went wrong')

Extra context

https://docs.sentry.io/platforms/python/#extra-context

from sentry_sdk import configure_scope

with configure_scope() as scope:
    scope.set_extra("character_name", "Mighty Fighter")

NotificationMixin not working Without set APP_ERROR_RECIPIENT_EMAIL and APP_ERROR_EMAIL_SENDER

I create the class on django

class Notifier(NotificationMixin):
    def notify(self, request, exception,
               email_subject=None,
               email_body=None,
               from_email=None,
               recipient_list=None):
            BaseSlackNotification.send_notification(f"{exception}\n {request}",
                                                settings.SLACK_NEW_ERR0R)

I put on settings
APP_ERROR_NOTIFICATION_MODULE = "apps.commons.slack_notifications.Notifier"
and not works, in debugger never stop on method on exception, but the except show's on error page

Add support for Django REST Framework

The class DefaultDjangoContextBuilder of the error_tracker.django.utils needs some extensions. In particular, in case of Django REST Framework (DRF), the request instance has no POST (request.POST is empty dict). Instead data is stored in request.data. You could simply check, if it's instance of rest_framework.request.Request, then handle as DRF request, otherwise standard Django.

Error: ModuleNotFoundError: No module named 'flask

Hi, I tried to use this with Django and in the moment in put 'error_tracker.DjangoErrorTracker' in settings.py I have the error below.

File ".../lib/python3.7/site-packages/error_tracker/__init__.py", line 14, in <module>
    from error_tracker.flask import *
File ".../lib/python3.7/site-packages/error_tracker/flask/__init__.py", line 10, in <module>
    from .flask_error import AppErrorTracker
File ".../lib/python3.7/site-packages/error_tracker/flask/flask_error.py", line 15, in <module>
    from flask import _request_ctx_stack as stack
ModuleNotFoundError: No module named 'flask'

Thank you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.