Monitoring django rest framework api on production server
Asked Answered
S

5

11

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.

Saltillo answered 14/12, 2015 at 19:50 Comment(0)
S
15

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.

Sundown answered 14/12, 2015 at 22:37 Comment(2)
Hi, thanks you but, I'm using mongodb then, can I get trouble with migrations?Saltillo
In this case I recommend to use Django-norel a django support on non-relational databases.Sundown
S
5

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.

Stagecraft answered 14/12, 2015 at 19:55 Comment(1)
Think this aproach with combined with splunk.Pretty
B
2

maybe you could use drf-tracking

Barrack answered 14/12, 2015 at 20:47 Comment(0)
R
0

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:

  1. Create app in Apitally dashboard to get a client ID
  2. Install client library in your DRF project:
pip install "apitally[django_rest_framework]"
  1. 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.
}
  1. Deploy your application and Apitally starts tracking your API stats.

The dashboard looks like this:

enter image description here

Disclaimer: I'm the author of Apitally.

Rhoea answered 5/4, 2024 at 12:49 Comment(0)
A
0

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

silk image

image source: jazzband/django-silk github repository

Autohypnosis answered 5/4, 2024 at 13:20 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.