Merge "[Fix] init global_request_id if not in context"

This commit is contained in:
Zuul 2022-05-13 11:03:59 +00:00 committed by Gerrit Code Review
commit 6401da71b3
3 changed files with 18 additions and 1 deletions

View File

@ -452,7 +452,7 @@ class ContextFormatter(logging.Formatter):
# to an empty string so we don't throw an exception if
# they get used
for key in ('instance', 'color', 'user_identity', 'resource',
'user_name', 'project_name'):
'user_name', 'project_name', 'global_request_id'):
if key not in record.__dict__:
record.__dict__[key] = ''

View File

@ -964,6 +964,18 @@ class ContextFormatterTestCase(LogTestBase):
str(message)))
self.assertEqual(expected, self.stream.getvalue())
def test_global_request_id_logging(self):
fmt_str = "HAS CONTEXT [%(request_id)s %(global_request_id)s]: " \
"%(message)s"
self.config(logging_context_format_string=fmt_str)
ctxt = _fake_context()
ctxt.request_id = '99'
message = 'test'
self.log.info(message, context=ctxt)
expected = ("HAS CONTEXT [%s %s]: %s\n" %
(ctxt.request_id, ctxt.global_request_id, str(message)))
self.assertEqual(expected, self.stream.getvalue())
def test_user_identity_logging_set_format(self):
self.config(logging_context_format_string="HAS CONTEXT "
"[%(request_id)s "

View File

@ -0,0 +1,5 @@
---
fixes:
- |
Initialize the ``global_request_id`` context variable with a default
value if the key is not passed in the context.