28 |
28 |
else:
|
29 |
29 |
MiddlewareMixin = object
|
30 |
30 |
|
|
31 |
try:
|
|
32 |
import uwsgi
|
|
33 |
get_wid = uwsgi.worker_id
|
|
34 |
except ImportError:
|
|
35 |
import os
|
|
36 |
get_wid = os.getpid
|
31 |
37 |
|
32 |
|
requests_total_by_host_view_status_method = prometheus_client.Counter(
|
33 |
|
'django_http_requests_total_by_host_view_status_method',
|
34 |
|
'Count of requests by host, view, status, method.',
|
35 |
|
['host', 'view', 'status', 'method'])
|
|
38 |
requests_total_by_host_worker_view_status_method = prometheus_client.Counter(
|
|
39 |
'django_http_requests_total_by_host_worker_view_status_method',
|
|
40 |
'Count of requests by host, worker, view, status, method.',
|
|
41 |
['host', 'worker', 'view', 'status', 'method'])
|
36 |
42 |
|
37 |
|
requests_bytes_by_host_view_status_method = prometheus_client.Summary(
|
38 |
|
'django_http_requests_bytes_by_host_view_status_method',
|
39 |
|
'Bytes of requests by host, view, status, method.',
|
40 |
|
['host', 'view', 'status', 'method'])
|
|
43 |
requests_bytes_by_host_worker_view_status_method = prometheus_client.Summary(
|
|
44 |
'django_http_requests_bytes_by_host_worker_view_status_method',
|
|
45 |
'Bytes of requests by host, worker, view, status, method.',
|
|
46 |
['host', 'worker', 'view', 'status', 'method'])
|
41 |
47 |
|
42 |
|
requests_times_by_host_view_status_method = prometheus_client.Summary(
|
43 |
|
'django_http_requests_times_by_host_view_status_method',
|
44 |
|
'Duration of requests by host, view, status, method.',
|
45 |
|
['host', 'view', 'status', 'method'])
|
|
48 |
requests_times_by_host_worker_view_status_method = prometheus_client.Summary(
|
|
49 |
'django_http_requests_times_by_host_worker_view_status_method',
|
|
50 |
'Duration of requests by host, worker, view, status, method.',
|
|
51 |
['host', 'worker', 'view', 'status', 'method'])
|
46 |
52 |
|
47 |
|
requests_queries_count_by_host_view_status_method = prometheus_client.Summary(
|
48 |
|
'django_http_requests_queries_count_by_host_view_status_method',
|
49 |
|
'Query count by host, view, status, method.',
|
50 |
|
['host', 'view', 'status', 'method'])
|
|
53 |
requests_queries_count_by_host_worker_view_status_method = prometheus_client.Summary(
|
|
54 |
'django_http_requests_queries_count_by_host_worker_view_status_method',
|
|
55 |
'Query count by host, worker, view, status, method.',
|
|
56 |
['host', 'worker', 'view', 'status', 'method'])
|
51 |
57 |
|
52 |
|
requests_queries_time_by_host_view_status_method = prometheus_client.Summary(
|
53 |
|
'django_http_requests_queries_time_by_host_view_status_method',
|
54 |
|
'Query time by host, view, status, method.',
|
55 |
|
['host', 'view', 'status', 'method'])
|
|
58 |
requests_queries_time_by_host_worker_view_status_method = prometheus_client.Summary(
|
|
59 |
'django_http_requests_queries_time_by_host_worker_view_status_method',
|
|
60 |
'Query time by host, worker, view, status, method.',
|
|
61 |
['host', 'worker', 'view', 'status', 'method'])
|
56 |
62 |
|
57 |
63 |
|
58 |
64 |
|
... | ... | |
81 |
87 |
http_method = request.method
|
82 |
88 |
host_name = request.get_host()
|
83 |
89 |
total_time = time.time() - request._stats_t0
|
|
90 |
worker_id = get_wid()
|
84 |
91 |
|
85 |
92 |
status_code = response.status_code
|
86 |
93 |
view_name = getattr(request, '_view_name', '<unnamed view>')
|
87 |
|
requests_total_by_host_view_status_method.labels(
|
88 |
|
host_name, view_name, status_code, http_method).inc()
|
|
94 |
requests_total_by_host_worker_view_status_method.labels(
|
|
95 |
host_name, worker_id, view_name, status_code, http_method).inc()
|
89 |
96 |
|
90 |
97 |
if hasattr(response, 'content'):
|
91 |
98 |
response_size = len(response.content)
|
92 |
|
requests_bytes_by_host_view_status_method.labels(
|
93 |
|
host_name, view_name, status_code, http_method).observe(response_size)
|
|
99 |
requests_bytes_by_host_worker_view_status_method.labels(
|
|
100 |
host_name, worker_id, view_name, status_code,
|
|
101 |
http_method).observe(response_size)
|
94 |
102 |
|
95 |
|
requests_times_by_host_view_status_method.labels(
|
96 |
|
host_name, view_name, status_code, http_method).observe(total_time)
|
|
103 |
requests_times_by_host_worker_view_status_method.labels(
|
|
104 |
host_name, worker_id, view_name, status_code, http_method).observe(total_time)
|
97 |
105 |
|
98 |
106 |
if connection.queries_logged:
|
99 |
107 |
sql_queries_count = len(connection.queries)
|
100 |
108 |
sql_queries_time = sum([float(x['time']) for x in connection.queries])
|
101 |
|
requests_queries_count_by_host_view_status_method.labels(
|
102 |
|
host_name, view_name, status_code, http_method).observe(sql_queries_count)
|
103 |
|
requests_queries_time_by_host_view_status_method.labels(
|
104 |
|
host_name, view_name, status_code, http_method).observe(sql_queries_time)
|
|
109 |
requests_queries_count_by_host_worker_view_status_method.labels(
|
|
110 |
host_name, worker_id, view_name, status_code,
|
|
111 |
http_method).observe(sql_queries_count)
|
|
112 |
requests_queries_time_by_host_worker_view_status_method.labels(
|
|
113 |
host_name, worker_id, view_name, status_code,
|
|
114 |
http_method).observe(sql_queries_time)
|
105 |
115 |
|
106 |
116 |
return response
|