-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[api]
Metrics
: support real numerical data types
Extends `Metrics` data type support to any real number.
- Loading branch information
Showing
2 changed files
with
56 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import time | ||
from numbers import Number | ||
|
||
from datadog.api.base import SearchableAPIResource, SendableAPIResource | ||
from datadog.api.exceptions import ApiError | ||
|
@@ -14,17 +15,51 @@ class Metric(SearchableAPIResource, SendableAPIResource): | |
_METRIC_QUERY_ENDPOINT = '/query' | ||
_METRIC_SUBMIT_ENDPOINT = '/series' | ||
|
||
_SUPPORTED_DATA_TYPES = (int, float, long) | ||
|
||
@classmethod | ||
def _process_points(cls, points): | ||
""" | ||
Format `points` parameter. | ||
Input: | ||
(value or (timestamp * value)) list or singleton | ||
Returns: | ||
(timestamp * float value) list | ||
This comment has been minimized.
Sorry, something went wrong. |
||
""" | ||
now = time.time() | ||
if isinstance(points, cls._SUPPORTED_DATA_TYPES): | ||
points = [(now, points)] | ||
elif isinstance(points, tuple): | ||
points = [points] | ||
points_lst = points if isinstance(points, list) else [points] | ||
|
||
def rec_parse(points_lst): | ||
""" | ||
Recursively parse a list of values or a list of timestamp * value to a list of | ||
(timestamp, `float` value)s. | ||
""" | ||
try: | ||
if not points_lst: | ||
return [] | ||
|
||
point = points_lst.pop() | ||
timestamp = now if isinstance(point, Number) else point[0] | ||
value = float(point) if isinstance(point, Number) else float(point[1]) | ||
|
||
point = [(timestamp, value)] | ||
|
||
return point + rec_parse(points_lst) | ||
|
||
except TypeError as e: | ||
raise TypeError( | ||
u"{0}: " | ||
"`points` parameter must use real numerical values.".format(e) | ||
) | ||
|
||
except IndexError as e: | ||
raise IndexError( | ||
u"{0}: " | ||
"`points` must be a list of values or a list of timestamp * value".format(e) | ||
) | ||
|
||
return points | ||
return rec_parse(points_lst) | ||
|
||
@classmethod | ||
def send(cls, metrics=None, **single_metric): | ||
|
@@ -34,7 +69,7 @@ def send(cls, metrics=None, **single_metric): | |
:param metric: the name of the time series | ||
:type metric: string | ||
:param points: list of points to submit | ||
:param points: singleton or list of points to submit | ||
This comment has been minimized.
Sorry, something went wrong.
clutchski
|
||
:type points: list | ||
:param host: host name that produced the metric | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
this looks like you're multipling timestamp * value. maybe say "Returns a list of timestamp value pairs
[(timestamp, value), ...]
"