Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Make it possible to get the request_id from python clients
Browse files Browse the repository at this point in the history
This is so callers can extract the request_id for tracing
and associating with events (this is especially useful in Heat
where there are a lot of client calls and one needs to map
a Heat action to the underlying service requests).

Partial-bug: #1342922
Change-Id: I6c60946c8fcdb44f1b18733296982bf0d32334dc
  • Loading branch information
Angus Salkeld committed Aug 28, 2014
1 parent 0533894 commit 94245b1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
2 changes: 2 additions & 0 deletions openstack/common/apiclient/base.py
Expand Up @@ -488,6 +488,8 @@ def get(self):
new = self.manager.get(self.id)
if new:
self._add_details(new._info)
self._add_details(
{'x_request_id': self.manager.client.last_request_id})

def __eq__(self, other):
if not isinstance(other, Resource):
Expand Down
7 changes: 7 additions & 0 deletions openstack/common/apiclient/client.py
Expand Up @@ -98,6 +98,7 @@ def __init__(self,
self.http = http or requests.Session()

self.cached_token = None
self.last_request_id = None

def _http_log_req(self, method, url, kwargs):
if not self.debug:
Expand Down Expand Up @@ -177,6 +178,8 @@ def request(self, method, url, **kwargs):
start_time, time.time()))
self._http_log_resp(resp)

self.last_request_id = resp.headers.get('x-openstack-request-id')

if resp.status_code >= 400:
_logger.debug(
"Request returned failure status: %s",
Expand Down Expand Up @@ -323,6 +326,10 @@ def client_request(self, method, url, **kwargs):
return self.http_client.client_request(
self, method, url, **kwargs)

@property
def last_request_id(self):
return self.http_client.last_request_id

def head(self, url, **kwargs):
return self.client_request("HEAD", url, **kwargs)

Expand Down
2 changes: 2 additions & 0 deletions openstack/common/apiclient/fake_client.py
Expand Up @@ -168,6 +168,8 @@ def client_request(self, client, method, url, **kwargs):
else:
status, body = resp
headers = {}
self.last_request_id = headers.get('x-openstack-request-id',
'req-test')
return TestResponse({
"status_code": status,
"text": body,
Expand Down
29 changes: 24 additions & 5 deletions tests/unit/apiclient/test_base.py
Expand Up @@ -66,16 +66,23 @@ class FakeHTTPClient(fake_client.FakeHTTPClient):
crud_resource_json = {"id": "1", "domain_id": "my-domain"}

def get_human_resources(self, **kw):
return (200, {}, {'human_resources': [
{'id': 1, 'name': '256 MB Server'},
{'id': 2, 'name': '512 MB Server'},
{'id': 'aa1', 'name': '128 MB Server'}
]})
return (200, {},
{'human_resources': [
{'id': 1, 'name': '256 MB Server'},
{'id': 2, 'name': '512 MB Server'},
{'id': 'aa1', 'name': '128 MB Server'},
{'id': 3, 'name': '4 MB Server'},
]})

def get_human_resources_1(self, **kw):
res = self.get_human_resources()[2]['human_resources'][0]
return (200, {}, {'human_resource': res})

def get_human_resources_3(self, **kw):
res = self.get_human_resources()[2]['human_resources'][3]
return (200, {'x-openstack-request-id': 'req-flappy'},
{'human_resource': res})

def put_human_resources_1(self, **kw):
kw = kw["json"]["human_resource"].copy()
kw["id"] = "1"
Expand Down Expand Up @@ -152,6 +159,18 @@ def test_resource_lazy_getattr(self):
# Missing stuff still fails after a second get
self.assertRaises(AttributeError, getattr, f, 'blahblah')

def test_resource_req_id(self):
f = HumanResource(self.tc.human_resources, {'id': 1})
self.assertEqual(f.name, '256 MB Server')
self.assertEqual(f.x_request_id, 'req-test')
self.http_client.assert_called('GET', '/human_resources/1')

def test_resource_req_id_header(self):
f = HumanResource(self.tc.human_resources, {'id': 3})
self.assertEqual(f.name, '4 MB Server')
self.assertEqual(f.x_request_id, 'req-flappy')
self.http_client.assert_called('GET', '/human_resources/3')

def test_eq(self):
# Two resources of the same type with the same id: equal
r1 = base.Resource(None, {'id': 1, 'name': 'hi'})
Expand Down

0 comments on commit 94245b1

Please sign in to comment.