@@ -51,17 +51,15 @@ and passes them to the OpenTelemetry `FastAPIInstrumentor.instrument_app()` meth
51
51
52
52
## Endpoint arguments and validation errors
53
53
54
- [ ` logfire.instrument_fastapi() ` ] [ logfire.Logfire.instrument_fastapi ] will emit a span for each request
55
- called ` FastAPI arguments ` which shows how long it takes FastAPI to parse and validate the endpoint function
56
- arguments from the request and resolve any dependencies.
57
- By default the span will also contain the following attributes:
54
+ [ ` logfire.instrument_fastapi() ` ] [ logfire.Logfire.instrument_fastapi ] adds the following attributes to the request spans:
58
55
59
- - ` values ` : A dictionary mapping argument names of the endpoint function to parsed and validated values.
60
- - ` errors ` : A list of validation errors for any invalid inputs.
56
+ - ` fastapi.arguments. values` : A dictionary mapping argument names of the endpoint function to parsed and validated values.
57
+ - ` fastapi.arguments. errors` : A list of validation errors for any invalid inputs.
61
58
62
- You can customize this by passing an ` request_attributes_mapper ` function to ` instrument_fastapi ` . This function will be called
63
- with the ` Request ` or ` WebSocket ` object and the default attributes dictionary. It should return a new dictionary of
64
- attributes, or ` None ` to set the span level to 'debug' so that it's hidden by default. For example:
59
+ You can customize these attributes by passing a ` request_attributes_mapper ` function to ` instrument_fastapi ` .
60
+ This function will be called with the ` Request ` or ` WebSocket ` object
61
+ and a dictionary containing keys ` values ` and ` errors ` corresponding to the attributes above.
62
+ It should return a new dictionary of attributes. For example:
65
63
66
64
``` py
67
65
import logfire
@@ -73,12 +71,14 @@ def request_attributes_mapper(request, attributes):
73
71
if attributes[" errors" ]:
74
72
# Only log validation errors, not valid arguments
75
73
return {
74
+ # This will become the `fastapi.arguments.errors` attribute
76
75
" errors" : attributes[" errors" ],
76
+ # Arbitrary custom attributes can also be added here
77
77
" my_custom_attribute" : ... ,
78
78
}
79
79
else :
80
80
# Don't log anything for valid requests
81
- return None
81
+ return {}
82
82
83
83
84
84
logfire.configure()
@@ -89,9 +89,23 @@ logfire.instrument_fastapi(app, request_attributes_mapper=request_attributes_map
89
89
The [ ` request_attributes_mapper ` ] [ logfire.Logfire.instrument_fastapi(request_attributes_mapper) ] function mustn't mutate the
90
90
contents of ` values ` or ` errors ` , but it can safely replace them with new values.
91
91
92
- !!! note
93
- The attributes on the ` FastAPI arguments ` span are also set on the root span created by OpenTelemetry for easier querying.
94
- The ` values ` and ` error ` attributes are under the names ` fastapi.arguments.values ` and ` fastapi.arguments.errors ` to avoid name collisions.
92
+ ## Timestamps of argument parsing and endpoint execution
93
+
94
+ [ ` logfire.instrument_fastapi() ` ] [ logfire.Logfire.instrument_fastapi ] also adds the following attributes to the request spans:
95
+
96
+ - The times when parsing arguments and resolving dependencies started and ended:
97
+ - ` fastapi.arguments.start_timestamp `
98
+ - ` fastapi.arguments.end_timestamp `
99
+ - The times when the actual endpoint function started and ended executing, leaving out the time spent on dependencies and middleware:
100
+ - ` fastapi.endpoint_function.start_timestamp `
101
+ - ` fastapi.endpoint_function.end_timestamp `
102
+
103
+ ## Spans for argument parsing and endpoint execution
104
+
105
+ You can also enable spans for argument parsing and endpoint execution with ` logfire.instrument_fastapi(app, extra_spans=True) ` .
106
+ The main request span will still have the attributes described above, but it will also have two extra child spans.
107
+ This is mostly redundant now and is mainly provided for backwards compatibility.
108
+ It can also be useful for grouping together child logs and spans produced by the request.
95
109
96
110
[ fastapi ] : https://fastapi.tiangolo.com/
97
111
[ opentelemetry-asgi ] : https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/asgi/asgi.html
0 commit comments