Django SDK
APIToolkit Django SDK is a middleware that can be used to monitor HTTP requests. It is provides additional functionalities on top of the open telemetry instrumentation which creates a custom span for each request capturing details about the request including request, response bodies errors and outgoing requests.
Table of Contents
Installation
Kindly run the command below to install the apitoolkit django sdks and necessary opentelemetry packages:
pip install apitoolkit-django opentelemetry-distro opentelemetry-exporter-otlp
# then install the bootstrap script
opentelemetry-bootstrap -a install
Setup Open Telemetry
Setting up open telemetry allows you to send traces, metrics and logs to the APIToolkit platform. To setup open telemetry, you need to configure the following environment variables:
export OTEL_EXPORTER_OTLP_ENDPOINT="http://otelcol.apitoolkit.io:4317"
export OTEL_SERVICE_NAME="my-service" # Specifies the name of the service.
export OTEL_RESOURCE_ATTRIBUTES="at-project-key={ENTER_YOUR_API_KEY_HERE}" # Adds your API KEY to the resource.
export OTEL_EXPORTER_OTLP_PROTOCOL="grpc" # Specifies the protocol to use for the OpenTelemetry exporter.
export DJANGO_SETTINGS_MODULE="{YOUR_DJANGO_SERVER_NAME}.settings" # Specifies the Django settings module to use.
Then run the command below to start your server with opentelemetry instrumented:
opentelemetry-instrument python3 manage.py runserver --noreload
APItoolkit Django Configuration
After setting up open telemetry, you can now configure the apitoolkit django middleware.
By adding the following APItoolkit variables to your Django settings (settings.py) file:
from pathlib import Path
from dotenv import load_dotenv
import os
load_dotenv()
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
APITOOLKIT_SERVICE_NAME= "my-service"
APITOOLKIT_CAPTURE_REQUEST_BODY = True
APITOOLKIT_CAPTURE_RESPONSE_BODY = True
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.sessions',
]
...
Then add the apitoolkit_django.APIToolkit middleware into the settings.py middleware list, like so:
MIDDLEWARE = [
'apitoolkit_django.APIToolkit', # Initialize APItoolkit
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
...,
]
[!NOTE]
The
{ENTER_YOUR_API_KEY_HERE}demo string should be replaced with the API key generated from the APItoolkit dashboard.The
{YOUR_DJANGO_SERVER_NAME}demo string should be replaced with the name of your Django server.
[!IMPORTANT]
To learn more configuration options (redacting fields, error reporting, outgoing requests, etc.), please read this SDK documentation.
Contributing and Help
To contribute to the development of this SDK or request help from the community and our team, kindly do any of the following:
- Read our Contributors Guide.
- Join our community Discord Server.
- Create a new issue in this repository.
License
This repository is published under the MIT license.
Release files for apitoolkit-django 2.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| apitoolkit_django-2.0.1.tar.gz | 4.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| apitoolkit_django-2.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.8 kB
Release files / apitoolkit_django-2.0.1.tar.gz
| Download URL | apitoolkit_django-2.0.1.tar.gz |
|---|---|
| Size | 4.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5413e2f23f228d7bbe036cd394da42aa658582060f7a3613b5cc5b485a28df89
|
|
BLAKE2b-256 checksum How to use checksums |
cba4936de1719c22452597ebb7494a288f731d9556ffc45a48cd0b237609fd5f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.12.3
|
Release files / apitoolkit_django-2.0.1-py3-none-any.whl
| Download URL | apitoolkit_django-2.0.1-py3-none-any.whl |
|---|---|
| Size | 4.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3e8f0f410444a98cf19e45c18563481fd3ee4734928a04934f7674d1ac0eb60c
|
|
BLAKE2b-256 checksum How to use checksums |
79cb3a4c22310b9b8029e8ee30ec6f44ac32cad36943a349979fa94d661da1f0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.12.3
|