Skip to content

Commit 3912b90

Browse files
committed
- Added unit tests for the removed severity field from jsonPayload sent to stackdriver
- Added support for overriding the logName sent to stackdriver. By default, it's `projects/<project id>/logs/<tag>`, but `<tag>` should be replacable by a field from the log entry, because this will allow to group the logs properly and visualize in the logs dropdown in stackdriver. Signed-off-by: Todor Petrov <[email protected]>
1 parent 3a0d2cc commit 3912b90

File tree

5 files changed

+182
-15
lines changed

5 files changed

+182
-15
lines changed

plugins/out_stackdriver/stackdriver.c

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -994,30 +994,31 @@ static int get_msgpack_obj(msgpack_object * subobj, const msgpack_object * o,
994994
return -1;
995995
}
996996

997-
static int get_severity_level(severity_t * s, const msgpack_object * o,
998-
const flb_sds_t key)
997+
static int get_string(flb_sds_t * s, const msgpack_object * o, const flb_sds_t key)
999998
{
1000999
msgpack_object tmp;
1001-
if (get_msgpack_obj(&tmp, o, key, flb_sds_len(key), MSGPACK_OBJECT_STR) == 0
1002-
&& validate_severity_level(s, tmp.via.str.ptr, tmp.via.str.size) == 0) {
1000+
if (get_msgpack_obj(&tmp, o, key, flb_sds_len(key), MSGPACK_OBJECT_STR) == 0) {
1001+
*s = flb_sds_create_len(tmp.via.str.ptr, tmp.via.str.size);
10031002
return 0;
10041003
}
1004+
10051005
*s = 0;
10061006
return -1;
10071007
}
10081008

1009-
static int get_trace(flb_sds_t * t, const msgpack_object * o, const flb_sds_t key)
1009+
static int get_severity_level(severity_t * s, const msgpack_object * o,
1010+
const flb_sds_t key)
10101011
{
10111012
msgpack_object tmp;
1012-
if (get_msgpack_obj(&tmp, o, key, flb_sds_len(key), MSGPACK_OBJECT_STR) == 0) {
1013-
*t = flb_sds_create_len(tmp.via.str.ptr, tmp.via.str.size);
1013+
if (get_msgpack_obj(&tmp, o, key, flb_sds_len(key), MSGPACK_OBJECT_STR) == 0
1014+
&& validate_severity_level(s, tmp.via.str.ptr, tmp.via.str.size) == 0) {
10141015
return 0;
10151016
}
1016-
1017-
*t = 0;
1017+
*s = 0;
10181018
return -1;
10191019
}
10201020

1021+
10211022
static int get_stream(msgpack_object_map map)
10221023
{
10231024
int i;
@@ -1118,8 +1119,11 @@ static int pack_json_payload(int insert_id_extracted,
11181119
ctx->labels_key,
11191120
ctx->severity_key,
11201121
ctx->trace_key,
1122+
ctx->log_name_key,
11211123
stream
1122-
/* more special fields are required to be added */
1124+
/* more special fields are required to be added, but, if this grows with more
1125+
than a few records, it might need to be converted to flb_hash
1126+
*/
11231127
};
11241128

11251129
if (insert_id_extracted == FLB_TRUE) {
@@ -1152,7 +1156,7 @@ static int pack_json_payload(int insert_id_extracted,
11521156
* check length of key to avoid partial matching
11531157
* e.g. labels key = labels && kv->key = labelss
11541158
*/
1155-
if (flb_sds_cmp(removed, kv->key.via.str.ptr, len) == 0) {
1159+
if (removed && flb_sds_cmp(removed, kv->key.via.str.ptr, len) == 0) {
11561160
to_remove += 1;
11571161
break;
11581162
}
@@ -1229,7 +1233,7 @@ static int pack_json_payload(int insert_id_extracted,
12291233
len = kv->key.via.str.size;
12301234
for (j = 0; j < len_to_be_removed; j++) {
12311235
removed = to_be_removed[j];
1232-
if (flb_sds_cmp(removed, kv->key.via.str.ptr, len) == 0) {
1236+
if (removed && flb_sds_cmp(removed, kv->key.via.str.ptr, len) == 0) {
12331237
key_not_found = 0;
12341238
break;
12351239
}
@@ -1278,6 +1282,7 @@ static int stackdriver_format(struct flb_config *config,
12781282
char path[PATH_MAX];
12791283
char time_formatted[255];
12801284
const char *newtag;
1285+
const char *new_log_name;
12811286
msgpack_object *obj;
12821287
msgpack_object *labels_ptr;
12831288
msgpack_unpacked result;
@@ -1294,6 +1299,10 @@ static int stackdriver_format(struct flb_config *config,
12941299
int trace_extracted = FLB_FALSE;
12951300
flb_sds_t trace;
12961301

1302+
/* Parameters for log name */
1303+
int log_name_extracted = FLB_FALSE;
1304+
flb_sds_t log_name;
1305+
12971306
/* Parameters for insertId */
12981307
msgpack_object insert_id_obj;
12991308
insert_id_status in_status;
@@ -1616,11 +1625,18 @@ static int stackdriver_format(struct flb_config *config,
16161625
/* Extract trace */
16171626
trace_extracted = FLB_FALSE;
16181627
if (ctx->trace_key
1619-
&& get_trace(&trace, obj, ctx->trace_key) == 0) {
1628+
&& get_string(&trace, obj, ctx->trace_key) == 0) {
16201629
trace_extracted = FLB_TRUE;
16211630
entry_size += 1;
16221631
}
16231632

1633+
/* Extract log name */
1634+
log_name_extracted = FLB_FALSE;
1635+
if (ctx->log_name_key
1636+
&& get_string(&log_name, obj, ctx->log_name_key) == 0) {
1637+
log_name_extracted = FLB_TRUE;
1638+
}
1639+
16241640
/* Extract insertId */
16251641
in_status = validate_insert_id(&insert_id_obj, obj);
16261642
if (in_status == INSERTID_VALID) {
@@ -1767,9 +1783,21 @@ static int stackdriver_format(struct flb_config *config,
17671783
newtag = "stderr";
17681784
}
17691785
}
1786+
1787+
if (log_name_extracted == FLB_FALSE) {
1788+
new_log_name = newtag;
1789+
}
1790+
else {
1791+
new_log_name = log_name;
1792+
}
1793+
17701794
/* logName */
17711795
len = snprintf(path, sizeof(path) - 1,
1772-
"projects/%s/logs/%s", ctx->project_id, newtag);
1796+
"projects/%s/logs/%s", ctx->project_id, new_log_name);
1797+
1798+
if (log_name_extracted == FLB_TRUE) {
1799+
flb_sds_destroy(log_name);
1800+
}
17731801

17741802
msgpack_pack_str(&mp_pck, 7);
17751803
msgpack_pack_str_body(&mp_pck, "logName", 7);

plugins/out_stackdriver/stackdriver.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ struct flb_stackdriver {
111111
flb_sds_t resource;
112112
flb_sds_t severity_key;
113113
flb_sds_t trace_key;
114+
flb_sds_t log_name_key;
114115

115116
/* oauth2 context */
116117
struct flb_oauth2 *o;

plugins/out_stackdriver/stackdriver_conf.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,11 @@ struct flb_stackdriver *flb_stackdriver_conf_create(struct flb_output_instance *
280280
ctx->trace_key = flb_sds_create(DEFAULT_TRACE_KEY);
281281
}
282282

283+
tmp = flb_output_get_property("log_name_key", ins);
284+
if (tmp) {
285+
ctx->log_name_key = flb_sds_create(tmp);
286+
}
287+
283288
if (flb_sds_cmp(ctx->resource, "k8s_container",
284289
flb_sds_len(ctx->resource)) == 0 ||
285290
flb_sds_cmp(ctx->resource, "k8s_node",
@@ -356,6 +361,7 @@ int flb_stackdriver_conf_destroy(struct flb_stackdriver *ctx)
356361
flb_sds_destroy(ctx->resource);
357362
flb_sds_destroy(ctx->severity_key);
358363
flb_sds_destroy(ctx->trace_key);
364+
flb_sds_destroy(ctx->log_name_key);
359365
flb_sds_destroy(ctx->labels_key);
360366
flb_sds_destroy(ctx->tag_prefix);
361367

tests/runtime/data/stackdriver/stackdriver_test_log_name.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@
22
"1591111124," \
33
"{" \
44
"\"custom_log_name_key\": \"custom_log_name\"" \
5-
"}]"
5+
"}]"
6+
7+
#define LOG_NAME_NO_OVERRIDE "[" \
8+
"1591111124," \
9+
"{" \
10+
"}]"

tests/runtime/out_stackdriver.c

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "data/stackdriver/stackdriver_test_k8s_resource.h"
3636
#include "data/stackdriver/stackdriver_test_labels.h"
3737
#include "data/stackdriver/stackdriver_test_trace.h"
38+
#include "data/stackdriver/stackdriver_test_log_name.h"
3839
#include "data/stackdriver/stackdriver_test_insert_id.h"
3940
#include "data/stackdriver/stackdriver_test_source_location.h"
4041
#include "data/stackdriver/stackdriver_test_http_request.h"
@@ -588,6 +589,38 @@ static void cb_check_trace_common_case(void *ctx, int ffd,
588589
flb_sds_destroy(res_data);
589590
}
590591

592+
static void cb_check_log_name_override(void *ctx, int ffd,
593+
int res_ret, void *res_data, size_t res_size,
594+
void *data)
595+
{
596+
int ret;
597+
598+
/* logName in the entries is created using the value under log_name_key */
599+
ret = mp_kv_cmp(
600+
res_data, res_size, "$entries[0]['logName']", "projects/fluent-bit-test/logs/custom_log_name");
601+
TEST_CHECK(ret == FLB_TRUE);
602+
603+
/* log_name_key has been removed from jsonPayload */
604+
ret = mp_kv_exists(res_data, res_size, "$entries[0]['jsonPayload']['custom_log_name_key']");
605+
TEST_CHECK(ret == FLB_FALSE);
606+
607+
flb_sds_destroy(res_data);
608+
}
609+
610+
static void cb_check_log_name_no_override(void *ctx, int ffd,
611+
int res_ret, void *res_data, size_t res_size,
612+
void *data)
613+
{
614+
int ret;
615+
616+
/* logName in the entries is created using the tag */
617+
ret = mp_kv_cmp(
618+
res_data, res_size, "$entries[0]['logName']", "projects/fluent-bit-test/logs/test");
619+
TEST_CHECK(ret == FLB_TRUE);
620+
621+
flb_sds_destroy(res_data);
622+
}
623+
591624
static void cb_check_k8s_node_resource(void *ctx, int ffd,
592625
int res_ret, void *res_data, size_t res_size,
593626
void *data)
@@ -1058,12 +1091,20 @@ static void cb_check_multi_entries_severity(void *ctx, int ffd,
10581091
ret = mp_kv_cmp(res_data, res_size, "$entries[0]['severity']", "INFO");
10591092
TEST_CHECK(ret == FLB_TRUE);
10601093

1094+
// verifies that severity is removed from jsonPayload
1095+
ret = mp_kv_exists(res_data, res_size, "$entries[0]['jsonPayload']['severity']");
1096+
TEST_CHECK(ret == FLB_FALSE);
1097+
10611098
ret = mp_kv_exists(res_data, res_size, "$entries[1]['severity']");
10621099
TEST_CHECK(ret == FLB_FALSE);
10631100

10641101
ret = mp_kv_cmp(res_data, res_size, "$entries[2]['severity']", "DEBUG");
10651102
TEST_CHECK(ret == FLB_TRUE);
10661103

1104+
// verifies that severity is removed from jsonPayload
1105+
ret = mp_kv_exists(res_data, res_size, "$entries[2]['jsonPayload']['severity']");
1106+
TEST_CHECK(ret == FLB_FALSE);
1107+
10671108
ret = mp_kv_exists(res_data, res_size, "$entries[3]['severity']");
10681109
TEST_CHECK(ret == FLB_FALSE);
10691110

@@ -1913,6 +1954,88 @@ void flb_test_trace_common_case()
19131954
flb_destroy(ctx);
19141955
}
19151956

1957+
void flb_test_log_name_override()
1958+
{
1959+
int ret;
1960+
int size = sizeof(LOG_NAME_OVERRIDE) - 1;
1961+
flb_ctx_t *ctx;
1962+
int in_ffd;
1963+
int out_ffd;
1964+
1965+
/* Create context, flush every second (some checks omitted here) */
1966+
ctx = flb_create();
1967+
flb_service_set(ctx, "flush", "1", "grace", "1", NULL);
1968+
1969+
/* Lib input mode */
1970+
in_ffd = flb_input(ctx, (char *) "lib", NULL);
1971+
flb_input_set(ctx, in_ffd, "tag", "test", NULL);
1972+
1973+
/* Stackdriver output */
1974+
out_ffd = flb_output(ctx, (char *) "stackdriver", NULL);
1975+
flb_output_set(ctx, out_ffd,
1976+
"match", "test",
1977+
"resource", "gce_instance",
1978+
"log_name_key", "custom_log_name_key",
1979+
NULL);
1980+
1981+
/* Enable test mode */
1982+
ret = flb_output_set_test(ctx, out_ffd, "formatter",
1983+
cb_check_log_name_override,
1984+
NULL, NULL);
1985+
1986+
/* Start */
1987+
ret = flb_start(ctx);
1988+
TEST_CHECK(ret == 0);
1989+
1990+
/* Ingest data sample */
1991+
flb_lib_push(ctx, in_ffd, (char *) LOG_NAME_OVERRIDE, size);
1992+
1993+
sleep(2);
1994+
flb_stop(ctx);
1995+
flb_destroy(ctx);
1996+
}
1997+
1998+
void flb_test_log_name_no_override()
1999+
{
2000+
int ret;
2001+
int size = sizeof(LOG_NAME_NO_OVERRIDE) - 1;
2002+
flb_ctx_t *ctx;
2003+
int in_ffd;
2004+
int out_ffd;
2005+
2006+
/* Create context, flush every second (some checks omitted here) */
2007+
ctx = flb_create();
2008+
flb_service_set(ctx, "flush", "1", "grace", "1", NULL);
2009+
2010+
/* Lib input mode */
2011+
in_ffd = flb_input(ctx, (char *) "lib", NULL);
2012+
flb_input_set(ctx, in_ffd, "tag", "test", NULL);
2013+
2014+
/* Stackdriver output */
2015+
out_ffd = flb_output(ctx, (char *) "stackdriver", NULL);
2016+
flb_output_set(ctx, out_ffd,
2017+
"match", "test",
2018+
"resource", "gce_instance",
2019+
"log_name_key", "custom_log_name_key",
2020+
NULL);
2021+
2022+
/* Enable test mode */
2023+
ret = flb_output_set_test(ctx, out_ffd, "formatter",
2024+
cb_check_log_name_no_override,
2025+
NULL, NULL);
2026+
2027+
/* Start */
2028+
ret = flb_start(ctx);
2029+
TEST_CHECK(ret == 0);
2030+
2031+
/* Ingest data sample */
2032+
flb_lib_push(ctx, in_ffd, (char *) LOG_NAME_NO_OVERRIDE, size);
2033+
2034+
sleep(2);
2035+
flb_stop(ctx);
2036+
flb_destroy(ctx);
2037+
}
2038+
19162039
void flb_test_resource_global_custom_prefix()
19172040
{
19182041
/* configuring tag_prefix for non-k8s resource type should have no effect at all */
@@ -3987,6 +4110,10 @@ TEST_LIST = {
39874110
/* test trace */
39884111
{"trace_common_case", flb_test_trace_common_case},
39894112

4113+
/* test log name */
4114+
{"log_name_override", flb_test_log_name_override},
4115+
{"log_name_no_override", flb_test_log_name_no_override},
4116+
39904117
/* test insertId */
39914118
{"insertId_common_case", flb_test_insert_id_common_case},
39924119
{"empty_insertId", flb_test_empty_insert_id},

0 commit comments

Comments
 (0)