Skip to content

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

Kludex
Copy link
Member

@Kludex Kludex commented Jul 7, 2025

@alexmojaki As I told you, this only works if instrumentation happens before import.

@Kludex Kludex marked this pull request as draft July 7, 2025 06:49
Copy link

cloudflare-workers-and-pages bot commented Jul 7, 2025

Deploying logfire-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 8e22479
Status: ✅  Deploy successful!
Preview URL: https://566fc505.logfire-docs.pages.dev
Branch Preview URL: https://kludex-feat-no-need-for-app.logfire-docs.pages.dev

View logs

@Kludex Kludex marked this pull request as ready for review July 17, 2025 09:08
@Kludex Kludex requested a review from alexmojaki July 17, 2025 09:09
@@ -48,7 +48,7 @@ def find_mounted_apps(app: FastAPI) -> list[FastAPI]:

def instrument_fastapi(
logfire_instance: Logfire,
app: FastAPI,
app: FastAPI | None = None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment about how this differs from public signature

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please suggest.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that Logfire.instrument_fastapi requires this argument. It's only omitted when called via the logfire run CLI. This is because FastApiInstrumentor.instrument() has to be called before from fastapi import FastAPI which is easy to get wrong.

**opentelemetry_kwargs,
)
if app is None:
FastAPIInstrumentor().instrument(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it seems too hard to also apply the other fastapi instrumentation, at least comment about it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have the app instance, how would I go about it to make the custom logic work?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if request.app not in registry then check for a 'global' FastAPIInstrumentation next.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants