Opened 7 years ago

Closed 6 years ago

#28595 closed New feature (fixed)

Database "execute hooks" for instrumentation

Reported by: Shai Berger Owned by: Shai Berger
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Adam Johnson, Carl Meyer Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Quoting, editing and summarizing a 5-month old thread from django-developers:

Add the ability to install general instrumentation hooks around the database execute() and executemany() calls.

Such hooks would allow all sorts of interesting features. For one, they could replace the current special-case allowing assertNumQueries() & friends to record queries out of debug mode (it's an ugly hack, really), but they could also support other use cases:

  • Prevent database access during execution of render() calls and serialization, to make sure all database access is being done explicitly in the view.
  • Preventing database access during tests unless specifically requested by the test (per @carljm, pytest-django does this, currently via monkeypatching).
  • Listen in on all executed queries for performance analysis (per @adamchainz, django-perf-rec currently monkey patches connection.ops.last_executed_query to achieve this)

Change History (5)

comment:1 by Shai Berger, 7 years ago

Needs documentation: unset
Needs tests: unset

(I was a little confused about the meaning of the "needs tests" and "needs docs" flags)

comment:2 by Tim Graham <timograham@…>, 7 years ago

In d612026c:

Refs #28595 -- Added a hook to add execute wrappers for database queries.

Thanks Adam Johnson, Carl Meyer, Anssi Kääriäinen, Mariusz Felisiak,
Michael Manfre, and Tim Graham for discussion and review.

comment:3 by Tim Graham, 7 years ago

Has patch: unset

comment:4 by Raphael Gaschignard, 6 years ago

I might be missing something here, so I am not changing the state myself. But I believe this instrumentation has been successfully added in 2.0? As found at https://docs.djangoproject.com/en/2.2/topics/db/instrumentation/

comment:5 by Adam Johnson, 6 years ago

Resolution: fixed
Status: assignedclosed

Thanks Raphael, yes this feature was merged in 2.0, the ticket was just left in limbo as open. Closing now.

Note: See TracTickets for help on using tickets.
Back to Top