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.0.tar.gz (80.5 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.0-py3-none-any.whl (38.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for thelab_instrumentation-0.6.0.tar.gz
Algorithm Hash digest
SHA256 e5cb1b4211899a5751e8adfc9dbb6b511a479edc00264c543d036c6f0cd12d93
MD5 c7607f6d15fe63cb9d38069cf903820f
BLAKE2b-256 e935668a08db2adaae9e0b847ad5590c00ae7ad0c8b520af6c4028667ec67cee

See more details on using hashes here.

Provenance

The following attestation bundles were made for thelab_instrumentation-0.6.0.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.0-py3-none-any.whl.

File metadata

File hashes

Hashes for thelab_instrumentation-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4e7422b2a4613771f88c983a3fcd7c0be54573e24e1e4dfb26478e9f1e08f7d1
MD5 2c0dea347e45bc0ebcd674f9f0810c26
BLAKE2b-256 d2d0d367578583c81227dfc7839fb74a485627df4891698e6c655b7c89ba4573

See more details on using hashes here.

Provenance

The following attestation bundles were made for thelab_instrumentation-0.6.0-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