Skip to content

docs(fastapi): add usage example and setup order guidance#4314

Open
samaarr wants to merge 4 commits intoopen-telemetry:mainfrom
samaarr:docs/fastapi-add-usage-example
Open

docs(fastapi): add usage example and setup order guidance#4314
samaarr wants to merge 4 commits intoopen-telemetry:mainfrom
samaarr:docs/fastapi-add-usage-example

Conversation

@samaarr
Copy link

@samaarr samaarr commented Mar 7, 2026

The existing README contained no usage example, leaving users to
figure out correct setup from the source code or external blog posts.

This PR adds:

  • A minimal working example showing correct TracerProvider → instrument_app() → routes order
  • A note clarifying that instrumentation must happen before the app starts serving requests
  • An example for excluding URLs (e.g. /healthz, /metrics) from instrumentation

I ran into the setup ordering issue while building a demo project with
FastAPI + OTel Collector and found the lack of any example in the README
was a common stumbling block.

Fixes: none (docs improvement)

The existing README had no usage example. New contributors and users
had no indication that TracerProvider must be configured before
instrument_app() is called, or that instrumentation should happen
before routes are defined.

Add a minimal working example showing correct setup order, a note
clarifying the instrumentation timing requirement, and an example
for excluding URLs from instrumentation.
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Mar 7, 2026

CLA Signed

The committers listed above are authorized under a signed CLA.


.. note::

``instrument_app()`` must be called after the ``FastAPI`` instance is created

Choose a reason for hiding this comment

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

Unsure of actual functionality, but this text section seems to contradict the previous section. Correct me if I am wrong, but you initially say instrument_app should be called before defining routes, but here you say calling instrument_app after defining routes is supported.

Copy link
Author

Choose a reason for hiding this comment

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

instrument_app() must be called after the FastAPI instance is
created but before any routes are defined, so that the instrumentation
middleware is in place before the app starts serving requests.

Choose a reason for hiding this comment

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

Yes, there is still a contradiction between this section and lines 22-24.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for your input, I've removed the Usage section from the readme entirely and moved the example into the module docstring instead, per emdneto's suggestion. The ordering note in the docstring now reads: "Instrument after creating the app and before serving requests; calling it after routes are registered is also supported." I think there is no contradiction now.


.. code-block:: python

FastAPIInstrumentor.instrument_app(app, excluded_urls="/healthz,/metrics")

Choose a reason for hiding this comment

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

Let's go ahead and provide examples for urls that would be excluded according to these params to verify the exclusion of partial matches, if that is the case. The README for falcon instrumentation is a good example: https://github.com/open-telemetry/opentelemetry-python-contrib/blob/main/instrumentation/opentelemetry-instrumentation-falcon/README.rst.

Copy link
Author

Choose a reason for hiding this comment

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

Updated. The docstring now shows both a partial match example (with a comment explaining that "healthz" matches /healthz, /internal/healthz, etc.) and an anchored pattern example for exact path matching.

Copy link
Member

@emdneto emdneto left a comment

Choose a reason for hiding this comment

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

Copy link
Contributor

@xrmx xrmx left a comment

Choose a reason for hiding this comment

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

What Emidio said

@samaarr
Copy link
Author

samaarr commented Mar 14, 2026

Thanks @emdneto and @xrmx. I moved the example out of the README and into the module docstring so it shows up on the readthedocs page. README reverted. @JWinermaSplunk the ordering contradiction and excluded_urls partial match examples are also addressed in the updated docstring.

@xrmx
Copy link
Contributor

xrmx commented Mar 16, 2026

@samaarr I don't see the README.rst changes reverted

@xrmx xrmx moved this to Reviewed PRs that need fixes in Python PR digest Mar 16, 2026
@samaarr
Copy link
Author

samaarr commented Mar 16, 2026

Done. commit 99dec59 restores the README to the original. The diff now only touches init.py.

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

Labels

None yet

Projects

Status: Reviewed PRs that need fixes

Development

Successfully merging this pull request may close these issues.

4 participants