Skip to content

Commit 18dc299

Browse files
committed
ml_parser: Implement default parameter based initializer
Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent 3575cca commit 18dc299

File tree

2 files changed

+146
-56
lines changed

2 files changed

+146
-56
lines changed

include/fluent-bit/multiline/flb_ml_parser.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,40 @@
2323
#include <fluent-bit/flb_info.h>
2424
#include <fluent-bit/flb_parser.h>
2525

26+
/* fwd decl */
27+
struct flb_config;
28+
29+
/*
30+
* Versioned parameter bag for creating multiline parsers.
31+
* - Call flb_ml_parser_params_default(name) to obtain defaults,
32+
* then tweak only the fields you need before passing to v2.
33+
*/
34+
struct flb_ml_parser_params {
35+
uint16_t size; /* sizeof(struct flb_ml_parser_params) */
36+
37+
/* creation parameters (mirror of old positional args) */
38+
char *name;
39+
int type; /* FLB_ML_REGEX / FLB_ML_ENDSWITH / FLB_ML_EQ */
40+
char *match_str; /* used for ENDSWITH/EQ; NULL for REGEX */
41+
int negate; /* 0/1 */
42+
int flush_ms; /* default: FLB_ML_FLUSH_TIMEOUT */
43+
char *key_content;
44+
char *key_group;
45+
char *key_pattern;
46+
struct flb_parser *parser_ctx; /* immediate */
47+
char *parser_name; /* delayed init */
48+
49+
/* for future toggles */
50+
uint32_t flags;
51+
};
52+
53+
/* Fill sane defaults (version/size/type=REGEX/flush_ms=FLB_ML_FLUSH_TIMEOUT) */
54+
struct flb_ml_parser_params flb_ml_parser_params_default(const char *name);
55+
56+
/* New initializer with params */
57+
struct flb_ml_parser *flb_ml_parser_create_params(struct flb_config *ctx,
58+
const struct flb_ml_parser_params *p);
59+
2660
int flb_ml_parser_init(struct flb_ml_parser *ml_parser);
2761

2862
int flb_ml_parser_builtin_create(struct flb_config *config);

src/multiline/flb_ml_parser.c

Lines changed: 112 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,107 @@
2020
#include <fluent-bit/flb_info.h>
2121
#include <fluent-bit/flb_log.h>
2222
#include <fluent-bit/multiline/flb_ml.h>
23+
#include <fluent-bit/multiline/flb_ml_parser.h>
2324
#include <fluent-bit/multiline/flb_ml_rule.h>
2425
#include <fluent-bit/multiline/flb_ml_mode.h>
2526
#include <fluent-bit/multiline/flb_ml_group.h>
2627

28+
/* ---------------- params + defaults ---------------- */
29+
struct flb_ml_parser_params flb_ml_parser_params_default(const char *name)
30+
{
31+
struct flb_ml_parser_params p;
32+
memset(&p, 0, sizeof(p));
33+
34+
p.size = sizeof(p);
35+
p.name = (char *) name;
36+
p.type = FLB_ML_REGEX; /* sane default */
37+
p.negate = 0;
38+
p.flush_ms = FLB_ML_FLUSH_TIMEOUT; /* header constant */
39+
/* other pointers remain NULL by default */
40+
return p;
41+
}
42+
43+
/* New canonical creator that mirrors old behavior using params */
44+
struct flb_ml_parser *flb_ml_parser_create_params(struct flb_config *ctx,
45+
const struct flb_ml_parser_params *p)
46+
{
47+
struct flb_ml_parser *ml_parser;
48+
49+
if (!ctx || !p || p->size < sizeof(*p) || !p->name) {
50+
return NULL;
51+
}
52+
53+
ml_parser = flb_calloc(1, sizeof(struct flb_ml_parser));
54+
if (!ml_parser) {
55+
flb_errno();
56+
return NULL;
57+
}
58+
59+
/* name/type */
60+
ml_parser->name = flb_sds_create(p->name);
61+
ml_parser->type = p->type;
62+
if (!ml_parser->name) {
63+
flb_free(ml_parser);
64+
return NULL;
65+
}
66+
67+
/* ENDSWITH/EQ optimization string */
68+
if (p->match_str) {
69+
ml_parser->match_str = flb_sds_create(p->match_str);
70+
if (!ml_parser->match_str) {
71+
flb_sds_destroy(ml_parser->name);
72+
flb_free(ml_parser);
73+
return NULL;
74+
}
75+
}
76+
77+
/* sub-parser (immediate / delayed) */
78+
ml_parser->parser = p->parser_ctx;
79+
if (p->parser_name) {
80+
ml_parser->parser_name = flb_sds_create(p->parser_name);
81+
if (!ml_parser->parser_name) {
82+
flb_ml_parser_destroy(ml_parser);
83+
return NULL;
84+
}
85+
}
86+
87+
/* basic props */
88+
ml_parser->negate = p->negate;
89+
ml_parser->flush_ms = (p->flush_ms > 0) ? p->flush_ms : FLB_ML_FLUSH_TIMEOUT;
90+
91+
/* prepare rules list & link into registry */
92+
mk_list_init(&ml_parser->regex_rules);
93+
mk_list_add(&ml_parser->_head, &ctx->multiline_parsers);
94+
95+
/* optional keys */
96+
if (p->key_content) {
97+
ml_parser->key_content = flb_sds_create(p->key_content);
98+
if (!ml_parser->key_content) {
99+
flb_ml_parser_destroy(ml_parser);
100+
return NULL;
101+
}
102+
}
103+
if (p->key_group) {
104+
ml_parser->key_group = flb_sds_create(p->key_group);
105+
if (!ml_parser->key_group) {
106+
flb_ml_parser_destroy(ml_parser);
107+
return NULL;
108+
}
109+
}
110+
if (p->key_pattern) {
111+
ml_parser->key_pattern = flb_sds_create(p->key_pattern);
112+
if (!ml_parser->key_pattern) {
113+
flb_ml_parser_destroy(ml_parser);
114+
return NULL;
115+
}
116+
}
117+
118+
/* keep back-pointer to config for later rule init */
119+
ml_parser->config = ctx;
120+
121+
return ml_parser;
122+
}
123+
27124
int flb_ml_parser_init(struct flb_ml_parser *ml_parser)
28125
{
29126
int ret;
@@ -92,6 +189,7 @@ int flb_ml_parser_builtin_create(struct flb_config *config)
92189
return ret;
93190
}
94191

