From c8c72f18a6a8fd09d3b6284e49525396b24e8395 Mon Sep 17 00:00:00 2001 From: Andrew Moss <1043609+amoss@users.noreply.github.com> Date: Mon, 16 Dec 2019 15:12:00 +0100 Subject: Labels issues (#7515) Initial work on host labels from the dedicated branch. Includes work for issues #7096, #7400, #7411, #7369, #7410, #7458, #7459, #7412 and #7408 by @vlvkobal, @thiagoftsm, @cakrit and @amoss. --- web/api/formatters/charts2json.c | 7 ++++- web/api/formatters/json_wrapper.c | 27 +++++++++++++--- web/api/formatters/rrdset2json.c | 6 +++- web/api/netdata-swagger.json | 66 +++++++++++++++++++++++++++++++++++++++ web/api/netdata-swagger.yaml | 48 ++++++++++++++++++++++++++++ web/api/web_api_v1.c | 35 +++++++++++++++++++-- web/api/web_api_v1.h | 2 ++ 7 files changed, 183 insertions(+), 8 deletions(-) (limited to 'web/api') diff --git a/web/api/formatters/charts2json.c b/web/api/formatters/charts2json.c index db3ef37eaf..efe6eaa257 100644 --- a/web/api/formatters/charts2json.c +++ b/web/api/formatters/charts2json.c @@ -56,7 +56,6 @@ void charts2json(RRDHOST *host, BUFFER *wb) { ",\n\t\"history\": %ld" ",\n\t\"memory_mode\": \"%s\"" ",\n\t\"custom_info\": \"%s\"" - ",\n\t\"charts\": {" , host->hostname , host->program_version , get_release_channel() @@ -68,6 +67,12 @@ void charts2json(RRDHOST *host, BUFFER *wb) { , custom_dashboard_info_js_filename ); + buffer_strcat(wb, ",\n\t\"labels\": {\n"); + host_labels2json(host, wb, 2); + buffer_strcat(wb, "\t}"); + + buffer_strcat(wb, ",\n\t\"charts\": {"); + c = 0; rrdhost_rdlock(host); rrdset_foreach_read(st, host) { diff --git a/web/api/formatters/json_wrapper.c b/web/api/formatters/json_wrapper.c index 5ef2caf101..08354d0465 100644 --- a/web/api/formatters/json_wrapper.c +++ b/web/api/formatters/json_wrapper.c @@ -26,16 +26,35 @@ void rrdr_json_wrapper_begin(RRDR *r, BUFFER *wb, uint32_t format, RRDR_OPTIONS " %sapi%s: 1,\n" " %sid%s: %s%s%s,\n" " %sname%s: %s%s%s,\n" - " %sview_update_every%s: %d,\n" + , kq, kq + , kq, kq, sq, r->st->id, sq + , kq, kq, sq, r->st->name, sq); + + RRDHOST *host = r->st->rrdhost; + buffer_sprintf(wb, " %slabels%s: {\n", kq, kq); + + int count = 0; + netdata_rwlock_rdlock(&host->labels_rwlock); + for (struct label *label = host->labels; label; label = label->next) { + if(count > 0) buffer_strcat(wb, ",\n"); + + char value[CONFIG_MAX_VALUE * 2 + 1]; + escape_json_string(value, label->value, CONFIG_MAX_VALUE * 2); + buffer_sprintf(wb, " %s%s%s: %s%s%s", kq, label->key, kq, sq, value, sq); + + count++; + } + netdata_rwlock_unlock(&host->labels_rwlock); + + buffer_strcat(wb, "\n },\n"); + + buffer_sprintf(wb, " %sview_update_every%s: %d,\n" " %supdate_every%s: %d,\n" " %sfirst_entry%s: %u,\n" " %slast_entry%s: %u,\n" " %sbefore%s: %u,\n" " %safter%s: %u,\n" " %sdimension_names%s: [" - , kq, kq - , kq, kq, sq, r->st->id, sq - , kq, kq, sq, r->st->name, sq , kq, kq, r->update_every , kq, kq, r->st->update_every , kq, kq, (uint32_t)rrdset_first_entry_t(r->st) diff --git a/web/api/formatters/rrdset2json.c b/web/api/formatters/rrdset2json.c index 4d91399270..8e3297d5b3 100644 --- a/web/api/formatters/rrdset2json.c +++ b/web/api/formatters/rrdset2json.c @@ -73,7 +73,11 @@ void rrdset2json(RRDSET *st, BUFFER *wb, size_t *dimensions_count, size_t *memor if(dimensions_count) *dimensions_count += dimensions; if(memory_used) *memory_used += memory; - buffer_sprintf(wb, "\n\t\t\t},\n\t\t\t\"chart_variables\": "); + buffer_strcat(wb, "\n\t\t\t},\n\t\t\t\"labels\": {\n"); + host_labels2json(st->rrdhost, wb, 4); + buffer_strcat(wb, "\t\t\t}"); + + buffer_sprintf(wb, ",\n\t\t\t\"chart_variables\": "); health_api_v1_chart_custom_variables2json(st, wb); buffer_strcat(wb, ",\n\t\t\t\"green\": "); diff --git a/web/api/netdata-swagger.json b/web/api/netdata-swagger.json index f09c8fe312..9c8c4ac063 100644 --- a/web/api/netdata-swagger.json +++ b/web/api/netdata-swagger.json @@ -988,6 +988,17 @@ "description": "Container technology detection method.", "example": "dockerenv" }, + "labels": { + "type": "object", + "description": "List of host labels.", + "properties": { + "app": { + "type": "string", + "description": "Host label.", + "example": "netdata" + } + } + }, "collectors": { "type": "array", "items": { @@ -1068,6 +1079,17 @@ "type": "number", "description": "The default update frequency of the netdata server. All charts have an update frequency equal or bigger than this." }, + "labels": { + "type": "object", + "description": "List of host labels.", + "properties": { + "app": { + "type": "string", + "description": "Host label.", + "example": "netdata" + } + } + }, "charts": { "type": "object", "description": "An object containing all the chart objects available at the netdata server. This is used as an indexed array. The key of each chart object is the id of the chart.", @@ -1170,6 +1192,17 @@ } } }, + "labels": { + "type": "object", + "description": "List of host labels.", + "properties": { + "app": { + "type": "string", + "description": "Host label.", + "example": "netdata" + } + } + }, "chart_variables": { "type": "object", "additionalProperties": { @@ -1211,6 +1244,17 @@ "type": "string", "description": "The host containing the chart." }, + "labels": { + "type": "object", + "description": "List of host labels.", + "properties": { + "app": { + "type": "string", + "description": "Host label.", + "example": "netdata" + } + } + }, "chart_variables": { "type": "object", "additionalProperties": { @@ -1277,6 +1321,17 @@ "type": "string", "description": "The name of the chart." }, + "labels": { + "type": "object", + "description": "List of host labels.", + "properties": { + "app": { + "type": "string", + "description": "Host label.", + "example": "netdata" + } + } + }, "update_every": { "type": "number", "description": "The update frequency of this chart, in seconds. One value every this amount of time is kept in the round robin database (indepedently of the current view)." @@ -1684,6 +1739,17 @@ } } } + }, + "labels": { + "type": "object", + "description": "List of host labels.", + "properties": { + "app": { + "type": "string", + "description": "Host label.", + "example": "netdata" + } + } } } }, diff --git a/web/api/netdata-swagger.yaml b/web/api/netdata-swagger.yaml index 78644a76a8..e5e8226bc8 100644 --- a/web/api/netdata-swagger.yaml +++ b/web/api/netdata-swagger.yaml @@ -809,6 +809,14 @@ components: type: string description: Container technology detection method. example: dockerenv + labels: + type: object + description: List of host labels. + properties: + app: + type: string + description: Host label. + example: netdata collectors: type: array items: @@ -870,6 +878,14 @@ components: type: number description: The default update frequency of the netdata server. All charts have an update frequency equal or bigger than this. + labels: + type: object + description: List of host labels. + properties: + app: + type: string + description: Host label. + example: netdata charts: type: object description: An object containing all the chart objects available at the netdata @@ -963,6 +979,14 @@ components: name: type: string description: The name of the dimension + labels: + type: object + description: List of host labels. + properties: + app: + type: string + description: Host label. + example: netdata chart_variables: type: object additionalProperties: @@ -994,6 +1018,14 @@ components: host: type: string description: The host containing the chart. + labels: + type: object + description: List of host labels. + properties: + app: + type: string + description: Host label. + example: netdata chart_variables: type: object additionalProperties: @@ -1041,6 +1073,14 @@ components: name: type: string description: The name of the chart. + labels: + type: object + description: List of host labels. + properties: + app: + type: string + description: Host label. + example: netdata update_every: type: number description: The update frequency of this chart, in seconds. One value every this @@ -1333,6 +1373,14 @@ components: format: nullable value: type: number + labels: + type: object + description: List of host labels. + properties: + app: + type: string + description: Host label. + example: netdata alarm_log_entry: type: object properties: diff --git a/web/api/web_api_v1.c b/web/api/web_api_v1.c index 3efac5669d..9c3a1f75d8 100644 --- a/web/api/web_api_v1.c +++ b/web/api/web_api_v1.c @@ -761,10 +761,37 @@ static inline void web_client_api_request_v1_info_mirrored_hosts(BUFFER *wb) { rrd_unlock(); } +inline void host_labels2json(RRDHOST *host, BUFFER *wb, size_t indentation) { + char tabs[11]; + + if (indentation > 10) + indentation = 10; + + tabs[0] = '\0'; + while (indentation) { + strcat(tabs, "\t"); + indentation--; + } + + int count = 0; + netdata_rwlock_rdlock(&host->labels_rwlock); + for (struct label *label = host->labels; label; label = label->next) { + if(count > 0) buffer_strcat(wb, ",\n"); + buffer_strcat(wb, tabs); + + char value[CONFIG_MAX_VALUE * 2 + 1]; + escape_json_string(value, label->value, CONFIG_MAX_VALUE * 2); + buffer_sprintf(wb, "\"%s\": \"%s\"", label->key, value); + + count++; + } + buffer_strcat(wb, "\n"); + netdata_rwlock_unlock(&host->labels_rwlock); +} + inline int web_client_api_request_v1_info(RRDHOST *host, struct web_client *w, char *url) { (void)url; if (!netdata_ready) return HTTP_RESP_BACKEND_FETCH_FAILED; - BUFFER *wb = w->response.data; buffer_flush(wb); wb->contenttype = CT_APPLICATION_JSON; @@ -781,7 +808,7 @@ inline int web_client_api_request_v1_info(RRDHOST *host, struct web_client *w, c web_client_api_request_v1_info_summary_alarm_statuses(host, wb); buffer_strcat(wb, "\t},\n"); - buffer_sprintf(wb, "\t\"os_name\": %s,\n", (host->system_info->os_name) ? host->system_info->os_name : "\"\""); + buffer_sprintf(wb, "\t\"os_name\": \"%s\",\n", (host->system_info->os_name) ? host->system_info->os_name : "\"\""); buffer_sprintf(wb, "\t\"os_id\": \"%s\",\n", (host->system_info->os_id) ? host->system_info->os_id : ""); buffer_sprintf(wb, "\t\"os_id_like\": \"%s\",\n", (host->system_info->os_id_like) ? host->system_info->os_id_like : ""); buffer_sprintf(wb, "\t\"os_version\": \"%s\",\n", (host->system_info->os_version) ? host->system_info->os_version : ""); @@ -795,6 +822,10 @@ inline int web_client_api_request_v1_info(RRDHOST *host, struct web_client *w, c buffer_sprintf(wb, "\t\"container\": \"%s\",\n", (host->system_info->container) ? host->system_info->container : ""); buffer_sprintf(wb, "\t\"container_detection\": \"%s\",\n", (host->system_info->container_detection) ? host->system_info->container_detection : ""); + buffer_strcat(wb, "\t\"labels\": {\n"); + host_labels2json(host, wb, 2); + buffer_strcat(wb, "\t},\n"); + buffer_strcat(wb, "\t\"collectors\": ["); chartcollectors2json(host, wb); buffer_strcat(wb, "\n\t]\n"); diff --git a/web/api/web_api_v1.h b/web/api/web_api_v1.h index 3edb47e3c7..4fe3c8f6be 100644 --- a/web/api/web_api_v1.h +++ b/web/api/web_api_v1.h @@ -24,6 +24,8 @@ extern int web_client_api_request_v1_registry(RRDHOST *host, struct web_client * extern int web_client_api_request_v1_info(RRDHOST *host, struct web_client *w, char *url); extern int web_client_api_request_v1(RRDHOST *host, struct web_client *w, char *url); +extern void host_labels2json(RRDHOST *host, BUFFER *wb, size_t indentation); + extern void web_client_api_v1_init(void); extern void web_client_api_v1_management_init(void); -- cgit v1.2.3