Skip to content

Commit f255281

Browse files
authored
Remove extra FastAPI spans by default (#1268)
1 parent 445ed10 commit f255281

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

docs/integrations/web-frameworks/fastapi.md

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,15 @@ and passes them to the OpenTelemetry `FastAPIInstrumentor.instrument_app()` meth
5151

5252
## Endpoint arguments and validation errors
5353

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:
5855

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.
6158

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:
6563

6664
```py
6765
import logfire
@@ -73,12 +71,14 @@ def request_attributes_mapper(request, attributes):
7371
if attributes["errors"]:
7472
# Only log validation errors, not valid arguments
7573
return {
74+
# This will become the `fastapi.arguments.errors` attribute
7675
"errors": attributes["errors"],
76+
# Arbitrary custom attributes can also be added here
7777
"my_custom_attribute": ...,
7878
}
7979
else:
8080
# Don't log anything for valid requests
81-
return None
81+
return {}
8282

8383

8484
logfire.configure()
@@ -89,9 +89,23 @@ logfire.instrument_fastapi(app, request_attributes_mapper=request_attributes_map
8989
The [`request_attributes_mapper`][logfire.Logfire.instrument_fastapi(request_attributes_mapper)] function mustn't mutate the
9090
contents of `values` or `errors`, but it can safely replace them with new values.
9191

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.
95109

96110
[fastapi]: https://fastapi.tiangolo.com/
97111
[opentelemetry-asgi]: https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/asgi/asgi.html

logfire/_internal/integrations/fastapi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def instrument_fastapi(
6363
| None = None,
6464
excluded_urls: str | Iterable[str] | None = None,
6565
record_send_receive: bool = False,
66-
extra_spans: bool = True,
66+
extra_spans: bool = False,
6767
**opentelemetry_kwargs: Any,
6868
) -> AbstractContextManager[None]:
6969
"""Instrument a FastAPI app so that spans and logs are automatically created for each request.

logfire/_internal/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,7 @@ def instrument_fastapi(
10351035
| None = None,
10361036
excluded_urls: str | Iterable[str] | None = None,
10371037
record_send_receive: bool = False,
1038-
extra_spans: bool = True,
1038+
extra_spans: bool = False,
10391039
**opentelemetry_kwargs: Any,
10401040
) -> AbstractContextManager[None]:
10411041
"""Instrument a FastAPI app so that spans and logs are automatically created for each request.

0 commit comments

Comments
 (0)