#38 - Adding support to milliseconds and different units for latency and changing http status to a range, instead of a single value.

This commit is contained in:
Mitsuo Takaki
2018-03-18 16:58:02 -07:00
parent 726f5377b1
commit 1899e95642
8 changed files with 86 additions and 14 deletions

View File

@@ -10,6 +10,7 @@ import requests
from yaml import dump
from yaml import load
import latency_unit
import status as st
# This is the mandatory fields that must be in the configuration file in this
@@ -89,12 +90,15 @@ class Configuration(object):
self.component_id = os.environ.get('CACHET_COMPONENT_ID') or self.data['cachet']['component_id']
self.metric_id = os.environ.get('CACHET_METRIC_ID') or self.data['cachet'].get('metric_id')
self.default_metric_value = self.get_default_metric_value()
# The latency_unit configuration is not mandatory and we fallback to seconds, by default.
self.latency_unit = os.environ.get('LATENCY_UNIT') or self.data['cachet'].get('latency_unit') or 's'
# We need the current status so we monitor the status changes. This is necessary for creating incidents.
self.status = get_current_status(self.api_url, self.component_id, self.headers)
# Get remaining settings
self.public_incidents = int(os.environ.get('CACHET_PUBLIC_INCIDENTS') or self.data['cachet']['public_incidents'])
self.public_incidents = int(
os.environ.get('CACHET_PUBLIC_INCIDENTS') or self.data['cachet']['public_incidents'])
self.logger.info('Monitoring URL: %s %s' % (self.endpoint_method, self.endpoint_url))
self.expectations = [Expectaction.create(expectation) for expectation in self.data['endpoint']['expectation']]
@@ -207,7 +211,9 @@ class Configuration(object):
In case of failed connection trial pushes the default metric value.
"""
if 'metric_id' in self.data['cachet'] and hasattr(self, 'request'):
value = self.default_metric_value if self.status != 1 else self.request.elapsed.total_seconds()
# We convert the elapsed time from the request, in seconds, to the configured unit.
value = self.default_metric_value if self.status != 1 else latency_unit.convert_to_unit(self.latency_unit,
self.request.elapsed.total_seconds())
params = {'id': self.metric_id, 'value': value,
'timestamp': self.current_timestamp}
metrics_request = requests.post('%s/metrics/%d/points' % (self.api_url, self.metric_id), params=params,
@@ -226,7 +232,8 @@ class Configuration(object):
"""
if hasattr(self, 'incident_id') and self.status == st.COMPONENT_STATUS_OPERATIONAL:
# If the incident already exists, it means it was unhealthy but now it's healthy again.
params = {'status': 4, 'visible': self.public_incidents, 'component_id': self.component_id, 'component_status': self.status,
params = {'status': 4, 'visible': self.public_incidents, 'component_id': self.component_id,
'component_status': self.status,
'notify': True}
incident_request = requests.put('%s/incidents/%d' % (self.api_url, self.incident_id), params=params,
@@ -287,10 +294,19 @@ class Expectaction(object):
class HttpStatus(Expectaction):
def __init__(self, configuration):
self.status = configuration['status']
self.status_range = self.parse_range(configuration['status_range'])
def parse_range(self, range_string):
statuses = range_string.split("-")
if len(statuses) == 1:
# When there was no range given, we should treat the first number as a single status check.
return (int(statuses[0]), int(statuses[0]) + 1)
else:
# We shouldn't look into more than one value, as this is a range value.
return (int(statuses[0]), int(statuses[1]))
def get_status(self, response):
if response.status_code == self.status:
if response.status_code >= self.status_range[0] and response.status_code < self.status_range[1]:
return st.COMPONENT_STATUS_OPERATIONAL
else:
return st.COMPONENT_STATUS_PARTIAL_OUTAGE
@@ -299,7 +315,7 @@ class HttpStatus(Expectaction):
return 'Unexpected HTTP status (%s)' % (response.status_code,)
def __str__(self):
return repr('HTTP status: %s' % (self.status,))
return repr('HTTP status range: %s' % (self.status_range,))
class Latency(Expectaction):