2727#include <fluent-bit/flb_gzip.h>
2828#include <fluent-bit/flb_zstd.h>
2929#include <fluent-bit/flb_snappy.h>
30+ #include <fluent-bit/flb_record_accessor.h>
31+ #include <fluent-bit/flb_ra_key.h>
3032
3133#include <monkey/monkey.h>
3234#include <monkey/mk_core.h>
@@ -42,8 +44,8 @@ static inline char hex2nibble(char c)
4244 if ((c >= 0x30 ) && (c <= '9' )) {
4345 return c - 0x30 ;
4446 }
45- // 0x30-0x39 are digits, 0x41-0x46 A-F,
46- // so there is a gap at 0x40
47+
48+ /* 0x30-0x39 are digits, 0x41-0x46 A-F, so there is a gap at 0x40 */
4749 if ((c >= 'A' ) && (c <= 'F' )) {
4850 return (c - 'A' ) + 10 ;
4951 }
@@ -163,70 +165,54 @@ static int send_response(struct http_conn *conn, int http_status, char *message)
163165 return 0 ;
164166}
165167
166- /* implements functionality to get tag from key in record */
167- static flb_sds_t tag_key (struct flb_http * ctx , msgpack_object * map )
168+ static void sanitize_tag (flb_sds_t tag )
168169{
169- size_t map_size = map -> via .map .size ;
170- msgpack_object_kv * kv ;
171- msgpack_object key ;
172- msgpack_object val ;
173- char * key_str = NULL ;
174- char * val_str = NULL ;
175- size_t key_str_size = 0 ;
176- size_t val_str_size = 0 ;
177- int j ;
178- int check = FLB_FALSE ;
179- int found = FLB_FALSE ;
180- flb_sds_t tag ;
170+ size_t i ;
181171
182- kv = map -> via .map .ptr ;
172+ if (!tag ) {
173+ return ;
174+ }
183175
184- for (j = 0 ; j < map_size ; j ++ ) {
185- check = FLB_FALSE ;
186- found = FLB_FALSE ;
187- key = (kv + j )-> key ;
188- if (key .type == MSGPACK_OBJECT_BIN ) {
189- key_str = (char * ) key .via .bin .ptr ;
190- key_str_size = key .via .bin .size ;
191- check = FLB_TRUE ;
192- }
193- if (key .type == MSGPACK_OBJECT_STR ) {
194- key_str = (char * ) key .via .str .ptr ;
195- key_str_size = key .via .str .size ;
196- check = FLB_TRUE ;
176+ for (i = 0 ; i < flb_sds_len (tag ); i ++ ) {
177+ if (!isalnum (tag [i ]) && tag [i ] != '_' && tag [i ] != '.' ) {
178+ tag [i ] = '_' ;
197179 }
180+ }
181+ }
198182
199- if (check == FLB_TRUE ) {
200- if (strncmp (ctx -> tag_key , key_str , key_str_size ) == 0 ) {
201- val = (kv + j )-> val ;
202- if (val .type == MSGPACK_OBJECT_BIN ) {
203- val_str = (char * ) val .via .bin .ptr ;
204- val_str_size = val .via .bin .size ;
205- found = FLB_TRUE ;
206- break ;
207- }
208- if (val .type == MSGPACK_OBJECT_STR ) {
209- val_str = (char * ) val .via .str .ptr ;
210- val_str_size = val .via .str .size ;
211- found = FLB_TRUE ;
212- break ;
213- }
214- }
215- }
183+ /* implements functionality to get tag from key in record */
184+ static flb_sds_t tag_key (struct flb_http * ctx , msgpack_object * map )
185+ {
186+ struct flb_ra_value * ra_val ;
187+ flb_sds_t tag = NULL ;
188+
189+ /* If no record accessor is configured, return NULL */
190+ if (!ctx -> ra_tag_key ) {
191+ return NULL ;
216192 }
217193
218- if (found == FLB_TRUE ) {
219- tag = flb_sds_create_len (val_str , val_str_size );
220- if (!tag ) {
221- flb_errno ();
222- return NULL ;
194+ /* Use record accessor to get the value */
195+ ra_val = flb_ra_get_value_object (ctx -> ra_tag_key , * map );
196+ if (!ra_val ) {
197+ flb_plg_debug (ctx -> ins , "Could not find tag_key %s in record" , ctx -> tag_key );
198+ return NULL ;
199+ }
200+
201+ /* Convert the value to string */
202+ if (ra_val -> type == FLB_RA_STRING ) {
203+ tag = flb_sds_create_len (ra_val -> o .via .str .ptr , ra_val -> o .via .str .size );
204+ if (tag ) {
205+ sanitize_tag (tag );
223206 }
224- return tag ;
207+ }
208+ else {
209+ flb_plg_debug (ctx -> ins , "tag_key %s value is not a string" , ctx -> tag_key );
225210 }
226211
212+ /* Clean up the record accessor value */
213+ flb_ra_key_value_destroy (ra_val );
227214
228- flb_plg_warn (ctx -> ins , "Could not find tag_key %s in record" , ctx -> tag_key );
229- return NULL ;
215+ return tag ;
230216}
231217
232218static int process_pack_record (struct flb_http * ctx , struct flb_time * tm ,
@@ -878,7 +864,6 @@ int http_prot_handle(struct flb_http *ctx, struct http_conn *conn,
878864 struct mk_http_session * session ,
879865 struct mk_http_request * request )
880866{
881- int i ;
882867 int ret ;
883868 int len ;
884869 char * uri ;
@@ -927,12 +912,7 @@ int http_prot_handle(struct flb_http *ctx, struct http_conn *conn,
927912 /* New tag skipping the URI '/' */
928913 flb_sds_cat_safe (& tag , uri + 1 , len - 1 );
929914
930- /* Sanitize, only allow alphanum chars */
931- for (i = 0 ; i < flb_sds_len (tag ); i ++ ) {
932- if (!isalnum (tag [i ]) && tag [i ] != '_' && tag [i ] != '.' ) {
933- tag [i ] = '_' ;
934- }
935- }
915+ sanitize_tag (tag );
936916 }
937917
938918 mk_mem_free (uri );
0 commit comments