@cryptotaxi247 / netdata-1 / commits / 92642269f

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)

Valentin Rakush committed Aug 20, 2019 at 12:11 UTC 92642269f19f585b04d439a3bbe4e60d33918974
8 files changed +92 -27
database/rrdvar.c
+22 -6
@@ -248,6 +248,7 @@ int health_variable_lookup(const char *variable, uint32_t hash, RRDCALC *rc, cal
248 struct variable2json_helper {
249 BUFFER *buf;
250 size_t counter;
251 + RRDVAR_OPTIONS options;
252 };
253
254 static int single_variable2json(void *entry, void *data) {
@@ -255,22 +256,37 @@ static int single_variable2json(void *entry, void *data) {
256 RRDVAR *rv = (RRDVAR *)entry;
257 calculated_number value = rrdvar2number(rv);
258
258 - if(unlikely(isnan(value) || isinf(value)))
259 - buffer_sprintf(helper->buf, "%s\n\t\t\"%s\": null", helper->counter?",":"", rv->name);
260 - else
261 - buffer_sprintf(helper->buf, "%s\n\t\t\"%s\": %0.5" LONG_DOUBLE_MODIFIER, helper->counter?",":"", rv->name, (LONG_DOUBLE)value);
259 + if (helper->options == RRDVAR_OPTION_DEFAULT || rv->options & helper->options) {
260 + if(unlikely(isnan(value) || isinf(value)))
261 + buffer_sprintf(helper->buf, "%s\n\t\t\"%s\": null", helper->counter?",":"", rv->name);
262 + else
263 + buffer_sprintf(helper->buf, "%s\n\t\t\"%s\": %0.5" LONG_DOUBLE_MODIFIER, helper->counter?",":"", rv->name, (LONG_DOUBLE)value);
264
263 - helper->counter++;
265 + helper->counter++;
266 + }
267
268 return 0;
269 }
270
271 +void health_api_v1_chart_custom_variables2json(RRDSET *st, BUFFER *buf) {
272 + struct variable2json_helper helper = {
273 + .buf = buf,
274 + .counter = 0,
275 + .options = RRDVAR_OPTION_CUSTOM_CHART_VAR
276 + };
277 +
278 + buffer_sprintf(buf, "{");
279 + avl_traverse_lock(&st->rrdvar_root_index, single_variable2json, (void *)&helper);
280 + buffer_strcat(buf, "\n\t\t\t}");
281 +}
282 +
283 void health_api_v1_chart_variables2json(RRDSET *st, BUFFER *buf) {
284 RRDHOST *host = st->rrdhost;
285
286 struct variable2json_helper helper = {
287 .buf = buf,
273 - .counter = 0
288 + .counter = 0,
289 + .options = RRDVAR_OPTION_DEFAULT
290 };
291
292 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);
health/health.h
+1
@@ -62,6 +62,7 @@ extern void health_alarms2json(RRDHOST *host, BUFFER *wb, int all);
62 extern void health_alarm_log2json(RRDHOST *host, BUFFER *wb, uint32_t after);
63
64 void health_api_v1_chart_variables2json(RRDSET *st, BUFFER *buf);
65 +void health_api_v1_chart_custom_variables2json(RRDSET *st, BUFFER *buf);
66
67 extern int health_alarm_log_open(RRDHOST *host);
68 extern void health_alarm_log_close(RRDHOST *host);
web/api/formatters/json_wrapper.c
+8 -5
@@ -182,11 +182,14 @@ void rrdr_json_wrapper_begin(RRDR *r, BUFFER *wb, uint32_t format, RRDR_OPTIONS
182
183 rrdr_buffer_print_format(wb, format);
184
185 - buffer_sprintf(wb, "%s,\n"
186 - " %sresult%s: "
187 - , sq
188 - , kq, kq
189 - );
185 + if((options & RRDR_OPTION_CUSTOM_VARS) && (options & RRDR_OPTION_JSON_WRAP)) {
186 + buffer_sprintf(wb, "%s,\n %schart_variables%s: ", sq, kq, kq);
187 + health_api_v1_chart_custom_variables2json(r->st, wb);
188 + }
189 + else
190 + buffer_sprintf(wb, "%s", sq);
191 +
192 + buffer_sprintf(wb, ",\n %sresult%s: ", kq, kq);
193
194 if(string_value) buffer_strcat(wb, sq);
195 //info("JSONWRAPPER(): %s: END", r->st->id);
web/api/formatters/rrdset2json.c
+4 -1
@@ -73,7 +73,10 @@ void rrdset2json(RRDSET *st, BUFFER *wb, size_t *dimensions_count, size_t *memor
73 if(dimensions_count) *dimensions_count += dimensions;
74 if(memory_used) *memory_used += memory;
75
76 - buffer_strcat(wb, "\n\t\t\t},\n\t\t\t\"green\": ");
76 + buffer_sprintf(wb, "\n\t\t\t},\n\t\t\t\"chart_variables\": ");
77 + health_api_v1_chart_custom_variables2json(st, wb);
78 +
79 + buffer_strcat(wb, ",\n\t\t\t\"green\": ");
80 buffer_rrd_value(wb, st->green);
81 buffer_strcat(wb, ",\n\t\t\t\"red\": ");
82 buffer_rrd_value(wb, st->red);
web/api/netdata-swagger.json
+33 -8
@@ -243,7 +243,8 @@
243 "percentage",
244 "unaligned",
245 "match-ids",
246 - "match-names"
246 + "match-names",
247 + "showcustomvars"
248 ],
249 "collectionFormat": "pipes"
250 },
@@ -975,6 +976,14 @@
976 }
977 }
978 },
979 + "chart_variables": {
980 + "type": "object",
981 + "properties": {
982 + "key": {
983 + "$ref": "#/definitions/chart_variables"
984 + }
985 + }
986 + },
987 "green": {
988 "type": "number",
989 "description": "Chart health green threshold"
@@ -1011,13 +1020,8 @@
1020 "chart_variables": {
1021 "type": "object",
1022 "properties": {
1014 - "varname1": {
1015 - "type": "number",
1016 - "format": "float"
1017 - },
1018 - "varname2": {
1019 - "type": "number",
1020 - "format": "float"
1023 + "key": {
1024 + "$ref": "#/definitions/chart_variables"
1025 }
1026 }
1027 },
@@ -1049,6 +1053,19 @@
1053 }
1054 }
1055 },
1056 + "chart_variables": {
1057 + "type": "object",
1058 + "properties": {
1059 + "varname1": {
1060 + "type": "number",
1061 + "format": "float"
1062 + },
1063 + "varname2": {
1064 + "type": "number",
1065 + "format": "float"
1066 + }
1067 + }
1068 + },
1069 "dimension": {
1070 "type": "object",
1071 "properties": {
@@ -1145,6 +1162,14 @@
1162 "type": "string",
1163 "description": "The format of the result returned."
1164 },
1165 + "chart_variables": {
1166 + "type": "object",
1167 + "properties": {
1168 + "key": {
1169 + "$ref": "#/definitions/chart_variables"
1170 + }
1171 + }
1172 + },
1173 "result": {
1174 "description": "The result requested, in the format requested."
1175 }
web/api/netdata-swagger.yaml
+22 -7
@@ -163,7 +163,7 @@ paths:
163 type: array
164 items:
165 type: string
166 - enum: [ 'nonzero', 'flip', 'jsonwrap', 'min2max', 'seconds', 'milliseconds', 'abs', 'absolute', 'absolute-sum', 'null2zero', 'objectrows', 'google_json', 'percentage', 'unaligned', 'match-ids', 'match-names' ]
166 + enum: [ 'nonzero', 'flip', 'jsonwrap', 'min2max', 'seconds', 'milliseconds', 'abs', 'absolute', 'absolute-sum', 'null2zero', 'objectrows', 'google_json', 'percentage', 'unaligned', 'match-ids', 'match-names', 'showcustomvars' ]
167 collectionFormat: pipes
168 default: [seconds, jsonwrap]
169 allowEmptyValue: false
@@ -654,6 +654,11 @@ definitions:
654 properties:
655 key:
656 $ref: '#/definitions/dimension'
657 + chart_variables:
658 + type: object
659 + properties:
660 + key:
661 + $ref: '#/definitions/chart_variables'
662 green:
663 type: number
664 description: 'Chart health green threshold'
@@ -681,12 +686,8 @@ definitions:
686 chart_variables:
687 type: object
688 properties:
684 - varname1:
685 - type: number
686 - format: float
687 - varname2:
688 - type: number
689 - format: float
689 + key:
690 + $ref: '#/definitions/chart_variables'
691 family_variables:
692 type: object
693 properties:
@@ -705,6 +706,15 @@ definitions:
706 varname2:
707 type: number
708 format: float
709 + chart_variables:
710 + type: object
711 + properties:
712 + varname1:
713 + type: number
714 + format: float
715 + varname2:
716 + type: number
717 + format: float
718 dimension:
719 type: object
720 properties:
@@ -776,6 +786,11 @@ definitions:
786 format:
787 type: string
788 description: 'The format of the result returned.'
789 + chart_variables:
790 + type: object
791 + properties:
792 + key:
793 + $ref: '#/definitions/chart_variables'
794 result:
795 description: 'The result requested, in the format requested.'
796 alarms:
web/api/queries/rrdr.h
+1
@@ -22,6 +22,7 @@ typedef enum rrdr_options {
22 RRDR_OPTION_DISPLAY_ABS = 0x00002000, // for badges, display the absolute value, but calculate colors with sign
23 RRDR_OPTION_MATCH_IDS = 0x00004000, // when filtering dimensions, match only IDs
24 RRDR_OPTION_MATCH_NAMES = 0x00008000, // when filtering dimensions, match only names
25 + RRDR_OPTION_CUSTOM_VARS = 0x00010000, // when wraping response in a JSON, return custom variables in response
26 } RRDR_OPTIONS;
27
28 typedef enum rrdr_value_flag {
web/api/web_api_v1.c
+1
@@ -32,6 +32,7 @@ static struct {
32 , {"match-ids" , 0 , RRDR_OPTION_MATCH_IDS}
33 , {"match_names" , 0 , RRDR_OPTION_MATCH_NAMES}
34 , {"match-names" , 0 , RRDR_OPTION_MATCH_NAMES}
35 + , {"showcustomvars" , 0 , RRDR_OPTION_CUSTOM_VARS}
36 , { NULL, 0, 0}
37 };
38