From 0697533c9692ec55f711b9eed35ea6dbb4e69326 Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Wed, 18 Apr 2018 20:07:19 +0200 Subject: Fix wrong handling of metrics arguments Always handle arguments as an array even if we receive a simple JSON object. This imply using an intermediary variable Change-Id: I0af8fdcd87e4d3f94d88d64ff7195f6c78002635 Signed-off-by: Romain Forlot --- src/plugins/influxdb-writer.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) (limited to 'src/plugins/influxdb-writer.c') diff --git a/src/plugins/influxdb-writer.c b/src/plugins/influxdb-writer.c index a3763d5..a236fe3 100644 --- a/src/plugins/influxdb-writer.c +++ b/src/plugins/influxdb-writer.c @@ -96,9 +96,17 @@ CURL *make_curl_write_post(const char *url, json_object *metricsJ) char **post_data; char write[URL_MAXIMUM_LENGTH]; struct series_t *serie = NULL; + json_object *metricsArrayJ = NULL; - lpd = json_object_is_type(metricsJ, json_type_array) ? - json_object_array_length(metricsJ) + 1 : 2; + if(json_object_is_type(metricsJ, json_type_array)) { + lpd = json_object_array_length(metricsJ); + metricsArrayJ = metricsJ; + } + else { + metricsArrayJ = json_object_new_array(); + json_object_array_add(metricsArrayJ, metricsJ); + lpd = 1; + } serie = malloc(sizeof(struct series_t)); post_data = malloc(lpd); @@ -106,21 +114,27 @@ CURL *make_curl_write_post(const char *url, json_object *metricsJ) for(i = 0; i < lpd; i++) { bzero(serie, sizeof(struct series_t)); - if(unpack_metric_from_api(json_object_array_get_idx(metricsJ, i), serie)) { - AFB_ERROR("ERROR unpacking metric. %s", json_object_to_json_string(metricsJ)); + if(unpack_metric_from_api(json_object_array_get_idx(metricsArrayJ, i), serie)) { + AFB_ERROR("ERROR unpacking metric. %s", json_object_to_json_string(metricsArrayJ)); break; } else { bzero(write, URL_MAXIMUM_LENGTH); - format_write_args(write, serie); - post_data[i] = i == lpd - 1 ? NULL : write; + if(! serie->name) { + post_data[i] = NULL; + } + else { + format_write_args(write, serie); + strcpy(post_data[i], write); + } } } + post_data[i] = NULL; /* Check that we just do not broke the for loop before trying preparing CURL request object */ curl = i == lpd ? - curl_wrap_prepare_post(url, NULL, 1, (const char * const*)post_data):NULL; + curl_wrap_prepare_post(url, NULL, 1, " ", (const char * const*)post_data) : NULL; free(serie); free(post_data); -- cgit 1.2.3-korg