I have an API created using django rest framework in a Linode server. Now, I want to check the number and the response code of each request, I want to get stats for my api. How can I do it? thankyou so much.
DRF Tracking is utilities to track requests to DRF API views, it may be good fit for you:
install: pip install drf-tracking
apply migrations: python manage.py migrate
add the following to you API views:
from rest_framework import generics
from rest_framework_tracking.mixins import LoggingMixin
class LoggingView(LoggingMixin, generics.GenericAPIView):
def get(self, request):
return Response('with logging')
There is also an other alternative Django Analytics if you want to have more than choice.
So the simplest way to get started is to check your webserver's access logs. That should give you the number of requests in and responses out, including status code. If you want more feature-full statistics as well as monitoring and alerting, you may want to look into something like NewRelic.
Apitally may also be a good fit here.
It comes with a middleware for Django REST Framework (DRF) that captures request and response metadata and provides a simple dashboard with various API metrics & insights, including number of requests, error rates, response times, payload sizes, etc.
There is a specific setup guide for Django REST Framework that you can follow to get started. The basic steps are:
- Create app in Apitally dashboard to get a client ID
- Install client library in your DRF project:
pip install "apitally[django_rest_framework]"
- Add Apitally middleware in your Django settings:
MIDDLEWARE = [
"apitally.django.ApitallyMiddleware",
# Other middleware ...
]
APITALLY_MIDDLEWARE = {
"client_id": "your-client-id",
"env": "dev", # or "prod" etc.
}
- Deploy your application and Apitally starts tracking your API stats.
The dashboard looks like this:
Disclaimer: I'm the author of Apitally.
DRF Tracking library on the accepted answer is dead.
Its fork is available here drf-api-tracking.
Another library that is maintained by Jazzband is django-silk.
It defines itself as
Silky smooth profiling for Django: a live profiling and inspection tool for the Django framework. Silk intercepts and stores HTTP requests and database queries before presenting them in a user interface for further inspection
image source: jazzband/django-silk github repository
© 2022 - 2025 — McMap. All rights reserved.