From ec76e254da4f4727463dcb3a76adf33e5d3f7ffb Mon Sep 17 00:00:00 2001 From: Sean Dague Date: Wed, 24 May 2017 07:17:30 -0400 Subject: [PATCH] Allow global_request_id in Client constructor This allows us to pass in a global_request_id in the client constructor so that subsequent calls with this client pass that to the servers. This enables cross project request_id tracking. oslo spec I65de8261746b25d45e105394f4eeb95b9cb3bd42 Change-Id: Iea1e754a263a01dae5ed598fdda134394aff54b0 --- glanceclient/common/http.py | 9 +++++++++ glanceclient/tests/unit/test_http.py | 13 +++++++++++++ 2 files changed, 22 insertions(+) diff --git a/glanceclient/common/http.py b/glanceclient/common/http.py index c46b89b..da38613 100644 --- a/glanceclient/common/http.py +++ b/glanceclient/common/http.py @@ -40,6 +40,7 @@ osprofiler_web = importutils.try_import("osprofiler.web") LOG = logging.getLogger(__name__) USER_AGENT = 'python-glanceclient' CHUNKSIZE = 1024 * 64 # 64kB +REQ_ID_HEADER = 'X-OpenStack-Request-ID' def encode_headers(headers): @@ -130,6 +131,7 @@ class HTTPClient(_BaseHTTPClient): self.identity_headers = kwargs.get('identity_headers') self.auth_token = kwargs.get('token') self.language_header = kwargs.get('language_header') + self.global_request_id = kwargs.get('global_request_id') if self.identity_headers: self.auth_token = self.identity_headers.pop('X-Auth-Token', self.auth_token) @@ -225,6 +227,9 @@ class HTTPClient(_BaseHTTPClient): if not headers.get('X-Auth-Token'): headers['X-Auth-Token'] = self.auth_token + if self.global_request_id: + headers.setdefault(REQ_ID_HEADER, self.global_request_id) + if osprofiler_web: headers.update(osprofiler_web.get_trace_id_headers()) @@ -312,10 +317,14 @@ class SessionClient(adapter.Adapter, _BaseHTTPClient): def __init__(self, session, **kwargs): kwargs.setdefault('user_agent', USER_AGENT) kwargs.setdefault('service_type', 'image') + self.global_request_id = kwargs.pop('global_request_id', None) super(SessionClient, self).__init__(session, **kwargs) def request(self, url, method, **kwargs): headers = kwargs.pop('headers', {}) + if self.global_request_id: + headers.setdefault(REQ_ID_HEADER, self.global_request_id) + kwargs['raise_exc'] = False data = self._set_common_request_kwargs(headers, kwargs) try: diff --git a/glanceclient/tests/unit/test_http.py b/glanceclient/tests/unit/test_http.py index ee82cf8..c57d5d4 100644 --- a/glanceclient/tests/unit/test_http.py +++ b/glanceclient/tests/unit/test_http.py @@ -15,6 +15,7 @@ import functools import json import logging +import uuid import fixtures from keystoneauth1 import session @@ -151,6 +152,18 @@ class TestClient(testtools.TestCase): headers = self.mock.last_request.headers self.assertEqual(kwargs['language_header'], headers['Accept-Language']) + def test_request_id_header_passed(self): + global_id = encodeutils.safe_encode("req-%s" % uuid.uuid4()) + kwargs = {'global_request_id': global_id} + http_client = http.HTTPClient(self.endpoint, **kwargs) + + path = '/v2/images/my-image' + self.mock.get(self.endpoint + path) + http_client.get(path) + + headers = self.mock.last_request.headers + self.assertEqual(global_id, headers['X-OpenStack-Request-ID']) + def test_language_header_not_passed_no_language(self): kwargs = {} http_client = http.HTTPClient(self.endpoint, **kwargs)