dogslow-sentry


Namedogslow-sentry JSON
Version 2.0.1 PyPI version JSON
download
home_pagehttps://github.com/intgr/dogslow-sentry
SummaryA Django middleware that logs tracebacks of slow requests to Sentry.
upload_time2023-12-08 10:56:00
maintainer
docs_urlNone
authorMarti Raudsepp
requires_python>=3.7
licenseGNU LGPL
keywords django debug watchdog middleware traceback sentry
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ==============================================
Dogslow-Sentry -- Django Slow Request Watchdog
==============================================

|PyPI version badge| |Tests status|

.. |PyPI version badge| image:: https://badge.fury.io/py/dogslow-sentry.svg
   :target: https://pypi.org/project/dogslow-sentry/

.. |Tests status| image:: https://github.com/intgr/dogslow-sentry/workflows/Tests/badge.svg?branch=main
   :target: https://github.com/intgr/dogslow-sentry/actions?query=workflow:Tests

Overview
--------

Dogslow is a Django watchdog middleware that logs tracebacks of slow
requests. Dogslow-sentry requires Python 3.7+, Django 2.2+.

It started as an `internal project inside Bitbucket`_ to help trace
operational problems.

In 2021, the dogslow-sentry fork was created to add Sentry-specific information
to reports, like full stack trace, request information, fingerprint for issue
grouping, breadcrumbs, etc.

.. _internal project inside Bitbucket: http://blog.bitbucket.org/2011/05/17/tracking-slow-requests-with-dogslow/


Installation
------------

Install dogslow-sentry::

    $ pip install dogslow-sentry

Then add ``dogslow_sentry.WatchdogMiddleware`` to your Django settings file::

    MIDDLEWARE = [
        'dogslow_sentry.WatchdogMiddleware',
        ...
    ]

For best results, make it one of the first middlewares that is run.


Configuration
-------------

Naturally, dogslow-sentry expects a `working Sentry configuration for Django`_.

.. _working Sentry configuration for Django: https://docs.sentry.io/platforms/python/guides/django/

You can use the following configuration in your ``settings.py``
file to tune the watchdog::

    # Watchdog is enabled by default, to temporarily disable, set to False:
    DOGSLOW = True

    # Log requests taking longer than 25 seconds:
    DOGSLOW_TIMER = 25

    # Enable logging to Sentry
    DOGSLOW_SENTRY = True

    # Also log slow request tracebacks to Python logger
    DOGSLOW_LOGGER = 'dogslow_sentry'
    DOGSLOW_LOG_LEVEL = 'WARNING'

    # Tuple of url pattern names that should not be monitored:
    # (defaults to none -- everything monitored)
    DOGSLOW_IGNORE_URLS = ('some_view', 'other_view')


Usage
-----

Every incoming HTTP request gets a 25 second timeout in the watchdog. If a
request does not return within that time, the watchdog activates and takes a
peek at the request thread's stack and writes the backtrace (including all
local stack variables -- Django style) to a log file.

Note that ``dogslow`` only takes a peek at the thread's stack. It does not
interrupt the request, or influence it in any other way. Using ``dogslow`` is
therefore safe to use in production.


Changelog
---------

2.0.1 (2023-12-08)

* Use ISO 8601-like YYYY-MM-DD date format for email/file logging.
* Fixed deprecation warnings on Python 3.12 (utcnow function).
* Enabled CI testing with Python 3.11, 3.12 and Django 5.0, 4.2, 4.1.

2.0.0 (2021-12-13)

* Configured GitHub Actions for CI.
* Enabled testing with Python 3.10 and Django 4.0.
* Fixed deprecation warning when using Python 3.10.

2.0.0b1 (2021-07-19)

* Initial pre-release of ``dogslow-sentry`` fork.
* Improved Sentry integration.
* Dropped Python 2.7 support, now requires Python 3.7+, Django 2.2+.
* Many minor tweaks. Reformatted code with Black.

1.2 (2018-01-04)

* Last release of upstream ``dogslow`` package.


Caveats
-------

Dogslow uses multithreading. It has a single background thread that handles the
watchdog timeouts and takes the tracebacks, so that the original request
threads are not interrupted. This has some consequences.


Multithreading and the GIL
~~~~~~~~~~~~~~~~~~~~~~~~~~

In CPython, the GIL (Global Interpreter Lock) prevents multiple threads from
executing Python code simultaneously. Only when a thread explicitly releases
its lock on the GIL, can a second thread run.

Releasing the GIL is done automatically whenever a Python program makes
blocking calls outside of the interpreter, for example when doing IO.

For ``dogslow`` this means that it can only reliably intercept requests that
are slow because they are doing IO, calling sleep or busy waiting to acquire
locks themselves.

