Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/plugins/out_stackdriver/stackdriver.c b/plugins/out_stackdriver/stackdriver.c
- index 0ff35601..b1833cf0 100644
- --- a/plugins/out_stackdriver/stackdriver.c
- +++ b/plugins/out_stackdriver/stackdriver.c
- @@ -330,6 +330,63 @@ static int cb_stackdriver_init(struct flb_output_instance *ins,
- return 0;
- }
- +static int validate_severity_level(const char * severity)
- +{
- + int i = 0;
- +
- + const char * severity_level[] = {
- + "EMERGENCY",
- + "ALERT",
- + "CRITICAL",
- + "ERROR",
- + "WARNING",
- + "NOTICE",
- + "INFO",
- + "DEBUG",
- + "DEFAULT",
- + NULL
- + };
- +
- + if (severity == NULL) {
- + return -1;
- + }
- +
- + for (i = 0; severity_level[i] != NULL; i++) {
- + if (strcasecmp(severity, severity_level[i]) == 0) {
- + return 0;
- + }
- + }
- + return -1;
- +}
- +
- +static int map_get_str_kv(flb_sds_t buf, const msgpack_object *o, const flb_sds_t key) {
- + int i = 0;
- + msgpack_object_kv * p = NULL;
- +
- + if (o == NULL || o->type != MSGPACK_OBJECT_MAP || buf == NULL) {
- + return -1;
- + }
- +
- + for (i = 0; i < o->via.map.size; i++) {
- + p = &o->via.map.ptr[i];
- + if (p->key.type != MSGPACK_OBJECT_STR)
- + continue;
- +
- + if (p->key.via.str.size != flb_sds_len(key))
- + continue;
- +
- + if (strncmp(key, p->key.via.str.ptr, flb_sds_len(key)) == 0) {
- + snprintf(buf, p->val.via.str.size + 1, "%s", p->val.via.str.ptr);
- + return 0;
- + }
- + }
- + return -1;
- +}
- +
- +static int get_severity_level(flb_sds_t severity, const msgpack_object * o, const flb_sds_t key) {
- + return (map_get_str_kv(severity, o, key) || validate_severity_level(severity));
- +}
- +
- static int stackdriver_format(const void *data, size_t bytes,
- const char *tag, size_t tag_len,
- char **out_data, size_t *out_size,
- @@ -344,6 +401,7 @@ static int stackdriver_format(const void *data, size_t bytes,
- char time_formatted[255];
- struct tm tm;
- struct flb_time tms;
- + flb_sds_t severity = flb_sds_create_size(10);
- msgpack_object *obj;
- msgpack_unpacked result;
- msgpack_sbuffer mp_sbuf;
- @@ -437,7 +495,7 @@ static int stackdriver_format(const void *data, size_t bytes,
- * "timestamp": "..."
- * }
- */
- - msgpack_pack_map(&mp_pck, 3);
- + msgpack_pack_map(&mp_pck, 4);
- /* jsonPayload */
- msgpack_pack_str(&mp_pck, 11);
- @@ -453,6 +511,15 @@ static int stackdriver_format(const void *data, size_t bytes,
- msgpack_pack_str(&mp_pck, len);
- msgpack_pack_str_body(&mp_pck, path, len);
- + /* severity level */
- + if (get_severity_level(severity, obj, ctx->severity_key)) {
- + sprintf(severity, "DEFAULT");
- + }
- + msgpack_pack_str(&mp_pck, 8);
- + msgpack_pack_str_body(&mp_pck, "severity", 8);
- + msgpack_pack_str(&mp_pck, strlen(severity));
- + msgpack_pack_str_body(&mp_pck, severity, strlen(severity));
- +
- /* timestamp */
- msgpack_pack_str(&mp_pck, 9);
- msgpack_pack_str_body(&mp_pck, "timestamp", 9);
- @@ -473,6 +540,7 @@ static int stackdriver_format(const void *data, size_t bytes,
- ret = flb_msgpack_raw_to_json_str(mp_sbuf.data, mp_sbuf.size,
- &json_buf, &json_size);
- msgpack_sbuffer_destroy(&mp_sbuf);
- + flb_sds_destroy(severity);
- if (ret != 0) {
- flb_error("[out_stackdriver] error formatting JSON payload");
- diff --git a/plugins/out_stackdriver/stackdriver.h b/plugins/out_stackdriver/stackdriver.h
- index 4741f178..d06da0f6 100644
- --- a/plugins/out_stackdriver/stackdriver.h
- +++ b/plugins/out_stackdriver/stackdriver.h
- @@ -46,6 +46,9 @@
- /* Default Resource type */
- #define FLB_SDS_RESOURCE_TYPE "global"
- +/* Default severity level */
- +#define FLB_SDS_SEVERITY_KEY "level"
- +
- struct flb_stackdriver {
- /* credentials */
- flb_sds_t credentials_file;
- @@ -68,6 +71,7 @@ struct flb_stackdriver {
- /* other */
- flb_sds_t resource;
- + flb_sds_t severity_key;
- /* oauth2 context */
- struct flb_oauth2 *o;
- diff --git a/plugins/out_stackdriver/stackdriver_conf.c b/plugins/out_stackdriver/stackdriver_conf.c
- index c1878987..3f1d602e 100644
- --- a/plugins/out_stackdriver/stackdriver_conf.c
- +++ b/plugins/out_stackdriver/stackdriver_conf.c
- @@ -279,6 +279,14 @@ struct flb_stackdriver *flb_stackdriver_conf_create(struct flb_output_instance *
- ctx->resource = flb_sds_create(FLB_SDS_RESOURCE_TYPE);
- }
- + tmp = flb_output_get_property("severity_key", ins);
- + if (tmp) {
- + ctx->severity_key = flb_sds_create(tmp);
- + }
- + else {
- + ctx->severity_key = flb_sds_create(FLB_SDS_SEVERITY_KEY);
- + }
- +
- return ctx;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement