Send given metrics to the Graphite server.
436272
@Override
    public void write(Iterable<QueryResult> results) {
        logger.debug("Export to '{}', proxy {} metrics {}", url, proxy, results);

        List<QueryResult> counters = new ArrayList<QueryResult>();
        List<QueryResult> gauges = new ArrayList<QueryResult>();
        for (QueryResult result : results) {
            if (METRIC_TYPE_GAUGE.equals(result.getType())) {
                gauges.add(result);
            } else if (METRIC_TYPE_COUNTER.equals(result.getType())) {
                counters.add(result);
            } else if (null == result.getType()) {
                logger.info("Unspecified type for result {}, export it as counter");
                counters.add(result);
            } else {
                logger.info("Unsupported metric type '{}' for result {}, export it as counter", result.getType(), result);
                counters.add(result);
            }
        }


        HttpURLConnection urlConnection = null;
        try {
            if (proxy == null) {
                urlConnection = (HttpURLConnection) url.openConnection();
            } else {
                urlConnection = (HttpURLConnection) url.openConnection(proxy);
            }
            urlConnection.setRequestMethod("POST");
            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);
            urlConnection.setReadTimeout(libratoApiTimeoutInMillis);
            urlConnection.setRequestProperty("content-type", "application/json; charset=utf-8");
            urlConnection.setRequestProperty("Authorization", "Basic " + basicAuthentication);
            urlConnection.setRequestProperty("User-Agent", httpUserAgent);

            serialize(counters, gauges, urlConnection.getOutputStream());
            int responseCode = urlConnection.getResponseCode();
            if (responseCode != 200) {
                exceptionCounter.incrementAndGet();
                logger.warn("Failure {}:'{}' to send result to Librato server '{}' with proxy {}, user {}", responseCode, urlConnection.getResponseMessage(), url, proxy, user);
            }
            if (logger.isTraceEnabled()) {
                IoUtils2.copy(urlConnection.getInputStream(), System.out);
            }
        } catch (Exception e) {
            exceptionCounter.incrementAndGet();
            logger.warn("Failure to send result to Librato server '{}' with proxy {}, user {}", url, proxy, user, e);
        } finally {
            if (urlConnection != null) {
                try {
                    InputStream in = urlConnection.getInputStream();
                    IoUtils2.copy(in, IoUtils2.nullOutputStream());
                    IoUtils2.closeQuietly(in);
                    InputStream err = urlConnection.getErrorStream();
                    if (err != null) {
                        IoUtils2.copy(err, IoUtils2.nullOutputStream());
                        IoUtils2.closeQuietly(err);
                    }
                } catch (IOException e) {
                    logger.warn("Exception flushing http connection", e);
                }
            }

        }
    }
76280
def process(self, metric):
        
        if self._match_metric(metric):
            self.metrics.append(metric)
        if self.should_flush():
            self._send()
1043592
def send(self, stats):
        ""
        buf = cStringIO.StringIO()
        now = int(time.time())
        num_stats = 0

        # timer stats
        pct = stats.percent
        timers = stats.timers
        for key, vals in timers.iteritems():
            if not vals:
                continue

            # compute statistics
            num = len(vals)
            vals = sorted(vals)
            vmin = vals[0]
            vmax = vals[-1]
            mean = vmin
            max_at_thresh = vmax
            if num > 1:
                idx = round((pct / 100.0) * num)
                tmp = vals[:int(idx)]
                if tmp:
                    max_at_thresh = tmp[-1]
                    mean = sum(tmp) / idx

            key = 'stats.timers.%s' % key
            buf.write('%s.mean %f %d\n' % (key, mean, now))
            buf.write('%s.upper %f %d\n' % (key, vmax, now))
            buf.write('%s.upper_%d %f %d\n' % (key, pct, max_at_thresh, now))
            buf.write('%s.lower %f %d\n' % (key, vmin, now))
            buf.write('%s.count %d %d\n' % (key, num, now))
            num_stats += 1

        # counter stats
        counts = stats.counts
        for key, val in counts.iteritems():
            buf.write('stats.%s %f %d\n' % (key, val / stats.interval, now))
            buf.write('stats_counts.%s %f %d\n' % (key, val, now))
            num_stats += 1

        # counter stats
        gauges = stats.gauges
        for key, val in gauges.iteritems():
            buf.write('stats.%s %f %d\n' % (key, val, now))
            buf.write('stats_counts.%s %f %d\n' % (key, val, now))
            num_stats += 1

        buf.write('statsd.numStats %d %d\n' % (num_stats, now))

        # TODO: add support for N retries

        for host in self._hosts:
            # flush stats to graphite
            try:
                sock = socket.create_connection(host)
                sock.sendall(buf.getvalue())
                sock.close()
            except Exception, ex:
                self.error(E_SENDFAIL % ('graphite', host, ex))
