Append 'Openstack-Request-Id' header to the response

This patch will append request_id to response headers. This is helpful
for admin to track problems across projects.

Accroding to OpenStack HTTP Header Guidelines, use
'Openstack-Request-Id' instead of 'X-Openstack-Request-ID'.

According the suggestions from API WG, a microversion is not needed here.

Change-Id: Ica067aff19fe17052fcaf9c7384c9da7d1f7997d
Partial-Bug: #1505119
This commit is contained in:
Lin Tan 2015-10-21 19:56:12 +08:00 committed by Jim Rollenhagen
parent f19e4d6ce2
commit 4bf707d86a
3 changed files with 33 additions and 0 deletions

View File

@ -89,6 +89,15 @@ class ContextHook(hooks.PecanHook):
show_password=show_password, show_password=show_password,
**creds) **creds)
def after(self, state):
if state.request.context == {}:
# An incorrect url path will not create RequestContext
return
# NOTE(lintan): RequestContext will generate a request_id if no one
# passing outside, so it always contain a request_id.
request_id = state.request.context.request_id
state.response.headers['Openstack-Request-Id'] = request_id
class RPCHook(hooks.PecanHook): class RPCHook(hooks.PecanHook):
"""Attach the rpcapi object to the request so controllers can get to it.""" """Attach the rpcapi object to the request so controllers can get to it."""

View File

@ -285,6 +285,26 @@ class TestContextHook(base.BaseApiTest):
is_admin=False, is_admin=False,
roles=headers['X-Roles'].split(',')) roles=headers['X-Roles'].split(','))
@mock.patch.object(context, 'RequestContext')
def test_context_hook_after_add_request_id(self, mock_ctx):
headers = fake_headers(admin=True)
reqstate = FakeRequestState(headers=headers)
reqstate.set_context()
reqstate.request.context.request_id = 'fake-id'
context_hook = hooks.ContextHook(None)
context_hook.after(reqstate)
self.assertIn('Openstack-Request-Id',
reqstate.response.headers)
self.assertEqual(
'fake-id',
reqstate.response.headers['Openstack-Request-Id'])
def test_context_hook_after_miss_context(self):
response = self.get_json('/bad/path',
expect_errors=True)
self.assertNotIn('Openstack-Request-Id',
response.headers)
class TestTrustedCallHook(base.BaseApiTest): class TestTrustedCallHook(base.BaseApiTest):
def test_trusted_call_hook_not_admin(self): def test_trusted_call_hook_not_admin(self):

View File

@ -0,0 +1,4 @@
---
features:
- Append request_id as ``Openstack-Request-Id`` header to the response.