pass host tags to json backends; fixes #3479
Costa Tsaousis (ktsaou) committed
Mar 18, 2018 at 18:49 UTC
b754d2053550ee18ee0b4c5ccb3e2a72dd4f0ca2
1 file changed
+34
-2
src/backends.c
+34
-2
@@ -347,9 +347,24 @@ static inline int format_dimension_collected_json_plaintext(
347
(void)before;
348
(void)options;
349
350
+ const char *tags_pre = "", *tags_post = "", *tags = host->tags;
351
+ if(!tags) tags = "";
352
+
353
+ if(*tags) {
354
+ if(*tags == '{' || *tags == '[' || *tags == '"') {
355
+ tags_pre = "\"host_tags\":";
356
+ tags_post = ",";
357
+ }
358
+ else {
359
+ tags_pre = "\"host_tags\":\"";
360
+ tags_post = "\",";
361
+ }
362
+ }
363
+
364
buffer_sprintf(b, "{"
365
"\"prefix\":\"%s\","
366
"\"hostname\":\"%s\","
367
+ "%s%s%s"
368
369
"\"chart_id\":\"%s\","
370
"\"chart_name\":\"%s\","
@@ -362,9 +377,10 @@ static inline int format_dimension_collected_json_plaintext(
377
"\"name\":\"%s\","
378
"\"value\":" COLLECTED_NUMBER_FORMAT ","
379
365
- "\"timestamp\": %u}\n",
380
+ "\"timestamp\": %u}\n",
381
prefix,
382
hostname,
383
+ tags_pre, tags, tags_post,
384
385
st->id,
386
st->name,
@@ -400,9 +416,24 @@ static inline int format_dimension_stored_json_plaintext(
416
calculated_number value = backend_calculate_value_from_stored_data(st, rd, after, before, options, &first_t, &last_t);
417
418
if(!isnan(value)) {
419
+ const char *tags_pre = "", *tags_post = "", *tags = host->tags;
420
+ if(!tags) tags = "";
421
+
422
+ if(*tags) {
423
+ if(*tags == '{' || *tags == '[' || *tags == '"') {
424
+ tags_pre = "\"host_tags\":";
425
+ tags_post = ",";
426
+ }
427
+ else {
428
+ tags_pre = "\"host_tags\":\"";
429
+ tags_post = "\",";
430
+ }
431
+ }
432
+
433
buffer_sprintf(b, "{"
434
"\"prefix\":\"%s\","
435
"\"hostname\":\"%s\","
436
+ "%s%s%s"
437
438
"\"chart_id\":\"%s\","
439
"\"chart_name\":\"%s\","
@@ -418,7 +449,8 @@ static inline int format_dimension_stored_json_plaintext(
449
"\"timestamp\": %u}\n",
450
prefix,
451
hostname,
421
-
452
+ tags_pre, tags, tags_post,
453
+
454
st->id,
455
st->name,
456
st->family,