Skip to main content

Pellet

Pellet helps improve your Django app performance by discovering N+1 queries.

The Django ORM makes it easy to forget using select_related and prefetch_related correctly and can accidentally cause N+1 queries to happen.

Pellet ultimately aims to recreate Bullet for Django.

Installing Pellet

pip install pellet

Enabling Pellet

  1. Add pellet.middleware.PelletMiddleware to your django middleware list.
  2. Configure pellet behaviour by using the PELLET variable in your django settings file.

Configuring Pellet

You can configure pellet by setting the PELLET variable in your django settings file. The default values used by pellet if this variable or any field in the object is not found is as follows:

PELLET = {
    # Enable/Disable pellet
    # If set to False the pellet
    # middleware does nothing
    "enabled": False,

    # Enable this if you want count and time
    # metrics at a query level
    "query_level_metrics_enabled": False,

    # Settings related to response headers
    # set by pellet
    "headers": {

        # Enables setting response headers
        "enabled": False,

        # Header to be used for setting total query count
        "query_count_header": "X-Pellet-Count",

        # Header to be used for setting total query time
        "query_time_header": "X-Pellet-Time"
    },

    # Settings related to pellet debug mode
    "debug": {

        # Enable debug mode
        # Don't enable on prod as it will slow down your app
        "enabled": False,

        # Query count thresholds which will
        # be used by pellet to report metrics
        # on the console
        "count_threshold": {

            # Min number of times a query should happen
            # for it to be classified as N+1
            # Queries with less count than this will
            # not show up in the debug table
            "min": 2,

            # Max number of times a query should happen
            # for it to be classified as a low impact
            # performance issue
            "low": 5,

            # Max number of times a query should happen
            # for it to be classified as a warning impact
            # performance issue
            # Every query happening more times than this
            # is classified as a high impact performance issue
            "medium": 10
        }
    },

    # Path to a callback function which will be called
    # with the request, response and
    # pellet metrics object
    "callback": None
}

Callback function

The callback function should accept three arguments:

  1. request -> django request object
  2. response -> django response object
  3. pellet_metrics -> dict containing metrics collected by pellet

Example functionality:

  1. collect and send api call level pellet metrics to an external service like datadog
  2. make integration tests fail for an api if too many queries are happening by raising an exception
  3. send alert emails, slack messages, etc on too many queries

Steps:

  1. Create a callback function:
# app/user/callbacks.py

from pellet.utils import get_sanitised_path

def write_datadog_metrics(path, metrics):
    # Writes metrics to datadog
    pass

def pellet_callback(request, response, pellet_metrics):
    # Get id stripped path
    # eg: /api/user/1/ -> /api/user/_id_/
    sanitised_path = get_sanitised_path(request.path)
    write_datadog_metrics(sanitised_path, pellet_metrics)
  1. Specify the callback function in the pellet config object.
PELLET = {
    # ..... rest of pellet config
    "callback": "app.user.callbacks.pellet_callback"
}

Metadata

Release files for pellet 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pellet 0.1.3
File Size Uploaded
pellet-0.1.3.tar.gz 5.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pellet 0.1.3
File Interpreter ABI Platform
pellet-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 11.8 kB

Release files / pellet-0.1.3.tar.gz

Download URL pellet-0.1.3.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
fbbb2e6eee7791138d8bc2854cda1badd46c6c829e97b91950acfcb6fea34aea
BLAKE2b-256 checksum
How to use checksums
03a2d8d8950c00d3996f4c00060fdcd206b32d6bf65b4cb15ab4acaddce0155b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.14 CPython/3.10.1 Darwin/21.4.0

Release files / pellet-0.1.3-py3-none-any.whl

Download URL pellet-0.1.3-py3-none-any.whl
Size 5.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4d1a3e46422493e6345e5b36d2d4017be214b88f29c8242c1ec0970efcc13144
BLAKE2b-256 checksum
How to use checksums
9eb555f08076afbd17ac873f894b130855dcd94ba2006f6c56e4772a9d45812b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.14 CPython/3.10.1 Darwin/21.4.0

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page