Skip to content

Commit 4438fd7

Browse files
samuelcolvinocelotl
authored andcommitted
add header parameters to FastAPIInstrumentor().instrument_app
1 parent c3e9f75 commit 4438fd7

File tree

2 files changed

+481
-8
lines changed

2 files changed

+481
-8
lines changed

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

+18-8
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A
7676
if span and span.is_recording():
7777
span.set_attribute("custom_user_attribute_from_response_hook", "some-value")
7878
79-
FastAPIInstrumentor().instrument(server_request_hook=server_request_hook, client_request_hook=client_request_hook, client_response_hook=client_response_hook)
79+
FastAPIInstrumentor().instrument_app(server_request_hook=server_request_hook, client_request_hook=client_request_hook, client_response_hook=client_response_hook)
8080
8181
Capture HTTP request and response headers
8282
*****************************************
@@ -86,9 +86,10 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A
8686
Request headers
8787
***************
8888
To capture HTTP request headers as span attributes, set the environment variable
89-
``OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST`` to a comma delimited list of HTTP header names.
89+
``OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST`` to a comma delimited list of HTTP header names,
90+
or pass the ``http_capture_headers_server_request`` keyword argument to the ``instrument_app`` method.
9091
91-
For example,
92+
For example using the environment variable,
9293
::
9394
9495
export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST="content-type,custom_request_header"
@@ -120,9 +121,10 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A
120121
Response headers
121122
****************
122123
To capture HTTP response headers as span attributes, set the environment variable
123-
``OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE`` to a comma delimited list of HTTP header names.
124+
``OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE`` to a comma delimited list of HTTP header names,
125+
or pass the ``http_capture_headers_server_response`` keyword argument to the ``instrument_app`` method.
124126
125-
For example,
127+
For example using the environment variable,
126128
::
127129
128130
export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE="content-type,custom_response_header"
@@ -155,10 +157,12 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A
155157
******************
156158
In order to prevent storing sensitive data such as personally identifiable information (PII), session keys, passwords,
157159
etc, set the environment variable ``OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS``
158-
to a comma delimited list of HTTP header names to be sanitized. Regexes may be used, and all header names will be
159-
matched in a case-insensitive manner.
160+
to a comma delimited list of HTTP header names to be sanitized, or pass the ``http_capture_headers_sanitize_fields``
161+
keyword argument to the ``instrument_app`` method.
160162
161-
For example,
163+
Regexes may be used, and all header names will be matched in a case-insensitive manner.
164+
165+
For example using the environment variable,
162166
::
163167
164168
export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS=".*session.*,set-cookie"
@@ -227,6 +231,9 @@ def instrument_app(
227231
tracer_provider=None,
228232
meter_provider=None,
229233
excluded_urls=None,
234+
http_capture_headers_server_request: list[str] | None = None,
235+
http_capture_headers_server_response: list[str] | None = None,
236+
http_capture_headers_sanitize_fields: list[str] | None = None,
230237
):
231238
"""Instrument an uninstrumented FastAPI application."""
232239
if not hasattr(app, "_is_instrumented_by_opentelemetry"):
@@ -265,6 +272,9 @@ def instrument_app(
265272
# Pass in tracer/meter to get __name__and __version__ of fastapi instrumentation
266273
tracer=tracer,
267274
meter=meter,
275+
http_capture_headers_server_request=http_capture_headers_server_request,
276+
http_capture_headers_server_response=http_capture_headers_server_response,
277+
http_capture_headers_sanitize_fields=http_capture_headers_sanitize_fields,
268278
)
269279
app._is_instrumented_by_opentelemetry = True
270280
if app not in _InstrumentedFastAPI._instrumented_fastapi_apps:

0 commit comments

Comments
 (0)