Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yann/rebase send multiple metrics #59

Merged
merged 2 commits into from
Jun 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions datadog/api/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def _process_points(cls, points):
return points

@classmethod
def send(cls, *metrics, **single_metric):
def send(cls, metrics=None, **single_metric):
"""
Submit a metric or a list of metrics to the metric API

Expand Down Expand Up @@ -53,7 +53,7 @@ def send(cls, *metrics, **single_metric):
for metric in metrics:
if isinstance(metric, dict):
metric['points'] = cls._process_points(metric['points'])
metrics_dict = {"series": metrics[0]}
metrics_dict = {"series": metrics}
else:
single_metric['points'] = cls._process_points(single_metric['points'])
metrics = [single_metric]
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/api/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ def setUp(self):
self.request_mock = request_class_mock.return_value
self.request_mock.request = Mock(return_value=MockReponse())

def get_request_data(self):
"""

"""
_, kwargs = self.request_mock.request.call_args
return json.loads(kwargs['data'])

def request_called_with(self, method, url, data=None, params=None):
(req_method, req_url), others = self.request_mock.request.call_args
assert method == req_method, req_method
Expand Down
49 changes: 48 additions & 1 deletion tests/unit/api/test_api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# stdlib
from copy import deepcopy
from functools import wraps
import os
import tempfile
from time import time

# 3p
import mock
Expand Down Expand Up @@ -194,9 +196,41 @@ def test_actionable(self):
self.request_called_with('POST', "host/api/v1/actionname/" + str(actionable_object_id),
data={'mydata': "val"})


class TestMetricResource(DatadogAPIWithInitialization):

def submit_and_assess_metric_payload(self, serie):
"""
Helper to assess the metric payload format.
"""
now = time()

if isinstance(serie, dict):
Metric.send(**deepcopy(serie))
serie = [serie]
else:
Metric.send(deepcopy(serie))

payload = self.get_request_data()

for i, metric in enumerate(payload['series']):
assert set(metric.keys()) == set(['metric', 'points', 'host'])

assert metric['metric'] == serie[i]['metric']
assert metric['host'] == api._host_name

# points is a list of 1 point
assert isinstance(metric['points'], list) and len(metric['points']) == 1
# it consists of a [time, value] pair
assert len(metric['points'][0]) == 2
# its value == value we sent
assert metric['points'][0][1] == serie[i]['points']
# it's time not so far from current time
assert now - 1 < metric['points'][0][0] < now + 1

def test_metric_submit_query_switch(self):
"""
Specific to Metric subpackages: endpoints are different for submission and queries
Endpoints are different for submission and queries.
"""
Metric.send(points="val")
self.request_called_with('POST', "host/api/v1/series",
Expand All @@ -205,3 +239,16 @@ def test_metric_submit_query_switch(self):
Metric.query(start="val1", end="val2")
self.request_called_with('GET', "host/api/v1/query",
params={'from': "val1", 'to': "val2"})

def test_points_submission(self):
"""
Assess the data payload format, when submitting a single or multiple points.
"""
# Single point
serie = dict(metric='metric.1', points=13)
self.submit_and_assess_metric_payload(serie)

# Multiple point
serie = [dict(metric='metric.1', points=13),
dict(metric='metric.2', points=19)]
self.submit_and_assess_metric_payload(serie)