Skip to content

Commit

Permalink
core: add conditionals evaluation library. (#9749)
Browse files Browse the repository at this point in the history
this is a library to evaluate conditionals
based on rules, rules are declared as matching elements
using record accessor.

Signed-off-by: Jorge Niedbalski <jnr@metaklass.org>
  • Loading branch information
niedbalski authored Dec 28, 2024
1 parent d77c06d commit 50947cd
Show file tree
Hide file tree
Showing 5 changed files with 1,641 additions and 0 deletions.
91 changes: 91 additions & 0 deletions include/fluent-bit/flb_conditionals.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/* Fluent Bit
* ==========
* Copyright (C) 2015-2024 The Fluent Bit Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef FLB_CONDITIONS_H
#define FLB_CONDITIONS_H

#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_sds.h>
#include <fluent-bit/flb_regex.h>
#include <fluent-bit/flb_mp.h>
#include <fluent-bit/flb_cfl_record_accessor.h>
#include <monkey/mk_core.h>
#include <fluent-bit/flb_mp_chunk.h>

/* Context types enum */
enum record_context_type {
RECORD_CONTEXT_BODY = 0,
RECORD_CONTEXT_METADATA = 1
};

struct flb_condition;

enum flb_condition_operator {
FLB_COND_OP_AND,
FLB_COND_OP_OR
};

enum flb_rule_operator {
FLB_RULE_OP_EQ,
FLB_RULE_OP_NEQ,
FLB_RULE_OP_GT,
FLB_RULE_OP_LT,
FLB_RULE_OP_REGEX,
FLB_RULE_OP_IN,
FLB_RULE_OP_NOT_IN
};

struct flb_condition_rule {
struct flb_cfl_record_accessor *ra; /* Record accessor for the field */
enum record_context_type context; /* Whether rule applies to body or metadata */
enum flb_rule_operator op;
union {
flb_sds_t str_val;
double num_val;
struct {
flb_sds_t *values;
int count;
} array;
} value;
struct flb_regex *regex;
struct mk_list _head;
};

struct flb_condition {
enum flb_condition_operator op;
struct mk_list rules;
};

/* Core condition functions */
struct flb_condition *flb_condition_create(enum flb_condition_operator op);

int flb_condition_add_rule(struct flb_condition *cond,
const char *field,
enum flb_rule_operator op,
void *value,
int value_count,
enum record_context_type context);

void flb_condition_destroy(struct flb_condition *cond);

/* Evaluation function */
int flb_condition_evaluate(struct flb_condition *cond,
struct flb_mp_chunk_record *record);

#endif /* FLB_CONDITIONS_H */
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ set(src
flb_lock.c
flb_cfl_ra_key.c
flb_cfl_record_accessor.c
flb_conditionals.c
)

# Config format
Expand Down
Loading

0 comments on commit 50947cd

Please sign in to comment.