Process metric by sending it to datadog api
340872
def process(self, metric):
        

        self.queue.append(metric)
        if len(self.queue) >= self.queue_size:
            logging.debug("Queue is full, sending logs to Logentries")
            self._send()
946922
@Override
    public void sendMetrics(final UnifiedPushMetricsMessage metricsMessage,
            final Callback<UnifiedPushMetricsMessage> callback) {
        new AsyncTask<Void, Void, Exception>() {
            @Override
            protected Exception doInBackground(Void... params) {

                try {

                    if ((metricsMessage.getMessageId() == null) || (metricsMessage.getMessageId().trim().equals(""))) {
                        throw new IllegalStateException("Message ID cannot be null or blank");
                    }

                    HttpProvider provider = httpProviderProvider.get(metricsURL, TIMEOUT);
                    setPasswordAuthentication(variantId, secret, provider);

                    try {
                        provider.put(metricsMessage.getMessageId(), "");
                        return null;
                    } catch (HttpException ex) {
                        return ex;
                    }

                } catch (Exception ex) {
                    return ex;
                }

            }

            @SuppressWarnings("unchecked")
            @Override
            protected void onPostExecute(Exception result) {
                if (result == null) {
                    callback.onSuccess(metricsMessage);
                } else {
                    callback.onFailure(result);
                }
            }

        }.execute((Void) null);
    }
504327
def _handle_final_metric_data(self, data):
        
        id_ = data['parameter_id']
        value = data['value']
        if id_ in _customized_parameter_ids:
            self.tuner.receive_customized_trial_result(id_, _trial_params[id_], value)
        else:
            self.tuner.receive_trial_result(id_, _trial_params[id_], value)
393029
def export_metrics(self, metrics):
        
        metric_protos = []
        for metric in metrics:
            metric_protos.append(_get_metric_proto(metric))

        self._rpc_handler.send(
            metrics_service_pb2.ExportMetricsServiceRequest(
                metrics=metric_protos))
158073
def process(self, metric):
        
        entry = {'timestamp': metric.timestamp, 'value': metric.value,
                 "tags": {}}
        entry["tags"]["hostname"] = metric.host

        if self.cleanMetrics:
            metric = MetricWrapper(metric, self.log)
            if self.skipAggregates and metric.isAggregate():
                return
            for tagKey in metric.getTags():
                entry["tags"][tagKey] = metric.getTags()[tagKey]

        entry['metric'] = (self.prefix + metric.getCollectorPath() +
                           '.' + metric.getMetricPath())

        for [key, value] in self.tags:
            entry["tags"][key] = value

        self.entrys.append(entry)

        # send data if list is long enough
        if (len(self.entrys) >= self.batch):
            # Compress data
            if self.compression >= 1:
                data = StringIO.StringIO()
                with contextlib.closing(gzip.GzipFile(fileobj=data,
                                        compresslevel=self.compression,
                                        mode="w")) as f:
                    f.write(json.dumps(self.entrys))
                self._send(data.getvalue())
            else:
                # no compression
                data = json.dumps(self.entrys)
                self._send(data)
1324543
def metric_details(request, pk, format=None):
    
    metric = get_object_or_404(Metric, pk=pk)
    # get
    if request.method == 'GET':
        try:
            results = metric.select(q=request.query_params.get('q', metric.query))
        except InfluxDBClientError as e:
            return Response({'detail': e.content}, status=e.code)
        return Response(list(results.get_points(metric.name)))
    # post
    else:
        if not request.data:
            return Response({'detail': 'expected values in POST data or JSON payload'},
                            status=400)
        data = request.data.copy()
        # try converting strings to floats when sending form-data
        if request.content_type != 'application/json':
            for key, value in data.items():
                try:
                    data[key] = float(value) if '.' in value else int(value)
                except ValueError:
                    pass
        # write
        metric.write(data)
        return Response({'detail': 'ok'})
606658
private Deferred<Object> resolveMetric() {
    if (metric == null || metric.isEmpty()) {
      throw new IllegalArgumentException("The metric cannot be empty");
    }
    if (tags == null) {
      throw new IllegalArgumentException("Tags cannot be null or empty "
          + "when getting a TSUID");
    }
    
    class TagsCB implements Callback<Object, ArrayList<byte[]>> {
      @Override
      public Object call(final ArrayList<byte[]> tag_list) throws Exception {
        setTagUIDs(tag_list);
        return null;
      }
      @Override
      public String toString() {
        return "Tag resolution callback";
      }
    }
    
    class MetricCB implements Callback<Deferred<Object>, byte[]> {
      @Override
      public Deferred<Object> call(final byte[] uid) 
          throws Exception {
        setMetricUID(uid);
        if (tags.isEmpty()) {
          setTagUIDs(new ArrayList<byte[]>(0));
          return null;
        }
        return Tags.resolveAllAsync(tsdb, tags).addCallback(new TagsCB());
      }
      @Override
      public String toString() {
        return "Metric resolution callback";
      }
    }
    
    return tsdb.getUIDAsync(UniqueIdType.METRIC, metric)
        .addCallbackDeferring(new MetricCB());
  }
