def _buffered_plaintext_send_metric(self, base_name, name, value, t, force=False): self.batch_count += 1 self.batch_buffer += "{0}.{1} {2} {3}\n".format( base_name, name, value, now()) # Check if we reach batch size and send if self.batch_count >= self.batch_size: self._send_plaintext()
def prepare_metric(self, name, type, metric, keys, snapshot_keys=[]): base_name = re.sub(r"\s+", "_", name) if self.prefix: base_name = "{0}.{1}".format(self.prefix, base_name) time = now() type = "gauge" if type != "counter" else "counter" if self.filters: keys = filter(lambda key: key in self.filters, keys) snapshot_keys = filter(lambda key: key in self.filters, snapshot_keys) if self.excludes: keys = filter(lambda key: key not in self.excludes, keys) snapshot_keys = filter(lambda key: key in self.excludes, snapshot_keys) for name in keys: value = getattr(metric, name) yield type, { "name": "{0}.{1}".format(base_name, name), "source": self.source, "time": time, "value": value } if hasattr(metric, 'snapshot'): snapshot = metric.snapshot for name in snapshot_keys: value = getattr(snapshot, name) yield type, { "name": "{0}.{1}".format(base_name, name), "source": self.source, "time": time, "value": value }
def __init__(self, average_class=EWMA): self.counter = AtomicLong(0) self.start_time = now() self.last_tick = AtomicLong(self.start_time) self.interval = EWMA.INTERVAL self.m1_rate = EWMA.m1() self.m5_rate = EWMA.m5() self.m15_rate = EWMA.m15()
def send_metric(self, name, type, metric, keys, snapshot_keys=None): if snapshot_keys is None: snapshot_keys = [] base_name = re.sub(r"\s+", "_", name) if self.prefix: base_name = "{0}.{1}".format(self.prefix, base_name) for name in keys: value = True value = getattr(metric, name) self._buffered_send_metric(base_name, name, value, now()) if hasattr(metric, 'snapshot'): snapshot = metric.snapshot for name in snapshot_keys: value = True value = getattr(snapshot, name) self._buffered_send_metric(base_name, name, value, now())
def update(self, value, timestamp=None): if timestamp is None: timestamp = now() self.rescale_if_necessary() with self.lock: try: priority = self.weight(timestamp) / random.random() except (OverflowError, ZeroDivisionError): priority = sys.float_info.max if len(self.values) < self.reservoir_size: heapq.heappush(self.values, (priority, value)) else: heapq.heappushpop(self.values, (priority, value))
def rescale_if_necessary(self): time = now() next_time = self.next_scale_time.value if time > next_time: self.rescale(time, next_time)
def clear(self): with self.lock: self.values = [] self.start_time = now() self.next_scale_time.value = \ self.start_time + self.rescale_threshold
def clear(self): with self.lock: self.values = [] self.start_time = now() self.next_scale_time.value = self.start_time + self.rescale_threshold