[placement] clean up some nits in the requestlog middleware

In the review of I4215cc69cedae5637102b75e0b54fd26acb1826c there
were some suggested improvements that did not get implemented
because we needed the better logging sooner than later.

This change implements those cleanups including:

* use ',' instead of '%' for string interpolation in calls to LOG
* add microversion information to log string, if present

Change-Id: I6682215d72b644217b5122073d993a99f7830e5b
Closes-Bug: #1619690
This commit is contained in:
Chris Dent 2016-09-07 09:23:02 +00:00
parent ed435f2680
commit 76b53798b6
2 changed files with 22 additions and 13 deletions

View File

@ -14,6 +14,7 @@
from oslo_log import log as logging
from nova.api.openstack.placement import microversion
LOG = logging.getLogger(__name__)
@ -25,15 +26,16 @@ class RequestLog(object):
"""
format = ('%(REMOTE_ADDR)s "%(REQUEST_METHOD)s %(REQUEST_URI)s" '
'status: %(status)s len: %(bytes)s')
'status: %(status)s len: %(bytes)s '
'microversion: %(microversion)s')
def __init__(self, application):
self.application = application
def __call__(self, environ, start_response):
LOG.debug('Starting request: %s "%s %s"' %
(environ['REMOTE_ADDR'], environ['REQUEST_METHOD'],
self._get_uri(environ)))
LOG.debug('Starting request: %s "%s %s"',
environ['REMOTE_ADDR'], environ['REQUEST_METHOD'],
self._get_uri(environ))
if LOG.isEnabledFor(logging.INFO):
return self._log_app(environ, start_response)
else:
@ -69,14 +71,12 @@ class RequestLog(object):
if size is None:
size = '-'
log_format = {
'REMOTE_ADDR': environ.get('REMOTE_ADDR') or '-',
'REMOTE_ADDR': environ.get('REMOTE_ADDR', '-'),
'REQUEST_METHOD': environ['REQUEST_METHOD'],
'REQUEST_URI': req_uri,
'status': status.split(None, 1)[0],
'bytes': size,
'microversion': environ.get(
microversion.MICROVERSION_ENVIRON, '-'),
}
# We don't need to worry about trying to avoid the cost of
# interpolation here because we only reach this code if INFO
# is enabled.
message = self.format % log_format
LOG.info(message)
LOG.info(self.format, log_format)

View File

@ -33,6 +33,8 @@ class TestRequestLog(test.NoDBTestCase):
self.environ = self.req.environ
# The blank does not include remote address, so add it.
self.environ['REMOTE_ADDR'] = '127.0.0.1'
# nor a microversion
self.environ['placement.microversion'] = '2.1'
def test_get_uri(self):
req_uri = requestlog.RequestLog._get_uri(self.environ)
@ -57,7 +59,14 @@ class TestRequestLog(test.NoDBTestCase):
app = requestlog.RequestLog(self.application)
app(self.environ, start_response_mock)
mocked_log.debug.assert_called_once_with(
'Starting request: 127.0.0.1 "GET /resource_providers?name=myrp"')
'Starting request: %s "%s %s"', '127.0.0.1', 'GET',
'/resource_providers?name=myrp')
mocked_log.info.assert_called_once_with(
'127.0.0.1 "GET /resource_providers?name=myrp" '
'status: 200 len: 0')
'%(REMOTE_ADDR)s "%(REQUEST_METHOD)s %(REQUEST_URI)s" '
'status: %(status)s len: %(bytes)s microversion: %(microversion)s',
{'microversion': '2.1',
'status': '200',
'REQUEST_URI': '/resource_providers?name=myrp',
'REQUEST_METHOD': 'GET',
'REMOTE_ADDR': '127.0.0.1',
'bytes': '0'})