docs(fastapi): add usage example and setup order guidance#4314
docs(fastapi): add usage example and setup order guidance#4314samaarr wants to merge 1 commit intoopen-telemetry:mainfrom
Conversation
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.
|
|
|
|
||
| .. note:: | ||
|
|
||
| ``instrument_app()`` must be called after the ``FastAPI`` instance is created |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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.
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:
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)