Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

fix: json fields dictionary has modification side effect #654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 25, 2022
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
1 change: 1 addition & 0 deletions 1 google/cloud/logging_v2/handlers/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ def _format_and_parse_message(record, formatter_handler):
pass
# if json_fields was set, create a dictionary using that
if passed_json_fields and isinstance(passed_json_fields, collections.abc.Mapping):
passed_json_fields = passed_json_fields.copy()
losalex marked this conversation as resolved.
Show resolved Hide resolved
if message != "None":
passed_json_fields["message"] = message
return passed_json_fields
Expand Down
18 changes: 18 additions & 0 deletions 18 tests/unit/handlers/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,24 @@ def test_json_fields_with_json_message(self):
self.assertEqual(result["key_m"], message["key_m"])
self.assertEqual(result["key_j"], json_fields["key_j"])

def test_json_fields_input_unmodified(self):
# Related issue: https://github.com/googleapis/python-logging/issues/652
from google.cloud.logging_v2.handlers.handlers import _format_and_parse_message

message = "hello world"
json_fields = {"hello": "world"}
json_fields_orig = json_fields.copy()
record = logging.LogRecord("logname", None, None, None, message, None, None)
setattr(record, "json_fields", json_fields)
handler = logging.StreamHandler()
_format_and_parse_message(record, handler)
# ensure json_fields has no side-effects
self.assertEqual(set(json_fields.keys()), set(json_fields_orig.keys()))
for (key, value) in json_fields_orig.items():
self.assertEqual(
value, json_fields[key], f"expected_payload[{key}] != result[{key}]"
)


class TestSetupLogging(unittest.TestCase):
def _call_fut(self, handler, excludes=None):
Expand Down
30 changes: 30 additions & 0 deletions 30 tests/unit/handlers/test_structured_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,36 @@ def test_format_with_nested_json(self):
result = json.loads(handler.format(record))
self.assertEqual(result["outer"], json_fields["outer"])

def test_json_fields_input_unmodified(self):
# Related issue: https://github.com/googleapis/python-logging/issues/652
import logging

handler = self._make_one()
message = "hello world"
json_fields = {
"hello": "world",
}
json_fields_orig = json_fields.copy()
record = logging.LogRecord(
None,
logging.INFO,
None,
None,
message,
None,
None,
)
record.created = None
setattr(record, "json_fields", json_fields)
handler.filter(record)
handler.format(record)
# ensure json_fields has no side-effects
self.assertEqual(set(json_fields.keys()), set(json_fields_orig.keys()))
for (key, value) in json_fields_orig.items():
self.assertEqual(
value, json_fields[key], f"expected_payload[{key}] != result[{key}]"
)

def test_emits_instrumentation_info(self):
import logging
import mock
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.