Skip to content

Commit dfe8ce5

Browse files
committed
add 'get_custom_header_attributes' helper to opentelemetry-util-http
1 parent 1f6bf9c commit dfe8ce5

File tree

2 files changed

+33
-33
lines changed
  • instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests
  • util/opentelemetry-util-http/src/opentelemetry/util/http

2 files changed

+33
-33
lines changed

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

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def response_hook(span, request_obj, response):
186186
import functools
187187
import types
188188
from timeit import default_timer
189-
from typing import Any, Callable, Collection, Mapping, Optional
189+
from typing import Any, Callable, Collection, Optional
190190
from urllib.parse import urlparse
191191

192192
from requests.models import PreparedRequest, Response
@@ -245,8 +245,8 @@ def response_hook(span, request_obj, response):
245245
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_CLIENT_RESPONSE,
246246
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS,
247247
ExcludeList,
248-
SanitizeValue,
249248
detect_synthetic_user_agent,
249+
get_custom_header_attributes,
250250
get_custom_headers,
251251
get_excluded_urls,
252252
normalise_request_header_name,
@@ -289,35 +289,6 @@ def _set_http_status_code_attribute(
289289
)
290290

291291

292-
def _get_custom_header_attributes(
293-
headers: Mapping[str, str | list[str]] | None,
294-
captured_headers: list[str] | None,
295-
sensitive_headers: list[str] | None,
296-
normalize_function: Callable[[str], str],
297-
) -> dict[str, list[str]]:
298-
"""Extract and sanitize HTTP headers for span attributes.
299-
300-
Args:
301-
headers: The HTTP headers to process, either from a request or response.
302-
Can be None if no headers are available.
303-
captured_headers: List of header regexes to capture as span attributes.
304-
If None or empty, no headers will be captured.
305-
sensitive_headers: List of header regexes whose values should be sanitized
306-
(redacted). If None, no sanitization is applied.
307-
normalize_function: Function to normalize header names.
308-
309-
Returns:
310-
Dictionary of normalized header attribute names to their values
311-
as lists of strings.
312-
"""
313-
if not headers or not captured_headers:
314-
return {}
315-
sanitize: SanitizeValue = SanitizeValue(sensitive_headers or ())
316-
return sanitize.sanitize_header_values(
317-
headers, captured_headers, normalize_function
318-
)
319-
320-
321292
# pylint: disable=unused-argument
322293
# pylint: disable=R0915
323294
def _instrument(
@@ -389,7 +360,7 @@ def get_or_create_headers():
389360
if user_agent:
390361
span_attributes[USER_AGENT_ORIGINAL] = user_agent
391362
span_attributes.update(
392-
_get_custom_header_attributes(
363+
get_custom_header_attributes(
393364
headers,
394365
captured_request_headers,
395366
sensitive_headers,
@@ -489,7 +460,7 @@ def get_or_create_headers():
489460
sem_conv_opt_in_mode,
490461
)
491462
span_attributes.update(
492-
_get_custom_header_attributes(
463+
get_custom_header_attributes(
493464
result.headers,
494465
captured_response_headers,
495466
sensitive_headers,

util/opentelemetry-util-http/src/opentelemetry/util/http/__init__.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,35 @@ def get_custom_headers(env_var: str) -> list[str]:
257257
return []
258258

259259

260+
def get_custom_header_attributes(
261+
headers: Mapping[str, str | list[str]] | None,
262+
captured_headers: list[str] | None,
263+
sensitive_headers: list[str] | None,
264+
normalize_function: Callable[[str], str],
265+
) -> dict[str, list[str]]:
266+
"""Extract and sanitize HTTP headers for span attributes.
267+
268+
Args:
269+
headers: The HTTP headers to process, either from a request or response.
270+
Can be None if no headers are available.
271+
captured_headers: List of header regexes to capture as span attributes.
272+
If None or empty, no headers will be captured.
273+
sensitive_headers: List of header regexes whose values should be sanitized
274+
(redacted). If None, no sanitization is applied.
275+
normalize_function: Function to normalize header names.
276+
277+
Returns:
278+
Dictionary of normalized header attribute names to their values
279+
as lists of strings.
280+
"""
281+
if not headers or not captured_headers:
282+
return {}
283+
sanitize: SanitizeValue = SanitizeValue(sensitive_headers or ())
284+
return sanitize.sanitize_header_values(
285+
headers, captured_headers, normalize_function
286+
)
287+
288+
260289
def _parse_active_request_count_attrs(req_attrs):
261290
active_requests_count_attrs = {
262291
key: req_attrs[key]

0 commit comments

Comments
 (0)