Skip to content

Commit

Permalink
update docs and improve fastapi tests
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed May 1, 2024
1 parent dc39259 commit 2ea4ebd
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ def client_response_hook(span: Span, message: dict):
The name of the added span attribute will follow the format ``http.request.header.<header_name>`` where ``<header_name>``
is the normalized HTTP header name (lowercase, with ``-`` replaced by ``_``). The value of the attribute will be a
single item list containing all the header values.
list containing the header values.
For example:
``http.request.header.custom_request_header = ["<value1>,<value2>"]``
``http.request.header.custom_request_header = ["<value1>", "<value2>"]``
Response headers
****************
Expand Down Expand Up @@ -163,10 +163,10 @@ def client_response_hook(span: Span, message: dict):
The name of the added span attribute will follow the format ``http.response.header.<header_name>`` where ``<header_name>``
is the normalized HTTP header name (lowercase, with ``-`` replaced by ``_``). The value of the attribute will be a
single item list containing all the header values.
list containing the header values.
For example:
``http.response.header.custom_response_header = ["<value1>,<value2>"]``
``http.response.header.custom_response_header = ["<value1>", "<value2>"]``
Sanitizing headers
******************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def client_response_hook(span: Span, message: dict):
single item list containing all the header values.
For example:
``http.request.header.custom_request_header = ["<value1>,<value2>"]``
``http.request.header.custom_request_header = ["<value1>", "<value2>"]``
Response headers
****************
Expand Down Expand Up @@ -146,10 +146,10 @@ def client_response_hook(span: Span, message: dict):
The name of the added span attribute will follow the format ``http.response.header.<header_name>`` where ``<header_name>``
is the normalized HTTP header name (lowercase, with ``-`` replaced by ``_``). The value of the attribute will be a
single item list containing all the header values.
list containing the header values.
For example:
``http.response.header.custom_response_header = ["<value1>,<value2>"]``
``http.response.header.custom_response_header = ["<value1>", "<value2>"]``
Sanitizing headers
******************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest
from timeit import default_timer
from typing import Mapping, Tuple
from unittest.mock import patch

import fastapi
Expand Down Expand Up @@ -549,6 +549,24 @@ def test_mark_span_internal_in_presence_of_span_from_other_framework(self):
)


class MultiMapping(Mapping):

def __init__(self, *items: Tuple[str, str]):
self._items = items

def __len__(self):
return len(self._items)

def __getitem__(self, __key):
raise NotImplementedError('use .items() instead')

def __iter__(self):
raise NotImplementedError('use .items() instead')

def items(self):
return self._items


@patch.dict(
"os.environ",
{
Expand All @@ -575,13 +593,15 @@ def _create_app():

@app.get("/foobar")
async def _():
headers = {
"custom-test-header-1": "test-header-value-1",
"custom-test-header-2": "test-header-value-2",
"my-custom-regex-header-1": "my-custom-regex-value-1,my-custom-regex-value-2",
"My-Custom-Regex-Header-2": "my-custom-regex-value-3,my-custom-regex-value-4",
"My-Secret-Header": "My Secret Value",
}
headers = MultiMapping(
("custom-test-header-1", "test-header-value-1"),
("custom-test-header-2", "test-header-value-2"),
("my-custom-regex-header-1", "my-custom-regex-value-1"),
("my-custom-regex-header-1", "my-custom-regex-value-2"),
("My-Custom-Regex-Header-2", "my-custom-regex-value-3"),
("My-Custom-Regex-Header-2", "my-custom-regex-value-4"),
("My-Secret-Header", "My Secret Value"),
)
content = {"message": "hello world"}
return JSONResponse(content=content, headers=headers)

Expand Down Expand Up @@ -657,10 +677,12 @@ def test_http_custom_response_headers_in_span_attributes(self):
"test-header-value-2",
),
"http.response.header.my_custom_regex_header_1": (
"my-custom-regex-value-1,my-custom-regex-value-2",
"my-custom-regex-value-1",
"my-custom-regex-value-2",
),
"http.response.header.my_custom_regex_header_2": (
"my-custom-regex-value-3,my-custom-regex-value-4",
"my-custom-regex-value-3",
"my-custom-regex-value-4",
),
"http.response.header.my_secret_header": ("[REDACTED]",),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ def client_response_hook(span: Span, message: dict):
The name of the added span attribute will follow the format ``http.request.header.<header_name>`` where ``<header_name>``
is the normalized HTTP header name (lowercase, with ``-`` replaced by ``_``). The value of the attribute will be a
single item list containing all the header values.
list containing the header values.
For example:
``http.request.header.custom_request_header = ["<value1>,<value2>"]``
``http.request.header.custom_request_header = ["<value1>", "<value2>"]``
Response headers
****************
Expand Down Expand Up @@ -142,10 +142,10 @@ def client_response_hook(span: Span, message: dict):
The name of the added span attribute will follow the format ``http.response.header.<header_name>`` where ``<header_name>``
is the normalized HTTP header name (lowercase, with ``-`` replaced by ``_``). The value of the attribute will be a
single item list containing all the header values.
list containing the header values.
For example:
``http.response.header.custom_response_header = ["<value1>,<value2>"]``
``http.response.header.custom_response_header = ["<value1>", "<value2>"]``
Sanitizing headers
******************
Expand Down

0 comments on commit 2ea4ebd

Please sign in to comment.