NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

metrics.py (2787B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\datadog\api\metrics.py
      2 import time
      3 from numbers import Number
      4 from datadog.api.exceptions import ApiError
      5 from datadog.api.resources import SearchableAPIResource, SendableAPIResource
      6 
      7 class Metric(SearchableAPIResource, SendableAPIResource):
      8     _class_url = None
      9     _json_name = 'series'
     10     _METRIC_QUERY_ENDPOINT = '/query'
     11     _METRIC_SUBMIT_ENDPOINT = '/series'
     12 
     13     @classmethod
     14     def _process_points(cls, points):
     15         return cls._format_points(points if isinstance(points, list) else [points], time.time())
     16 
     17     @classmethod
     18     def _format_points(cls, points_lst, now):
     19         formatted_points = []
     20         while points_lst:
     21             try:
     22                 if not points_lst:
     23                     return []
     24                 point = points_lst.pop()
     25                 timestamp = now if isinstance(point, Number) else point[0]
     26                 value = float(point) if isinstance(point, Number) else float(point[1])
     27                 point = (timestamp, value)
     28                 formatted_points.append(point)
     29             except TypeError as e:
     30                 raise TypeError(u'{0}: `points` parameter must use real numerical values.'.format(e))
     31             except IndexError as e:
     32                 raise IndexError(u'{0}: `points` must be a list of values or a list of (timestamp, value) pairs'.format(e))
     33 
     34         return formatted_points
     35 
     36     @classmethod
     37     def send(cls, metrics = None, **single_metric):
     38 
     39         def rename_metric_type(metric):
     40             if 'metric_type' in metric:
     41                 metric['type'] = metric.pop('metric_type')
     42 
     43         cls._class_url = cls._METRIC_SUBMIT_ENDPOINT
     44         try:
     45             if metrics:
     46                 for metric in metrics:
     47                     if isinstance(metric, dict):
     48                         rename_metric_type(metric)
     49                         metric['points'] = cls._process_points(metric['points'])
     50 
     51                 metrics_dict = {'series': metrics}
     52             else:
     53                 rename_metric_type(single_metric)
     54                 single_metric['points'] = cls._process_points(single_metric['points'])
     55                 metrics = [single_metric]
     56                 metrics_dict = {'series': metrics}
     57         except KeyError:
     58             raise KeyError("'points' parameter is required")
     59 
     60         return super(Metric, cls).send(attach_host_name=True, **metrics_dict)
     61 
     62     @classmethod
     63     def query(cls, **params):
     64         cls._class_url = cls._METRIC_QUERY_ENDPOINT
     65         try:
     66             params['from'] = params.pop('start')
     67             params['to'] = params.pop('end')
     68         except KeyError as e:
     69             raise ApiError("The parameter '{0}' is required".format(e.args[0]))
     70 
     71         return super(Metric, cls)._search(**params)