Skip to content

Commit 7c9bf65

Browse files
committed
router: implement context variable resolution
This patch implements context variable resolution in router condition evaluation. Signed-off-by: Eduardo Silva <[email protected]>
1 parent 6b4604c commit 7c9bf65

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

include/fluent-bit/flb_router.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
struct flb_mp_chunk_cobj;
3434
struct flb_log_event_encoder;
3535
struct flb_log_event_decoder;
36+
struct flb_mp_chunk_record;
3637

3738
struct flb_router_chunk_context {
3839
struct flb_mp_chunk_cobj *chunk_cobj;
@@ -103,6 +104,7 @@ struct flb_route_condition {
103104
struct flb_route_output {
104105
flb_sds_t name;
105106
flb_sds_t fallback;
107+
struct flb_output_instance *ins;
106108
struct cfl_list _head;
107109
};
108110

@@ -156,6 +158,8 @@ int flb_router_chunk_context_prepare_logs(struct flb_router_chunk_context *conte
156158
int flb_route_condition_eval(struct flb_event_chunk *chunk,
157159
struct flb_router_chunk_context *context,
158160
struct flb_route *route);
161+
int flb_router_condition_evaluate_record(struct flb_route *route,
162+
struct flb_mp_chunk_record *record);
159163
int flb_condition_eval_logs(struct flb_event_chunk *chunk,
160164
struct flb_router_chunk_context *context,
161165
struct flb_route *route);

src/flb_router_condition.c

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,25 @@ static inline struct cfl_variant *get_body_variant(struct flb_mp_chunk_record *r
5050
}
5151

5252
static struct cfl_variant *get_otel_container_variant(struct flb_mp_chunk_record *record,
53-
const char *key)
53+
const char *key,
54+
int use_group_attributes)
5455
{
55-
struct cfl_variant *body;
56+
struct cfl_variant *source;
5657
struct cfl_variant *container;
5758

58-
body = get_body_variant(record);
59-
if (!body || body->type != CFL_VARIANT_KVLIST) {
59+
/* For OTLP, resource/scope attributes are in group_attributes, not body */
60+
if (use_group_attributes && record->cobj_group_attributes && record->cobj_group_attributes->variant) {
61+
source = record->cobj_group_attributes->variant;
62+
}
63+
else {
64+
source = get_body_variant(record);
65+
}
66+
67+
if (!source || source->type != CFL_VARIANT_KVLIST) {
6068
return NULL;
6169
}
6270

63-
container = cfl_kvlist_fetch(body->data.as_kvlist, key);
71+
container = cfl_kvlist_fetch(source->data.as_kvlist, key);
6472
if (!container || container->type != CFL_VARIANT_KVLIST) {
6573
return NULL;
6674
}
@@ -84,7 +92,8 @@ static struct cfl_variant *get_otel_attributes_variant(struct flb_mp_chunk_recor
8492
return NULL;
8593
}
8694

87-
container = get_otel_container_variant(record, container_key);
95+
/* For OTLP resource/scope attributes, look in group_attributes first */
96+
container = get_otel_container_variant(record, container_key, 1);
8897
if (!container) {
8998
return NULL;
9099
}
@@ -101,7 +110,8 @@ static struct cfl_variant *get_otel_scope_metadata_variant(struct flb_mp_chunk_r
101110
{
102111
struct cfl_variant *scope;
103112

104-
scope = get_otel_container_variant(record, "scope");
113+
/* For OTLP scope metadata, also check group_attributes first */
114+
scope = get_otel_container_variant(record, "scope", 1);
105115
if (!scope || scope->type != CFL_VARIANT_KVLIST) {
106116
return NULL;
107117
}
@@ -400,6 +410,31 @@ struct flb_condition *flb_router_route_get_condition(struct flb_route *route)
400410
return route_condition_get_compiled(route->condition);
401411
}
402412

413+
int flb_router_condition_evaluate_record(struct flb_route *route,
414+
struct flb_mp_chunk_record *record)
415+
{
416+
struct flb_condition *compiled;
417+
418+
if (!route || !record) {
419+
return FLB_FALSE;
420+
}
421+
422+
if (!route->condition) {
423+
return FLB_TRUE;
424+
}
425+
426+
compiled = flb_router_route_get_condition(route);
427+
if (!compiled) {
428+
if (route->condition->is_default) {
429+
return FLB_TRUE;
430+
}
431+
432+
return FLB_FALSE;
433+
}
434+
435+
return flb_condition_evaluate_ex(compiled, record, route_logs_get_variant);
436+
}
437+
403438
static int parse_rule_operator(const flb_sds_t op_str,
404439
enum flb_rule_operator *out)
405440
{

src/flb_router_config.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,6 +1396,8 @@ int flb_router_apply_config(struct flb_config *config)
13961396
continue;
13971397
}
13981398

1399+
route_output->ins = output_ins;
1400+
13991401
if (input_has_direct_route(input_ins, output_ins)) {
14001402
continue;
14011403
}

0 commit comments

Comments
 (0)