897145
def run(self, service_id, **kwargs):
        
        log = self.get_logger(**kwargs)

        log.info("Loading Service for metric sync")
        try:
            service = Service.objects.get(id=service_id)
            log.info("Getting metrics for <%s>" % (service.name))
            metrics = self.get_metrics(service.url, service.token)
            result = metrics.json()
            if "metrics_available" in result:
                for key in result["metrics_available"]:
                    check = WidgetData.objects.filter(service=service, key=key)
                    if not check.exists():
                        WidgetData.objects.create(
                            service=service,
                            key=key,
                            title="TEMP - Pending update"
                        )
                        log.info("Add WidgetData for <%s>" % (key,))
            return "Completed metric sync for <%s>" % (service.name)
        except ObjectDoesNotExist:
            logger.error('Missing Service', exc_info=True)

        except SoftTimeLimitExceeded:
            logger.error(
                'Soft time limit exceed processing pull of service metrics \
                 via Celery.',
                exc_info=True)
747011
private void sendRegisterRequest() {
    Metrics.MetricPublisher publisher = Metrics.MetricPublisher.newBuilder().
        setHostname(hostname).
        setPort(instance.getInfo().getTaskId()).
        setComponentName(instance.getInfo().getComponentName()).
        setInstanceId(instance.getInstanceId()).
        setInstanceIndex(instance.getInfo().getComponentIndex()).
        build();
    Metrics.MetricPublisherRegisterRequest request =
        Metrics.MetricPublisherRegisterRequest.newBuilder().
            setPublisher(publisher).
            build();

    // The timeout would be the reconnect-interval-seconds
    sendRequest(request, null,
        Metrics.MetricPublisherRegisterResponse.newBuilder(),
        systemConfig.getInstanceReconnectMetricsmgrInterval());
  }
626297
def _process(self, metric):
        
        metric = self.key + '.' + str(metric)
        self.graphite._process(metric)
574468
def scheduleMeasurement(self, measurementId, duration, start):
        
        # TODO subtract 1s from start and format
        results = {}
        for device in self.getDevices(RecordingDeviceStatus.INITIALISED.name):
            logger.info('Sending measurement ' + measurementId + ' to ' + device.payload['serviceURL'])
            try:
                resp = self.httpclient.put(device.payload['serviceURL'] + '/measurements/' + measurementId,
                                           json={'duration': duration, 'at': start.strftime(DATETIME_FORMAT)})
                logger.info('Response for ' + measurementId + ' from ' + device.payload['serviceURL'] + ' is ' +
                            str(resp.status_code))
                results[device] = resp.status_code
            except Exception as e:
                logger.exception(e)
                results[device] = 500
        return results
771140
def StreamMetrics(self, request_iterator, context):
        """"""
        LOG.debug("StreamMetrics called")

        # set up arguments
        collect_args = (next(request_iterator))
        max_metrics_buffer = 0
        max_collect_duration = 0
        cfg = Metric(pb=collect_args.Metrics_Arg.metrics[0])
        try:
            max_metrics_buffer = int(cfg.config["max-metrics-buffer"])
        except Exception as ex:
            LOG.debug("Unable to get schedule parameters: {}".format(ex))
        try:
            max_collect_duration = int(cfg.config["max-collect-duration"])
        except Exception as ex:
            LOG.debug("Unable to get schedule parameters: {}".format(ex))
        if max_metrics_buffer > 0:
            self.max_metrics_buffer = max_metrics_buffer
        if max_collect_duration > 0:
            self.max_collect_duration = max_collect_duration

        # start collection thread
        thread = threading.Thread(target=self._stream_wrapper, args=(collect_args,),)
        thread.daemon = True
        thread.start()

        # stream metrics
        metrics = []
        metrics_to_stream = []
        stream_timeout = self.max_collect_duration
        while context.is_active():
            try:
                # wait for metrics until timeout is reached
                t_start = time.time()
                metrics = self.metrics_queue.get(block=True, timeout=stream_timeout)
                elapsed = round(time.time() - t_start)
                stream_timeout -= elapsed
            except queue.Empty:
                LOG.debug("Max collect duration exceeded. Streaming {} metrics".format(len(metrics_to_stream)))
                metrics_col = CollectReply(Metrics_Reply=MetricsReply(metrics=[m.pb for m in metrics_to_stream]))
                metrics_to_stream = []
                stream_timeout = self.max_collect_duration
                yield metrics_col
            else:
                for metric in metrics:
                    metrics_to_stream.append(metric)
                    if len(metrics_to_stream) == self.max_metrics_buffer:
                        LOG.debug("Max metrics buffer reached. Streaming {} metrics".format(len(metrics_to_stream)))
                        metrics_col = CollectReply(
                            Metrics_Reply=MetricsReply(metrics=[m.pb for m in metrics_to_stream]))
                        metrics_to_stream = []
                        stream_timeout = self.max_collect_duration
                        yield metrics_col
                # stream metrics if max_metrics_buffer is 0 or enough metrics has been collected
                if self.max_metrics_buffer == 0:
                    LOG.debug("Max metrics buffer set to 0. Streaming {} metrics".format(len(metrics_to_stream)))
                    metrics_col = CollectReply(Metrics_Reply=MetricsReply(metrics=[m.pb for m in metrics_to_stream]))
                    metrics_to_stream = []
                    stream_timeout = self.max_collect_duration
                    yield metrics_col

        # sent notification if stream has been stopped
        self.done_queue.put(True)
