Skip to content

Commit 859cc7a

Browse files
author
Daniel Rogers
committed
Add support for regular expression matching and sanitizing of headers.
Also: Add test cases for regular expression matching. Add test cases for "all" keyword. Add test cases for header sanitizing. Add documentation for regular expression matching and header sanitation. Various documentation cleanups and standardization. Fix keys() in class ASGIGetter so it returns the HTTP header keys instead of a list of available request data. This makes it consistent with the WSGIGetter keys() method. Make ASGIGetter.get() compare all keys in a case insensitive manner. Use resp.text instead of resp.body for Falcon 3 to avoid a deprecation warning. Remove duplicate documentation from instrumentation/opentelemetry-instrumentation-flask/README.rst as in ae7a415 Various formatting fixes.
1 parent ac84e99 commit 859cc7a

File tree

29 files changed

+2575
-429
lines changed

29 files changed

+2575
-429
lines changed

CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
([#1116](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1116))
2222
- fixed typo in `system.network.io` metric configuration
2323
([#1135](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1135))
24-
24+
- Fix keys() in class ASGIGetter so it returns the HTTP header keys instead of a list of available request data.
25+
([#1172](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1172))
26+
- Use resp.text instead of resp.body for Falcon 3 to avoid a deprecation warning.
27+
([#1172](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1172))
28+
- Make ASGIGetter.get() compare all keys in a case insensitive manner.
29+
([#1172](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1172))
2530

2631
### Added
2732
- `opentelemetry-instrumentation-aiohttp-client` Add support for optional custom trace_configs argument.
@@ -42,6 +47,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4247
([#1110](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1110))
4348
- Integrated sqlcommenter plugin into opentelemetry-instrumentation-django
4449
([#896](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/896))
50+
- Add support for regular expression matching of HTTP headers.
51+
([#1172](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1172))
52+
- Add support for sanitizing HTTP header values.
53+
([#1172](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1172))
4554

4655

4756
## [1.12.0rc1-0.31b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.12.0rc1-0.31b0) - 2022-05-17

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

Lines changed: 116 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515

1616
"""
1717
The opentelemetry-instrumentation-asgi package provides an ASGI middleware that can be used
18-
on any ASGI framework (such as Django-channels / Quart) to track requests
19-
timing through OpenTelemetry.
18+
on any ASGI framework (such as Django-channels / Quart) to track request timing through OpenTelemetry.
2019
2120
Usage (Quart)
2221
-------------
@@ -71,9 +70,14 @@ async def hello():
7170
Request/Response hooks
7271
**********************
7372
74-
Utilize request/reponse hooks to execute custom logic to be performed before/after performing a request. The server request hook takes in a server span and ASGI
75-
scope object for every incoming request. The client request hook is called with the internal span and an ASGI scope which is sent as a dictionary for when the method recieve is called.
76-
The client response hook is called with the internal span and an ASGI event which is sent as a dictionary for when the method send is called.
73+
This instrumentation supports request and response hooks. These are functions that get called
74+
right after a span is created for a request and right before the span is finished for the response.
75+
76+
- The server request hook is passed a server span and ASGI scope object for every incoming request.
77+
- The client request hook is called with the internal span and an ASGI scope when the method ``receive`` is called.
78+
- The client response hook is called with the internal span and an ASGI event when the method ``send`` is called.
79+
80+
For example,
7781
7882
.. code-block:: python
7983
@@ -93,59 +97,99 @@ def client_response_hook(span: Span, message: dict):
9397
9498
Capture HTTP request and response headers
9599
*****************************************
96-
You can configure the agent to capture predefined HTTP headers as span attributes, according to the `semantic convention <https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#http-request-and-response-headers>`_.
100+
You can configure the agent to capture specified HTTP headers as span attributes, according to the
101+
`semantic convention <https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#http-request-and-response-headers>`_.
97102
98103
Request headers
99104
***************
100-
To capture predefined HTTP request headers as span attributes, set the environment variable ``OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST``
101-
to a comma-separated list of HTTP header names.
105+
To capture HTTP request headers as span attributes, set the environment variable
106+
``OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST`` to a comma delimited list of HTTP header names.
102107
103108
For example,
104-
105109
::
106110
107111
export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST="content-type,custom_request_header"
108112
109-
will extract ``content-type`` and ``custom_request_header`` from request headers and add them as span attributes.
113+
will extract ``content-type`` and ``custom_request_header`` from the request headers and add them as span attributes.
114+
115+
Request header names in ASGI are case-insensitive. So, giving the header name as ``CUStom-Header`` in the environment
116+
variable will capture the header named ``custom-header``.
110117
111-
It is recommended that you should give the correct names of the headers to be captured in the environment variable.
112-
Request header names in ASGI are case insensitive. So, giving header name as ``CUStom-Header`` in environment variable will be able capture header with name ``custom-header``.
118+
Regular expressions may also be used to match multiple headers that correspond to the given pattern. For example:
119+
::
120+
121+
export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST="Accept.*,X-.*"
122+
123+
Would match all request headers that start with ``Accept`` and ``X-``.
124+
125+
Additionally, the special keyword ``all`` can be used to capture all request headers.
126+
::
113127
114-
The name of the added span attribute will follow the format ``http.request.header.<header_name>`` where ``<header_name>`` being the normalized HTTP header name (lowercase, with - characters replaced by _ ).
115-
The value of the attribute will be single item list containing all the header values.
128+
export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST="all"
116129
117-
Example of the added span attribute,
130+
The name of the added span attribute will follow the format ``http.request.header.<header_name>`` where ``<header_name>``
131+
is the normalized HTTP header name (lowercase, with ``-`` replaced by ``_``). The value of the attribute will be a
132+
single item list containing all the header values.
133+
134+
For example:
118135
``http.request.header.custom_request_header = ["<value1>,<value2>"]``
119136
120137
Response headers
121138
****************
122-
To capture predefined HTTP response headers as span attributes, set the environment variable ``OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE``
123-
to a comma-separated list of HTTP header names.
139+
To capture HTTP response headers as span attributes, set the environment variable
140+
``OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE`` to a comma delimited list of HTTP header names.
124141
125142
For example,
126-
127143
::
128144
129145
export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE="content-type,custom_response_header"
130146
131-
will extract ``content-type`` and ``custom_response_header`` from response headers and add them as span attributes.
147+
will extract ``content-type`` and ``custom_response_header`` from the response headers and add them as span attributes.
148+
149+
Response header names in ASGI are case-insensitive. So, giving the header name as ``CUStom-Header`` in the environment
150+
variable will capture the header named ``custom-header``.
151+
152+
Regular expressions may also be used to match multiple headers that correspond to the given pattern. For example:
153+
::
154+
155+
export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE="Content.*,X-.*"
156+
157+
Would match all response headers that start with ``Content`` and ``X-``.
158+
159+
Additionally, the special keyword ``all`` can be used to capture all response headers.
160+
::
132161
133-
It is recommended that you should give the correct names of the headers to be captured in the environment variable.
134-
Response header names captured in ASGI are case insensitive. So, giving header name as ``CUStomHeader`` in environment variable will be able capture header with name ``customheader``.
162+
export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE="all"
135163
136-
The name of the added span attribute will follow the format ``http.response.header.<header_name>`` where ``<header_name>`` being the normalized HTTP header name (lowercase, with - characters replaced by _ ).
137-
The value of the attribute will be single item list containing all the header values.
164+
The name of the added span attribute will follow the format ``http.response.header.<header_name>`` where ``<header_name>``
165+
is the normalized HTTP header name (lowercase, with ``-`` replaced by ``_``). The value of the attribute will be a
166+
single item list containing all the header values.
138167
139-
Example of the added span attribute,
168+
For example:
140169
``http.response.header.custom_response_header = ["<value1>,<value2>"]``
141170
171+
Sanitizing headers
172+
******************
173+
In order to prevent storing sensitive data such as personally identifiable information (PII), session keys, passwords,
174+
etc, set the environment variable ``OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS``
175+
to a comma delimited list of HTTP header names to be sanitized. Regexes may be used, and all header names will be
176+
matched in a case-insensitive manner.
177+
178+
For example,
179+
::
180+
181+
export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS=".*session.*,set-cookie"
182+
183+
will replace the value of headers such as ``session-id`` and ``set-cookie`` with ``[REDACTED]`` in the span.
184+
142185
Note:
143-
Environment variable names to capture http headers are still experimental, and thus are subject to change.
186+
The environment variable names used to capture HTTP headers are still experimental, and thus are subject to change.
144187
145188
API
146189
---
147190
"""
148191

192+
import re
149193
import typing
150194
import urllib
151195
from functools import wraps
@@ -167,8 +211,10 @@ def client_response_hook(span: Span, message: dict):
167211
from opentelemetry.trace import Span, set_span_in_context
168212
from opentelemetry.trace.status import Status, StatusCode
169213
from opentelemetry.util.http import (
214+
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS,
170215
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST,
171216
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE,
217+
SanitizeValue,
172218
get_custom_headers,
173219
normalise_request_header_name,
174220
normalise_response_header_name,
@@ -198,19 +244,23 @@ def get(
198244
if not headers:
199245
return None
200246

201-
# asgi header keys are in lower case
247+
# ASGI header keys are in lower case
202248
key = key.lower()
203249
decoded = [
204250
_value.decode("utf8")
205251
for (_key, _value) in headers
206-
if _key.decode("utf8") == key
252+
if _key.decode("utf8").lower() == key
207253
]
208254
if not decoded:
209255
return None
210256
return decoded
211257

212258
def keys(self, carrier: dict) -> typing.List[str]:
213-
return list(carrier.keys())
259+
return [
260+
_key.decode("utf8")
261+
for (_key, _value) in carrier.get("headers")
262+
]
263+
214264

215265

216266
asgi_getter = ASGIGetter()
@@ -286,15 +336,27 @@ def collect_custom_request_headers_attributes(scope):
286336
Refer specification https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#http-request-and-response-headers"""
287337

288338
attributes = {}
289-
custom_request_headers = get_custom_headers(
339+
340+
sanitized_fields = get_custom_headers(
341+
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS
342+
)
343+
344+
s = SanitizeValue(sanitized_fields)
345+
346+
custom_request_headers_name = get_custom_headers(
290347
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST
291348
)
292349

293-
for header in custom_request_headers:
294-
values = asgi_getter.get(scope, header)
295-
if values:
296-
key = normalise_request_header_name(header)
297-
attributes.setdefault(key, []).extend(values)
350+
if custom_request_headers_name:
351+
custom_request_headers_regex_compiled = re.compile(
352+
"|".join("^" + i + "$" for i in
353+
custom_request_headers_name), re.IGNORECASE)
354+
355+
for header_name in list(filter(custom_request_headers_regex_compiled.match, asgi_getter.keys(scope))):
356+
header_values = asgi_getter.get(scope, header_name.lower())
357+
if header_values:
358+
key = normalise_request_header_name(header_name.lower())
359+
attributes[key] = [s.sanitize_header_value(header=header_name, value=header_values[0])]
298360

299361
return attributes
300362

@@ -303,15 +365,27 @@ def collect_custom_response_headers_attributes(message):
303365
"""returns custom HTTP response headers to be added into SERVER span as span attributes
304366
Refer specification https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#http-request-and-response-headers"""
305367
attributes = {}
306-
custom_response_headers = get_custom_headers(
368+
369+
sanitized_fields = get_custom_headers(
370+
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS
371+
)
372+
373+
s = SanitizeValue(sanitized_fields)
374+
375+
custom_response_headers_name = get_custom_headers(
307376
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE
308377
)
309378

310-
for header in custom_response_headers:
311-
values = asgi_getter.get(message, header)
312-
if values:
313-
key = normalise_response_header_name(header)
314-
attributes.setdefault(key, []).extend(values)
379+
if custom_response_headers_name:
380+
custom_response_headers_regex_compiled = re.compile(
381+
"|".join("^" + i + "$" for i in
382+
custom_response_headers_name), re.IGNORECASE)
383+
384+
for header_name in list(filter(custom_response_headers_regex_compiled.match, asgi_getter.keys(message))):
385+
header_values = asgi_getter.get(message, header_name.lower())
386+
if header_values:
387+
key = normalise_response_header_name(header_name.lower())
388+
attributes[key] = [s.sanitize_header_value(header=header_name, value=header_values[0])]
315389

316390
return attributes
317391

@@ -349,7 +423,7 @@ def set_status_code(span, status_code):
349423
def get_default_span_details(scope: dict) -> Tuple[str, dict]:
350424
"""Default implementation for get_default_span_details
351425
Args:
352-
scope: the asgi scope dictionary
426+
scope: the ASGI scope dictionary
353427
Returns:
354428
a tuple of the span name, and any attributes to attach to the span.
355429
"""
@@ -406,7 +480,7 @@ async def __call__(self, scope, receive, send):
406480
"""The ASGI application
407481
408482
Args:
409-
scope: A ASGI environment.
483+
scope: An ASGI environment.
410484
receive: An awaitable callable yielding dictionaries
411485
send: An awaitable callable taking a single dictionary as argument.
412486
"""

0 commit comments

Comments
 (0)