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.4.tar.gz (82.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.4-py3-none-any.whl (39.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: thelab_instrumentation-0.6.4.tar.gz
  • Upload date:
  • Size: 82.5 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.4.tar.gz
Algorithm Hash digest
SHA256 532c4268dfc0fcc989aa532dc11940c99410300bfe5eb758dcf8d431562b4ecd
MD5 f63d8d8b9ced55965af53277d70fce27
BLAKE2b-256 d13a5c2cc7b7ef1f68b3e755c965e0ebdb1efa29bf80e3985a8b7f57aeea8c73

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for thelab_instrumentation-0.6.4-py3-none-any.whl
Algorithm Hash digest
SHA256 ffbfc56c3aabdfd6f9c8ac9cdf18857abfe3c5a00af4e1cb73cd391d10c9aecf
MD5 1dd7184de1cd53be9afd4555c04281cf
BLAKE2b-256 4e56e6149c99b3b9ca37392cbb32274214ba22787b5a6aaaa5384f2fbf3cb199

See more details on using hashes here.

Provenance

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