def _safe_request(self, url, params): try: response = requests.get(url, params=params) except RequestException, e: log.warning("Something was wrong with Graphite service") log.debug(e) return None
def datapoints(self, query, maxdatapoints=None): # Graphite publishes the endpoint `/render` to retrieve # datapoins from one or mulitple targets, we make sure that # only retrieve one target at once, Gramola supports only # rendering of one target. params = { 'target': query.target, 'from': query.get_since().strftime(DATE_FORMAT), 'to': query.get_until().strftime(DATE_FORMAT), # graphite supports mulitple output format, we have # to configure the json output 'format': 'json' } if maxdatapoints: params['maxDataPoints'] = maxdatapoints if self.configuration.url[-1] != '/': url = self.configuration.url + '/render' else: url = self.configuration.url + 'render' response = self._safe_request(url, params) if response is None: return [] elif len(response) == 0: log.warning('Metric `{}` not found'.format(query.target)) return [] elif len(response) > 1: log.warning('Multiple targets found, geting only the first one') # Grahpite allocate values automatically to a each bucket of time, storage schema, the # Last value can become Null until a new value arrive for the last bucket, we prefer # drop this last value if it is Null, waiting for when the real value is available or # the Null is confirmed because it keeps there. if not response[0]["datapoints"][-1][0]: response[0]["datapoints"].pop(len(response[0]["datapoints"])-1) # Graphite returns a list of lists, we turn it into a list of tuples to # make it compatible with the datapoints return type. # FIXME: Gramola not supports None values because we change None values from None # to 0.0 values = [(col[0] or 0, col[1]) for col in response[0]["datapoints"]] return values
def datapoints(self, query, maxdatapoints=None): # Graphite publishes the endpoint `/render` to retrieve # datapoins from one or mulitple targets, we make sure that # only retrieve one target at once, Gramola supports only # rendering of one target. params = { 'target': query.target, 'from': query.get_since().strftime(DATE_FORMAT), 'to': query.get_until().strftime(DATE_FORMAT), # graphite supports mulitple output format, we have # to configure the json output 'format': 'json' } if maxdatapoints: params['maxDataPoints'] = maxdatapoints if self.configuration.url[-1] != '/': url = self.configuration.url + '/render' else: url = self.configuration.url + 'render' response = self._safe_request(url, params) if response is None: return [] elif len(response) == 0: log.warning('Metric `{}` not found'.format(query.target)) return [] elif len(response) > 1: log.warning('Multiple targets found, geting only the first one') # Grahpite allocate values automatically to a each bucket of time, storage schema, the # Last value can become Null until a new value arrive for the last bucket, we prefer # drop this last value if it is Null, waiting for when the real value is available or # the Null is confirmed because it keeps there. if not response[0]["datapoints"][-1][0]: response[0]["datapoints"].pop(len(response[0]["datapoints"]) - 1) # Graphite returns a list of lists, we turn it into a list of tuples to # make it compatible with the datapoints return type. # FIXME: Gramola not supports None values because we change None values from None # to 0.0 values = [(col[0] or 0, col[1]) for col in response[0]["datapoints"]] return values
class GraphiteDataSource(DataSource): DATA_SOURCE_CONFIGURATION_CLS = GraphiteDataSourceConfig METRIC_QUERY_CLS = GraphiteMetricQuery TYPE = 'graphite' def _safe_request(self, url, params): try: response = requests.get(url, params=params) except RequestException, e: log.warning("Something was wrong with Graphite service") log.debug(e) return None if response.status_code != 200: log.warning("Get an invalid {} HTTP code from Grahpite".format( response.status_code)) return None return response.json()