diff options
author | Akihiro Motoki <motoki@da.jp.nec.com> | 2013-12-09 18:38:29 +0900 |
---|---|---|
committer | Akihiro Motoki <motoki@da.jp.nec.com> | 2013-12-09 18:38:29 +0900 |
commit | 085c4705cf86142b6c6abd2068cf77a73a9b1d19 (patch) | |
tree | 8b86f055f3675041f0b631131dbae06b223bf1e8 | |
parent | 87e57c978045a357347895bd92230b101e8ab8b4 (diff) | |
download | oslo-middleware-085c4705cf86142b6c6abd2068cf77a73a9b1d19.tar.gz |
Middlelware to ensure request ID
The feature of the proposed middleware is simple:
* Generate request-id for each REST request
* Set the generated request-id to request environment
* Add X-OpenStack-Request-Id header to API response to return request-id
blueprint middleware-request-id
Note that to use this middleware with Context, we need to change
auth middlware so that request-id will be retreived from request env.
Change-Id: Ic7967cd62e7b743343d70f751b9238339171e013
-rw-r--r-- | openstack/common/middleware/request_id.py | 38 | ||||
-rw-r--r-- | tests/unit/middleware/test_request_id.py | 37 |
2 files changed, 75 insertions, 0 deletions
diff --git a/openstack/common/middleware/request_id.py b/openstack/common/middleware/request_id.py new file mode 100644 index 0000000..d71eb6e --- /dev/null +++ b/openstack/common/middleware/request_id.py @@ -0,0 +1,38 @@ +# Copyright (c) 2013 NEC Corporation +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +"""Middleware that ensures request ID. + +It ensures to assign request ID for each API request and set it to +request environment. The request ID is also added to API response. +""" + +from openstack.common import context +from openstack.common.middleware import base + + +ENV_REQUEST_ID = 'openstack.request_id' +HTTP_RESP_HEADER_REQUEST_ID = 'x-openstack-request-id' + + +class RequestIdMiddleware(base.Middleware): + + def process_request(self, req): + self.req_id = context.generate_request_id() + req.environ[ENV_REQUEST_ID] = self.req_id + + def process_response(self, response): + response.headers.add(HTTP_RESP_HEADER_REQUEST_ID, self.req_id) + return response diff --git a/tests/unit/middleware/test_request_id.py b/tests/unit/middleware/test_request_id.py new file mode 100644 index 0000000..bdd903d --- /dev/null +++ b/tests/unit/middleware/test_request_id.py @@ -0,0 +1,37 @@ +# Copyright (c) 2013 NEC Corporation +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + +from testtools import matchers +import webob +import webob.dec + +from openstack.common.middleware import request_id +from openstack.common import test + + +class RequestIdTest(test.BaseTestCase): + def test_generate_request_id(self): + @webob.dec.wsgify + def application(req): + return req.environ[request_id.ENV_REQUEST_ID] + + app = request_id.RequestIdMiddleware(application) + req = webob.Request.blank('/test') + res = req.get_response(app) + res_req_id = res.headers.get(request_id.HTTP_RESP_HEADER_REQUEST_ID) + self.assertThat(res_req_id, matchers.StartsWith('req-')) + # request-id in request environ is returned as response body + self.assertEqual(res_req_id, res.body) |