Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. diff --git a/plugins/out_stackdriver/stackdriver.c b/plugins/out_stackdriver/stackdriver.c
  2. index 0ff35601..b1833cf0 100644
  3. --- a/plugins/out_stackdriver/stackdriver.c
  4. +++ b/plugins/out_stackdriver/stackdriver.c
  5. @@ -330,6 +330,63 @@ static int cb_stackdriver_init(struct flb_output_instance *ins,
  6. return 0;
  7. }
  8.  
  9. +static int validate_severity_level(const char * severity)
  10. +{
  11. + int i = 0;
  12. +
  13. + const char * severity_level[] = {
  14. + "EMERGENCY",
  15. + "ALERT",
  16. + "CRITICAL",
  17. + "ERROR",
  18. + "WARNING",
  19. + "NOTICE",
  20. + "INFO",
  21. + "DEBUG",
  22. + "DEFAULT",
  23. + NULL
  24. + };
  25. +
  26. + if (severity == NULL) {
  27. + return -1;
  28. + }
  29. +
  30. + for (i = 0; severity_level[i] != NULL; i++) {
  31. + if (strcasecmp(severity, severity_level[i]) == 0) {
  32. + return 0;
  33. + }
  34. + }
  35. + return -1;
  36. +}
  37. +
  38. +static int map_get_str_kv(flb_sds_t buf, const msgpack_object *o, const flb_sds_t key) {
  39. + int i = 0;
  40. + msgpack_object_kv * p = NULL;
  41. +
  42. + if (o == NULL || o->type != MSGPACK_OBJECT_MAP || buf == NULL) {
  43. + return -1;
  44. + }
  45. +
  46. + for (i = 0; i < o->via.map.size; i++) {
  47. + p = &o->via.map.ptr[i];
  48. + if (p->key.type != MSGPACK_OBJECT_STR)
  49. + continue;
  50. +
  51. + if (p->key.via.str.size != flb_sds_len(key))
  52. + continue;
  53. +
  54. + if (strncmp(key, p->key.via.str.ptr, flb_sds_len(key)) == 0) {
  55. + snprintf(buf, p->val.via.str.size + 1, "%s", p->val.via.str.ptr);
  56. + return 0;
  57. + }
  58. + }
  59. + return -1;
  60. +}
  61. +
  62. +static int get_severity_level(flb_sds_t severity, const msgpack_object * o, const flb_sds_t key) {
  63. + return (map_get_str_kv(severity, o, key) || validate_severity_level(severity));
  64. +}
  65. +
  66. static int stackdriver_format(const void *data, size_t bytes,
  67. const char *tag, size_t tag_len,
  68. char **out_data, size_t *out_size,
  69. @@ -344,6 +401,7 @@ static int stackdriver_format(const void *data, size_t bytes,
  70. char time_formatted[255];
  71. struct tm tm;
  72. struct flb_time tms;
  73. + flb_sds_t severity = flb_sds_create_size(10);
  74. msgpack_object *obj;
  75. msgpack_unpacked result;
  76. msgpack_sbuffer mp_sbuf;
  77. @@ -437,7 +495,7 @@ static int stackdriver_format(const void *data, size_t bytes,
  78. * "timestamp": "..."
  79. * }
  80. */
  81. - msgpack_pack_map(&mp_pck, 3);
  82. + msgpack_pack_map(&mp_pck, 4);
  83.  
  84. /* jsonPayload */
  85. msgpack_pack_str(&mp_pck, 11);
  86. @@ -453,6 +511,15 @@ static int stackdriver_format(const void *data, size_t bytes,
  87. msgpack_pack_str(&mp_pck, len);
  88. msgpack_pack_str_body(&mp_pck, path, len);
  89.  
  90. + /* severity level */
  91. + if (get_severity_level(severity, obj, ctx->severity_key)) {
  92. + sprintf(severity, "DEFAULT");
  93. + }
  94. + msgpack_pack_str(&mp_pck, 8);
  95. + msgpack_pack_str_body(&mp_pck, "severity", 8);
  96. + msgpack_pack_str(&mp_pck, strlen(severity));
  97. + msgpack_pack_str_body(&mp_pck, severity, strlen(severity));
  98. +
  99. /* timestamp */
  100. msgpack_pack_str(&mp_pck, 9);
  101. msgpack_pack_str_body(&mp_pck, "timestamp", 9);
  102. @@ -473,6 +540,7 @@ static int stackdriver_format(const void *data, size_t bytes,
  103. ret = flb_msgpack_raw_to_json_str(mp_sbuf.data, mp_sbuf.size,
  104. &json_buf, &json_size);
  105. msgpack_sbuffer_destroy(&mp_sbuf);
  106. + flb_sds_destroy(severity);
  107.  
  108. if (ret != 0) {
  109. flb_error("[out_stackdriver] error formatting JSON payload");
  110. diff --git a/plugins/out_stackdriver/stackdriver.h b/plugins/out_stackdriver/stackdriver.h
  111. index 4741f178..d06da0f6 100644
  112. --- a/plugins/out_stackdriver/stackdriver.h
  113. +++ b/plugins/out_stackdriver/stackdriver.h
  114. @@ -46,6 +46,9 @@
  115. /* Default Resource type */
  116. #define FLB_SDS_RESOURCE_TYPE "global"
  117.  
  118. +/* Default severity level */
  119. +#define FLB_SDS_SEVERITY_KEY "level"
  120. +
  121. struct flb_stackdriver {
  122. /* credentials */
  123. flb_sds_t credentials_file;
  124. @@ -68,6 +71,7 @@ struct flb_stackdriver {
  125.  
  126. /* other */
  127. flb_sds_t resource;
  128. + flb_sds_t severity_key;
  129.  
  130. /* oauth2 context */
  131. struct flb_oauth2 *o;
  132. diff --git a/plugins/out_stackdriver/stackdriver_conf.c b/plugins/out_stackdriver/stackdriver_conf.c
  133. index c1878987..3f1d602e 100644
  134. --- a/plugins/out_stackdriver/stackdriver_conf.c
  135. +++ b/plugins/out_stackdriver/stackdriver_conf.c
  136. @@ -279,6 +279,14 @@ struct flb_stackdriver *flb_stackdriver_conf_create(struct flb_output_instance *
  137. ctx->resource = flb_sds_create(FLB_SDS_RESOURCE_TYPE);
  138. }
  139.  
  140. + tmp = flb_output_get_property("severity_key", ins);
  141. + if (tmp) {
  142. + ctx->severity_key = flb_sds_create(tmp);
  143. + }
  144. + else {
  145. + ctx->severity_key = flb_sds_create(FLB_SDS_SEVERITY_KEY);
  146. + }
  147. +
  148. return ctx;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement