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
a python profiling library that was recently updated with coroutine support to enable
21
+
better coroutine-aware profiling.
22
+
23
23
Note however that `yappi` adds considerable runtime overhead, and should typically be used during
24
24
development rather than production.
25
-
25
+
26
26
The middleware provided in this package is intended to be sufficiently performant for production use.
27
-
28
27
29
28
## Adding timing middleware
30
29
31
30
The `add_timing_middleware` function takes the following arguments:
32
31
33
-
*`app: FastAPI` : The app to which to add the timing middleware
34
-
*`record: Optional[Callable[[str], None]] = None` : The callable to call on the generated timing messages.
35
-
If not provided, defaults to `print`; a good choice is the `info` method of a `logging.Logger` instance
36
-
*`prefix: str = ""` : A prefix to prepend to the generated route names. This can be useful for, e.g.,
37
-
distinguishing between mounted ASGI apps.
38
-
*`exclude: Optional[str] = None` : If provided, any route whose generated name includes this value will not have its
39
-
timing stats recorded.
40
-
32
+
-`app: FastAPI` : The app to which to add the timing middleware
33
+
-`record: Optional[Callable[[str], None]] = None` : The callable to call on the generated timing messages.
34
+
If not provided, defaults to `print`; a good choice is the `info` method of a `logging.Logger` instance
35
+
-`prefix: str = ""` : A prefix to prepend to the generated route names. This can be useful for, e.g.,
36
+
distinguishing between mounted ASGI apps.
37
+
-`exclude: Optional[str] = None` : If provided, any route whose generated name includes this value will not have its
38
+
timing stats recorded.
39
+
-`format_message: Optional[Callable[[TimingInfo], Any]] = None` : Override the default message formatter. For example, to output structured JSON logs.
40
+
41
41
Here's an example demonstrating what the logged output looks like (note that the commented output has been
42
-
split to multiple lines for ease of reading here, but each timing record is actually a single line):
42
+
split to multiple lines for ease of reading here, but each timing record is actually a single line):
43
43
44
44
```python hl_lines="15 37 42 45 53"
45
45
{!./src/timing1.py!}
@@ -58,4 +58,12 @@ Note that this requires the app that generated the `Request` instance to have ha
58
58
added using the `add_timing_middleware` function.
59
59
60
60
This can be used to output multiple records at distinct times in order to introspect the relative
61
-
contributions of different execution steps in a single endpoint.
61
+
contributions of different execution steps in a single endpoint.
62
+
63
+
## Custom message formatter
64
+
65
+
It may be useful to provide an alternate format for the timing output. For example, some log management solutions prefer ingesting structured logs. A `json_formatter` function is included, but any custom formatter may be provided:
0 commit comments