From 039f3d893dae74f1d3b3b0bca6f60904ae4a9b15 Mon Sep 17 00:00:00 2001 From: Doug Hellmann Date: Mon, 13 Aug 2012 12:28:31 -0400 Subject: [PATCH] Add separate notification handler for instance flavor This notification handler records a meter event with the instance flavor included, making it easier to query for the amount of time an instance existed as a specific flavor. Change-Id: Ic162ac021f864c2a98c47127288867e940469ceb Signed-off-by: Doug Hellmann --- ceilometer/compute/notifications.py | 27 ++++++++++++++++++++++++++- setup.py | 1 + tests/compute/test_notifications.py | 16 ++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/ceilometer/compute/notifications.py b/ceilometer/compute/notifications.py index 64d8bbebb..ae31b5aea 100644 --- a/ceilometer/compute/notifications.py +++ b/ceilometer/compute/notifications.py @@ -124,4 +124,29 @@ class EphemeralDiskSize(_Base): timestamp=message['timestamp'], duration=0, resource_metadata={}), - ] + ] + + +class InstanceFlavor(_Base): + + @staticmethod + def process_notification(message): + counters = [] + metadata = instance.get_metadata_from_event(message) + instance_type = message.get('payload', {}).get('instance_type') + if instance_type: + counters.append( + counter.Counter( + source='?', + name='instance:%s' % instance_type, + type='absolute', + volume=1, + user_id=message['payload']['user_id'], + project_id=message['payload']['tenant_id'], + resource_id=message['payload']['instance_id'], + timestamp=message['timestamp'], + duration=0, + resource_metadata=metadata, + ) + ) + return counters diff --git a/setup.py b/setup.py index 726d85f09..c3cd0d702 100755 --- a/setup.py +++ b/setup.py @@ -38,6 +38,7 @@ setuptools.setup( entry_points=textwrap.dedent(""" [ceilometer.collector.compute] instance = ceilometer.compute.notifications:Instance + instance_flavor = ceilometer.compute.notifications:InstanceFlavor memory = ceilometer.compute.notifications:Memory vcpus = ceilometer.compute.notifications:VCpus root_disk_size = ceilometer.compute.notifications:RootDiskSize diff --git a/tests/compute/test_notifications.py b/tests/compute/test_notifications.py index 971c29428..eb73c821d 100644 --- a/tests/compute/test_notifications.py +++ b/tests/compute/test_notifications.py @@ -197,6 +197,13 @@ class TestNotifications(unittest.TestCase): c = counters[0] self.assertEqual(c.volume, 1) + def test_instance_create_flavor(self): + ic = notifications.InstanceFlavor() + counters = ic.process_notification(INSTANCE_CREATE_END) + self.assertEqual(len(counters), 1) + c = counters[0] + self.assertEqual(c.volume, 1) + def test_instance_create_memory(self): ic = notifications.Memory() counters = ic.process_notification(INSTANCE_CREATE_END) @@ -231,8 +238,17 @@ class TestNotifications(unittest.TestCase): counters = ic.process_notification(INSTANCE_EXISTS) self.assertEqual(len(counters), 1) + def test_instance_exists_flavor(self): + ic = notifications.Instance() + counters = ic.process_notification(INSTANCE_EXISTS) + self.assertEqual(len(counters), 1) + def test_instance_delete_instance(self): ic = notifications.Instance() counters = ic.process_notification(INSTANCE_DELETE_START) self.assertEqual(len(counters), 1) + def test_instance_delete_flavor(self): + ic = notifications.Instance() + counters = ic.process_notification(INSTANCE_DELETE_START) + self.assertEqual(len(counters), 1)