From 4c8f269dd19c742bd80ce3ff7db55ff53be4af7d Mon Sep 17 00:00:00 2001 From: Chris Buccella Date: Fri, 14 Feb 2014 23:29:28 +0000 Subject: [PATCH] Add x-openstack-request-id to cinder responses x-openstack-request-id is the common header name for request ID going forward. Currently, only glance and neutron return this header. Using the request_id middleware is a convenient way to ensure this header is in the response. We will also be using the middleware to generate the request ID. The ID will be generated by the middleware and is inserted into the request environment. The current code generates the request ID in the constructor of RequestConext; with this change, the ID is generated beforehand and passed in to the RequestContext constructor. Not much different here: a request ID is generated and is available for use in the handling of the request. On the response end, the middleware is again used, this time to attach the x-openstack-request-id header, using the value of the generated request ID. The current code attaches the x-compute-request-id header. This change is a necessary step toward logging the mapping of request IDs across OpenStack services. UpgradeImpact: api-paste.ini is modified Change-Id: I30c4e5efa77f9ebffb485d2ea6a6c3f78eb1573f Implements: blueprint add-standard-req-id-header --- cinder/api/middleware/auth.py | 7 ++++++- cinder/tests/api/middleware/test_auth.py | 9 +++++++++ etc/cinder/api-paste.ini | 15 +++++++++------ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/cinder/api/middleware/auth.py b/cinder/api/middleware/auth.py index b1b4c999f..8572949cf 100644 --- a/cinder/api/middleware/auth.py +++ b/cinder/api/middleware/auth.py @@ -28,6 +28,7 @@ from cinder.api.openstack import wsgi from cinder import context from cinder.openstack.common import jsonutils from cinder.openstack.common import log as logging +from cinder.openstack.common.middleware import request_id from cinder import wsgi as base_wsgi @@ -91,6 +92,9 @@ class CinderKeystoneContext(base_wsgi.Middleware): project_id = req.headers['X_TENANT'] project_name = req.headers.get('X_TENANT_NAME') + + req_id = req.environ.get(request_id.ENV_REQUEST_ID) + # Get the auth token auth_token = req.headers.get('X_AUTH_TOKEN', req.headers.get('X_STORAGE_TOKEN')) @@ -115,7 +119,8 @@ class CinderKeystoneContext(base_wsgi.Middleware): roles=roles, auth_token=auth_token, remote_address=remote_address, - service_catalog=service_catalog) + service_catalog=service_catalog, + request_id=req_id) req.environ['cinder.context'] = ctx return self.application diff --git a/cinder/tests/api/middleware/test_auth.py b/cinder/tests/api/middleware/test_auth.py index 600fed80d..bf602d7d4 100644 --- a/cinder/tests/api/middleware/test_auth.py +++ b/cinder/tests/api/middleware/test_auth.py @@ -15,6 +15,7 @@ import webob import cinder.api.middleware.auth +from cinder.openstack.common.middleware import request_id from cinder import test @@ -65,3 +66,11 @@ class TestCinderKeystoneContextMiddleware(test.TestCase): self.assertEqual(response.status, '200 OK') self.assertEqual(self.context.project_id, 'testtenantid') self.assertEqual(self.context.project_name, 'testtenantname') + + def test_request_id_extracted_from_env(self): + req_id = 'dummy-request-id' + self.request.headers['X_PROJECT_ID'] = 'testtenantid' + self.request.headers['X_USER_ID'] = 'testuserid' + self.request.environ[request_id.ENV_REQUEST_ID] = req_id + self.request.get_response(self.middleware) + self.assertEqual(req_id, self.context.request_id) diff --git a/etc/cinder/api-paste.ini b/etc/cinder/api-paste.ini index 5bfd73822..6829edbf6 100644 --- a/etc/cinder/api-paste.ini +++ b/etc/cinder/api-paste.ini @@ -10,15 +10,18 @@ use = call:cinder.api:root_app_factory [composite:openstack_volume_api_v1] use = call:cinder.api.middleware.auth:pipeline_factory -noauth = faultwrap sizelimit noauth apiv1 -keystone = faultwrap sizelimit authtoken keystonecontext apiv1 -keystone_nolimit = faultwrap sizelimit authtoken keystonecontext apiv1 +noauth = request_id faultwrap sizelimit noauth apiv1 +keystone = request_id faultwrap sizelimit authtoken keystonecontext apiv1 +keystone_nolimit = request_id faultwrap sizelimit authtoken keystonecontext apiv1 [composite:openstack_volume_api_v2] use = call:cinder.api.middleware.auth:pipeline_factory -noauth = faultwrap sizelimit noauth apiv2 -keystone = faultwrap sizelimit authtoken keystonecontext apiv2 -keystone_nolimit = faultwrap sizelimit authtoken keystonecontext apiv2 +noauth = request_id faultwrap sizelimit noauth apiv2 +keystone = request_id faultwrap sizelimit authtoken keystonecontext apiv2 +keystone_nolimit = request_id faultwrap sizelimit authtoken keystonecontext apiv2 + +[filter:request_id] +paste.filter_factory = cinder.openstack.common.middleware.request_id:RequestIdMiddleware.factory [filter:faultwrap] paste.filter_factory = cinder.api.middleware.fault:FaultWrapper.factory -- 2.45.2