Skip to content

Commit 71deeda

Browse files
authored
out_stackdriver: support dot '.' in tag_prefix option (#2468)
Signed-off-by: Jeff Luo <[email protected]>
1 parent e6851e3 commit 71deeda

File tree

2 files changed

+90
-18
lines changed

2 files changed

+90
-18
lines changed

plugins/out_stackdriver/stackdriver.c

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,31 @@ static bool validate_msgpack_unpacked_data(msgpack_object root)
288288
root.via.array.ptr[1].type == MSGPACK_OBJECT_MAP;
289289
}
290290

291+
void replace_prefix_dot(flb_sds_t s, int tag_prefix_len)
292+
{
293+
int i;
294+
int str_len;
295+
char c;
296+
297+
if (!s) {
298+
return;
299+
}
300+
301+
str_len = flb_sds_len(s);
302+
if (tag_prefix_len > str_len) {
303+
flb_error("[output] tag_prefix shouldn't be longer than local_resource_id");
304+
return;
305+
}
306+
307+
for (i = 0; i < tag_prefix_len; i++) {
308+
c = s[i];
309+
310+
if (c == '.') {
311+
s[i] = '_';
312+
}
313+
}
314+
}
315+
291316
static flb_sds_t get_str_value_from_msgpack_map(msgpack_object_map map,
292317
const char *key, int key_size)
293318
{
@@ -428,9 +453,11 @@ static int process_local_resource_id(struct flb_stackdriver *ctx, char *type)
428453
int len_k8s_container;
429454
int len_k8s_node;
430455
int len_k8s_pod;
456+
int prefix_len;
431457
struct local_resource_id_list *ptr;
432458
struct mk_list *list = NULL;
433459
struct mk_list *head;
460+
flb_sds_t new_local_resource_id;
434461

435462
if (!ctx->local_resource_id) {
436463
flb_plg_error(ctx->ins, "local_resource_is is not assigned");
@@ -441,8 +468,20 @@ static int process_local_resource_id(struct flb_stackdriver *ctx, char *type)
441468
len_k8s_node = sizeof(K8S_NODE) - 1;
442469
len_k8s_pod = sizeof(K8S_POD) - 1;
443470

471+
prefix_len = flb_sds_len(ctx->tag_prefix);
472+
if (flb_sds_casecmp(ctx->tag_prefix, ctx->local_resource_id, prefix_len) != 0) {
473+
flb_plg_error(ctx->ins, "tag_prefix [%s] doesn't match the prefix of"
474+
" local_resource_id [%s]", ctx->tag_prefix,
475+
ctx->local_resource_id);
476+
return -1;
477+
}
478+
479+
new_local_resource_id = flb_sds_create_len(ctx->local_resource_id,
480+
flb_sds_len(ctx->local_resource_id));
481+
replace_prefix_dot(new_local_resource_id, prefix_len);
482+
444483
if (strncmp(type, K8S_CONTAINER, len_k8s_container) == 0) {
445-
list = parse_local_resource_id_to_list(ctx->local_resource_id, K8S_CONTAINER);
484+
list = parse_local_resource_id_to_list(new_local_resource_id, K8S_CONTAINER);
446485
if (!list) {
447486
goto error;
448487
}
@@ -451,11 +490,6 @@ static int process_local_resource_id(struct flb_stackdriver *ctx, char *type)
451490
mk_list_foreach(head, list) {
452491
ptr = mk_list_entry(head, struct local_resource_id_list, _head);
453492
if (first) {
454-
/* check the prefix */
455-
if (flb_sds_len(ptr->val) != flb_sds_len(ctx->tag_prefix) ||
456-
strncmp(ptr->val, ctx->tag_prefix, flb_sds_len(ctx->tag_prefix)) != 0) {
457-
goto error;
458-
}
459493
first = FLB_FALSE;
460494
continue;
461495
}
@@ -488,19 +522,14 @@ static int process_local_resource_id(struct flb_stackdriver *ctx, char *type)
488522
}
489523
}
490524
else if (strncmp(type, K8S_NODE, len_k8s_node) == 0) {
491-
list = parse_local_resource_id_to_list(ctx->local_resource_id, K8S_NODE);
525+
list = parse_local_resource_id_to_list(new_local_resource_id, K8S_NODE);
492526
if (!list) {
493527
goto error;
494528
}
495529

496530
mk_list_foreach(head, list) {
497531
ptr = mk_list_entry(head, struct local_resource_id_list, _head);
498532
if (first) {
499-
/* check the prefix */
500-
if (flb_sds_len(ptr->val) != flb_sds_len(ctx->tag_prefix) ||
501-
strncmp(ptr->val, ctx->tag_prefix, flb_sds_len(ctx->tag_prefix)) != 0) {
502-
goto error;
503-
}
504533
first = FLB_FALSE;
505534
continue;
506535
}
@@ -518,19 +547,14 @@ static int process_local_resource_id(struct flb_stackdriver *ctx, char *type)
518547
}
519548
}
520549
else if (strncmp(type, K8S_POD, len_k8s_pod) == 0) {
521-
list = parse_local_resource_id_to_list(ctx->local_resource_id, K8S_POD);
550+
list = parse_local_resource_id_to_list(new_local_resource_id, K8S_POD);
522551
if (!list) {
523552
goto error;
524553
}
525554

526555
mk_list_foreach(head, list) {
527556
ptr = mk_list_entry(head, struct local_resource_id_list, _head);
528557
if (first) {
529-
/* check the prefix */
530-
if (flb_sds_len(ptr->val) != flb_sds_len(ctx->tag_prefix) ||
531-
strncmp(ptr->val, ctx->tag_prefix, flb_sds_len(ctx->tag_prefix)) != 0) {
532-
goto error;
533-
}
534558
first = FLB_FALSE;
535559
continue;
536560
}
@@ -563,6 +587,7 @@ static int process_local_resource_id(struct flb_stackdriver *ctx, char *type)
563587
flb_slist_destroy(list);
564588
flb_free(list);
565589
}
590+
flb_sds_destroy(new_local_resource_id);
566591

567592
return ret;
568593

@@ -584,6 +609,8 @@ static int process_local_resource_id(struct flb_stackdriver *ctx, char *type)
584609
flb_sds_destroy(ctx->namespace_name);
585610
flb_sds_destroy(ctx->pod_name);
586611
}
612+
613+
flb_sds_destroy(new_local_resource_id);
587614
return -1;
588615
}
589616

tests/runtime/out_stackdriver.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2168,6 +2168,50 @@ void flb_test_resource_k8s_container_custom_tag_prefix()
21682168
flb_destroy(ctx);
21692169
}
21702170