540053
def send(self, path, value, metric_type):
        
        msg = self._msg_format.format(
            path=self._build_path(path, metric_type),
            value=value,
            metric_type=metric_type)

        LOGGER.debug('Sending %s to %s:%s', msg.encode('ascii'),
                     self._host, self._port)

        try:
            if self._tcp:
                if self._sock.closed():
                    return
                return self._sock.write(msg.encode('ascii'))

            self._sock.sendto(msg.encode('ascii'), (self._host, self._port))
        except iostream.StreamClosedError as error:  # pragma: nocover
            LOGGER.warning('Error sending TCP statsd metric: %s', error)
        except (OSError, socket.error) as error:  # pragma: nocover
            LOGGER.exception('Error sending statsd metric: %s', error)
675721
def _send(self, key, value, metric_type):
        
        try:
            payload = self._build_payload(key, value, metric_type)
            LOGGER.debug('Sending statsd payload: %r', payload)
            self._socket.sendto(payload.encode('utf-8'), self._address)
        except socket.error:  # pragma: nocover
            LOGGER.exception('Error sending statsd metric')
1349233
private void writeMetrics() throws IOException {
        if (metrics.size() > 0) {
            try {
                byte[] payload = pickleMetrics(metrics);
                byte[] header = ByteBuffer.allocate(4).putInt(payload.length).array();

                OutputStream outputStream = socket.getOutputStream();
                outputStream.write(header);
                outputStream.write(payload);
                outputStream.flush();

                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Wrote {} metrics", metrics.size());
                }
            } catch (IOException e) {
                this.failures++;
                throw e;
            } finally {
                // if there was an error, we might miss some data. for now, drop those on the floor and
                // try to keep going.
                metrics.clear();
            }

        }
    }
538256
def add_metric(*args, **kwargs):
    """"""
    _args = ['add-metric']
    _kvpairs = []
    _kvpairs.extend(args)
    _kvpairs.extend(['{}={}'.format(k, v) for k, v in kwargs.items()])
    _args.extend(sorted(_kvpairs))
    try:
        subprocess.check_call(_args)
        return
    except EnvironmentError as e:
        if e.errno != errno.ENOENT:
            raise
    log_message = 'add-metric failed: {}'.format(' '.join(_kvpairs))
    log(log_message, level='INFO')
906196
def write(self, *messages):
        """"""

        for i in self.graph.outputs_of(BEGIN):
            for message in messages:
                self[i].write(message)
116610
def Send(self,
           data,
           status=200,
           ctype="application/octet-stream",
           additional_headers=None,
           last_modified=0):
    """"""
    if additional_headers:
      additional_header_strings = [
          "%s: %s\r\n" % (name, val)
          for name, val in iteritems(additional_headers)
      ]
    else:
      additional_header_strings = []

    header = ""
    header += "HTTP/1.0 %s\r\n" % self.statustext[status]
    header += "Server: GRR Server\r\n"
    header += "Content-type: %s\r\n" % ctype
    header += "Content-Length: %d\r\n" % len(data)
    header += "Last-Modified: %s\r\n" % self.date_time_string(last_modified)
    header += "".join(additional_header_strings)
    header += "\r\n"

    self.wfile.write(header.encode("utf-8"))
    self.wfile.write(data)
384
def run(self):
    """"""
    self.graphite.start()
    while True:
      log.debug('Graphite pusher is sleeping for %d seconds', self.period)
      time.sleep(self.period)
      log.debug('Pushing stats to Graphite')
      try:
        self.push()
        log.debug('Done pushing stats to Graphite')
      except:
        log.exception('Exception while pushing stats to Graphite')
        raise
65237
def send(self, sock, msg):
    """"""
    data = pickle.dumps(msg)
    buf = struct.pack('>I', len(data)) + data
    sock.sendall(buf)
659878
def send_dict(*args, **kwargs):
    
    if not _module_instance:
        raise GraphiteSendException(
            "Must call graphitesend.init() before sending")
    _module_instance.send_dict(*args, **kwargs)
    return _module_instance
