def _publish_metrics(self): msg = MetricMessage() for metric in self._metrics: msg.append((metric.name, metric.aggs, metric.poll())) self.publish_message(msg) if self._on_publish is not None: self._on_publish(self)
def publish_metric(self, metric_name, aggregates, values): timestamp_buckets = {} for timestamp, value in values: ts_key = int(timestamp) / self.bucket_size ts_bucket = timestamp_buckets.get(ts_key) if ts_bucket is None: ts_bucket = timestamp_buckets[ts_key] = [] ts_bucket.append((timestamp, value)) for ts_key, ts_bucket in timestamp_buckets.iteritems(): bucket = self.find_bucket(metric_name, ts_key) routing_key = self.ROUTING_KEY_TEMPLATE % bucket msg = MetricMessage() msg.append((metric_name, aggregates, ts_bucket)) self.publish_message(msg, routing_key=routing_key)
def publish_metrics(self): """ Publish all waiting metrics. """ msg = MetricMessage() self._collect_oneshot_metrics(msg) self._collect_polled_metrics(msg) self.publish_message(msg) if self._on_publish is not None: self._on_publish(self)
def consume_message(self, vumi_message): msg = MetricMessage.from_dict(vumi_message.payload) for metric_name, aggregators, values in msg.datapoints(): self.callback(metric_name, aggregators, values)
def recv_datapoints(self, exchange, queue): """Retrieve datapoints from a broker.""" vumi_msgs = self._broker.get_messages(exchange, queue) msgs = [MetricMessage.from_dict(vm.payload) for vm in vumi_msgs] return [msg.datapoints() for msg in msgs]
def send_datapoints(self, exchange, queue, datapoints): """Publish datapoints to a broker.""" msg = MetricMessage() msg.extend(datapoints) self._broker.publish_message(exchange, queue, msg)
def publish_aggregate(self, metric_name, timestamp, value): # TODO: perhaps change interface to publish multiple metrics? msg = MetricMessage() msg.append((metric_name, (), [(timestamp, value)])) self.publish_message(msg)
def recv_datapoints(self, exchange, queue): """Retrieve datapoints from a broker.""" contents = self._broker.get_dispatched(exchange, queue) vumi_msgs = [Message.from_json(content.body) for content in contents] msgs = [MetricMessage.from_dict(vm.payload) for vm in vumi_msgs] return [msg.datapoints() for msg in msgs]