AlarmStateTransitionedEvent timestamp now in ms

This will ensure a unique timestamp. Influx V9 will only keep one
entry with the same timestamp

Some of the tests weren't using a numerica timestamp which caused
problems when the code tried to divide the timestamp by 1000

Change-Id: Ie0fefc61abe9a95503fa7611971a21ed2cb56224
This commit is contained in:
Craig Bryant 2015-04-14 12:03:56 -06:00
parent 930e0dab10
commit c4b469c11d
5 changed files with 19 additions and 14 deletions

View File

@ -61,7 +61,8 @@ class Notification(object):
self.alarm_id = alarm['alarmId']
self.alarm_name = alarm['alarmName']
self.alarm_timestamp = alarm['timestamp']
# The event timestamp is in milliseconds
self.alarm_timestamp = alarm['timestamp'] / 1000
self.message = alarm['stateChangeReason']
self.state = alarm['newState']
self.tenant_id = alarm['tenantId']

View File

@ -76,9 +76,10 @@ class AlarmProcessor(BaseProcessor):
log.debug('Actions are disabled for this alarm.')
return False
alarm_age = time.time() - alarm['timestamp'] # Should all be in seconds since epoch
alarm_age = time.time() - alarm['timestamp'] / 1000
if (self._alarm_ttl is not None) and (alarm_age > self._alarm_ttl):
log.warn('Received alarm older than the ttl, skipping. Alarm from %s' % time.ctime(alarm['timestamp']))
log.warn('Received alarm older than the ttl, skipping. Alarm from %s' %
time.ctime(alarm['timestamp'] / 1000))
return False
return True

View File

@ -77,7 +77,7 @@ class TestAlarmProcessor(unittest.TestCase):
"""Should cause the alarm_ttl to fire log a warning and push to finished queue."""
alarm_dict = {"tenantId": "0", "alarmDefinitionId": "0", "alarmId": "1", "alarmName": "test Alarm",
"oldState": "OK", "newState": "ALARM", "stateChangeReason": "I am alarming!",
"timestamp": 1375346830, "actionsEnabled": 1, "metrics": "cpu_util"}
"timestamp": 1375346830042, "actionsEnabled": 1, "metrics": "cpu_util"}
alarm = self._create_raw_alarm(0, 2, alarm_dict)
notifications, partition, offset = self._run_alarm_processor(alarm, None)
@ -96,7 +96,7 @@ class TestAlarmProcessor(unittest.TestCase):
"""
alarm_dict = {"tenantId": "0", "alarmDefinitionId": "0", "alarmId": "1", "alarmName": "test Alarm",
"oldState": "OK", "newState": "ALARM", "stateChangeReason": "I am alarming!",
"timestamp": time.time(), "actionsEnabled": 1, "metrics": "cpu_util"}
"timestamp": time.time() * 1000, "actionsEnabled": 1, "metrics": "cpu_util"}
alarm = self._create_raw_alarm(0, 3, alarm_dict)
notifications, partition, offset = self._run_alarm_processor(alarm, None)
@ -110,7 +110,7 @@ class TestAlarmProcessor(unittest.TestCase):
"""
alarm_dict = {"tenantId": "0", "alarmDefinitionId": "0", "alarmId": "1", "alarmName": "test Alarm",
"oldState": "OK", "newState": "ALARM", "stateChangeReason": "I am alarming!",
"timestamp": time.time(), "actionsEnabled": 1, "metrics": "cpu_util"}
"timestamp": time.time() * 1000, "actionsEnabled": 1, "metrics": "cpu_util"}
alarm = self._create_raw_alarm(0, 4, alarm_dict)
sql_response = [['test notification', 'EMAIL', 'me@here.com']]
@ -125,7 +125,7 @@ class TestAlarmProcessor(unittest.TestCase):
def test_two_valid_notifications(self):
alarm_dict = {"tenantId": "0", "alarmDefinitionId": "0", "alarmId": "1", "alarmName": "test Alarm",
"oldState": "OK", "newState": "ALARM", "stateChangeReason": "I am alarming!",
"timestamp": time.time(), "actionsEnabled": 1, "metrics": "cpu_util"}
"timestamp": time.time() * 1000, "actionsEnabled": 1, "metrics": "cpu_util"}
alarm = self._create_raw_alarm(0, 5, alarm_dict)

View File

@ -22,9 +22,10 @@ from monasca_notification import notification
def test_json():
"""Test the to_json method to verify it behaves as expected.
"""
ts = 1429029121239
alarm = {'alarmId': 'alarmId',
'alarmName': 'alarmName',
'timestamp': 'timestamp',
'timestamp': ts,
'stateChangeReason': 'stateChangeReason',
'newState': 'newState',
'tenantId': 'tenantId',
@ -40,14 +41,14 @@ def test_json():
u'alarm_name': u'alarmName',
u'alarm_id': u'alarmId',
u'state': u'newState',
u'alarm_timestamp': u'timestamp',
u'alarm_timestamp': ts / 1000,
u'address': u'address',
u'message': u'stateChangeReason',
u'retry_count': 0,
u'raw_alarm': {
u'alarmId': u'alarmId',
u'alarmName': u'alarmName',
u'timestamp': u'timestamp',
u'timestamp': ts,
u'stateChangeReason': u'stateChangeReason',
u'newState': u'newState',
u'tenantId': u'tenantId',
@ -60,7 +61,7 @@ def test_json():
def test_equal():
alarm = {'alarmId': 'alarmId',
'alarmName': 'alarmName',
'timestamp': 'timestamp',
'timestamp': 1429029121239,
'stateChangeReason': 'stateChangeReason',
'newState': 'newState',
'tenantId': 'tenantId',
@ -78,7 +79,7 @@ def test_equal():
def test_unequal():
alarm = {'alarmId': 'alarmId',
'alarmName': 'alarmName',
'timestamp': 'timestamp',
'timestamp': 1429029121239,
'stateChangeReason': 'stateChangeReason',
'newState': 'newState',
'tenantId': 'tenantId',

View File

@ -32,7 +32,7 @@ def alarm(metrics):
"oldState": "OK",
"newState": "ALARM",
"stateChangeReason": "I am alarming!",
"timestamp": 1429023453.632428,
"timestamp": 1429023453632,
"metrics": metrics}
@ -97,9 +97,11 @@ class TestWebhook(unittest.TestCase):
headers = self.trap.get(timeout=1)
self.assertEqual(url, "http://mock:3333/")
self.maxDiff = None
# timestamp is in milliseconds while alarm_timestamp is in seconds
self.assertEqual(json.loads(data),
{"metrics": [{"dimensions": {"hostname": "foo1", "service": "bar1"}}], "alarm_id": "0",
"state": "ALARM", "alarm_timestamp": 1429023453.632428, "tenant_id": "0",
"state": "ALARM", "alarm_timestamp": 1429023453, "tenant_id": "0",
"old_state": "OK", "alarm_description": "test Alarm description",
"message": "I am alarming!", "alarm_definition_id": 0, "alarm_name": "test Alarm"})
self.assertEqual(headers, {'content-type': 'application/json'})