Exemple #1
0
 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()
Exemple #2
0
 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()
Exemple #3
0
    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
                }
Exemple #4
0
    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()
Exemple #5
0
    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())
Exemple #6
0
    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()
Exemple #7
0
    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())
Exemple #8
0
    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))
Exemple #9
0
    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))
Exemple #10
0
 def rescale_if_necessary(self):
     time = now()
     next_time = self.next_scale_time.value
     if time > next_time:
         self.rescale(time, next_time)
Exemple #11
0
 def clear(self):
     with self.lock:
         self.values = []
         self.start_time = now()
         self.next_scale_time.value = \
             self.start_time + self.rescale_threshold
Exemple #12
0
 def rescale_if_necessary(self):
     time = now()
     next_time = self.next_scale_time.value
     if time > next_time:
         self.rescale(time, next_time)
Exemple #13
0
 def clear(self):
     with self.lock:
         self.values = []
         self.start_time = now()
         self.next_scale_time.value = self.start_time + self.rescale_threshold