2424#include <fluent-bit/flb_log_event_encoder.h>
2525#include <fluent-bit/flb_log_event_decoder.h>
2626#include <fluent-bit/flb_mp_chunk.h>
27+ #include <cfl/cfl_kvlist.h>
2728
2829#define FLB_ROUTE_CONDITION_COMPILED_SUCCESS 1
2930#define FLB_ROUTE_CONDITION_COMPILED_FAILURE -1
3031
3132static struct flb_condition * route_condition_get_compiled (struct flb_route_condition * condition );
3233
34+ static inline struct cfl_variant * get_object_variant (struct cfl_object * object )
35+ {
36+ if (!object ) {
37+ return NULL ;
38+ }
39+
40+ return object -> variant ;
41+ }
42+
43+ static inline struct cfl_variant * get_body_variant (struct flb_mp_chunk_record * record )
44+ {
45+ if (!record || !record -> cobj_record ) {
46+ return NULL ;
47+ }
48+
49+ return record -> cobj_record -> variant ;
50+ }
51+
52+ static struct cfl_variant * get_otel_container_variant (struct flb_mp_chunk_record * record ,
53+ const char * key )
54+ {
55+ struct cfl_variant * body ;
56+ struct cfl_variant * container ;
57+
58+ body = get_body_variant (record );
59+ if (!body || body -> type != CFL_VARIANT_KVLIST ) {
60+ return NULL ;
61+ }
62+
63+ container = cfl_kvlist_fetch (body -> data .as_kvlist , key );
64+ if (!container || container -> type != CFL_VARIANT_KVLIST ) {
65+ return NULL ;
66+ }
67+
68+ return container ;
69+ }
70+
71+ static struct cfl_variant * get_otel_attributes_variant (struct flb_mp_chunk_record * record ,
72+ enum record_context_type context_type )
73+ {
74+ struct cfl_variant * container ;
75+ const char * container_key = NULL ;
76+
77+ if (context_type == RECORD_CONTEXT_OTEL_RESOURCE_ATTRIBUTES ) {
78+ container_key = "resource" ;
79+ }
80+ else if (context_type == RECORD_CONTEXT_OTEL_SCOPE_ATTRIBUTES ) {
81+ container_key = "scope" ;
82+ }
83+ else {
84+ return NULL ;
85+ }
86+
87+ container = get_otel_container_variant (record , container_key );
88+ if (!container ) {
89+ return NULL ;
90+ }
91+
92+ container = cfl_kvlist_fetch (container -> data .as_kvlist , "attributes" );
93+ if (!container || container -> type != CFL_VARIANT_KVLIST ) {
94+ return NULL ;
95+ }
96+
97+ return container ;
98+ }
99+
100+ static struct cfl_variant * get_otel_scope_metadata_variant (struct flb_mp_chunk_record * record )
101+ {
102+ struct cfl_variant * scope ;
103+
104+ scope = get_otel_container_variant (record , "scope" );
105+ if (!scope || scope -> type != CFL_VARIANT_KVLIST ) {
106+ return NULL ;
107+ }
108+
109+ return scope ;
110+ }
111+
112+ static struct cfl_variant * route_logs_get_variant (struct flb_condition_rule * rule ,
113+ void * ctx )
114+ {
115+ struct flb_mp_chunk_record * record = (struct flb_mp_chunk_record * ) ctx ;
116+
117+ if (!rule || !record ) {
118+ return NULL ;
119+ }
120+
121+ switch (rule -> context ) {
122+ case RECORD_CONTEXT_METADATA :
123+ return get_object_variant (record -> cobj_metadata );
124+ case RECORD_CONTEXT_BODY :
125+ return get_body_variant (record );
126+ case RECORD_CONTEXT_GROUP_METADATA :
127+ return get_object_variant (record -> cobj_group_metadata );
128+ case RECORD_CONTEXT_GROUP_ATTRIBUTES :
129+ return get_object_variant (record -> cobj_group_attributes );
130+ case RECORD_CONTEXT_OTEL_RESOURCE_ATTRIBUTES :
131+ case RECORD_CONTEXT_OTEL_SCOPE_ATTRIBUTES :
132+ return get_otel_attributes_variant (record , rule -> context );
133+ case RECORD_CONTEXT_OTEL_SCOPE_METADATA :
134+ return get_otel_scope_metadata_variant (record );
135+ default :
136+ break ;
137+ }
138+
139+ return NULL ;
140+ }
141+
33142int flb_router_chunk_context_init (struct flb_router_chunk_context * context )
34143{
35144 if (!context ) {
@@ -185,7 +294,7 @@ int flb_condition_eval_logs(struct flb_event_chunk *chunk,
185294 cfl_list_foreach (head , & context -> chunk_cobj -> records ) {
186295 record = cfl_list_entry (head , struct flb_mp_chunk_record , _head );
187296
188- if (flb_condition_evaluate (compiled , record ) == FLB_TRUE ) {
297+ if (flb_condition_evaluate_ex (compiled , record , route_logs_get_variant ) == FLB_TRUE ) {
189298 result = FLB_TRUE ;
190299 break ;
191300 }
@@ -391,7 +500,7 @@ static struct flb_condition *route_condition_compile(struct flb_route_condition
391500 return NULL ;
392501 }
393502 ret = flb_condition_add_rule (compiled , rule -> field , op ,
394- rule -> value , 1 , RECORD_CONTEXT_BODY );
503+ rule -> value , 1 , rule -> context );
395504 break ;
396505 case FLB_RULE_OP_GT :
397506 case FLB_RULE_OP_LT :
@@ -406,7 +515,7 @@ static struct flb_condition *route_condition_compile(struct flb_route_condition
406515 return NULL ;
407516 }
408517 ret = flb_condition_add_rule (compiled , rule -> field , op ,
409- & numeric_value , 1 , RECORD_CONTEXT_BODY );
518+ & numeric_value , 1 , rule -> context );
410519 break ;
411520 case FLB_RULE_OP_IN :
412521 case FLB_RULE_OP_NOT_IN :
@@ -417,7 +526,7 @@ static struct flb_condition *route_condition_compile(struct flb_route_condition
417526 ret = flb_condition_add_rule (compiled , rule -> field , op ,
418527 rule -> values ,
419528 (int ) rule -> values_count ,
420- RECORD_CONTEXT_BODY );
529+ rule -> context );
421530 break ;
422531 default :
423532 flb_condition_destroy (compiled );
0 commit comments