@@ -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"
@@ -171,6 +175,9 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A
171
175
API
172
176
---
173
177
"""
178
+
179
+ from __future__ import annotations
180
+
174
181
import logging
175
182
from importlib .metadata import PackageNotFoundError , distribution
176
183
from typing import Collection
@@ -227,6 +234,9 @@ def instrument_app(
227
234
tracer_provider = None ,
228
235
meter_provider = None ,
229
236
excluded_urls = None ,
237
+ http_capture_headers_server_request : list [str ] | None = None ,
238
+ http_capture_headers_server_response : list [str ] | None = None ,
239
+ http_capture_headers_sanitize_fields : list [str ] | None = None ,
230
240
):
231
241
"""Instrument an uninstrumented FastAPI application."""
232
242
if not hasattr (app , "_is_instrumented_by_opentelemetry" ):
@@ -265,6 +275,9 @@ def instrument_app(
265
275
# Pass in tracer/meter to get __name__and __version__ of fastapi instrumentation
266
276
tracer = tracer ,
267
277
meter = meter ,
278
+ http_capture_headers_server_request = http_capture_headers_server_request ,
279
+ http_capture_headers_server_response = http_capture_headers_server_response ,
280
+ http_capture_headers_sanitize_fields = http_capture_headers_sanitize_fields ,
268
281
)
269
282
app ._is_instrumented_by_opentelemetry = True
270
283
if app not in _InstrumentedFastAPI ._instrumented_fastapi_apps :
@@ -314,6 +327,15 @@ def _instrument(self, **kwargs):
314
327
_InstrumentedFastAPI ._client_response_hook = kwargs .get (
315
328
"client_response_hook"
316
329
)
330
+ _InstrumentedFastAPI ._http_capture_headers_server_request = kwargs .get (
331
+ "http_capture_headers_server_request"
332
+ )
333
+ _InstrumentedFastAPI ._http_capture_headers_server_response = (
334
+ kwargs .get ("http_capture_headers_server_response" )
335
+ )
336
+ _InstrumentedFastAPI ._http_capture_headers_sanitize_fields = (
337
+ kwargs .get ("http_capture_headers_sanitize_fields" )
338
+ )
317
339
_excluded_urls = kwargs .get ("excluded_urls" )
318
340
_InstrumentedFastAPI ._excluded_urls = (
319
341
_excluded_urls_from_env
@@ -368,6 +390,9 @@ def __init__(self, *args, **kwargs):
368
390
# Pass in tracer/meter to get __name__and __version__ of fastapi instrumentation
369
391
tracer = tracer ,
370
392
meter = meter ,
393
+ http_capture_headers_server_request = _InstrumentedFastAPI ._http_capture_headers_server_request ,
394
+ http_capture_headers_server_response = _InstrumentedFastAPI ._http_capture_headers_server_response ,
395
+ http_capture_headers_sanitize_fields = _InstrumentedFastAPI ._http_capture_headers_sanitize_fields ,
371
396
)
372
397
self ._is_instrumented_by_opentelemetry = True
373
398
_InstrumentedFastAPI ._instrumented_fastapi_apps .add (self )
0 commit comments