logfire.instrument_fastapi() accepts arbitrary additional keyword arguments
and passes them to the OpenTelemetry FastAPIInstrumentor.instrument_app() method. See their documentation for more details.
logfire.instrument_fastapi() will emit a span for each request
called FastAPI arguments which shows how long it takes FastAPI to parse and validate the endpoint function
arguments from the request and resolve any dependencies.
By default the span will also contain the following attributes:
values: A dictionary mapping argument names of the endpoint function to parsed and validated values.
errors: A list of validation errors for any invalid inputs.
You can customize this by passing an request_attributes_mapper function to instrument_fastapi. This function will be called
with the Request or WebSocket object and the default attributes dictionary. It should return a new dictionary of
attributes, or None to set the span level to 'debug' so that it's hidden by default. For example:
importlogfireapp=...defrequest_attributes_mapper(request,attributes):ifattributes["errors"]:# Only log validation errors, not valid argumentsreturn{"errors":attributes["errors"],"my_custom_attribute":...,}else:# Don't log anything for valid requestsreturnNonelogfire.configure()logfire.instrument_fastapi(app,request_attributes_mapper=request_attributes_mapper)
Note
The request_attributes_mapper function mustn't mutate the
contents of values or errors, but it can safely replace them with new values.
Note
The attributes on the FastAPI arguments span are also set on the root span created by OpenTelemetry for easier querying.
The values and error attributes are under the names fastapi.arguments.values and fastapi.arguments.errors to avoid name collisions.