Skip to content

Commit dd5bf24

Browse files
committed
add 'get_custom_header_attributes' helper to opentelemetry-util-http
1 parent 8b160bf commit dd5bf24

File tree

2 files changed

+33
-34
lines changed
  • instrumentation/opentelemetry-instrumentation-aiohttp-client/src/opentelemetry/instrumentation/aiohttp_client
  • util/opentelemetry-util-http/src/opentelemetry/util/http

2 files changed

+33
-34
lines changed

instrumentation/opentelemetry-instrumentation-aiohttp-client/src/opentelemetry/instrumentation/aiohttp_client/__init__.py

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def response_hook(span: Span, params: typing.Union[
196196
import types
197197
import typing
198198
from timeit import default_timer
199-
from typing import Callable, Collection, Mapping
199+
from typing import Collection
200200
from urllib.parse import urlparse
201201

202202
import aiohttp
@@ -244,13 +244,12 @@ def response_hook(span: Span, params: typing.Union[
244244
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_CLIENT_REQUEST,
245245
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_CLIENT_RESPONSE,
246246
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS,
247-
SanitizeValue,
248247
get_custom_headers,
249248
get_excluded_urls,
250249
normalise_request_header_name,
251250
normalise_response_header_name,
252251
redact_url,
253-
sanitize_method,
252+
sanitize_method, get_custom_header_attributes,
254253
)
255254

256255
_UrlFilterT = typing.Optional[typing.Callable[[yarl.URL], str]]
@@ -304,35 +303,6 @@ def _set_http_status_code_attribute(
304303
)
305304

306305

307-
def _get_custom_header_attributes(
308-
headers: typing.Optional[Mapping[str, typing.Union[str, list[str]]]],
309-
captured_headers: typing.Optional[list[str]],
310-
sensitive_headers: typing.Optional[list[str]],
311-
normalize_function: Callable[[str], str],
312-
) -> dict[str, list[str]]:
313-
"""Extract and sanitize HTTP headers for span attributes.
314-
315-
Args:
316-
headers: The HTTP headers to process, either from a request or response.
317-
Can be None if no headers are available.
318-
captured_headers: List of header regexes to capture as span attributes.
319-
If None or empty, no headers will be captured.
320-
sensitive_headers: List of header regexes whose values should be sanitized
321-
(redacted). If None, no sanitization is applied.
322-
normalize_function: Function to normalize header names.
323-
324-
Returns:
325-
Dictionary of normalized header attribute names to their values
326-
as lists of strings.
327-
"""
328-
if not headers or not captured_headers:
329-
return {}
330-
sanitize: SanitizeValue = SanitizeValue(sensitive_headers or ())
331-
return sanitize.sanitize_header_values(
332-
headers, captured_headers, normalize_function
333-
)
334-
335-
336306
# pylint: disable=too-many-locals
337307
# pylint: disable=too-many-statements
338308
def create_trace_config(
@@ -525,7 +495,7 @@ async def on_request_start(
525495
pass
526496

527497
span_attributes.update(
528-
_get_custom_header_attributes(
498+
get_custom_header_attributes(
529499
{
530500
key: params.headers.getall(key)
531501
for key in params.headers.keys()
@@ -567,7 +537,7 @@ async def on_request_end(
567537
)
568538

569539
trace_config_ctx.span.set_attributes(
570-
_get_custom_header_attributes(
540+
get_custom_header_attributes(
571541
{
572542
key: params.response.headers.getall(key)
573543
for key in params.response.headers.keys()

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)