Skip to content

Commit ee3ff12

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

File tree

2 files changed

+150
-56
lines changed

2 files changed

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

196+
/* Legacy positional-args API -> thin wrapper to params */
95197
struct flb_ml_parser *flb_ml_parser_create(struct flb_config *ctx,
96198
char *name,
97199
int type, char *match_str, int negate,
@@ -102,62 +204,20 @@ struct flb_ml_parser *flb_ml_parser_create(struct flb_config *ctx,
102204
struct flb_parser *parser_ctx,
103205
char *parser_name)
104206
{
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;
207+
struct flb_ml_parser_params p = flb_ml_parser_params_default(name);
208+
209+
/* override with legacy parameters */
210+
p.type = type;
211+
p.match_str = match_str;
212+
p.negate = negate;
213+
p.flush_ms = flush_ms;
214+
p.key_content = key_content;
215+
p.key_group = key_group;
216+
p.key_pattern = key_pattern;
217+
p.parser_ctx = parser_ctx;
218+
p.parser_name = parser_name;
219+
220+
return flb_ml_parser_create_params(ctx, &p);
161221
}
162222

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

0 commit comments

Comments
 (0)