Datadog Async Handler
A modern, high-performance Python logging handler that sends logs directly to Datadog via HTTP API with asynchronous batching, retry logic, and comprehensive error handling.
✨ Features
- 🚀 High Performance: Asynchronous batching and background processing
- 🔄 Reliable Delivery: Automatic retry with exponential backoff
- 📊 Batching: Configurable batch size and flush intervals
- 🏷️ Rich Metadata: Automatic service, environment, and custom tag support
- 🔧 Easy Integration: Drop-in replacement for standard logging handlers
- 🌐 Multi-Site Support: Works with all Datadog sites (US, EU, etc.)
- 📝 Type Safe: Full type hints and mypy compatibility
- ⚡ Modern: Built with Python 3.9+ and latest best practices
🚀 Quick Start
Installation
pip install datadog-async-handler
Basic Usage
import logging
from datadog_http_handler import DatadogHTTPHandler
# Configure the handler
handler = DatadogHTTPHandler(
api_key="your-datadog-api-key", # or set DD_API_KEY env var
service="my-application",
source="python",
tags="env:production,team:backend"
)
# Set up logging
logger = logging.getLogger(__name__)
logger.addHandler(handler)
logger.setLevel(logging.INFO)
# Start logging!
logger.info("Application started successfully", extra={
"user_id": "12345",
"action": "startup"
})
Environment Variables
The handler automatically picks up standard Datadog environment variables:
export DD_API_KEY="your-api-key"
export DD_SERVICE="my-application"
export DD_ENV="production"
export DD_VERSION="1.2.3"
export DD_TAGS="team:backend,component:api"
export DD_SITE="datadoghq.com" # or datadoghq.eu, ddog-gov.com, etc.
📖 Documentation
Configuration Options
| Parameter | Type | Default | Description |
|---|---|---|---|
api_key |
str |
None |
Datadog API key (required) |
site |
str |
"datadoghq.com" |
Datadog site |
service |
str |
None |
Service name |
source |
str |
"python" |
Log source |
hostname |
str |
None |
Hostname |
tags |
str |
None |
Comma-separated tags |
batch_size |
int |
10 |
Number of logs per batch |
flush_interval_seconds |
float |
5.0 |
Batch flush interval |
timeout_seconds |
float |
10.0 |
Request timeout |
max_retries |
int |
3 |
Maximum retry attempts |
Framework Integration Examples
Django
# settings.py
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'datadog': {
'class': 'datadog_http_handler.DatadogHTTPHandler',
'api_key': 'your-api-key',
'service': 'django-app',
'source': 'django',
},
},
'root': {
'handlers': ['datadog'],
'level': 'INFO',
},
}
FastAPI
import logging
from fastapi import FastAPI
from datadog_http_handler import DatadogHTTPHandler
app = FastAPI()
# Configure logging
handler = DatadogHTTPHandler(service="fastapi-app", source="fastapi")
logging.getLogger().addHandler(handler)
logging.getLogger().setLevel(logging.INFO)
@app.get("/")
async def root():
logging.info("API endpoint called", extra={"endpoint": "/"})
return {"message": "Hello World"}
Flask
import logging
from flask import Flask
from datadog_http_handler import DatadogHTTPHandler
app = Flask(__name__)
# Configure logging
handler = DatadogHTTPHandler(service="flask-app", source="flask")
app.logger.addHandler(handler)
app.logger.setLevel(logging.INFO)
@app.route("/")
def hello():
app.logger.info("Flask endpoint called", extra={"endpoint": "/"})
return "Hello World!"
🔧 Development
Setup
# Clone the repository
git clone https://github.com/enlyft/datadog-http-handler.git
cd datadog-http-handler
# Install UV (modern Python package manager)
curl -LsSf https://astral.sh/uv/install.sh | sh
# Install dependencies
uv pip install -e ".[dev]"
# Install pre-commit hooks
pre-commit install
Running Tests
# Run all tests
hatch run test
# Run with coverage
hatch run test-cov
# Run specific tests
hatch run test tests/test_handler.py::test_basic_logging
Code Quality
# Format code
hatch run format
# Lint code
hatch run lint
# Type checking
hatch run type-check
# Run all checks
hatch run all
🤝 Contributing
We welcome contributions! Please see our Contributing Guide for details.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Run the test suite
- Submit a pull request
📋 Requirements
- Python 3.9+
datadog-api-client>=2.0.0
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🔗 Links
🆚 Comparison with Other Solutions
| Feature | datadog-async-handler | datadog-http-handler | python-datadog | datadog-logger |
|---|---|---|---|---|
| Async Batching | ✅ | ❌ | ❌ | ❌ |
| Retry Logic | ✅ | ❌ | ❌ | ❌ |
| Type Hints | ✅ | ❌ | ❌ | ❌ |
| Modern Python | ✅ (3.9+) | ❌ (3.6+) | ❌ (2.7+) | ❌ (3.6+) |
| Official API Client | ✅ | ❌ | ❌ | ❌ |
| Background Processing | ✅ | ❌ | ❌ | ❌ |
| Memory Efficient | ✅ | ❌ | ❌ | ❌ |
| Active Maintenance | ✅ | ❌ (2019) | ✅ | ❌ |
| Comprehensive Tests | ✅ | ❌ | ✅ | ❌ |
Made with ❤️ for the Python and Datadog communities.
Metadata
Release files for datadog-async-handler 0.1.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| datadog_async_handler-0.1.4.tar.gz | 120.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| datadog_async_handler-0.1.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 131.9 kB
Release files / datadog_async_handler-0.1.4.tar.gz
| Download URL | datadog_async_handler-0.1.4.tar.gz |
|---|---|
| Size | 120.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3e3251c1ebf9dd9444982910ee1bc0089d2712c5f9d9ac55d8cfb95112fa6b81
|
|
BLAKE2b-256 checksum How to use checksums |
665f04877fd76bfa015fc25daf5da5961ef0d3267e002ff529c233edcdb6e921
|
| 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 Sep 2, 2026.
Transparency logRelease files / datadog_async_handler-0.1.4-py3-none-any.whl
| Download URL | datadog_async_handler-0.1.4-py3-none-any.whl |
|---|---|
| Size | 11.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d74a4e5db85a0c6517b44e9923d46883816ed3cf0081e62f6f38da578796a5be
|
|
BLAKE2b-256 checksum How to use checksums |
481356b117b8974fe913a74d11511b44dfb489e076f02ab8c6dfd02c66312e5c
|
| 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 Sep 2, 2026.
Transparency log