891256
def CollectMetrics(self, request, context):
        """"""
        LOG.debug("CollectMetrics called")
        try:
            metrics_to_collect = []
            for metric in request.metrics:
                metrics_to_collect.append(Metric(pb=metric))
            metrics_collected = self.plugin.collect(metrics_to_collect)
            return MetricsReply(metrics=[m.pb for m in metrics_collected])
        except Exception as err:
            msg = "message: {}\n\nstack trace: {}".format(
                err, traceback.format_exc())
            return MetricsReply(metrics=[], error=msg)
878636
def process(self, metric):
        
        event = self._metric_to_riemann_event(metric)
        try:
            self.client.send_event(event)
        except Exception as e:
            self.log.error(
                "RiemannHandler: Error sending event to Riemann: %s", e)
559388
private void uploadMetrics(TopologyMetric tpMetric) throws Exception {
        long start = System.currentTimeMillis();

        if (tpMetric == null) {
            return;
        }
        try {
            synchronized (lock) {
                if (client == null || !client.isValid()) {
                    client = new NimbusClientWrapper();
                    client.init(conf);
                }
            }

            MetricInfo topologyMetrics = tpMetric.get_topologyMetric();
            MetricInfo componentMetrics = tpMetric.get_componentMetric();
            MetricInfo taskMetrics = tpMetric.get_taskMetric();
            MetricInfo streamMetrics = tpMetric.get_streamMetric();
            MetricInfo workerMetrics = tpMetric.get_workerMetric();
            MetricInfo nettyMetrics = tpMetric.get_nettyMetric();
            MetricInfo compStreamMetrics = tpMetric.get_compStreamMetric();

            int totalSize = topologyMetrics.get_metrics_size()
                    + componentMetrics.get_metrics_size()
                    + taskMetrics.get_metrics_size()
                    + streamMetrics.get_metrics_size()
                    + workerMetrics.get_metrics_size()
                    + nettyMetrics.get_metrics_size()
                    + compStreamMetrics.get_metrics_size();

            // for small topologies, send all metrics together to ease the
            // pressure of nimbus
            if (totalSize < MAX_BATCH_SIZE) {
                TopologyMetric allMetrics = new TopologyMetric(
                        topologyMetrics, componentMetrics, workerMetrics, taskMetrics, streamMetrics, nettyMetrics);
                allMetrics.set_compStreamMetric(compStreamMetrics);

                client.getClient().uploadTopologyMetrics(topologyId, allMetrics);
            } else {
                TopologyMetric partialMetrics = new TopologyMetric(topologyMetrics, componentMetrics,
                        dummy, dummy, dummy, dummy);
                partialMetrics.set_compStreamMetric(compStreamMetrics);
                client.getClient().uploadTopologyMetrics(topologyId, partialMetrics);

                batchUploadMetrics(workerMetrics, MetaType.WORKER);
                batchUploadMetrics(taskMetrics, MetaType.TASK);
                batchUploadMetrics(streamMetrics, MetaType.STREAM);
                batchUploadMetrics(nettyMetrics, MetaType.NETTY);
            }
        } catch (Exception e) {
            String errorInfo = "Failed to upload worker metrics";
            LOG.error(errorInfo, e);
            if (client != null) {
                client.cleanup();
            }
            zkCluster.report_task_error(
                    context.getTopologyId(), context.getThisTaskId(), errorInfo, ErrorConstants.WARN, ErrorConstants.CODE_USER);
        }

        metricLogger.info("upload metrics, cost:{}", System.currentTimeMillis() - start);
    }
935167
def send(self, data):
        """"""
        # send message
        _LOGGER.debug("send: " + data)
        self.socket.send(data.encode('ascii'))
        # sleep needed to prevent flooding the GC100 with sends
        sleep(.01)
10413
def report_stats(self, payload, is_retry=False):
        
        if self.debug:
            if self.pickle_proto:
                print "reporting pickled stats"
            else:
                print "reporting stats -> {\n%s}" % payload
        try:
            graphite = socket.socket()
            with eventlet.Timeout(self.graphite_timeout, True):
                graphite.connect(self.graphite_addr)
                graphite.sendall(payload)
                graphite.close()
        except eventlet.timeout.Timeout:
            self.logger.critical("Timeout sending to graphite")
            if self.debug:
                print "Timeout talking to graphite"
            if not is_retry:
                self.logger.critical('Attempting 1 retry!')
                self.report_stats(payload, is_retry=True)
            else:
                self.logger.critical('Already retried once, giving up')
        except Exception as err:
            self.logger.critical("error connecting to graphite: %s" % err)
            if self.debug:
                print "error connecting to graphite: %s" % err
        finally:
            try:
                graphite.close()
            except Exception:
                pass