diff options
author | Valentin Rakush <52716954+alpes214@users.noreply.github.com> | 2019-08-20 12:11:43 +0300 |
---|---|---|
committer | Chris Akritidis <43294513+cakrit@users.noreply.github.com> | 2019-08-20 11:11:43 +0200 |
commit | 92642269f19f585b04d439a3bbe4e60d33918974 (patch) | |
tree | a507dfe903ebacafb4e485a2ee6edcb54867448a | |
parent | d52c20ec4c632459d90797725214be8ffeb66251 (diff) |
Add alarm variables to the response of chart and data (#6615)
##### Summary
Implements feature #6054
Now requests like
http://localhost:19999/api/v1/chart?chart=example.random
http://localhost:19999/api/v1/data?chart=example.random&options=jsonwrap&options=showcustomvars
- return chart variables in their responses. Chart variables include only those with options set to RRDVAR_OPTION_CUSTOM_CHART_VAR
- for /api/v1/data requests chart variables are returned when parameter options=jsonwrap and options=showcustomvars
##### Component Name
[/database](https://github.com/netdata/netdata/tree/master/database/)
[/web/api/formatters](https://github.com/netdata/netdata/tree/master/web/api/formatters)
-rw-r--r-- | database/rrdvar.c | 28 | ||||
-rw-r--r-- | health/health.h | 1 | ||||
-rw-r--r-- | web/api/formatters/json_wrapper.c | 13 | ||||
-rw-r--r-- | web/api/formatters/rrdset2json.c | 5 | ||||
-rw-r--r-- | web/api/netdata-swagger.json | 41 | ||||
-rw-r--r-- | web/api/netdata-swagger.yaml | 29 | ||||
-rw-r--r-- | web/api/queries/rrdr.h | 1 | ||||
-rw-r--r-- | web/api/web_api_v1.c | 1 |
8 files changed, 92 insertions, 27 deletions
diff --git a/database/rrdvar.c b/database/rrdvar.c index 95ab6859ea..0858d0bdcd 100644 --- a/database/rrdvar.c +++ b/database/rrdvar.c @@ -248,6 +248,7 @@ int health_variable_lookup(const char *variable, uint32_t hash, RRDCALC *rc, cal struct variable2json_helper { BUFFER *buf; size_t counter; + RRDVAR_OPTIONS options; }; static int single_variable2json(void *entry, void *data) { @@ -255,22 +256,37 @@ static int single_variable2json(void *entry, void *data) { RRDVAR *rv = (RRDVAR *)entry; calculated_number value = rrdvar2number(rv); - if(unlikely(isnan(value) || isinf(value))) - buffer_sprintf(helper->buf, "%s\n\t\t\"%s\": null", helper->counter?",":"", rv->name); - else - buffer_sprintf(helper->buf, "%s\n\t\t\"%s\": %0.5" LONG_DOUBLE_MODIFIER, helper->counter?",":"", rv->name, (LONG_DOUBLE)value); + if (helper->options == RRDVAR_OPTION_DEFAULT || rv->options & helper->options) { + if(unlikely(isnan(value) || isinf(value))) + buffer_sprintf(helper->buf, "%s\n\t\t\"%s\": null", helper->counter?",":"", rv->name); + else + buffer_sprintf(helper->buf, "%s\n\t\t\"%s\": %0.5" LONG_DOUBLE_MODIFIER, helper->counter?",":"", rv->name, (LONG_DOUBLE)value); - helper->counter++; + helper->counter++; + } return 0; } +void health_api_v1_chart_custom_variables2json(RRDSET *st, BUFFER *buf) { + struct variable2json_helper helper = { + .buf = buf, + .counter = 0, + .options = RRDVAR_OPTION_CUSTOM_CHART_VAR + }; + + buffer_sprintf(buf, "{"); + avl_traverse_lock(&st->rrdvar_root_index, single_variable2json, (void *)&helper); + buffer_strcat(buf, "\n\t\t\t}"); +} + void health_api_v1_chart_variables2json(RRDSET *st, BUFFER *buf) { RRDHOST *host = st->rrdhost; struct variable2json_helper helper = { .buf = buf, - .counter = 0 + .counter = 0, + .options = RRDVAR_OPTION_DEFAULT }; buffer_sprintf(buf, "{\n\t\"chart\": \"%s\",\n\t\"chart_name\": \"%s\",\n\t\"chart_context\": \"%s\",\n\t\"chart_variables\": {", st->id, st->name, st->context); diff --git a/health/health.h b/health/health.h index 6920d12de7..0bf3b8ffa8 100644 --- a/health/health.h +++ b/health/health.h @@ -62,6 +62,7 @@ extern void health_alarms2json(RRDHOST *host, BUFFER *wb, int all); extern void health_alarm_log2json(RRDHOST *host, BUFFER *wb, uint32_t after); void health_api_v1_chart_variables2json(RRDSET *st, BUFFER *buf); +void health_api_v1_chart_custom_variables2json(RRDSET *st, BUFFER *buf); extern int health_alarm_log_open(RRDHOST *host); extern void health_alarm_log_close(RRDHOST *host); diff --git a/web/api/formatters/json_wrapper.c b/web/api/formatters/json_wrapper.c index 4911d2f0ea..5ef2caf101 100644 --- a/web/api/formatters/json_wrapper.c +++ b/web/api/formatters/json_wrapper.c @@ -182,11 +182,14 @@ void rrdr_json_wrapper_begin(RRDR *r, BUFFER *wb, uint32_t format, RRDR_OPTIONS rrdr_buffer_print_format(wb, format); - buffer_sprintf(wb, "%s,\n" - " %sresult%s: " - , sq - , kq, kq - ); + if((options & RRDR_OPTION_CUSTOM_VARS) && (options & RRDR_OPTION_JSON_WRAP)) { + buffer_sprintf(wb, "%s,\n %schart_variables%s: ", sq, kq, kq); + health_api_v1_chart_custom_variables2json(r->st, wb); + } + else + buffer_sprintf(wb, "%s", sq); + + buffer_sprintf(wb, ",\n %sresult%s: ", kq, kq); if(string_value) buffer_strcat(wb, sq); //info("JSONWRAPPER(): %s: END", r->st->id); diff --git a/web/api/formatters/rrdset2json.c b/web/api/formatters/rrdset2json.c index 2bbd2a70f3..4d91399270 100644 --- a/web/api/formatters/rrdset2json.c +++ b/web/api/formatters/rrdset2json.c @@ -73,7 +73,10 @@ 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_strcat(wb, "\n\t\t\t},\n\t\t\t\"green\": "); + buffer_sprintf(wb, "\n\t\t\t},\n\t\t\t\"chart_variables\": "); + health_api_v1_chart_custom_variables2json(st, wb); + + buffer_strcat(wb, ",\n\t\t\t\"green\": "); buffer_rrd_value(wb, st->green); buffer_strcat(wb, ",\n\t\t\t\"red\": "); buffer_rrd_value(wb, st->red); diff --git a/web/api/netdata-swagger.json b/web/api/netdata-swagger.json index 63bc5638de..44e862d174 100644 --- a/web/api/netdata-swagger.json +++ b/web/api/netdata-swagger.json @@ -243,7 +243,8 @@ "percentage", "unaligned", "match-ids", - "match-names" + "match-names", + "showcustomvars" ], "collectionFormat": "pipes" }, @@ -975,6 +976,14 @@ } } }, + "chart_variables": { + "type": "object", + "properties": { + "key": { + "$ref": "#/definitions/chart_variables" + } + } + }, "green": { "type": "number", "description": "Chart health green threshold" @@ -1011,13 +1020,8 @@ "chart_variables": { "type": "object", "properties": { - "varname1": { - "type": "number", - "format": "float" - }, - "varname2": { - "type": "number", - "format": "float" + "key": { + "$ref": "#/definitions/chart_variables" } } }, @@ -1049,6 +1053,19 @@ } } }, + "chart_variables": { + "type": "object", + "properties": { + "varname1": { + "type": "number", + "format": "float" + }, + "varname2": { + "type": "number", + "format": "float" + } + } + }, "dimension": { "type": "object", "properties": { @@ -1145,6 +1162,14 @@ "type": "string", "description": "The format of the result returned." }, + "chart_variables": { + "type": "object", + "properties": { + "key": { + "$ref": "#/definitions/chart_variables" + } + } + }, "result": { "description": "The result requested, in the format requested." } diff --git a/web/api/netdata-swagger.yaml b/web/api/netdata-swagger.yaml index 3386e01a7e..0052e646c2 100644 --- a/web/api/netdata-swagger.yaml +++ b/web/api/netdata-swagger.yaml @@ -163,7 +163,7 @@ paths: type: array items: type: string - enum: [ 'nonzero', 'flip', 'jsonwrap', 'min2max', 'seconds', 'milliseconds', 'abs', 'absolute', 'absolute-sum', 'null2zero', 'objectrows', 'google_json', 'percentage', 'unaligned', 'match-ids', 'match-names' ] + enum: [ 'nonzero', 'flip', 'jsonwrap', 'min2max', 'seconds', 'milliseconds', 'abs', 'absolute', 'absolute-sum', 'null2zero', 'objectrows', 'google_json', 'percentage', 'unaligned', 'match-ids', 'match-names', 'showcustomvars' ] collectionFormat: pipes default: [seconds, jsonwrap] allowEmptyValue: false @@ -654,6 +654,11 @@ definitions: properties: key: $ref: '#/definitions/dimension' + chart_variables: + type: object + properties: + key: + $ref: '#/definitions/chart_variables' green: type: number description: 'Chart health green threshold' @@ -681,12 +686,8 @@ definitions: chart_variables: type: object properties: - varname1: - type: number - format: float - varname2: - type: number - format: float + key: + $ref: '#/definitions/chart_variables' family_variables: type: object properties: @@ -705,6 +706,15 @@ definitions: varname2: type: number format: float + chart_variables: + type: object + properties: + varname1: + type: number + format: float + varname2: + type: number + format: float dimension: type: object properties: @@ -776,6 +786,11 @@ definitions: format: type: string description: 'The format of the result returned.' + chart_variables: + type: object + properties: + key: + $ref: '#/definitions/chart_variables' result: description: 'The result requested, in the format requested.' alarms: diff --git a/web/api/queries/rrdr.h b/web/api/queries/rrdr.h index 4f6350389b..6473ae7457 100644 --- a/web/api/queries/rrdr.h +++ b/web/api/queries/rrdr.h @@ -22,6 +22,7 @@ typedef enum rrdr_options { RRDR_OPTION_DISPLAY_ABS = 0x00002000, // for badges, display the absolute value, but calculate colors with sign RRDR_OPTION_MATCH_IDS = 0x00004000, // when filtering dimensions, match only IDs RRDR_OPTION_MATCH_NAMES = 0x00008000, // when filtering dimensions, match only names + RRDR_OPTION_CUSTOM_VARS = 0x00010000, // when wraping response in a JSON, return custom variables in response } RRDR_OPTIONS; typedef enum rrdr_value_flag { diff --git a/web/api/web_api_v1.c b/web/api/web_api_v1.c index 2273224bb1..2d12049632 100644 --- a/web/api/web_api_v1.c +++ b/web/api/web_api_v1.c @@ -32,6 +32,7 @@ static struct { , {"match-ids" , 0 , RRDR_OPTION_MATCH_IDS} , {"match_names" , 0 , RRDR_OPTION_MATCH_NAMES} , {"match-names" , 0 , RRDR_OPTION_MATCH_NAMES} + , {"showcustomvars" , 0 , RRDR_OPTION_CUSTOM_VARS} , { NULL, 0, 0} }; |