From 43884bfbb4c6e2e07a98f368610b686aedc1e42b Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Wed, 4 Dec 2013 18:53:18 +0000 Subject: [PATCH] Add Sample priority With the routing notifier we can now issue high-frequency notifications and not worry about clogging up the queue. The Sample priority will be used for this situation. Change-Id: Ia4aa77b7aa4ca9458e97ca3ed81101d92934b691 --- oslo/messaging/notify/_impl_log.py | 4 +++- oslo/messaging/notify/notifier.py | 18 ++++++++++++++++++ tests/test_notifier.py | 18 ++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/oslo/messaging/notify/_impl_log.py b/oslo/messaging/notify/_impl_log.py index 3a0971845..5f6c1ed3d 100644 --- a/oslo/messaging/notify/_impl_log.py +++ b/oslo/messaging/notify/_impl_log.py @@ -30,4 +30,6 @@ class LogDriver(notifier._Driver): def notify(self, ctxt, message, priority): logger = logging.getLogger('%s.%s' % (self.LOGGER_BASE, message['event_type'])) - getattr(logger, priority.lower())(jsonutils.dumps(message)) + method = getattr(logger, priority.lower(), None) + if method: + method(jsonutils.dumps(message)) diff --git a/oslo/messaging/notify/notifier.py b/oslo/messaging/notify/notifier.py index 5e559c306..e2410668a 100644 --- a/oslo/messaging/notify/notifier.py +++ b/oslo/messaging/notify/notifier.py @@ -243,6 +243,24 @@ class Notifier(object): """ self._notify(ctxt, event_type, payload, 'CRITICAL') + def sample(self, ctxt, event_type, payload): + """Send a notification at sample level. + + Sample notifications are for high-frequency events + that typically contain small payloads. eg: "CPU = 70%" + + Not all drivers support the sample level + (log, for example) so these could be dropped. + + :param ctxt: a request context dict + :type ctxt: dict + :param event_type: describes the event, e.g. 'compute.create_instance' + :type event_type: str + :param payload: the notification payload + :type payload: dict + """ + self._notify(ctxt, event_type, payload, 'SAMPLE') + class _SubNotifier(Notifier): diff --git a/tests/test_notifier.py b/tests/test_notifier.py index 162b19511..81637c483 100644 --- a/tests/test_notifier.py +++ b/tests/test_notifier.py @@ -21,6 +21,7 @@ import fixtures import testscenarios from oslo import messaging +from oslo.messaging.notify import _impl_log from oslo.messaging.notify import _impl_messaging from oslo.messaging.notify import _impl_test from oslo.messaging.notify import notifier as msg_notifier @@ -104,6 +105,7 @@ class TestMessagingNotifier(test_utils.BaseTestCase): ('info', dict(priority='info')), ('warn', dict(priority='warn')), ('error', dict(priority='error')), + ('sample', dict(priority='sample')), ('critical', dict(priority='critical')), ] @@ -283,3 +285,19 @@ class TestLogNotifier(test_utils.BaseTestCase): self.mox.ReplayAll() notifier.info({}, 'test.notify', 'bar') + + def test_sample_priority(self): + # Ensure logger drops sample-level notifications. + driver = _impl_log.LogDriver(None, None, None) + + logger = self.mox.CreateMock( + logging.getLogger('oslo.messaging.notification.foo')) + logger.sample = None + self.mox.StubOutWithMock(logging, 'getLogger') + logging.getLogger('oslo.messaging.notification.foo').\ + AndReturn(logger) + + self.mox.ReplayAll() + + msg = {'event_type': 'foo'} + driver.notify(None, msg, "sample")