Skip to content

Commit 439b5d4

Browse files
committed
conditionals: add context support for routing conditions
This patch extends the conditionals interface to support context variables for conditional routing. Signed-off-by: Eduardo Silva <[email protected]>
1 parent 2baf131 commit 439b5d4

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

include/fluent-bit/flb_conditionals.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,20 @@
2828
#include <monkey/mk_core.h>
2929
#include <fluent-bit/flb_mp_chunk.h>
3030

31+
struct flb_condition_rule;
32+
33+
typedef struct cfl_variant *(*flb_condition_get_variant_fn)(struct flb_condition_rule *rule,
34+
void *ctx);
35+
3136
/* Context types enum */
3237
enum record_context_type {
33-
RECORD_CONTEXT_BODY = 0,
34-
RECORD_CONTEXT_METADATA = 1
38+
RECORD_CONTEXT_BODY = 0,
39+
RECORD_CONTEXT_METADATA = 1,
40+
RECORD_CONTEXT_GROUP_METADATA,
41+
RECORD_CONTEXT_GROUP_ATTRIBUTES,
42+
RECORD_CONTEXT_OTEL_RESOURCE_ATTRIBUTES,
43+
RECORD_CONTEXT_OTEL_SCOPE_ATTRIBUTES,
44+
RECORD_CONTEXT_OTEL_SCOPE_METADATA
3545
};
3646

3747
struct flb_condition;
@@ -88,6 +98,9 @@ int flb_condition_add_rule(struct flb_condition *cond,
8898
void flb_condition_destroy(struct flb_condition *cond);
8999

90100
/* Evaluation function */
101+
int flb_condition_evaluate_ex(struct flb_condition *cond,
102+
void *ctx,
103+
flb_condition_get_variant_fn get_variant);
91104
int flb_condition_evaluate(struct flb_condition *cond,
92105
struct flb_mp_chunk_record *record);
93106

src/flb_conditionals.c

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,16 @@
2525
#include <fluent-bit/flb_mp.h>
2626
#include <fluent-bit/flb_conditionals.h>
2727

28-
/* Function to get the record variant based on context */
29-
static inline struct cfl_variant *get_record_variant(struct flb_mp_chunk_record *record,
30-
enum record_context_type context_type)
28+
static struct cfl_variant *default_get_record_variant(struct flb_condition_rule *rule,
29+
void *ctx)
3130
{
32-
if (!record) {
31+
struct flb_mp_chunk_record *record = (struct flb_mp_chunk_record *) ctx;
32+
33+
if (!record || !rule) {
3334
return NULL;
3435
}
3536

36-
switch (context_type) {
37+
switch (rule->context) {
3738
case RECORD_CONTEXT_METADATA:
3839
if (record->cobj_metadata) {
3940
return record->cobj_metadata->variant;
@@ -45,6 +46,9 @@ static inline struct cfl_variant *get_record_variant(struct flb_mp_chunk_record
4546
return record->cobj_record->variant;
4647
}
4748
break;
49+
50+
default:
51+
break;
4852
}
4953

5054
return NULL;
@@ -356,8 +360,9 @@ static int evaluate_rule(struct flb_condition_rule *rule,
356360
return result;
357361
}
358362

359-
int flb_condition_evaluate(struct flb_condition *cond,
360-
struct flb_mp_chunk_record *record)
363+
int flb_condition_evaluate_ex(struct flb_condition *cond,
364+
void *ctx,
365+
flb_condition_get_variant_fn get_variant)
361366
{
362367
struct mk_list *head;
363368
struct flb_condition_rule *rule;
@@ -366,11 +371,16 @@ int flb_condition_evaluate(struct flb_condition *cond,
366371
int any_rule_evaluated = FLB_FALSE;
367372
int any_rule_matched = FLB_FALSE;
368373

369-
if (!cond || !record) {
370-
flb_trace("[condition] NULL condition or record, returning TRUE");
374+
if (!cond) {
375+
flb_trace("[condition] NULL condition, returning TRUE");
371376
return FLB_TRUE;
372377
}
373378

379+
if (!get_variant) {
380+
flb_trace("[condition] missing variant provider, returning FALSE");
381+
return FLB_FALSE;
382+
}
383+
374384
flb_trace("[condition] evaluating condition with %d rules", mk_list_size(&cond->rules));
375385

376386
if (mk_list_size(&cond->rules) == 0) {
@@ -382,8 +392,7 @@ int flb_condition_evaluate(struct flb_condition *cond,
382392
rule = mk_list_entry(head, struct flb_condition_rule, _head);
383393
flb_trace("[condition] processing rule with op=%d", rule->op);
384394

385-
/* Get the variant for this rule's context */
386-
record_variant = get_record_variant(record, rule->context);
395+
record_variant = get_variant(rule, ctx);
387396
any_rule_evaluated = FLB_TRUE;
388397
if (!record_variant) {
389398
flb_trace("[condition] no record variant found for context %d", rule->context);
@@ -427,3 +436,13 @@ int flb_condition_evaluate(struct flb_condition *cond,
427436
flb_trace("[condition] final evaluation result: TRUE");
428437
return FLB_TRUE;
429438
}
439+
440+
int flb_condition_evaluate(struct flb_condition *cond,
441+
struct flb_mp_chunk_record *record)
442+
{
443+
if (!cond || !record) {
444+
return FLB_TRUE;
445+
}
446+
447+
return flb_condition_evaluate_ex(cond, record, default_get_record_variant);
448+
}

0 commit comments

Comments
 (0)