Useful django patch for large project
Project description
Django Patchy
Useful django app and utils to develop large-scale web app
Requirements
Python (2.7)
Django (1.8, 1.9)
Installation
Install using pip…
pip install djangopatchy
Middlewares
LongRequestMiddleware
Let’s add a middleware to calculate the time elapse for each request
Add a middleware at the top:
MIDDLEWARE_CLASSES = (
'patchy.middleware.LongRequestMiddleware',
...
)
Add logger handler with pathcy.middleware:
Example:
'patchy.middleware': {
'handlers': ['sentry'],
'level': 'ERROR',
'propagate': True
}
Set the timeout threshold in settings(default to 1 second):
PATCHY_LONG_REQUEST_TIMEOUT = 2 # set the timeout to 2 seconds
Results:
For each web request, it has a header variable X-ELAPSED in seconds to indicate the time elapse.
X-ELAPSED: 0.005 # it means the request costs 5 ms
If it exceeds the PATCHY_LONG_REQUEST_TIMEOUT a error log message will be sent.
Add some urls to ignore list(optional), regular expression is supported.
PATCHY_LONG_REQUEST_IGNORE_URLS = [
r'^/_admin/.*$',
]
Utilities
long_sql_execute_wrapper
Let us rewrite the CursorWrapper.execute to calculate the sql process time
Add the python snippets in the djangoproject/__init__.py
# rewrite the sql operation method from django.db.backends import utils from patchy.utils import long_sql_execute_wrapper utils.CursorWrapper.execute = long_sql_execute_wrapperAdd logger handler with pathcy.utils:
Example:
'patchy.utils': {
'handlers': ['sentry'],
'level': 'ERROR',
'propagate': True
}
Set the timeout threshold in settings(default to 0.05 seond, which is 50 miliseconds):
PATCHY_LONG_SQL_TIMEOUT = 0.01 # set the timeout to 10 miliseconds
Result:
If the sql operation exceeds the PATCHY_LONG_SQL_TIMEOUT a error log message will be sent.
no_sql_monitoring
no_sql_monitoring is a decorator for wrapping code to skip sql monitoring.
from patchy.utils import no_sql_monitoring
@no_sql_monitoring
def transaction_status(request, transaction_no):
transaction = Transaction.objects.get(transaction_no=transaction_no)
return ResponseBuilder.build_success_json_response(
msg='查询订单成功',
data={
'transaction_no': transaction_no,
'transaction_status': transaction.transaction_status
})
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file djangopatchy-0.0.7.tar.gz.
File metadata
- Download URL: djangopatchy-0.0.7.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
886398630a585c8dc4bf744d75ada6e4e365a833561c62ea3dbc444855d9c269
|
|
| MD5 |
924df3630c724b31ace44b4e81d35012
|
|
| BLAKE2b-256 |
3b7a928b70257a826e2768e2b19e7c95d81c6c7c77fa29fe3433e15b08b66b42
|