Skip to main content

mitol-django-observability

MIT Open Learning Django plugin for OpenTelemetry tracing, structured logging (structlog), and alerting-as-code.

Installation

Add to INSTALLED_APPS:

"mitol.observability.apps.ObservabilityConfig",

See the RFC for full documentation.

Celery integration

To propagate structured log context (request ID, user ID, …) from web workers into Celery tasks and ensure Celery workers emit JSON logs through the same structlog pipeline, install the celery extra and follow the steps below.

1. Install the extra

pip install "mitol-django-observability[celery]"

This adds django-structlog[celery] (context propagation) and opentelemetry-instrumentation-celery (OTel task spans, auto-discovered).

2. Django settings

MIDDLEWARE = [
    # …
    "django_structlog.middlewares.RequestMiddleware",
]

DJANGO_STRUCTLOG_CELERY_ENABLED = True

3. Celery application

from celery import Celery
from celery.signals import setup_logging
from django_structlog.celery.steps import DjangoStructLogInitStep

from mitol.observability.celery import setup_celery_logging

app = Celery("yourproject")

@setup_logging.connect
def on_setup_logging(**kwargs):
    # Prevent Celery from overriding structlog's logging config in workers.
    setup_celery_logging(**kwargs)

# Propagate web-request context into task execution.
app.steps["worker"].add(DjangoStructLogInitStep)

How it works

  • setup_celery_logging calls configure_structlog(force=True), re-applying the structlog pipeline after Celery resets logging during worker boot.
  • DjangoStructLogInitStep installs signal handlers that bind the request context (captured by RequestMiddleware) to the structlog context vars before each task runs and clears it after.
  • opentelemetry-instrumentation-celery is auto-discovered via the opentelemetry_instrumentor entry-point group — no extra configuration required.

Postgres integration

To emit OTel spans for database queries, install the postgres extra:

pip install "mitol-django-observability[postgres]"

This adds two things:

  • opentelemetry-instrumentation-psycopg, auto-discovered via the opentelemetry_instrumentor entry-point group — no extra configuration required.
  • psycopg[c], the compiled binding, which is the implementation you want in production. The section below explains why the extra decides this on your behalf.

Build prerequisites

psycopg[c] is compiled from source at install time, so the build environment needs a C compiler, Python development headers, libpq-dev, and pg_config on PATH. Images built on mitodl/ol-python-base already provide all four.

CI runners generally do not. GitHub's ubuntu-24.04 image, for instance, ships PostgreSQL but not libpq-dev, so add it to your Aptfile (or equivalent) alongside the other -dev packages:

libpq-dev

If you are in an environment that genuinely cannot compile, install psycopg[binary] explicitly in your application — a pre-compiled wheel that bundles its own libpq and libssl. Those bundled libraries do not receive OS security updates, which is why it is not the default here.

Why the extra decides the driver

The psycopg instrumentation targets psycopg 3, so installing this extra puts psycopg 3 in your environment. That has a consequence which is easy to miss: it changes which driver Django uses. Django's Postgres backend prefers psycopg 3 over psycopg2 whenever psycopg 3 is importable (django/db/backends/postgresql/base.py):

try:
    try:
        import psycopg as Database
    except ImportError:
        import psycopg2 as Database

An application running on psycopg2 therefore switches to psycopg 3 the moment this instrumentation is installed, with no change to its own code and nothing in its dependency diff that names a driver.

Since the extra is already making that decision, it specifies the implementation too. psycopg 3 ships three interchangeable bindings to libpq, and a bare psycopg dependency selects the slowest:

dependency binding notes
psycopg pure Python, via ctypes upstream calls it "much slower"; intended for local development and small tasks
psycopg[c] compiled, linked against the system libpq psycopg's recommendation for production — system libpq/libssl upgrades carry through
psycopg[binary] compiled, bundles its own libpq/libssl no build tools needed, but the bundled libraries do not receive OS security updates

Extras union across dependency resolution, so requesting psycopg[c] here installs the compiled binding regardless of how the application declares psycopg itself.

Check what a deployed process actually resolved to — psycopg selects the best available implementation at import, so this reflects what is installed rather than what was requested:

>>> import psycopg
>>> psycopg.pq.__impl__
'c'

See psycopg's installation guide and pq module implementations for the full comparison.

Metadata

Release files for mitol-django-observability 2026.10.30

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

Source distribution (sdist)

Source distribution for mitol-django-observability 2026.10.30
File Size Uploaded Provenance Actions
mitol_django_observability-2026.10.30.tar.gz 19.6 kB With provenance Details for mitol_django_observability-2026.10.30.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mitol-django-observability 2026.10.30
File Interpreter ABI Platform Actions
mitol_django_observability-2026.10.30-py3-none-any.whl Python 3 none any Details for mitol_django_observability-2026.10.30-py3-none-any.whl

Total release size: 47.8 kB

Release files / mitol_django_observability-2026.10.30.tar.gz

Download URL mitol_django_observability-2026.10.30.tar.gz
Size 19.6 kB
Tags Source
SHA-256 checksum
How to use checksums
b479217034b5af7a5de3735f2267a96ed95adf24ca5c9f231ec4dc18ebca99b8
BLAKE2b-256 checksum
How to use checksums
9adce086f00449f1cbce42ad0669c83e288561c1879917ebd8549932868f4177
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 30, 2026.

Transparency log

Release files / mitol_django_observability-2026.10.30-py3-none-any.whl

Download URL mitol_django_observability-2026.10.30-py3-none-any.whl
Size 28.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
18ef6adabb5d437b007dfe0e4303552ff89043f6d1ecd1fb429cfa1fcfbad10e
BLAKE2b-256 checksum
How to use checksums
97f2371b074129550f08f952884fe86830acf62d74202c60e998a8cc2513d2a9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 30, 2026.

Transparency log

Security for mitol-django-observability 2026.10.30

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for mitol_django_observability-2026.10.30.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 30, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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