You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: ansible_base/lib/middleware/profiling/README.md
+12-26Lines changed: 12 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,27 +1,28 @@
1
-
# Request Profiling
1
+
# Request Profiling and Observability
2
2
3
-
The `ProfileRequestMiddleware` and `DABProfiler` class provide a way to profile requests and other code in your Django application. This functionality is a generalization of the profiling tools found in AWX and can be used by any `django-ansible-base` consumer.
3
+
The `ObservabilityMiddleware` provides a simple way to gain performance and debugging insights into your Django application. It acts as a single entry point for several underlying middleware components, ensuring they are always used in the correct order.
4
4
5
-
## `ProfileRequestMiddleware`
5
+
## `ObservabilityMiddleware`
6
6
7
-
This middleware provides performance insights for API requests. To use it, add it to your `MIDDLEWARE` list in your Django settings. For the most accurate and reliable timing, it is recommended to add this middleware to the top of the `MIDDLEWARE` list.
7
+
This single middleware bundles tracing, request profiling, and SQL query analysis. To use it, add it to the top of your `MIDDLEWARE` list in your Django settings.
The middleware always adds the following headers to the response:
18
18
19
+
*`X-Request-ID`: A unique identifier for the request. If the incoming request includes an `X-Request-ID` header, that value will be used; otherwise, a new UUID will be generated.
19
20
*`X-API-Time`: The total time taken to process the request, in seconds.
20
-
*`X-API-Node`: The cluster host ID of the node that served the request. This header is only added if it is not already present in the response.
21
+
*`X-API-Node`: The cluster host ID of the node that served the request.
21
22
22
23
### cProfile Support
23
24
24
-
When the `ANSIBLE_BASE_CPROFILE_REQUESTS` setting is enabled, the middleware will also perform a cProfile analysis for each request. The resulting `.prof` file is saved to a temporary directory on the node that served the request, and its path is returned in the `X-API-CProfile-File` response header.
25
+
When the `ANSIBLE_BASE_CPROFILE_REQUESTS` setting is enabled, the middleware will also perform a cProfile analysis for each request. The resulting `.prof` file is saved to a temporary directory on the node that served the request, and its path is returned in the `X-API-CProfile-File` response header. The filename will include the request's `X-Request-ID`.
25
26
26
27
To enable cProfile support, set the following in your Django settings:
27
28
@@ -30,32 +31,16 @@ To enable cProfile support, set the following in your Django settings:
30
31
ANSIBLE_BASE_CPROFILE_REQUESTS=True
31
32
```
32
33
33
-
##`SQLProfilingMiddleware`
34
+
### SQL Profiling Support
34
35
35
-
This middleware provides insights into the database queries executed during a request. When enabled, it adds the following headers to the response:
36
+
When the `ANSIBLE_BASE_SQL_PROFILING` setting is enabled, the middleware provides insights into the database queries executed during a request. It adds the following headers to the response:
36
37
37
38
*`X-API-Query-Count`: The total number of database queries executed during the request.
38
39
*`X-API-Query-Time`: The total time spent on database queries, in seconds.
39
40
40
41
It also injects contextual information as a comment into each SQL query, which is invaluable for debugging and tracing. For example:
To use it, add both the `TraceContextMiddleware` and the `SQLProfilingMiddleware` to your `MIDDLEWARE` list in your Django settings. The `TraceContextMiddleware` should come before the `SQLProfilingMiddleware`.
The core profiling logic is encapsulated in the `DABProfiler` class. This class can be imported and used directly for profiling non-HTTP contexts, such as background tasks or gRPC services.
53
+
For profiling non-HTTP contexts, such as background tasks or gRPC services, the `DABProfiler` class can be used directly.
69
54
70
55
The profiler's cProfile functionality is controlled by the `ANSIBLE_BASE_CPROFILE_REQUESTS` setting.
0 commit comments