From a44befb061fc2b5bb60ab120c64ca573757ab6af Mon Sep 17 00:00:00 2001 From: Adrian Czarnecki Date: Tue, 28 Apr 2020 13:46:40 -0700 Subject: [PATCH] Improve error handling in method that creates Monasca client Add error handler that prevent crash of forwarded when agent is not able to connect to keystone Story: 2007674 Task: 39781 Change-Id: If6366e5b94f9cbe3f21ce9dbeb26d28e3a36ae88 --- monasca_agent/forwarder/api/monasca_api.py | 29 ++++++++++++---------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/monasca_agent/forwarder/api/monasca_api.py b/monasca_agent/forwarder/api/monasca_api.py index 54bcdfc3..50794251 100644 --- a/monasca_agent/forwarder/api/monasca_api.py +++ b/monasca_agent/forwarder/api/monasca_api.py @@ -18,9 +18,9 @@ import copy import json import logging -from osc_lib import exceptions - +from keystoneauth1.exceptions import base as keystoneauth_exception from monascaclient import client +from osc_lib import exceptions from monasca_agent.common import keystone @@ -136,17 +136,20 @@ class MonascaAPI(object): self._post(tenant_group[tenant], tenant) def _get_mon_client(self): - k = keystone.Keystone(self._config) - endpoint = k.get_monasca_url() - session = k.get_session() - c = client.Client( - api_version=self._api_version, - endpoint=endpoint, - session=session, - timeout=self.write_timeout, - **keystone.get_args(self._config) - ) - return c + try: + k = keystone.Keystone(self._config) + endpoint = k.get_monasca_url() + session = k.get_session() + c = client.Client( + api_version=self._api_version, + endpoint=endpoint, + session=session, + timeout=self.write_timeout, + **keystone.get_args(self._config) + ) + return c + except keystoneauth_exception.ClientException as ex: + log.error('Failed to initialize Monasca client. {}'.format(ex)) def _send_message(self, **kwargs): try: