Add global_request_id param to Adapter constructor
In order to make it easier for projects to enable global_request_id passing, make it something the Adapter understands directly, so that the logic of adding extra headers doesn't need to be in every client. Part of the push to enable global_request_id throughout OpenStack. oslo spec I65de8261746b25d45e105394f4eeb95b9cb3bd42 Change-Id: Ic75be3acb8b77aae8da631e3c4cd6f545a9a35cb
This commit is contained in:
parent
72ad21ee66
commit
0906fb0424
@ -60,6 +60,10 @@ class Adapter(object):
|
||||
:param bool allow_version_hack: Allow keystoneauth to hack up catalog
|
||||
URLS to support older schemes.
|
||||
(optional, default True)
|
||||
:param str global_request_id: A global_request_id (in the form of
|
||||
``req-$uuid``) that will be passed on all
|
||||
requests. Enables cross project request id
|
||||
tracking.
|
||||
"""
|
||||
|
||||
client_name = None
|
||||
@ -71,7 +75,8 @@ class Adapter(object):
|
||||
version=None, auth=None, user_agent=None,
|
||||
connect_retries=None, logger=None, allow={},
|
||||
additional_headers=None, client_name=None,
|
||||
client_version=None, allow_version_hack=None):
|
||||
client_version=None, allow_version_hack=None,
|
||||
global_request_id=None):
|
||||
# NOTE(jamielennox): when adding new parameters to adapter please also
|
||||
# add them to the adapter call in httpclient.HTTPClient.__init__ as
|
||||
# well as to load_adapter_from_argparse below if the argument is
|
||||
@ -92,6 +97,8 @@ class Adapter(object):
|
||||
self.additional_headers = additional_headers or {}
|
||||
self.allow_version_hack = allow_version_hack
|
||||
|
||||
self.global_request_id = global_request_id
|
||||
|
||||
if client_name:
|
||||
self.client_name = client_name
|
||||
if client_version:
|
||||
@ -145,6 +152,10 @@ class Adapter(object):
|
||||
for k, v in self.additional_headers.items():
|
||||
kwargs.setdefault('headers', {}).setdefault(k, v)
|
||||
|
||||
if self.global_request_id is not None:
|
||||
kwargs.setdefault('headers', {}).setdefault(
|
||||
"X-OpenStack-Request-ID", self.global_request_id)
|
||||
|
||||
return self.session.request(url, method, **kwargs)
|
||||
|
||||
def get_token(self, auth=None):
|
||||
|
@ -900,6 +900,29 @@ class AdapterTest(utils.TestCase):
|
||||
self.assertTrue(adpt.auth.get_token_called)
|
||||
self.assertRequestHeaderEqual('User-Agent', self.USER_AGENT)
|
||||
|
||||
def test_setting_global_id_on_request(self):
|
||||
global_id = "req-%s" % uuid.uuid4()
|
||||
response = uuid.uuid4().hex
|
||||
self.stub_url('GET', text=response)
|
||||
adpt = adapter.Adapter(client_session.Session(),
|
||||
auth=CalledAuthPlugin(),
|
||||
service_type=self.SERVICE_TYPE,
|
||||
service_name=self.SERVICE_NAME,
|
||||
interface=self.INTERFACE,
|
||||
region_name=self.REGION_NAME,
|
||||
user_agent=self.USER_AGENT,
|
||||
version=self.VERSION,
|
||||
allow=self.ALLOW,
|
||||
global_request_id=global_id)
|
||||
resp = adpt.get('/')
|
||||
self.assertEqual(resp.text, response)
|
||||
|
||||
self._verify_endpoint_called(adpt)
|
||||
self.assertEqual(self.ALLOW,
|
||||
adpt.auth.endpoint_arguments['allow'])
|
||||
self.assertTrue(adpt.auth.get_token_called)
|
||||
self.assertRequestHeaderEqual('X-OpenStack-Request-ID', global_id)
|
||||
|
||||
def test_setting_variables_on_get_endpoint(self):
|
||||
adpt = self._create_loaded_adapter()
|
||||
url = adpt.get_endpoint()
|
||||
|
Loading…
Reference in New Issue
Block a user