Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions json_logging/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ def get_request_from_call_stack(self, within_formatter=False):
if isinstance(f_locals['req'], class_type):
return f_locals['req']

for key in f_locals:
if key not in {'request', 'req'} and isinstance(f_locals[key], class_type):
return f_locals[key]
for value in tuple(f_locals.values()):
if isinstance(value, class_type):
return value
if f.f_back is not None:
f = f.f_back
else:
Expand All @@ -207,4 +207,4 @@ def _get_correlation_id_in_request_header(request_adapter, request):


def is_not_match_any_pattern(path, patterns):
return all(map(lambda pattern: re.search(pattern, path) is None, patterns))
return all(map(lambda pattern: re.search(pattern, path) is None, patterns))
31 changes: 31 additions & 0 deletions tests/test_fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,37 @@ def excluded_silent():
undo_imports_from_package("json_logging")


def test_get_request_from_call_stack_handles_frame_locals_mutation(
client_and_log_handler,
):
"""Test stack inspection while a tracer changes the frame locals."""
import json_logging
from json_logging.util import RequestUtil

mutation_happened = False

def tracer(frame, event, arg):
nonlocal mutation_happened
if (
frame.f_code is RequestUtil.get_request_from_call_stack.__code__
and event == "line"
and "value" in frame.f_locals
and not mutation_happened
):
frame.f_locals["added_by_tracer"] = object()
mutation_happened = True
return tracer

previous_trace = sys.gettrace()
sys.settrace(tracer)
try:
assert json_logging._request_util.get_request_from_call_stack() is None
finally:
sys.settrace(previous_trace)

assert mutation_happened


def test_extra_property(client_and_log_handler):
"""Test adding an extra property to a log message"""
api_client, handler = client_and_log_handler
Expand Down
Loading