@@ -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+
291316static 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
0 commit comments