Skip to content

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

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

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
Copy Markdown

@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
Copy Markdown

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
Copy link
Copy Markdown

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
Copy Markdown
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.

Copy link
Copy Markdown

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
Copy Markdown
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")
Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Author

samaarr commented Mar 16, 2026

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

@github-actions
Copy link
Copy Markdown

This PR has been automatically marked as stale because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 days of this comment.
If you're still working on this, please add a comment or push new commits.

@github-actions github-actions Bot added the Stale label Mar 31, 2026
@github-actions
Copy link
Copy Markdown

This PR has been closed due to inactivity. Please reopen if you would like to continue working on it.

@github-actions github-actions Bot closed this Apr 14, 2026
@github-project-automation github-project-automation Bot moved this from Reviewed PRs that need fixes to Done in Python PR digest Apr 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants