@@ -76,7 +76,7 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A
76
76
if span and span.is_recording():
77
77
span.set_attribute("custom_user_attribute_from_response_hook", "some-value")
78
78
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)
80
80
81
81
Capture HTTP request and response headers
82
82
*****************************************
@@ -86,9 +86,10 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A
86
86
Request headers
87
87
***************
88
88
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.
90
91
91
- For example,
92
+ For example using the environment variable ,
92
93
::
93
94
94
95
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
120
121
Response headers
121
122
****************
122
123
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.
124
126
125
- For example,
127
+ For example using the environment variable ,
126
128
::
127
129
128
130
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
155
157
******************
156
158
In order to prevent storing sensitive data such as personally identifiable information (PII), session keys, passwords,
157
159
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 .
160
162
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,
162
166
::
163
167
164
168
export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS=".*session.*,set-cookie"
@@ -227,6 +231,9 @@ def instrument_app(
227
231
tracer_provider = None ,
228
232
meter_provider = None ,
229
233
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 ,
230
237
):
231
238
"""Instrument an uninstrumented FastAPI application."""
232
239
if not hasattr (app , "_is_instrumented_by_opentelemetry" ):
@@ -265,6 +272,9 @@ def instrument_app(
265
272
# Pass in tracer/meter to get __name__and __version__ of fastapi instrumentation
266
273
tracer = tracer ,
267
274
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 ,
268
278
)
269
279
app ._is_instrumented_by_opentelemetry = True
270
280
if app not in _InstrumentedFastAPI ._instrumented_fastapi_apps :
0 commit comments