Skip to content

Commit 50947cd

Browse files
authored
core: add conditionals evaluation library. (#9749)
this is a library to evaluate conditionals based on rules, rules are declared as matching elements using record accessor. Signed-off-by: Jorge Niedbalski <[email protected]>
1 parent d77c06d commit 50947cd

File tree

5 files changed

+1641
-0
lines changed

5 files changed

+1641
-0
lines changed

include/fluent-bit/flb_conditionals.h

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2+
3+
/* Fluent Bit
4+
* ==========
5+
* Copyright (C) 2015-2024 The Fluent Bit Authors
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
#ifndef FLB_CONDITIONS_H
21+
#define FLB_CONDITIONS_H
22+
23+
#include <fluent-bit/flb_info.h>
24+
#include <fluent-bit/flb_sds.h>
25+
#include <fluent-bit/flb_regex.h>
26+
#include <fluent-bit/flb_mp.h>
27+
#include <fluent-bit/flb_cfl_record_accessor.h>
28+
#include <monkey/mk_core.h>
29+
#include <fluent-bit/flb_mp_chunk.h>
30+
31+
/* Context types enum */
32+
enum record_context_type {
33+
RECORD_CONTEXT_BODY = 0,
34+
RECORD_CONTEXT_METADATA = 1
35+
};
36+
37+
struct flb_condition;
38+
39+
enum flb_condition_operator {
40+
FLB_COND_OP_AND,
41+
FLB_COND_OP_OR
42+
};
43+
44+
enum flb_rule_operator {
45+
FLB_RULE_OP_EQ,
46+
FLB_RULE_OP_NEQ,
47+
FLB_RULE_OP_GT,
48+
FLB_RULE_OP_LT,
49+
FLB_RULE_OP_REGEX,
50+
FLB_RULE_OP_IN,
51+
FLB_RULE_OP_NOT_IN
52+
};
53+
54+
struct flb_condition_rule {
55+
struct flb_cfl_record_accessor *ra; /* Record accessor for the field */
56+
enum record_context_type context; /* Whether rule applies to body or metadata */
57+
enum flb_rule_operator op;
58+
union {
59+
flb_sds_t str_val;
60+
double num_val;
61+
struct {
62+
flb_sds_t *values;
63+
int count;
64+
} array;
65+
} value;
66+
struct flb_regex *regex;
67+
struct mk_list _head;
68+
};
69+
70+
struct flb_condition {
71+
enum flb_condition_operator op;
72+
struct mk_list rules;
73+
};
74+
75+
/* Core condition functions */
76+
struct flb_condition *flb_condition_create(enum flb_condition_operator op);
77+
78+
int flb_condition_add_rule(struct flb_condition *cond,
79+
const char *field,
80+
enum flb_rule_operator op,
81+
void *value,
82+
int value_count,
83+
enum record_context_type context);
84+
85+
void flb_condition_destroy(struct flb_condition *cond);
86+
87+
/* Evaluation function */
88+
int flb_condition_evaluate(struct flb_condition *cond,
89+
struct flb_mp_chunk_record *record);
90+
91+
#endif /* FLB_CONDITIONS_H */

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ set(src
8888
flb_lock.c
8989
flb_cfl_ra_key.c
9090
flb_cfl_record_accessor.c
91+
flb_conditionals.c
9192
)
9293

9394
# Config format

0 commit comments

Comments
 (0)