2171+
void flb_test_resource_k8s_container_custom_tag_prefix_with_dot()
2172+
{
2173+
int ret;
2174+
int size = sizeof(K8S_CONTAINER_NO_LOCAL_RESOURCE_ID) - 1;
2175+
flb_ctx_t *ctx;
2176+
int in_ffd;
2177+
int out_ffd;
2178+
2179+
/* Create context, flush every second (some checks omitted here) */
2180+
ctx = flb_create();
2181+
flb_service_set(ctx, "flush", "1", "grace", "1", NULL);
2182+
2183+
/* Lib input mode */
2184+
in_ffd = flb_input(ctx, (char *) "lib", NULL);
2185+
flb_input_set(ctx, in_ffd, "tag", "kube.custom.tag.testnamespace.testpod.testctr", NULL);
2186+
2187+
/* Stackdriver output */
2188+
out_ffd = flb_output(ctx, (char *) "stackdriver", NULL);
2189+
flb_output_set(ctx, out_ffd,
2190+
"match", "kube.custom.tag.*",
2191+
"resource", "k8s_container",
2192+
"google_service_credentials", SERVICE_CREDENTIALS,
2193+
"k8s_cluster_name", "test_cluster_name",
2194+
"k8s_cluster_location", "test_cluster_location",
2195+
"tag_prefix", "kube.custom.tag",
2196+
NULL);
2197+
2198+
/* Enable test mode */
2199+
ret = flb_output_set_test(ctx, out_ffd, "formatter",
2200+
cb_check_k8s_container_resource,
2201+
NULL, NULL);
2202+
2203+
/* Start */
2204+
ret = flb_start(ctx);
2205+
TEST_CHECK(ret == 0);
2206+
2207+
/* Ingest data sample */
2208+
flb_lib_push(ctx, in_ffd, (char *) K8S_CONTAINER_NO_LOCAL_RESOURCE_ID, size);
2209+
2210+
sleep(2);
2211+
flb_stop(ctx);
2212+
flb_destroy(ctx);
2213+
}
2214+
21712215
void flb_test_resource_k8s_node_common()
21722216
{
21732217
int ret;
@@ -3592,6 +3636,7 @@ TEST_LIST = {
35923636
{"resource_k8s_container_no_local_resource_id", flb_test_resource_k8s_container_no_local_resource_id },
35933637
{"resource_k8s_container_multi_tag_value", flb_test_resource_k8s_container_multi_tag_value } ,
35943638
{"resource_k8s_container_custom_tag_prefix", flb_test_resource_k8s_container_custom_tag_prefix },
3639+
{"resource_k8s_container_custom_tag_prefix_with_dot", flb_test_resource_k8s_container_custom_tag_prefix_with_dot },
35953640
{"resource_k8s_node_common", flb_test_resource_k8s_node_common },
35963641
{"resource_k8s_node_no_local_resource_id", flb_test_resource_k8s_node_no_local_resource_id },
35973642
{"resource_k8s_pod_common", flb_test_resource_k8s_pod_common },

0 commit comments

Comments
 (0)