From c07d9d2f20d26ef21aede121a60c92b9f0af109e Mon Sep 17 00:00:00 2001 From: Johann CAHIER Date: Tue, 21 May 2019 09:49:04 +0200 Subject: Fix InfluxDB request format As per: https://docs.influxdata.com/influxdb/v1.7/write_protocols/ separator should be a comma between metric name and metadatas (tags). Bug-AGL: SPEC-2416 Change-Id: Iedfa21ec8e61aa406600711b38e5ac2b0e87d776 Signed-off-by: Johann CAHIER --- src/plugins/influxdb-writer.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/plugins/influxdb-writer.c b/src/plugins/influxdb-writer.c index a7ce8fc..e22c2bd 100644 --- a/src/plugins/influxdb-writer.c +++ b/src/plugins/influxdb-writer.c @@ -50,16 +50,19 @@ void influxdb_write_curl_cb(void *closure, int status, CURL *curl, const char *r } } - -static void serialize_list_to_query(char *query, struct list *node) { +// query: destination buffer +// node: head node of the list +// list_sep: separator to append in front of the list +// item_sep: separator to append between items of the list +static void serialize_list_to_query(char *query, struct list *node, const char *list_sep, const char *item_sep) { bool first = true; while(node != NULL) { if(first) { - concatenate(query, node->key, " "); + concatenate(query, node->key, list_sep); first = false; } else { - concatenate(query, node->key, ","); + concatenate(query, node->key, item_sep); } if(json_object_is_type(node->value, json_type_string)) @@ -77,13 +80,15 @@ static size_t format_write_args(char *query, struct series_t *serie) strncat(query, serie->name, strlen(serie->name)); - serialize_list_to_query(query, serie->serie_columns.tags); - serialize_list_to_query(query, serie->serie_columns.fields); - - asprintf(&ts, "%lu", serie->timestamp); - concatenate(query, ts, " "); + serialize_list_to_query(query, serie->serie_columns.tags, ",", ","); + serialize_list_to_query(query, serie->serie_columns.fields, " ", ","); - return strlen(query); + if (asprintf(&ts, "%lu", serie->timestamp) > 0) { + concatenate(query, ts, " "); + free(ts); + return strlen(query); + } + return -1; } CURL *make_curl_write_post(afb_api_t apiHandle, const char *url, json_object *metricsJ) @@ -121,7 +126,7 @@ CURL *make_curl_write_post(afb_api_t apiHandle, const char *url, json_object *me } else { len_write = format_write_args(write, serie); - if(len_write) { + if(len_write > 0) { post_data[i] = malloc(len_write + 1); strcpy(post_data[i], write); memset(write, 0, len_write); -- cgit 1.2.3-korg