Skip to content

Commit 4521300

Browse files
committed
Document FastAPI instrumentation setup
Clarify FastAPI auto-instrumentation configuration, propagation, WebSocket behavior, and log export. Also surface the opentelemetry-instrumentation README in the generated docs menu. Assisted-by: OpenAI Codex
1 parent 4447323 commit 4521300

2 files changed

Lines changed: 59 additions & 2 deletions

File tree

  • docs/instrumentation/base
  • instrumentation/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi

docs/instrumentation/base/instrumentation.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
OpenTelemetry Python Instrumentor
2-
=================================
1+
.. include:: ../../../opentelemetry-instrumentation/README.rst
2+
3+
API
4+
---
35

46
.. automodule:: opentelemetry.instrumentation
57
:members:

instrumentation/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,61 @@ async def foobar():
1818
1919
FastAPIInstrumentor.instrument_app(app)
2020
21+
Auto-instrumentation
22+
********************
23+
24+
FastAPI can also be instrumented without code changes by using the
25+
``opentelemetry-instrument`` command from the
26+
`opentelemetry-instrumentation <https://pypi.org/project/opentelemetry-instrumentation/>`_
27+
package together with the
28+
`opentelemetry-distro <https://pypi.org/project/opentelemetry-distro/>`_
29+
package:
30+
31+
.. code-block:: sh
32+
33+
pip install "opentelemetry-distro[otlp]" opentelemetry-instrumentation-fastapi
34+
opentelemetry-instrument uvicorn app:app
35+
36+
When auto-instrumentation is used with ``opentelemetry-distro``, the SDK
37+
configuration is read from standard OpenTelemetry environment variables such as
38+
``OTEL_SERVICE_NAME``, ``OTEL_TRACES_EXPORTER``, ``OTEL_METRICS_EXPORTER``,
39+
``OTEL_LOGS_EXPORTER``, ``OTEL_EXPORTER_OTLP_ENDPOINT``, and
40+
``OTEL_EXPORTER_OTLP_PROTOCOL``. FastAPI-specific options, such as excluded
41+
URLs and HTTP header capture, are documented below. When using
42+
``FastAPIInstrumentor.instrument_app(app)`` directly, configure the
43+
OpenTelemetry SDK providers and exporters in application code or use a distro
44+
package to configure them.
45+
46+
Trace propagation
47+
*****************
48+
49+
Incoming context is extracted automatically from FastAPI request headers by the
50+
underlying ASGI instrumentation, using the configured global OpenTelemetry
51+
propagator. No manual intervention is needed for incoming HTTP requests.
52+
Outgoing context propagation is handled by the instrumentation for the client
53+
library making the outbound request, such as HTTPX, Requests, or aiohttp.
54+
55+
WebSocket connections are instrumented as ASGI ``websocket`` scopes. Context is
56+
extracted from the WebSocket handshake headers, and spans are created for the
57+
connection and ASGI send/receive events. Propagation data inside WebSocket
58+
message payloads is not parsed automatically.
59+
60+
Logs
61+
****
62+
63+
FastAPI instrumentation emits traces and metrics. To export Python ``logging``
64+
records as OpenTelemetry logs, install and enable the
65+
`opentelemetry-instrumentation-logging <https://pypi.org/project/opentelemetry-instrumentation-logging/>`_
66+
package. With auto-instrumentation and ``opentelemetry-distro``, set
67+
``OTEL_LOGS_EXPORTER`` (for example, ``otlp``) to configure log export. Set
68+
``OTEL_PYTHON_LOG_CORRELATION=true`` when log messages should include the
69+
current trace and span IDs in the formatted log output.
70+
71+
OpenTelemetry logs are exported as log records that can be correlated with
72+
traces. They are not added to spans as span events automatically; use
73+
``span.add_event(...)`` for application data that should be recorded as span
74+
events.
75+
2176
Configuration
2277
-------------
2378

0 commit comments

Comments
 (0)