rename sample handler

process_notification and process_notifications are very similar.
renamed process_notification to build_sample so we don't accidentally
call wrong thing. also, it's a bit more descriptive.

Change-Id: Id838ae552e822479208337b9ece415981fb5b25a
This commit is contained in:
gord chung 2017-10-04 21:34:53 +00:00
parent 000c5d89a3
commit 2768334d01
9 changed files with 48 additions and 48 deletions

@ -90,7 +90,7 @@ class SensorNotification(endpoint.SampleEndpoint):
'payload': payload}
return info
def process_notification(self, message):
def build_sample(self, message):
"""Read and process a notification.
The guts of a message are in dict value of a 'payload' key

@ -224,7 +224,7 @@ class ProcessMeterNotifications(endpoint.SampleEndpoint):
definitions[meter_cfg['name']] = md
return definitions.values()
def process_notification(self, notification):
def build_sample(self, notification):
for d in self.definitions:
if d.match_type(notification['event_type']):
for s in d.to_samples(notification):

@ -20,7 +20,7 @@ from ceilometer import sample
class HTTPRequest(endpoint.SampleEndpoint):
event_types = ['http.request']
def process_notification(self, message):
def build_sample(self, message):
yield sample.Sample.from_notification(
name=message['event_type'],
type=sample.TYPE_DELTA,

@ -37,10 +37,10 @@ class SampleEndpoint(pipeline.NotificationEndpoint):
for message in notifications:
try:
with self.manager.publisher() as p:
p(list(self.process_notification(message)))
p(list(self.build_sample(message)))
except Exception:
LOG.error('Fail to process notification', exc_info=True)
def process_notification(notification):
def build_sample(notification):
"""Build sample from provided notification."""
pass

@ -22,7 +22,7 @@ class TelemetryIpc(endpoint.SampleEndpoint):
event_types = ['telemetry.polling']
def process_notification(self, message):
def build_sample(self, message):
samples = message['payload']['samples']
for sample_dict in samples:
yield sample.Sample(

@ -40,7 +40,7 @@ class TestNotifications(base.BaseTestCase):
"""
processor = ipmi.TemperatureSensorNotification(None)
counters = dict([(counter.resource_id, counter) for counter in
processor.process_notification(
processor.build_sample(
ipmi_test_data.SENSOR_DATA)])
self.assertEqual(10, len(counters),
@ -66,7 +66,7 @@ class TestNotifications(base.BaseTestCase):
"""
processor = ipmi.CurrentSensorNotification(None)
counters = dict([(counter.resource_id, counter) for counter in
processor.process_notification(
processor.build_sample(
ipmi_test_data.SENSOR_DATA)])
self.assertEqual(1, len(counters), 'expected 1 current reading')
@ -87,7 +87,7 @@ class TestNotifications(base.BaseTestCase):
"""
processor = ipmi.FanSensorNotification(None)
counters = dict([(counter.resource_id, counter) for counter in
processor.process_notification(
processor.build_sample(
ipmi_test_data.SENSOR_DATA)])
self.assertEqual(12, len(counters), 'expected 12 fan readings')
@ -108,7 +108,7 @@ class TestNotifications(base.BaseTestCase):
"""
processor = ipmi.VoltageSensorNotification(None)
counters = dict([(counter.resource_id, counter) for counter in
processor.process_notification(
processor.build_sample(
ipmi_test_data.SENSOR_DATA)])
self.assertEqual(4, len(counters), 'expected 4 volate readings')
@ -125,7 +125,7 @@ class TestNotifications(base.BaseTestCase):
"""Test that a meter which a disabled volume is skipped."""
processor = ipmi.TemperatureSensorNotification(None)
counters = dict([(counter.resource_id, counter) for counter in
processor.process_notification(
processor.build_sample(
ipmi_test_data.SENSOR_DATA)])
self.assertEqual(10, len(counters),
@ -140,7 +140,7 @@ class TestNotifications(base.BaseTestCase):
def test_empty_payload_no_metrics_success(self):
processor = ipmi.TemperatureSensorNotification(None)
counters = dict([(counter.resource_id, counter) for counter in
processor.process_notification(
processor.build_sample(
ipmi_test_data.EMPTY_PAYLOAD)])
self.assertEqual(0, len(counters), 'expected 0 readings')
@ -152,7 +152,7 @@ class TestNotifications(base.BaseTestCase):
messages = []
mylog.warning = lambda *args: messages.extend(args)
list(processor.process_notification(ipmi_test_data.MISSING_SENSOR))
list(processor.build_sample(ipmi_test_data.MISSING_SENSOR))
self.assertEqual(
'invalid sensor data for '
@ -168,7 +168,7 @@ class TestNotifications(base.BaseTestCase):
messages = []
mylog.warning = lambda *args: messages.extend(args)
list(processor.process_notification(ipmi_test_data.BAD_SENSOR))
list(processor.build_sample(ipmi_test_data.BAD_SENSOR))
self.assertEqual(
'invalid sensor data for '
@ -189,7 +189,7 @@ class TestNotifications(base.BaseTestCase):
messages = []
mylog.warning = lambda *args: messages.extend(args)
list(processor.process_notification(ipmi_test_data.NO_NODE_ID))
list(processor.build_sample(ipmi_test_data.NO_NODE_ID))
self.assertEqual(
'invalid sensor data for missing id: missing key in payload: '
@ -205,7 +205,7 @@ class TestNotifications(base.BaseTestCase):
messages = []
mylog.warning = lambda *args: messages.extend(args)
list(processor.process_notification(ipmi_test_data.NO_SENSOR_ID))
list(processor.build_sample(ipmi_test_data.NO_SENSOR_ID))
self.assertEqual(
'invalid sensor data for missing id: missing key in payload: '

@ -337,7 +337,7 @@ class TestMeterProcessing(test.BaseTestCase):
resource_id="$.payload.resource_id",
project_id="$.payload.project_id")]})
self._load_meter_def_file(cfg)
c = list(self.handler.process_notification(NOTIFICATION))
c = list(self.handler.build_sample(NOTIFICATION))
self.assertEqual(1, len(c))
s1 = c[0].as_dict()
self.assertEqual('test1', s1['name'])
@ -362,7 +362,7 @@ class TestMeterProcessing(test.BaseTestCase):
resource_id="$.payload.resource_id",
project_id="$.payload.project_id")]})
self._load_meter_def_file(cfg)
data = list(self.handler.process_notification(NOTIFICATION))
data = list(self.handler.build_sample(NOTIFICATION))
self.assertEqual(2, len(data))
expected_names = ['test1', 'test2']
for s in data:
@ -378,7 +378,7 @@ class TestMeterProcessing(test.BaseTestCase):
resource_id="$.payload.resource_id",
project_id="$.payload.project_id")]})
self._load_meter_def_file(cfg)
c = list(self.handler.process_notification(NOTIFICATION))
c = list(self.handler.build_sample(NOTIFICATION))
self.assertEqual(0, len(c))
def test_regex_match_meter(self):
@ -391,7 +391,7 @@ class TestMeterProcessing(test.BaseTestCase):
resource_id="$.payload.resource_id",
project_id="$.payload.project_id")]})
self._load_meter_def_file(cfg)
c = list(self.handler.process_notification(NOTIFICATION))
c = list(self.handler.build_sample(NOTIFICATION))
self.assertEqual(1, len(c))
def test_default_timestamp(self):
@ -407,7 +407,7 @@ class TestMeterProcessing(test.BaseTestCase):
project_id="$.payload.initiator.project_id",
multi="name")]})
self._load_meter_def_file(cfg)
c = list(self.handler.process_notification(event))
c = list(self.handler.build_sample(event))
self.assertEqual(1, len(c))
s1 = c[0].as_dict()
self.assertEqual(MIDDLEWARE_EVENT['metadata']['timestamp'],
@ -427,7 +427,7 @@ class TestMeterProcessing(test.BaseTestCase):
multi="name",
timestamp='$.payload.eventTime')]})
self._load_meter_def_file(cfg)
c = list(self.handler.process_notification(event))
c = list(self.handler.build_sample(event))
self.assertEqual(1, len(c))
s1 = c[0].as_dict()
self.assertEqual(MIDDLEWARE_EVENT['payload']['eventTime'],
@ -445,7 +445,7 @@ class TestMeterProcessing(test.BaseTestCase):
timestamp="$.payload.metrics"
"[?(@.name='cpu.frequency')].timestamp")]})
self._load_meter_def_file(cfg)
c = list(self.handler.process_notification(METRICS_UPDATE))
c = list(self.handler.build_sample(METRICS_UPDATE))
self.assertEqual(1, len(c))
s1 = c[0].as_dict()
self.assertEqual('compute.node.cpu.frequency', s1['name'])
@ -461,7 +461,7 @@ class TestMeterProcessing(test.BaseTestCase):
resource_id="$.payload.resource_id",
project_id="$.payload.project_id")]})
self._load_meter_def_file(cfg)
c = list(self.handler.process_notification(NOTIFICATION))
c = list(self.handler.build_sample(NOTIFICATION))
self.assertEqual(1, len(c))
s1 = c[0].as_dict()
meta = NOTIFICATION['payload'].copy()
@ -481,7 +481,7 @@ class TestMeterProcessing(test.BaseTestCase):
resource_id="$.payload.resource_id",
project_id="$.payload.project_id")]})
self._load_meter_def_file(cfg)
c = list(self.handler.process_notification(NOTIFICATION))
c = list(self.handler.build_sample(NOTIFICATION))
self.assertEqual(1, len(c))
s1 = c[0].as_dict()
self.assertEqual(5.0, s1['volume'])
@ -498,7 +498,7 @@ class TestMeterProcessing(test.BaseTestCase):
metadata={'proj': '$.payload.project_id',
'dict': '$.payload.resource_metadata'})]})
self._load_meter_def_file(cfg)
c = list(self.handler.process_notification(NOTIFICATION))
c = list(self.handler.build_sample(NOTIFICATION))
self.assertEqual(1, len(c))
s1 = c[0].as_dict()
meta = {'proj': s1['project_id'],
@ -516,7 +516,7 @@ class TestMeterProcessing(test.BaseTestCase):
project_id="$.payload.project_id",
user_metadata="$.payload.metadata",)]})
self._load_meter_def_file(cfg)
c = list(self.handler.process_notification(USER_META))
c = list(self.handler.build_sample(USER_META))
self.assertEqual(1, len(c))
s1 = c[0].as_dict()
meta = {'user_metadata': {'xyz': 'abc'}}
@ -534,7 +534,7 @@ class TestMeterProcessing(test.BaseTestCase):
user_metadata="$.payload.metadata",
metadata={'proj': '$.payload.project_id'})]})
self._load_meter_def_file(cfg)
c = list(self.handler.process_notification(USER_META))
c = list(self.handler.build_sample(USER_META))
self.assertEqual(1, len(c))
s1 = c[0].as_dict()
meta = {'user_metadata': {'xyz': 'abc'}, 'proj': s1['project_id']}
@ -557,7 +557,7 @@ class TestMeterProcessing(test.BaseTestCase):
resource_id="$.payload.resource_id",
project_id="$.payload.project_id")]})
self._load_meter_def_file(cfg)
c = list(self.handler.process_notification(NOTIFICATION))
c = list(self.handler.build_sample(NOTIFICATION))
self.assertEqual(2, len(c))
def test_multi_meter_payload(self):
@ -571,7 +571,7 @@ class TestMeterProcessing(test.BaseTestCase):
project_id="$.payload.initiator.project_id",
lookup=["name", "volume", "unit"])]})
self._load_meter_def_file(cfg)
c = list(self.handler.process_notification(MIDDLEWARE_EVENT))
c = list(self.handler.build_sample(MIDDLEWARE_EVENT))
self.assertEqual(2, len(c))
s1 = c[0].as_dict()
self.assertEqual('storage.objects.outgoing.bytes', s1['name'])
@ -595,7 +595,7 @@ class TestMeterProcessing(test.BaseTestCase):
project_id="$.payload.initiator.project_id",
lookup=["name", "unit"])]})
self._load_meter_def_file(cfg)
c = list(self.handler.process_notification(event))
c = list(self.handler.build_sample(event))
self.assertEqual(1, len(c))
s1 = c[0].as_dict()
self.assertEqual('storage.objects.outgoing.bytes', s1['name'])
@ -615,7 +615,7 @@ class TestMeterProcessing(test.BaseTestCase):
project_id="$.payload.initiator.project_id",
lookup="name")]})
self._load_meter_def_file(cfg)
c = list(self.handler.process_notification(event))
c = list(self.handler.build_sample(event))
self.assertEqual(0, len(c))
def test_multi_meter_payload_all_multi(self):
@ -631,7 +631,7 @@ class TestMeterProcessing(test.BaseTestCase):
lookup=['name', 'type', 'unit', 'volume',
'resource_id', 'project_id', 'user_id'])]})
self._load_meter_def_file(cfg)
c = list(self.handler.process_notification(FULL_MULTI_MSG))
c = list(self.handler.build_sample(FULL_MULTI_MSG))
self.assertEqual(2, len(c))
msg = FULL_MULTI_MSG['payload']
for idx, val in enumerate(c):
@ -659,7 +659,7 @@ class TestMeterProcessing(test.BaseTestCase):
project_id="$.payload.initiator.project_id",
lookup=["name", "unit", "volume"])]})
self._load_meter_def_file(cfg)
c = list(self.handler.process_notification(event))
c = list(self.handler.build_sample(event))
self.assertEqual(0, len(c))
LOG.warning.assert_called_with('Only 0 fetched meters contain '
'"volume" field instead of 2.')
@ -678,7 +678,7 @@ class TestMeterProcessing(test.BaseTestCase):
project_id="$.payload.initiator.project_id",
lookup=["name", "unit", "volume"])]})
self._load_meter_def_file(cfg)
c = list(self.handler.process_notification(event))
c = list(self.handler.build_sample(event))
self.assertEqual(0, len(c))
LOG.warning.assert_called_with('Only 1 fetched meters contain '
'"volume" field instead of 2.')
@ -695,7 +695,7 @@ class TestMeterProcessing(test.BaseTestCase):
resource_id="$.payload.host + '_'"
" + $.payload.nodename")]})
self._load_meter_def_file(cfg)
c = list(self.handler.process_notification(METRICS_UPDATE))
c = list(self.handler.build_sample(METRICS_UPDATE))
self.assertEqual(1, len(c))
s1 = c[0].as_dict()
self.assertEqual('compute.node.cpu.percent', s1['name'])
@ -714,7 +714,7 @@ class TestMeterProcessing(test.BaseTestCase):
resource_id="$.payload.host + '_'"
" + $.payload.nodename")]})
self._load_meter_def_file(cfg)
c = list(self.handler.process_notification(METRICS_UPDATE))
c = list(self.handler.build_sample(METRICS_UPDATE))
self.assertEqual(1, len(c))
s1 = c[0].as_dict()
self.assertEqual('compute.node.cpu.frequency', s1['name'])
@ -732,7 +732,7 @@ class TestMeterProcessing(test.BaseTestCase):
".value",
resource_id="'prefix-' + $.payload.nodename")]})
self._load_meter_def_file(cfg)
c = list(self.handler.process_notification(METRICS_UPDATE))
c = list(self.handler.build_sample(METRICS_UPDATE))
self.assertEqual(1, len(c))
s1 = c[0].as_dict()
self.assertEqual('compute.node.cpu.frequency', s1['name'])
@ -757,7 +757,7 @@ class TestMeterProcessing(test.BaseTestCase):
resource_id="$.payload.resource_id",
project_id="$.payload.project_id")]})
self._load_meter_def_file(cfg)
c = list(self.handler.process_notification(NOTIFICATION))
c = list(self.handler.build_sample(NOTIFICATION))
self.assertEqual(1, len(c))
def test_multi_files_multi_meters(self):
@ -778,7 +778,7 @@ class TestMeterProcessing(test.BaseTestCase):
resource_id="$.payload.resource_id",
project_id="$.payload.project_id")]})
self._load_meter_def_file([cfg1, cfg2])
data = list(self.handler.process_notification(NOTIFICATION))
data = list(self.handler.build_sample(NOTIFICATION))
self.assertEqual(2, len(data))
expected_names = ['test1', 'test2']
for s in data:
@ -802,7 +802,7 @@ class TestMeterProcessing(test.BaseTestCase):
resource_id="$.payload.resource_id",
project_id="$.payload.project_id")]})
self._load_meter_def_file([cfg1, cfg2])
data = list(self.handler.process_notification(NOTIFICATION))
data = list(self.handler.build_sample(NOTIFICATION))
self.assertEqual(1, len(data))
self.assertEqual(data[0].as_dict()['name'], 'test')
@ -828,7 +828,7 @@ class TestMeterProcessing(test.BaseTestCase):
project_id="$.payload.initiator.project_id",
lookup="name")]})
self._load_meter_def_file([cfg1, cfg2])
data = list(self.handler.process_notification(event))
data = list(self.handler.build_sample(event))
self.assertEqual(0, len(data))
def test_multi_files_unmatched_meter(self):
@ -849,7 +849,7 @@ class TestMeterProcessing(test.BaseTestCase):
resource_id="$.payload.resource_id",
project_id="$.payload.project_id")]})
self._load_meter_def_file([cfg1, cfg2])
data = list(self.handler.process_notification(NOTIFICATION))
data = list(self.handler.build_sample(NOTIFICATION))
self.assertEqual(1, len(data))
self.assertEqual(data[0].as_dict()['name'], 'test1')
@ -877,7 +877,7 @@ class TestMeterProcessing(test.BaseTestCase):
resource_id="$.payload.resource_id",
project_id="$.payload.project_id")]})
self._load_meter_def_file([cfg1, cfg2])
data = list(self.handler.process_notification(NOTIFICATION))
data = list(self.handler.build_sample(NOTIFICATION))
self.assertEqual(2, len(data))
expected_names = ['test1', 'test2']
for s in data:

@ -73,7 +73,7 @@ class TestNotifications(base.BaseTestCase):
self.setup_messaging(self.CONF)
def test_process_request_notification(self):
sample = list(middleware.HTTPRequest(mock.Mock()).process_notification(
sample = list(middleware.HTTPRequest(mock.Mock()).build_sample(
HTTP_REQUEST
))[0]
self.assertEqual(HTTP_REQUEST['payload']['request']['HTTP_X_USER_ID'],
@ -86,7 +86,7 @@ class TestNotifications(base.BaseTestCase):
def test_process_response_notification(self):
sample = list(middleware.HTTPResponse(
mock.Mock()).process_notification(HTTP_RESPONSE))[0]
mock.Mock()).build_sample(HTTP_RESPONSE))[0]
self.assertEqual(HTTP_RESPONSE['payload']['request']['HTTP_X_USER_ID'],
sample.user_id)
self.assertEqual(HTTP_RESPONSE['payload']['request']

@ -93,7 +93,7 @@ class _FakeNotificationPlugin(pipeline.NotificationEndpoint):
for topic in self.get_notification_topics(conf)
]
def process_notification(self, message):
def build_sample(self, message):
return []
@ -133,7 +133,7 @@ class TestNotification(tests_base.BaseTestCase):
self._do_process_notification_manager_start()
self.assertEqual(2, len(self.srv.listeners))
def test_process_notification(self):
def test_build_sample(self):
self._do_process_notification_manager_start()
self.srv.pipeline_manager.pipelines[0] = mock.MagicMock()