Skip to content

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

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

docs(fastapi): add usage example and setup order guidance#4314
samaarr wants to merge 1 commit 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.

  • ✅ login: samaarr / name: Samar Santosh Patil (03c0fd1)


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


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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants