Skip to main content

Zero-dependency observability SDK for Python and Django

Project description

your-org-observability

Zero-dependency observability SDK for Python and Django. Provides structured JSON logging, automatic request/response tracking, and health check endpoints.

Features

  • Zero dependencies - Only uses Python standard library
  • Structured logging - All logs are valid JSON matching a shared schema
  • Django integration - Automatic request/response logging middleware
  • Error handling - Django signal-based exception handler
  • Health checks - Pre-built /health, /health/live, /health/ready endpoints
  • Type hints - Full mypy strict compliance

Installation

pip install your-org-observability

For Django support, optionally install Django (usually already in your project):

pip install django

Quick Start

Basic Logger

from your_org_observability import logger

logger.info("Server started on port 8000")
logger.warn("Running low on memory")
logger.error("Database connection failed")

With Django

Add to your settings.py:

INSTALLED_APPS = [
    # ...
    'your_org_observability',
]

MIDDLEWARE = [
    # ...
    'your_org_observability.ObservabilityMiddleware',
]

In your views.py:

from django.http import JsonResponse
from your_org_observability import logger

def get_users(request):
    logger.info("Fetching users")
    return JsonResponse({"users": []})

In your urls.py:

from django.urls import path, include
from your_org_observability.health_check import get_health_urls

urlpatterns = [
    # ... your other patterns
    *get_health_urls("my-api"),
]

Configuration

Environment Variables

  • OBSERVABILITY_SERVICE - Service name (defaults to 'unknown-service')
  • OBSERVABILITY_ENV - Environment (defaults to DJANGO_ENV or 'development')
export OBSERVABILITY_SERVICE=my-api
export OBSERVABILITY_ENV=production
python manage.py runserver

Custom Logger Instance

from your_org_observability import ObservabilityLogger

logger = ObservabilityLogger({
    "service": "my-api",
    "environment": "production",
    "skip_routes": ["/health", "/metrics"],
})

logger.info("Custom logger initialized")

Middleware

ObservabilityMiddleware

Automatically logs all HTTP requests and responses with:

  • UUID4 request ID (attached to X-Request-Id header)
  • Request duration
  • HTTP method, route, status code
  • User ID (if request.user.is_authenticated)

Log level is determined by status code:

  • 2xx → info
  • 3xx → info
  • 4xx → warn
  • 5xx → error

Error Handling

Django Signal Handler

The error capturer automatically registers via Django signals when the middleware is installed.

For non-Django usage:

from your_org_observability import logger, setup_error_handlers

setup_error_handlers(logger)

Health Checks

Django Views

Add health check endpoints to your urls.py:

from your_org_observability.health_check import get_health_urls

urlpatterns = [
    # ... your other patterns
    *get_health_urls("my-api"),  # Adds /health, /health/live, /health/ready
]

With optional readiness check:

def check_db_ready():
    # Check if database is connected
    from django.db import connection
    try:
        connection.ensure_connection()
        return True
    except Exception:
        return False

urlpatterns = [
    *get_health_urls("my-api", readiness_check=check_db_ready),
]

Endpoints

  • GET /health/ - Full health check

    { "status": "ok", "service": "my-api", "uptime": 12345, "timestamp": "2026-04-08T10:30:45.123Z" }
    
  • GET /health/live/ - Liveness probe (always 200)

    { "alive": true }
    
  • GET /health/ready/ - Readiness probe (can be customized)

    { "ready": true }
    

Log Schema

Every log line is structured JSON with these fields:

{
  "level": "info",
  "msg": "user logged in",
  "service": "my-api",
  "environment": "production",
  "ts": "2026-04-08T10:30:45.123Z",
  "request_id": "f47ac10b-58cc-4372-a567-0e02b2c3d479",
  "user_id": "user123",
  "duration_ms": 45,
  "status_code": 200,
  "route": "/api/login/",
  "error": null,
  "traceback": null
}

This schema is identical across all your-org-observability SDKs (Node.js, Python, .NET).

Logging API

Methods

All logging methods accept keyword arguments that override the base schema:

logger.info(
    "User login",
    user_id="user123",
    route="/api/login/",
    duration_ms=45,
)

Levels

  • debug() - Debug-level log
  • info() - Info-level log
  • warn() - Warning-level log
  • error() - Error-level log
  • critical() - Critical-level log

Error Context

When called within an exception handler, error() and critical() automatically capture the exception:

try:
    vulnerable_operation()
except Exception:
    logger.error("Operation failed")  # Automatically includes traceback

Type Hints

Full mypy strict compliance:

mypy your_org_observability --strict

License

MIT

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

your_org_observability-1.0.1.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

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

your_org_observability-1.0.1-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file your_org_observability-1.0.1.tar.gz.

File metadata

  • Download URL: your_org_observability-1.0.1.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for your_org_observability-1.0.1.tar.gz
Algorithm Hash digest
SHA256 983ed5893343507b780c41158807d66b786df1ee48f438a44f79005d01bef373
MD5 90e4fe53d2e396f5065315e6696b5ba9
BLAKE2b-256 bb852a82a5798843da60cf229c5f135335870065124f80e3ff0c0ce604bef43a

See more details on using hashes here.

File details

Details for the file your_org_observability-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for your_org_observability-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 18c00184e577407f2c068e8c043f86004d162834f57c5bc04eadec665ae261aa
MD5 e598ffe53d3b5c4763576fd7a7b86554
BLAKE2b-256 54271f22173aa4c3e396c1079ffecf49851bbfe81854a60e61687f4907ae41da

See more details on using hashes here.

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