Skip to content

Commit 6c4ad5f

Browse files
author
Daniel Rogers
committed
Add support for regular expression matching and sanitizing of headers in ASGI.
Next part of #1184 Details: Add test cases for regular expression matching. 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. Move TestCustomHeaders to a separate file to avoid module length error.
1 parent 9beb6b2 commit 6c4ad5f

File tree

5 files changed

+479
-325
lines changed

5 files changed

+479
-325
lines changed

CHANGELOG.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
([#1369](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1369))
1717
- `opentelemetry-instrumentation-system-metrics` add supports to collect system thread count. ([#1339](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1339))
1818
- `opentelemetry-exporter-richconsole` Fixing RichConsoleExpoter to allow multiple traces, fixing duplicate spans and include resources ([#1336](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1336))
19+
- `opentelemetry-instrumentation-asgi` Add support for regular expression matching of HTTP headers.
20+
([#1333](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1333))
1921

20-
## [1.13.0-0.34b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.13.0-0.34b0) - 2022-09-26
22+
### Fixed
2123

24+
- `opentelemetry-instrumentation-asgi` Fix keys() in class ASGIGetter so it decodes the keys before returning them.
25+
([#1333](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1333))
26+
- `opentelemetry-instrumentation-asgi` Make ASGIGetter.get() compare all keys in a case insensitive manner.
27+
([#1333](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1333))
2228

29+
## [1.13.0-0.34b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.13.0-0.34b0) - 2022-09-26
2330

2431
- `opentelemetry-instrumentation-asyncpg` Fix high cardinality in the span name
2532
([#1324](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1324))
@@ -40,7 +47,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4047
- Add metric instrumentation in starlette
4148
([#1327](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1327))
4249

43-
4450
### Fixed
4551

4652
- `opentelemetry-instrumentation-boto3sqs` Make propagation compatible with other SQS instrumentations, add 'messaging.url' span attribute, and fix missing package dependencies.

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

+108-48
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/response 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 receive 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,54 +97,93 @@ 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``.
117+
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-.*"
110122
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``.
123+
Would match all request headers that start with ``Accept`` and ``X-``.
113124
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.
125+
To capture all request headers, set ``OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST`` to ``".*"``.
126+
::
127+
128+
export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST=".*"
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-.*"
132156
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``.
157+
Would match all response headers that start with ``Content`` and ``X-``.
135158
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.
159+
To capture all response headers, set ``OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE`` to ``".*"``.
160+
::
161+
162+
export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE=".*"
138163
139-
Example of the added span attribute,
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.
167+
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
---
@@ -169,8 +212,10 @@ def client_response_hook(span: Span, message: dict):
169212
from opentelemetry.trace import Span, set_span_in_context
170213
from opentelemetry.trace.status import Status, StatusCode
171214
from opentelemetry.util.http import (
215+
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS,
172216
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST,
173217
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE,
218+
SanitizeValue,
174219
_parse_active_request_count_attrs,
175220
_parse_duration_attrs,
176221
get_custom_headers,
@@ -202,19 +247,19 @@ def get(
202247
if not headers:
203248
return None
204249

205-
# asgi header keys are in lower case
250+
# ASGI header keys are in lower case
206251
key = key.lower()
207252
decoded = [
208253
_value.decode("utf8")
209254
for (_key, _value) in headers
210-
if _key.decode("utf8") == key
255+
if _key.decode("utf8").lower() == key
211256
]
212257
if not decoded:
213258
return None
214259
return decoded
215260

216261
def keys(self, carrier: dict) -> typing.List[str]:
217-
return list(carrier.keys())
262+
return [_key.decode("utf8") for (_key, _value) in carrier]
218263

219264

220265
asgi_getter = ASGIGetter()
@@ -289,35 +334,50 @@ def collect_custom_request_headers_attributes(scope):
289334
"""returns custom HTTP request headers to be added into SERVER span as span attributes
290335
Refer specification https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#http-request-and-response-headers"""
291336

292-
attributes = {}
293-
custom_request_headers = get_custom_headers(
294-
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST
337+
sanitize = SanitizeValue(
338+
get_custom_headers(
339+
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS
340+
)
295341
)
296342

297-
for header in custom_request_headers:
298-
values = asgi_getter.get(scope, header)
299-
if values:
300-
key = normalise_request_header_name(header)
301-
attributes.setdefault(key, []).extend(values)
343+
# Decode headers before processing.
344+
headers = {
345+
_key.decode("utf8"): _value.decode("utf8")
346+
for (_key, _value) in scope.get("headers")
347+
}
302348

303-
return attributes
349+
return sanitize.sanitize_header_values(
350+
headers,
351+
get_custom_headers(
352+
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST
353+
),
354+
normalise_request_header_name,
355+
)
304356

305357

306358
def collect_custom_response_headers_attributes(message):
307359
"""returns custom HTTP response headers to be added into SERVER span as span attributes
308360
Refer specification https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#http-request-and-response-headers"""
309-
attributes = {}
310-
custom_response_headers = get_custom_headers(
311-
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE
361+
362+
sanitize = SanitizeValue(
363+
get_custom_headers(
364+
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS
365+
)
312366
)
313367

314-
for header in custom_response_headers:
315-
values = asgi_getter.get(message, header)
316-
if values:
317-
key = normalise_response_header_name(header)
318-
attributes.setdefault(key, []).extend(values)
368+
# Decode headers before processing.
369+
headers = {
370+
_key.decode("utf8"): _value.decode("utf8")
371+
for (_key, _value) in message.get("headers")
372+
}
319373

320-
return attributes
374+
return sanitize.sanitize_header_values(
375+
headers,
376+
get_custom_headers(
377+
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE
378+
),
379+
normalise_response_header_name,
380+
)
321381

322382

323383
def get_host_port_url_tuple(scope):
@@ -353,7 +413,7 @@ def set_status_code(span, status_code):
353413
def get_default_span_details(scope: dict) -> Tuple[str, dict]:
354414
"""Default implementation for get_default_span_details
355415
Args:
356-
scope: the asgi scope dictionary
416+
scope: the ASGI scope dictionary
357417
Returns:
358418
a tuple of the span name, and any attributes to attach to the span.
359419
"""
@@ -427,7 +487,7 @@ async def __call__(self, scope, receive, send):
427487
"""The ASGI application
428488
429489
Args:
430-
scope: A ASGI environment.
490+
scope: An ASGI environment.
431491
receive: An awaitable callable yielding dictionaries
432492
send: An awaitable callable taking a single dictionary as argument.
433493
"""

0 commit comments

Comments
 (0)