From 94c4c52012e10ea2b0932519a758e5f05653a594 Mon Sep 17 00:00:00 2001 From: ZhiQiang Fan Date: Tue, 3 May 2016 20:17:04 +0800 Subject: [PATCH] log alarm rest notifier response Currently we do nothing after post the rest action, so we don't know the actual result of it, except it raises exception. This is not right because there is no exception even for 400 and 500 error code. This patch adds a log message at info level to enable cloud administrator track and audit the alarm notify result. like: Notifying alarm <537944be-1dfd-48fe-b272-2ae0b04e04e1> gets response: 500 Internal Server Error. Change-Id: I56ab6256260697d2e9fe66dfc30e0adbb4024814 --- aodh/notifier/rest.py | 10 +++++++--- aodh/tests/unit/test_notifier.py | 8 +++++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/aodh/notifier/rest.py b/aodh/notifier/rest.py index 8683b445c..8fad59c85 100644 --- a/aodh/notifier/rest.py +++ b/aodh/notifier/rest.py @@ -22,7 +22,7 @@ from oslo_serialization import jsonutils import requests import six.moves.urllib.parse as urlparse -from aodh.i18n import _ +from aodh.i18n import _LI from aodh import notifier LOG = log.getLogger(__name__) @@ -63,7 +63,7 @@ class RestAlarmNotifier(notifier.AlarmNotifier): headers['x-openstack-request-id'] = b'req-' + str( uuid.uuid4()).encode('ascii') - LOG.info(_( + LOG.info(_LI( "Notifying alarm %(alarm_name)s %(alarm_id)s with severity" " %(severity)s from %(previous)s to %(current)s with action " "%(action)s because %(reason)s. request-id: %(request_id)s ") % @@ -99,4 +99,8 @@ class RestAlarmNotifier(notifier.AlarmNotifier): session = requests.Session() session.mount(action.geturl(), requests.adapters.HTTPAdapter(max_retries=max_retries)) - session.post(action.geturl(), **kwargs) + resp = session.post(action.geturl(), **kwargs) + LOG.info(_LI('Notifying alarm <%(id)s> gets response: %(status_code)s ' + '%(reason)s.'), {'id': alarm_id, + 'status_code': resp.status_code, + 'reason': resp.reason}) diff --git a/aodh/tests/unit/test_notifier.py b/aodh/tests/unit/test_notifier.py index bee763bf6..6334b9d19 100644 --- a/aodh/tests/unit/test_notifier.py +++ b/aodh/tests/unit/test_notifier.py @@ -105,7 +105,8 @@ class TestAlarmNotifier(tests_base.BaseTestCase): notification['actions'] = [action] return notification - def test_notify_alarm_rest_action_ok(self): + @mock.patch('aodh.notifier.rest.LOG') + def test_notify_alarm_rest_action_ok(self, m_log): action = 'http://host/action' with mock.patch.object(requests.Session, 'post') as poster: @@ -124,6 +125,11 @@ class TestAlarmNotifier(tests_base.BaseTestCase): }, kwargs['headers']) self.assertEqual(DATA_JSON, jsonutils.loads(kwargs['data'])) + self.assertEqual(2, len(m_log.info.call_args_list)) + expected = mock.call('Notifying alarm <%(id)s> gets response: ' + '%(status_code)s %(reason)s.', + mock.ANY) + self.assertEqual(expected, m_log.info.call_args_list[1]) def test_notify_alarm_rest_action_with_ssl_client_cert(self): action = 'https://host/action'