Coder Social home page Coder Social logo

django-request-stats-example's Introduction

This project contains:

  • A reference implementation of Django middleware that allows to track your database queries within a request
    • It is self-sufficient for use in other projects, just drop it in and register.
    • DB_INSTRUMENTATION_ENABLED controls whether DB queries are counted and time is measured, default is True
    • REQUEST_LOGGING_DETAILED_DB_QUERY_DIAGNOSTICS_ACTIVE controls whether detailed query diagnostics with call stacks is enabled, default is False
    • REQUEST_LOGGING_DETAILED_DB_QUERY_DIAGNOSTICS_THRESHOLD controls at what number of repeats the query diagnostrics will be output, default being 0. So for example if you want to only see detailed logs about queries that repeat 5 times or more, set this value to 5.
  • An example "library" app that can be run to demonstrate:
    • how excessive queries can happen
    • how they can be tracked

Instructions:

  • Create a fresh Py 3.9.10 virtual environment (other Python versions can work but not guaranteed)
  • install requirements with pip install -r requirements.txt
  • run migrations python manage.py migrate
  • run the server python manage.py runserver
  • create a superuser python manage.py createsuperuser
  • go to admin panel http://localhost:8000/admin/ and add some authors, books and physical books
  • go to the book list endpoint http://127.0.0.1:8000/library/books/ and observe high level logged request stats
  • by default, detailed logging it turned off via REQUEST_LOGGING_DETAILED_DB_QUERY_DIAGNOSTICS_ACTIVE being False in lib_example/settings.py, switch that to True and rerun the list GET to observe the call stacks being reported.

See this blog post for the full motivation and explanation.

django-request-stats-example's People

Contributors

cb109 avatar contextref avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

cb109

django-request-stats-example's Issues

[NOT AN ISSUE] Question

REQUEST_LOGGING_DETAILED_DB_QUERY_DIAGNOSTICS_THRESHOLD controls above what number of repeats a query will be logged, default is 0

I didn't get this line. what it actually means.

and thanks for making it OS.

another question:-
when we enable detailed query and if that request is doing around 50 DB calls (worst case) then getting information from stack_str stack_str = "".join(traceback.format_stack()) doesn't help much it will be a mess.

removing all the function calls and getting only files name would be good? I'm not sure. what will you suggest?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.