-
Notifications
You must be signed in to change notification settings - Fork 168
Make app field optional in Flask, Starlette and FastAPI instrumentations #1228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 6 commits
d1f6f53
9649ac5
915cb74
4f029e3
a84cdd3
4852de2
71b91fa
e695d01
8e22479
e734f2b
df85e15
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,7 +48,7 @@ def find_mounted_apps(app: FastAPI) -> list[FastAPI]: | |
|
||
def instrument_fastapi( | ||
logfire_instance: Logfire, | ||
app: FastAPI, | ||
app: FastAPI | None = None, | ||
Kludex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*, | ||
capture_headers: bool = False, | ||
request_attributes_mapper: Callable[ | ||
|
@@ -78,39 +78,53 @@ def instrument_fastapi( | |
'meter_provider': logfire_instance.config.get_meter_provider(), | ||
**opentelemetry_kwargs, | ||
} | ||
FastAPIInstrumentor.instrument_app( | ||
app, | ||
excluded_urls=excluded_urls, | ||
server_request_hook=_server_request_hook(opentelemetry_kwargs.pop('server_request_hook', None)), | ||
**opentelemetry_kwargs, | ||
) | ||
if app is None: | ||
FastAPIInstrumentor().instrument( | ||
|
||
excluded_urls=excluded_urls, | ||
server_request_hook=_server_request_hook(opentelemetry_kwargs.pop('server_request_hook', None)), | ||
**opentelemetry_kwargs, | ||
Kludex marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
) | ||
|
||
registry = patch_fastapi() | ||
if app in registry: # pragma: no cover | ||
raise ValueError('This app has already been instrumented.') | ||
@contextmanager | ||
def uninstrument_context(): | ||
yield | ||
FastAPIInstrumentor().uninstrument() | ||
|
||
return uninstrument_context() | ||
else: | ||
FastAPIInstrumentor.instrument_app( | ||
app, | ||
excluded_urls=excluded_urls, | ||
server_request_hook=_server_request_hook(opentelemetry_kwargs.pop('server_request_hook', None)), | ||
**opentelemetry_kwargs, | ||
) | ||
|
||
mounted_apps = find_mounted_apps(app) | ||
mounted_apps.append(app) | ||
registry = patch_fastapi() | ||
if app in registry: # pragma: no cover | ||
raise ValueError('This app has already been instrumented.') | ||
|
||
for _app in mounted_apps: | ||
registry[_app] = FastAPIInstrumentation( | ||
logfire_instance, | ||
request_attributes_mapper or _default_request_attributes_mapper, | ||
) | ||
mounted_apps = find_mounted_apps(app) | ||
mounted_apps.append(app) | ||
|
||
@contextmanager | ||
def uninstrument_context(): | ||
# The user isn't required (or even expected) to use this context manager, | ||
# which is why the instrumenting and patching has already happened before this point. | ||
# It exists mostly for tests, and just in case users want it. | ||
try: | ||
yield | ||
finally: | ||
for _app in mounted_apps: | ||
del registry[_app] | ||
FastAPIInstrumentor.uninstrument_app(_app) | ||
for _app in mounted_apps: | ||
registry[_app] = FastAPIInstrumentation( | ||
logfire_instance, | ||
request_attributes_mapper or _default_request_attributes_mapper, | ||
) | ||
|
||
return uninstrument_context() | ||
@contextmanager | ||
def uninstrument_context(): | ||
# The user isn't required (or even expected) to use this context manager, | ||
# which is why the instrumenting and patching has already happened before this point. | ||
# It exists mostly for tests, and just in case users want it. | ||
try: | ||
yield | ||
finally: | ||
for _app in mounted_apps: | ||
del registry[_app] | ||
FastAPIInstrumentor.uninstrument_app(_app) | ||
|
||
return uninstrument_context() | ||
|
||
|
||
@lru_cache # only patch once | ||
|
@@ -151,13 +165,7 @@ class FastAPIInstrumentation: | |
def __init__( | ||
self, | ||
logfire_instance: Logfire, | ||
request_attributes_mapper: Callable[ | ||
[ | ||
Request | WebSocket, | ||
dict[str, Any], | ||
], | ||
dict[str, Any] | None, | ||
], | ||
request_attributes_mapper: Callable[[Request | WebSocket, dict[str, Any]], dict[str, Any] | None], | ||
Kludex marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
): | ||
self.logfire_instance = logfire_instance.with_settings(custom_scope_suffix='fastapi') | ||
self.request_attributes_mapper = request_attributes_mapper | ||
|
Uh oh!
There was an error while loading. Please reload this page.