Page MenuHomePhabricator
Paste P8458

latest
ActivePublic

Authored by jbond on Apr 30 2019, 10:57 AM.
Tags
None
Referenced Files
F28866515: raw.txt
Apr 30 2019, 10:57 AM
Subscribers
None
def latest(self, metric: str, host: str, timeperiod: Optional[str] = '2m') -> float:
"""Get the latest prometheous metric
Arguments:
metric (str): name of the prometheous metric
host (str): hostname of the node to search for
Returns:
float: The current value of the metric or -1 if no values returned
Raises:
PrometheusError: on faliure
"""
query = 'sum(irate({metric}{{instance=~"{host}(:[0-9]{{1,5}})?"}}[{timeperiod}]))'.format(
metric=metric, host=host, timeperiod=timeperiod)
result = self.query(query)
if not result:
logger.warning('no metrics returned for time period')
return -1
return float(result[0]['value'][1])
latest('apache_accesses_total', 'mw1222')