748638
def serialize(df, measurement, tag_columns=None, **extra_tags) -> bytes:
    """"""
    # Pre-processing
    if measurement is None:
        raise ValueError("Missing 'measurement'")
    if not isinstance(df.index, pd.DatetimeIndex):
        raise ValueError('DataFrame index is not DatetimeIndex')
    tag_columns = set(tag_columns or [])
    isnull = df.isnull().any(axis=1)

    # Make parser function
    tags = []
    fields = []
    for k, v in extra_tags.items():
        tags.append(f"{k}={escape(v, key_escape)}")
    for i, (k, v) in enumerate(df.dtypes.items()):
        k = k.translate(key_escape)
        if k in tag_columns:
            tags.append(f"{k}={{p[{i+1}]}}")
        elif issubclass(v.type, np.integer):
            fields.append(f"{k}={{p[{i+1}]}}i")
        elif issubclass(v.type, (np.float, np.bool_)):
            fields.append(f"{k}={{p[{i+1}]}}")
        else:
            # String escaping is skipped for performance reasons
            # Strings containing double-quotes can cause strange write errors
            # and should be sanitized by the user.
            # e.g., df[k] = df[k].astype('str').str.translate(str_escape)
            fields.append(f"{k}=\"{{p[{i+1}]}}\"")
    fmt = (f'{measurement}', f'{"," if tags else ""}', ','.join(tags),
           ' ', ','.join(fields), ' {p[0].value}')
    f = eval("lambda p: f'{}'".format(''.join(fmt)))

    # Map/concat
    if isnull.any():
        lp = map(f, _itertuples(df[~isnull]))
        rep = _replace(df)
        lp_nan = (reduce(lambda a, b: re.sub(*b, a), rep, f(p))
                  for p in _itertuples(df[isnull]))
        return '\n'.join(chain(lp, lp_nan)).encode('utf-8')
    else:
        return '\n'.join(map(f, _itertuples(df))).encode('utf-8')
1058271
def process_metrics(self, snmp_data):
        ""
        self.metrics = {}
        for i in range(0, len(snmp_data)):
            mib_name = self.models[self.modem_type]['metrics'][i]
            conv = metric_mib[mib_name]['conv']
            self.metrics[mib_name] = conv(snmp_data[i])
663370
def process(self, metric):
        
        for rmq_server in self.connections.keys():
            try:
                if ((self.connections[rmq_server] is None or
                     self.connections[rmq_server].is_open is False)):
                    self._bind(rmq_server)

                channel = self.channels[rmq_server]
                channel.basic_publish(exchange=self.rmq_exchange,
                                      routing_key='', body="%s" % metric)
            except Exception as exception:
                self.log.error(
                    "Failed publishing to %s, attempting reconnect",
                    rmq_server)
                self.log.debug("Caught exception: %s", exception)
                self._unbind(rmq_server)
                self._bind(rmq_server)
938198
def put(self):
        """"""
        try:
            self.cloudwatch.put_metric_data(
                    Namespace=self.namespace,
                    MetricData=[{
                        'MetricName': self.name,
                        'Value': self.value,
                        'Timestamp': self.timestamp
                    }]
            )
        except Exception:
            logging.exception("Error pushing {0} to CloudWatch.".format(str(self)))
1160989
def process(self, metric):
        
        if not boto:
            return

        collector = str(metric.getCollectorPath())
        metricname = str(metric.getMetricPath())

        # Send the data as ......

        for rule in self.rules:
            self.log.debug(
                "Comparing Collector: [%s] with (%s) "
                "and Metric: [%s] with (%s)",
                str(rule['collector']),
                collector,
                str(rule['metric']),
                metricname
            )

            if ((str(rule['collector']) == collector and
                 str(rule['metric']) == metricname)):

                if rule['collect_by_instance'] and self.instance_id:
                    self.send_metrics_to_cloudwatch(
                        rule,
                        metric,
                        {'InstanceId': self.instance_id})

                if rule['collect_without_dimension']:
                    self.send_metrics_to_cloudwatch(
                        rule,
                        metric,
                        {})