Django is a web application framework that many developers use to serve APIs.
Moesif is an API analytics and monitoring platform. moesifdjango is a middleware that makes integration with Moesif easy for Django based applications.
This is an example of django application with Moesif integrated. This example is based on the quick start tutorials of Django and Django rest framework.
In this example. dd trace uses a console writer so that all the datadog captured data is printed to console. But feel free to modify. In manage.py
:
from ddtrace import patch_all, tracer
from custom_writer import ConsoleWriter
patch_all()
# Set the custom writer to log traces to the console for easier debugging
# but you can use any writer you want after verify writing to console works.
tracer.configure(writer=ConsoleWriter())
moesifdjango's github readme already documented the steps for setup Moesif Django. But here is the key file where the Moesif integration is added:
jandog/settings.py
added Moesif middleware related settings.
-
Setup virtual env if needed
python -m venv ENV
. Start the virtual env bysource ENV/bin/activate
-
Install packages:
pip install -r requirements.txt
- Add Environment Variables.
copy over .env-template
to .env
and fill in your Datadog Configuration and Moesif Application Id
DD_AGENT_HOST=
DD_API_KEY=your_api_key_here
DD_TRACE_AGENT_PORT=
DD_TRACE_ENABLED=true
DD_TRACE_DEBUG=true
DD_SERVICE=python-django-example
MOESIF_APPLICATION_ID=
Your Moesif Collector Application Id can be found in the Moesif Portal. After signing up for a Moesif account, your Moesif Application Id will be displayed during the onboarding steps.
You can always find your Moesif Collector Application Id at any time by logging into the Moesif Portal, click on the bottom left user profile, and then clicking API Keys.
- Run the service:
ddtrace-run python manage.py runserver
If you prefer not to use ddtrace-run
, you can also use patch_all()
in code.
Note : If you get OperationalError
with Exception Value: no such table
, that means the schema has not been generated in database yet.
please run the following command line to resolve
python manage.py migrate
- See
urls.py
for some urls that you can hit the server with (e.g.http://localhost:8000/api/todos
), and the data should be captured in the corresponding Moesif account of the application id.
curl --request GET \
--url http://127.0.0.1:8000/api/todos/ \
--header 'User-Agent: insomnia/10.2.0'
curl --request POST \
--url http://127.0.0.1:8000/api/todos/ \
--header 'Content-Type: application/json' \
--header 'User-Agent: insomnia/10.2.0' \
--data ' {"title": "buy stuff", "description": "apples, oranges", "completed": false}'