def push_metrics(self): """Pushes the total amount of seconds the request took to get a response from the URL. It only will send a request if the metric id was set in the configuration. In case of failed connection trial pushes the default metric value. """ if 'metric_id' in self.data['cachet'] and hasattr(self, 'request'): # 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, headers=self.headers) if metrics_request.ok: # Successful metrics upload self.logger.info('Metric uploaded: %.6f %s' % (value, self.latency_unit)) else: self.logger.warning('Metric upload failed with status [%d]' % (metrics_request.status_code, ))
def push_metrics(self, metric_id: int, latency_time_unit: str, elapsed_time_in_seconds: int, timestamp: int): """Pushes the total amount of seconds the request took to get a response from the URL. """ value = latency_unit.convert_to_unit(latency_time_unit, elapsed_time_in_seconds) params = {"id": metric_id, "value": value, "timestamp": timestamp} return requests.post(f"{self.url}/metrics/{metric_id}/points", params=params, headers=self.headers)
def push_metrics(self): """Pushes the total amount of seconds the request took to get a response from the URL. It only will send a request if the metric id was set in the configuration. In case of failed connection trial pushes the default metric value. """ if 'metric_id' in self.data['cachet'] and hasattr(self, 'request'): # 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, headers=self.headers) if metrics_request.ok: # Successful metrics upload self.logger.info('Metric uploaded: %.6f %s' % (value, self.latency_unit)) else: self.logger.warning('Metric upload failed with status [%d]' % (metrics_request.status_code,))
def test_convert_to_unit_s(): assert convert_to_unit("s", 20) == 20
def test_convert_to_unit_ms(): assert convert_to_unit("ms", 1) == 1000
def test_convert_to_unit_h(): assert convert_to_unit("h", 7200) == 2
def test_convert_to_unit_m(): assert convert_to_unit("m", 3) == float(3) / 60