def test_tags_limit_indirect(mock_post): """ Test for failure when too many tags are used indirectly (when combined with global tags) """ gtags_list = [ "gtk-%d:gtv-%d" % (i, i) for i in range(apptuit_client.MAX_TAGS_LIMIT // 2 + 1) ] global_tags = ",".join(gtags_list) tags = { 'tagk-%d' % i: 'tagv-%d' % i for i in range(apptuit_client.MAX_TAGS_LIMIT // 2 + 1) } timestamp = int(time.time()) with patch.dict(os.environ, {APPTUIT_PY_TAGS: global_tags}): client = Apptuit(sanitize_mode=None, token="test_token") point1 = DataPoint("metric1", {"tagk1": "tagv1"}, timestamp, 3.14) point2 = DataPoint("metric1", tags, timestamp, 3.14) with assert_raises(ValueError): client.send([point1, point2]) with patch.dict(os.environ, {APPTUIT_PY_TAGS: global_tags}): client = Apptuit(sanitize_mode=None, token="test_token") series1 = TimeSeries('metric1', {"tagk1": "tagv1"}) series1.add_point(timestamp, 3.14) series2 = TimeSeries('metric1', tags) series2.add_point(timestamp, 3.14) with assert_raises(ValueError): client.send_timeseries([series1, series2])
def test_timeseries_payload(): """ Test payload from timeseries list """ token = "asdashdsauh_8aeraerf" client = __get_apptuit_client() series_list = [] tags1 = {"tagk1": "tagv1", "tagk2": "tagv2"} tags2 = {"tagk3": "tagv3"} metric1_name = 'metric1' metrics2_name = "metric2" series1 = TimeSeries(metric1_name, tags1) series2 = TimeSeries(metrics2_name, tags2) timestamp = int(time.time()) val1 = 3.14 val2 = 42.0 series1.add_point(timestamp, val1) series2.add_point(timestamp, val2) series_list.append(series1) series_list.append(series2) payload, points_count = client._create_payload_from_timeseries(series_list) assert_equals(points_count, 2) expected_payload = [{ "metric": metric1_name, "tags": tags1, "timestamp": timestamp, "value": val1 }, { "metric": metrics2_name, "tags": tags2, "timestamp": timestamp, "value": val2 }] assert_equals(expected_payload, payload)
def test_timeseries_length(): """ Test __len__ for TimeSeries """ series = TimeSeries("metric1", {"tagk1": "tagv1"}) for _ in range(10): series.add_point(int(time.time()), 3.14) assert_equals(len(series), 10)
def test_timeseries_payload_with_envtags(): """ Test payload creation from timeseries list with global tags in env variable """ token = "asdashdsauh_8aeraerf" global_tags = "gtagk1:gtagv1" mock_environ = patch.dict(os.environ, {APPTUIT_PY_TAGS: global_tags}) mock_environ.start() client = __get_apptuit_client() series_list = [] metric1_name = 'metric1' metric2_name = "metric2" tags2 = {"tagk3": "tagv3"} series1 = TimeSeries(metric1_name, tags=None) series2 = TimeSeries(metric2_name, tags2) timestamp = int(time.time()) val1 = 3.14 val2 = 42.0 series1.add_point(timestamp, val1) series2.add_point(timestamp, val2) series_list.append(series1) series_list.append(series2) payload, points_count = client._create_payload_from_timeseries(series_list) mock_environ.stop() assert_equals(points_count, 2) expected_payload = [{ "metric": metric1_name, "tags": { "gtagk1": "gtagv1" }, "timestamp": timestamp, "value": val1 }, { "metric": metric2_name, "tags": { "tagk3": "tagv3", "gtagk1": "gtagv1" }, "timestamp": timestamp, "value": val2 }] assert_equals(expected_payload, payload)
def test_tags_limit_direct(mock_post): """ Test for failure when too many tags are used for datapoints/series """ tags = { 'tagk-%d' % i: 'tagv-%d' % i for i in range(apptuit_client.MAX_TAGS_LIMIT + 1) } timestamp = int(time.time()) client = Apptuit(sanitize_mode=None, token="test_token") point1 = DataPoint("metric1", {"tagk1": "tagv1"}, timestamp, 3.14) point2 = DataPoint("metric1", tags, timestamp, 3.14) with assert_raises(ValueError): client.send([point1, point2]) series1 = TimeSeries('metric1', {"tagk1": "tagv1"}) series1.add_point(timestamp, 3.14) series2 = TimeSeries('metric1', tags) series2.add_point(timestamp, 3.14) with assert_raises(ValueError): client.send_timeseries([series1, series2])
def test_send_timeseries_empty(mock_post): """ Test the send_timeseries API with an empty series list """ mock_post.return_value.status_code = 204 token = "asdashdsauh_8aeraerf" client = __get_apptuit_client() series_list = [] client.send_timeseries(series_list) series1 = TimeSeries("metric", {"tagk1": "tagv1"}) client.send_timeseries([series1])
def test_send_timeseries(mock_post): """ Test the send_timeseries API """ mock_post.return_value.status_code = 204 token = "asdashdsauh_8aeraerf" client = __get_apptuit_client() series_list = [] tags1 = {"tagk1": "tagv1", "tagk2": "tagv2"} tags2 = {"tagk3": "tagv3"} metric1_name = 'metric1' metrics2_name = "metric2" series1 = TimeSeries(metric1_name, tags1) series2 = TimeSeries(metrics2_name, tags2) timestamp = int(time.time()) val1 = 3.14 val2 = 42.0 series1.add_point(timestamp, val1) series2.add_point(timestamp, val2) series_list.append(series1) series_list.append(series2) client.send_timeseries(series_list)
def test_timeseries_payload_negative(): """ Negative tests for payload creation from timeseries list """ token = "asdashdsauh_8aeraerf" client = __get_apptuit_client() series_list = [] metric1_name = 'metric1' metrics2_name = "metric2" tags2 = {"tagk3": "tagv3"} series1 = TimeSeries(metric1_name, tags=None) series2 = TimeSeries(metrics2_name, tags2) timestamp = int(time.time()) val1 = 3.14 val2 = 42.0 series1.add_point(timestamp, val1) series2.add_point(timestamp, val2) series_list.append(series1) series_list.append(series2) with assert_raises(ValueError): payload, points_count = client._create_payload_from_timeseries( series_list)
def test_timeseries_payload_with_globaltags(): """ Test payload creation from timeseries list with global tags """ token = "asdashdsauh_8aeraerf" global_tags = {"gtagk1": "gtagv1"} client = Apptuit(sanitize_mode=None, token=token, global_tags=global_tags) series_list = [] metric1_name = 'metric1' metric2_name = "metric2" tags2 = {"tagk3": "tagv3"} series1 = TimeSeries(metric1_name, tags=None) series2 = TimeSeries(metric2_name, tags2) timestamp = int(time.time()) val1 = 3.14 val2 = 42.0 series1.add_point(timestamp, val1) series2.add_point(timestamp, val2) series_list.append(series1) series_list.append(series2) payload, points_count = client._create_payload_from_timeseries(series_list) assert_equals(points_count, 2) expected_payload = [{ "metric": metric1_name, "tags": global_tags, "timestamp": timestamp, "value": val1 }, { "metric": metric2_name, "tags": { "tagk3": "tagv3", "gtagk1": "gtagv1" }, "timestamp": timestamp, "value": val2 }] assert_equals(expected_payload, payload)