From 2b7fb9aca5fce6da55be4fbafed7a18247a410da Mon Sep 17 00:00:00 2001 From: tmaitz Date: Fri, 11 Oct 2019 13:29:24 +0300 Subject: [PATCH 1/2] cast int metrics to float. avoid influx db cast exception --- yandextank/plugins/InfluxUploader/decoder.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/yandextank/plugins/InfluxUploader/decoder.py b/yandextank/plugins/InfluxUploader/decoder.py index bd9059e92..c69a10a3c 100644 --- a/yandextank/plugins/InfluxUploader/decoder.py +++ b/yandextank/plugins/InfluxUploader/decoder.py @@ -49,7 +49,8 @@ def decode_monitoring(self, data): {"host": host, "comment": host_data.get("comment")}, second_data["timestamp"], { - metric: value + # cast int to float. avoid https://github.com/yandex/yandex-tank/issues/776 + metric: float(value) if type(value) is int else value for metric, value in host_data["metrics"].iteritems() } ) From ee7a698da1941faa5bdfce65e4a93b53ca36dfef Mon Sep 17 00:00:00 2001 From: Joulan Date: Sat, 12 Oct 2019 14:02:33 +0300 Subject: [PATCH 2/2] pr comment fix and test for metrics cast --- yandextank/plugins/InfluxUploader/decoder.py | 2 +- .../InfluxUploader/tests/test_decoder.py | 65 +++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 yandextank/plugins/InfluxUploader/tests/test_decoder.py diff --git a/yandextank/plugins/InfluxUploader/decoder.py b/yandextank/plugins/InfluxUploader/decoder.py index c69a10a3c..c38530843 100644 --- a/yandextank/plugins/InfluxUploader/decoder.py +++ b/yandextank/plugins/InfluxUploader/decoder.py @@ -50,7 +50,7 @@ def decode_monitoring(self, data): second_data["timestamp"], { # cast int to float. avoid https://github.com/yandex/yandex-tank/issues/776 - metric: float(value) if type(value) is int else value + metric: float(value) if isinstance(value, int) else value for metric, value in host_data["metrics"].iteritems() } ) diff --git a/yandextank/plugins/InfluxUploader/tests/test_decoder.py b/yandextank/plugins/InfluxUploader/tests/test_decoder.py new file mode 100644 index 000000000..0ac12d06a --- /dev/null +++ b/yandextank/plugins/InfluxUploader/tests/test_decoder.py @@ -0,0 +1,65 @@ +# -*- coding: utf-8 -*- +from uuid import uuid4 +from yandextank.plugins.InfluxUploader.decoder import Decoder + + +class TestDecoder(object): + def test_metrics_cast(self): + test_uuid = str(uuid4()) + tank_tag = 'test_tank_tag' + comment = 'test comment' + raw_metrics = { + 'metric1': -123, + 'metric2': -123.456, + 'metric3': 123, + 'metric4': 123.456, + 'metric5': 0, + 'metric6': -0.1, + 'metric7': 0.1, + 'metric8': 'down', + } + timestamp = 123456789 + host = '127.0.0.1' + data = [ + { + 'data': { + host: { + 'comment': comment, + 'metrics': raw_metrics + } + }, + 'timestamp': timestamp + } + ] + expected_metrics = { + 'metric1': -123.0, + 'metric2': -123.456, + 'metric3': 123.0, + 'metric4': 123.456, + 'metric5': 0.0, + 'metric6': -0.1, + 'metric7': 0.1, + 'metric8': 'down' + } + + decoder = Decoder(tank_tag, test_uuid, {}, True, True) + result_points = decoder.decode_monitoring(data) + + assert (len(result_points) == 1) + r_point = result_points[0] + # check other props + assert (r_point['time'] == timestamp) + assert (r_point['measurement'] == 'monitoring') + assert (r_point['tags']['comment'] == comment) + assert (r_point['tags']['host'] == host) + assert (r_point['tags']['tank'] == tank_tag) + assert (r_point['tags']['uuid'] == test_uuid) + # check metric cast + assert (len(r_point['fields']) == len(expected_metrics)) + for metric, value in r_point['fields'].iteritems(): + if metric not in expected_metrics: + assert False + if not isinstance(value, type(expected_metrics[metric])): + assert False + if not value == expected_metrics[metric]: + assert False