-
Notifications
You must be signed in to change notification settings - Fork 889
docs(fastapi): add usage example and setup order guidance #4314
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,12 @@ | ||
| OpenTelemetry FastAPI Instrumentation | ||
| ======================================= | ||
|
|
||
| |pypi| | ||
|
|
||
| .. |pypi| image:: https://badge.fury.io/py/opentelemetry-instrumentation-fastapi.svg | ||
| :target: https://pypi.org/project/opentelemetry-instrumentation-fastapi/ | ||
|
|
||
|
|
||
| This library provides automatic and manual instrumentation of FastAPI web frameworks, | ||
| instrumenting http requests served by applications utilizing the framework. | ||
|
|
||
| auto-instrumentation using the opentelemetry-instrumentation package is also supported. | ||
|
|
||
| Installation | ||
|
|
@@ -19,8 +16,57 @@ Installation | |
|
|
||
| pip install opentelemetry-instrumentation-fastapi | ||
|
|
||
| Usage | ||
| ----- | ||
|
|
||
| Configure your ``TracerProvider`` and call ``FastAPIInstrumentor.instrument_app()`` | ||
| **before** defining routes or handling requests. Instrumentation applied after route | ||
| registration will not capture spans for those routes. | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| from fastapi import FastAPI | ||
| from opentelemetry import trace | ||
| from opentelemetry.sdk.trace import TracerProvider | ||
| from opentelemetry.sdk.trace.export import BatchSpanProcessor | ||
| from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter | ||
| from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor | ||
|
|
||
| # 1. Configure the TracerProvider first | ||
| provider = TracerProvider() | ||
| provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter())) | ||
| trace.set_tracer_provider(provider) | ||
|
|
||
| # 2. Create the FastAPI app | ||
| app = FastAPI() | ||
|
|
||
| # 3. Instrument the app before defining routes | ||
| FastAPIInstrumentor.instrument_app(app) | ||
|
|
||
| # 4. Define routes after instrumentation | ||
| @app.get("/") | ||
| async def root(): | ||
| return {"message": "Hello World"} | ||
|
|
||
| .. note:: | ||
|
|
||
| ``instrument_app()`` must be called after the ``FastAPI`` instance is created | ||
| but before the application starts serving requests. Calling it after routes are | ||
| registered is supported, but calling it after the application has started | ||
| handling requests may result in missing spans. | ||
|
|
||
| Exclude URLs | ||
| ------------ | ||
|
|
||
| To exclude certain URLs from instrumentation, pass a comma-separated string of | ||
| regex patterns via the ``excluded_urls`` parameter: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| FastAPIInstrumentor.instrument_app(app, excluded_urls="/healthz,/metrics") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
|
||
| References | ||
| ---------- | ||
|
|
||
| * `OpenTelemetry Project <https://opentelemetry.io/>`_ | ||
| * `OpenTelemetry Python Examples <https://github.com/open-telemetry/opentelemetry-python/tree/main/docs/examples>`_ | ||
| * `OpenTelemetry Python Examples <https://github.com/open-telemetry/opentelemetry-python/tree/main/docs/examples>`_ | ||
There was a problem hiding this comment.
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_appshould be called before defining routes, but here you say callinginstrument_appafter defining routes is supported.There was a problem hiding this comment.
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 theFastAPIinstance iscreated but before any routes are defined, so that the instrumentation
middleware is in place before the app starts serving requests.