192+
/* Legacy positional-args API -> thin wrapper to params */
95193
struct flb_ml_parser *flb_ml_parser_create(struct flb_config *ctx,
96194
char *name,
97195
int type, char *match_str, int negate,
@@ -102,62 +200,20 @@ struct flb_ml_parser *flb_ml_parser_create(struct flb_config *ctx,
102200
struct flb_parser *parser_ctx,
103201
char *parser_name)
104202
{
105-
struct flb_ml_parser *ml_parser;
106-
107-
ml_parser = flb_calloc(1, sizeof(struct flb_ml_parser));
108-
if (!ml_parser) {
109-
flb_errno();
110-
return NULL;
111-
}
112-
ml_parser->name = flb_sds_create(name);
113-
ml_parser->type = type;
114-
115-
if (match_str) {
116-
ml_parser->match_str = flb_sds_create(match_str);
117-
if (!ml_parser->match_str) {
118-
if (ml_parser->name) {
119-
flb_sds_destroy(ml_parser->name);
120-
}
121-
flb_free(ml_parser);
122-
return NULL;
123-
}
124-
}
125-
126-
ml_parser->parser = parser_ctx;
127-
128-
if (parser_name) {
129-
ml_parser->parser_name = flb_sds_create(parser_name);
130-
}
131-
ml_parser->negate = negate;
132-
ml_parser->flush_ms = flush_ms;
133-
mk_list_init(&ml_parser->regex_rules);
134-
mk_list_add(&ml_parser->_head, &ctx->multiline_parsers);
135-
136-
if (key_content) {
137-
ml_parser->key_content = flb_sds_create(key_content);
138-
if (!ml_parser->key_content) {
139-
flb_ml_parser_destroy(ml_parser);
140-
return NULL;
141-
}
142-
}
143-
144-
if (key_group) {
145-
ml_parser->key_group = flb_sds_create(key_group);
146-
if (!ml_parser->key_group) {
147-
flb_ml_parser_destroy(ml_parser);
148-
return NULL;
149-
}
150-
}
151-
152-
if (key_pattern) {
153-
ml_parser->key_pattern = flb_sds_create(key_pattern);
154-
if (!ml_parser->key_pattern) {
155-
flb_ml_parser_destroy(ml_parser);
156-
return NULL;
157-
}
158-
}
159-
160-
return ml_parser;
203+
struct flb_ml_parser_params p = flb_ml_parser_params_default(name);
204+
205+
/* override with legacy parameters */
206+
p.type = type;
207+
p.match_str = match_str;
208+
p.negate = negate;
209+
p.flush_ms = flush_ms > 0 ? flush_ms : FLB_ML_FLUSH_TIMEOUT;
210+
p.key_content = key_content;
211+
p.key_group = key_group;
212+
p.key_pattern = key_pattern;
213+
p.parser_ctx = parser_ctx;
214+
p.parser_name = parser_name;
215+
216+
return flb_ml_parser_create_params(ctx, &p);
161217
}
162218

163219
struct flb_ml_parser *flb_ml_parser_get(struct flb_config *ctx, char *name)

0 commit comments

Comments
 (0)