Exemplo n.º 1
0
    def send(cls,
             distributions=None,
             attach_host_name=True,
             compress_payload=False,
             **distribution):
        """
        Submit a distribution metric or a list of distribution metrics to the distribution metric
        API

        :param compress_payload: compress the payload using zlib
        :type compress_payload: bool
        :param metric: the name of the time series
        :type metric: string
        :param points: a (timestamp, [list of values]) pair or
        list of (timestamp, [list of values]) pairs
        :type points: list
        :param host: host name that produced the metric
        :type host: string
        :param tags:  list of tags associated with the metric.
        :type tags: string list
        :returns: Dictionary representing the API's JSON response
        """
        if distributions:
            # Multiple distributions are sent
            for d in distributions:
                if isinstance(d, dict):
                    d['points'] = format_points(d['points'])
            series_dict = {"series": distributions}
        else:
            # One distribution is sent
            distribution['points'] = format_points(distribution['points'])
            series_dict = {"series": [distribution]}
        return super(Distribution, cls).send(attach_host_name=attach_host_name,
                                             compress_payload=compress_payload,
                                             **series_dict)
Exemplo n.º 2
0
    def send(cls, metrics=None, attach_host_name=True, compress_payload=False, **single_metric):
        """
        Submit a metric or a list of metrics to the metric API
        A metric dictionary should consist of 5 keys: metric, points, host, tags, type (some of which optional),
        see below:

        :param metric: the name of the time series
        :type metric: string

        :param compress_payload: compress the payload using zlib
        :type compress_payload: bool

        :param metrics: a list of dictionaries, each item being a metric to send
        :type metrics: list

        :param points: a (timestamp, value) pair or list of (timestamp, value) pairs
        :type points: list

        :param host: host name that produced the metric
        :type host: string

        :param tags:  list of tags associated with the metric.
        :type tags: string list

        :param type: type of the metric
        :type type: 'gauge' or 'count' or 'rate' string

        >>> api.Metric.send(metric='my.series', points=[(now, 15), (future_10s, 16)])

        >>> metrics = [{'metric': 'my.series', 'type': 'gauge', 'points': [(now, 15), (future_10s, 16)]},
                {'metric': 'my.series2', 'type': 'gauge', 'points': [(now, 15), (future_10s, 16)]}]
        >>> api.Metric.send(metrics=metrics)

        :returns: Dictionary representing the API's JSON response
        """
        # Set the right endpoint
        cls._resource_name = cls._METRIC_SUBMIT_ENDPOINT

        # Format the payload
        try:
            if metrics:
                for metric in metrics:
                    if isinstance(metric, dict):
                        cls._rename_metric_type(metric)
                        metric["points"] = format_points(metric["points"])
                metrics_dict = {"series": metrics}
            else:
                cls._rename_metric_type(single_metric)
                single_metric["points"] = format_points(single_metric["points"])
                metrics = [single_metric]
                metrics_dict = {"series": metrics}

        except KeyError:
            raise KeyError("'points' parameter is required")

        return super(Metric, cls).send(
            attach_host_name=attach_host_name, compress_payload=compress_payload, **metrics_dict
        )
Exemplo n.º 3
0
    def send(cls, metrics=None, **single_metric):
        """
        Submit a metric or a list of metrics to the metric API

        :param metric: the name of the time series
        :type metric: string

        :param points: a (timestamp, value) pair or list of (timestamp, value) pairs
        :type points: list

        :param host: host name that produced the metric
        :type host: string

        :param tags:  list of tags associated with the metric.
        :type tags: string list

        :param type: type of the metric
        :type type: 'gauge' or 'count' or 'rate' string

        :returns: Dictionary representing the API's JSON response
        """
        # Set the right endpoint
        cls._resource_name = cls._METRIC_SUBMIT_ENDPOINT

        # Format the payload
        try:
            if metrics:
                for metric in metrics:
                    if isinstance(metric, dict):
                        cls._rename_metric_type(metric)
                        metric['points'] = format_points(metric['points'])
                metrics_dict = {"series": metrics}
            else:
                cls._rename_metric_type(single_metric)
                single_metric['points'] = format_points(
                    single_metric['points'])
                metrics = [single_metric]
                metrics_dict = {"series": metrics}

        except KeyError:
            raise KeyError("'points' parameter is required")

        return super(Metric, cls).send(attach_host_name=True, **metrics_dict)