In most cases this is fine. An important cause of slow Django requests is an
expensive database query. Since this is IO, ``dogslow`` can intercept those
fine. A scenario where CPython's GIL is problematic is when the request's
thread hits an infinite loop in Python code (or legitimate Python that is
extremely expensive and takes a long time to execute), never releasing the
GIL. Even though ``dogslow``'s watchdog timer thread does become runnable, it
cannot log the stack.


Co-routines and Greenlets
~~~~~~~~~~~~~~~~~~~~~~~~~

``Dogslow`` is intended for use in a synchronous worker configuration. A
webserver that uses dedicated threads (or single-threaded, dedicated worker
processes) to serve requests. Django's built-in wsgi server does this, as
does ``Gunicorn`` in its default sync-worker mode.

When running with a "co-routines framework" where multiple requests are served
concurrently by one thread, backtraces might become nonsensical.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/intgr/dogslow-sentry",
    "name": "dogslow-sentry",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "django debug watchdog middleware traceback sentry",
    "author": "Marti Raudsepp",
    "author_email": "marti@juffo.org",
    "download_url": "https://files.pythonhosted.org/packages/f6/3c/a4b2571aefe0549ac8475d2222c4c6e8169516bb45d8518390137db4b4e7/dogslow-sentry-2.0.1.tar.gz",
    "platform": null,
    "description": "==============================================\nDogslow-Sentry -- Django Slow Request Watchdog\n==============================================\n\n|PyPI version badge| |Tests status|\n\n.. |PyPI version badge| image:: https://badge.fury.io/py/dogslow-sentry.svg\n   :target: https://pypi.org/project/dogslow-sentry/\n\n.. |Tests status| image:: https://github.com/intgr/dogslow-sentry/workflows/Tests/badge.svg?branch=main\n   :target: https://github.com/intgr/dogslow-sentry/actions?query=workflow:Tests\n\nOverview\n--------\n\nDogslow is a Django watchdog middleware that logs tracebacks of slow\nrequests. Dogslow-sentry requires Python 3.7+, Django 2.2+.\n\nIt started as an `internal project inside Bitbucket`_ to help trace\noperational problems.\n\nIn 2021, the dogslow-sentry fork was created to add Sentry-specific information\nto reports, like full stack trace, request information, fingerprint for issue\ngrouping, breadcrumbs, etc.\n\n.. _internal project inside Bitbucket: http://blog.bitbucket.org/2011/05/17/tracking-slow-requests-with-dogslow/\n\n\nInstallation\n------------\n\nInstall dogslow-sentry::\n\n    $ pip install dogslow-sentry\n\nThen add ``dogslow_sentry.WatchdogMiddleware`` to your Django settings file::\n\n    MIDDLEWARE = [\n        'dogslow_sentry.WatchdogMiddleware',\n        ...\n    ]\n\nFor best results, make it one of the first middlewares that is run.\n\n\nConfiguration\n-------------\n\nNaturally, dogslow-sentry expects a `working Sentry configuration for Django`_.\n\n.. _working Sentry configuration for Django: https://docs.sentry.io/platforms/python/guides/django/\n\nYou can use the following configuration in your ``settings.py``\nfile to tune the watchdog::\n\n    # Watchdog is enabled by default, to temporarily disable, set to False:\n    DOGSLOW = True\n\n    # Log requests taking longer than 25 seconds:\n    DOGSLOW_TIMER = 25\n\n    # Enable logging to Sentry\n    DOGSLOW_SENTRY = True\n\n    # Also log slow request tracebacks to Python logger\n    DOGSLOW_LOGGER = 'dogslow_sentry'\n    DOGSLOW_LOG_LEVEL = 'WARNING'\n\n    # Tuple of url pattern names that should not be monitored:\n    # (defaults to none -- everything monitored)\n    DOGSLOW_IGNORE_URLS = ('some_view', 'other_view')\n\n\nUsage\n-----\n\nEvery incoming HTTP request gets a 25 second timeout in the watchdog. If a\nrequest does not return within that time, the watchdog activates and takes a\npeek at the request thread's stack and writes the backtrace (including all\nlocal stack variables -- Django style) to a log file.\n\nNote that ``dogslow`` only takes a peek at the thread's stack. It does not\ninterrupt the request, or influence it in any other way. Using ``dogslow`` is\ntherefore safe to use in production.\n\n\nChangelog\n---------\n\n2.0.1 (2023-12-08)\n\n* Use ISO 8601-like YYYY-MM-DD date format for email/file logging.\n* Fixed deprecation warnings on Python 3.12 (utcnow function).\n* Enabled CI testing with Python 3.11, 3.12 and Django 5.0, 4.2, 4.1.\n\n2.0.0 (2021-12-13)\n\n* Configured GitHub Actions for CI.\n* Enabled testing with Python 3.10 and Django 4.0.\n* Fixed deprecation warning when using Python 3.10.\n\n2.0.0b1 (2021-07-19)\n\n* Initial pre-release of ``dogslow-sentry`` fork.\n* Improved Sentry integration.\n* Dropped Python 2.7 support, now requires Python 3.7+, Django 2.2+.\n* Many minor tweaks. Reformatted code with Black.\n\n1.2 (2018-01-04)\n\n* Last release of upstream ``dogslow`` package.\n\n\nCaveats\n-------\n\nDogslow uses multithreading. It has a single background thread that handles the\nwatchdog timeouts and takes the tracebacks, so that the original request\nthreads are not interrupted. This has some consequences.\n\n\nMultithreading and the GIL\n~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nIn CPython, the GIL (Global Interpreter Lock) prevents multiple threads from\nexecuting Python code simultaneously. Only when a thread explicitly releases\nits lock on the GIL, can a second thread run.\n\nReleasing the GIL is done automatically whenever a Python program makes\nblocking calls outside of the interpreter, for example when doing IO.\n\nFor ``dogslow`` this means that it can only reliably intercept requests that\nare slow because they are doing IO, calling sleep or busy waiting to acquire\nlocks themselves.\n\nIn most cases this is fine. An important cause of slow Django requests is an\nexpensive database query. Since this is IO, ``dogslow`` can intercept those\nfine. A scenario where CPython's GIL is problematic is when the request's\nthread hits an infinite loop in Python code (or legitimate Python that is\nextremely expensive and takes a long time to execute), never releasing the\nGIL. Even though ``dogslow``'s watchdog timer thread does become runnable, it\ncannot log the stack.\n\n\nCo-routines and Greenlets\n~~~~~~~~~~~~~~~~~~~~~~~~~\n\n``Dogslow`` is intended for use in a synchronous worker configuration. A\nwebserver that uses dedicated threads (or single-threaded, dedicated worker\nprocesses) to serve requests. Django's built-in wsgi server does this, as\ndoes ``Gunicorn`` in its default sync-worker mode.\n\nWhen running with a \"co-routines framework\" where multiple requests are served\nconcurrently by one thread, backtraces might become nonsensical.\n",
    "bugtrack_url": null,
    "license": "GNU LGPL",
    "summary": "A Django middleware that logs tracebacks of slow requests to Sentry.",
    "version": "2.0.1",
    "project_urls": {
        "Homepage": "https://github.com/intgr/dogslow-sentry",
        "Release notes": "https://github.com/intgr/dogslow-sentry/blob/main/README.rst#changelog"
    },
    "split_keywords": [
        "django",
        "debug",
        "watchdog",
        "middleware",
        "traceback",
        "sentry"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1b4039cc034d52092c7b6d3a765942e7a5de9451300f570a71fa5b48a56fa590",
                "md5": "8a29ad463fd1de510bf801170379e106",
                "sha256": "4554986b0f7ab902c14b677c0885ded1c4e1dc83c13ba952096e7167beabb904"
            },
            "downloads": -1,
            "filename": "dogslow_sentry-2.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "8a29ad463fd1de510bf801170379e106",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 18848,
            "upload_time": "2023-12-08T10:55:58",
            "upload_time_iso_8601": "2023-12-08T10:55:58.311993Z",
            "url": "https://files.pythonhosted.org/packages/1b/40/39cc034d52092c7b6d3a765942e7a5de9451300f570a71fa5b48a56fa590/dogslow_sentry-2.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f63ca4b2571aefe0549ac8475d2222c4c6e8169516bb45d8518390137db4b4e7",
                "md5": "c2e556e1b32760e5745bebf3ccff35c5",
                "sha256": "6d518de06a0ec3af5558deca2bc024861bfc5e1344e8441ecd75c0ac57687d21"
            },
            "downloads": -1,
            "filename": "dogslow-sentry-2.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "c2e556e1b32760e5745bebf3ccff35c5",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 20805,
            "upload_time": "2023-12-08T10:56:00",
            "upload_time_iso_8601": "2023-12-08T10:56:00.431530Z",
            "url": "https://files.pythonhosted.org/packages/f6/3c/a4b2571aefe0549ac8475d2222c4c6e8169516bb45d8518390137db4b4e7/dogslow-sentry-2.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-12-08 10:56:00",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "intgr",
    "github_project": "dogslow-sentry",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "dogslow-sentry"
}
        
Elapsed time: 0.25884s