Skip to main content

Shared instrumentation and monitoring code for Django projects

Project description

thelab-instrumentation

Latest Release pipeline status coverage report

A fully type-safe instrumentation and monitoring library for Django projects. This package provides:

  • Metrics collection and reporting to various backends (CloudWatch, Logging)
  • RQ queue monitoring
  • Structured logging helpers for django-structlog
  • Task lifecycle logging for django-tasks and Django 6 native tasks
  • Clean, type-safe API with comprehensive mypy typing

Installation

Install the package using your package manager of choice:

# Using uv (recommended)
uv pip install thelab-instrumentation

# Using pip
pip install thelab-instrumentation

Add the package to your Django INSTALLED_APPS:

INSTALLED_APPS = [
    # ...
    'thelabinstrumentation',
    'thelabinstrumentation.rq',        # Include if using RQ monitoring
    'thelabinstrumentation.structlog',  # Include if using django-structlog
    # ...
]

Optional Dependencies

Install extras for the integrations you need:

# For RQ monitoring
uv pip install 'thelab-instrumentation[rq]'

# For structured logging
uv pip install 'thelab-instrumentation[structlog]'

# For django-tasks lifecycle logging
uv pip install 'thelab-instrumentation[tasks]'

Configuration

Configure the library in your Django settings:

THELAB_INSTRUMENTATION = {
    # Metrics backend (default: logging backend)
    'BACKEND': 'thelabinstrumentation.backends.cloudwatch.CloudWatchBackend',

    # Backend-specific options
    'OPTIONS': {
        # Cloudwatch Backend
        "namespace": 'MyApplication',
    },

    # Update interval in seconds (default: 60)
    'UPDATE_INTERVAL': 60,

    # Global dimensions added to all metrics
    'DIMENSIONS': {
        'Environment': 'production',
        'Application': 'my-app',
    },

    # Request headers to bind to structlog context (header name -> context var name)
    'STRUCTLOG_REQUEST_HEADERS': {
        'x-amz-cf-id': 'cf_id',
        'x-amzn-trace-id': 'x_amzn_trace_id',
    },
}

Structlog Integration

The thelabinstrumentation.structlog app provides:

HeaderBindingMiddleware — Reads configured request headers and binds them to structlog contextvars. Must be placed before django_structlog.middlewares.RequestMiddleware so that bound headers are included in the request_started log event.

QueryStatsMiddleware — Tracks per-request database query count and total query duration, binding them as db_query_count and db_query_duration_ms to structlog contextvars. Must be placed after django_structlog.middlewares.RequestMiddleware so that the stats are bound before request_finished is logged. Uses Django's connection.execute_wrapper() API internally, so it works with any database backend without configuration changes.

MIDDLEWARE = [
    # ...
    'thelabinstrumentation.structlog.middleware.HeaderBindingMiddleware',
    'django_structlog.middlewares.RequestMiddleware',
    'thelabinstrumentation.structlog.db.QueryStatsMiddleware',
    # ...
]

The headers to bind are configured via STRUCTLOG_REQUEST_HEADERS in THELAB_INSTRUMENTATION (see above). Each key is an HTTP header name and each value is the structlog context variable name it maps to.

bind_username — A signal receiver that automatically binds the authenticated user's username to structlog context. It connects to django_structlog.signals.bind_extra_request_metadata when the app is loaded — no manual wiring needed.

Task lifecycle logging — Signal receivers for task_enqueued, task_started, and task_finished that log task metadata to structlog context. Compatible with both django-tasks (backport for Django 5.x) and Django 6's native django.tasks. Connected automatically when the app is loaded and a tasks package is available.

Development

Setup Development Environment

# Clone the repository
git clone https://gitlab.com/thelabnyc/thelab-instrumentation.git
cd thelab-instrumentation

# Install dependencies
uv sync

# Install prek hooks
prek install

Run Tests

# Run all tests
uv run tox

# Run mypy type checking
uv run mypy thelabinstrumentation/

# Run linting
uv run ruff check

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

thelab_instrumentation-0.6.3.tar.gz (82.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

thelab_instrumentation-0.6.3-py3-none-any.whl (39.1 kB view details)

Uploaded Python 3

File details

Details for the file thelab_instrumentation-0.6.3.tar.gz.

File metadata

  • Download URL: thelab_instrumentation-0.6.3.tar.gz
  • Upload date:
  • Size: 82.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for thelab_instrumentation-0.6.3.tar.gz
Algorithm Hash digest
SHA256 bb832f43b2349703b7fa7ae114a2674472b2ca675fa2b9fb24825f4b458fda9d
MD5 e61b3ceac39cd8564c1134964edc77a4
BLAKE2b-256 d4d6e47877a89a9774d26406a2a89652a0553caa1680996f0ee1391e341b4c02

See more details on using hashes here.

Provenance

The following attestation bundles were made for thelab_instrumentation-0.6.3.tar.gz:

Publisher: .gitlab-ci.yml on thelabnyc/thelab-instrumentation

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file thelab_instrumentation-0.6.3-py3-none-any.whl.

File metadata

File hashes

Hashes for thelab_instrumentation-0.6.3-py3-none-any.whl
Algorithm Hash digest
SHA256 693c15fe13984fd4ef87912a507a2f7acac3f776e5c8d2911762cfb2e6cdb86a
MD5 1984599a13f03384c01b4c378ef344cc
BLAKE2b-256 1291e64361da54ed8effa82fca4d80afb0a1b28c63fba0d9160aacde466bd0b2

See more details on using hashes here.

Provenance

The following attestation bundles were made for thelab_instrumentation-0.6.3-py3-none-any.whl:

Publisher: .gitlab-ci.yml on thelabnyc/thelab-instrumentation

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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