-
Notifications
You must be signed in to change notification settings - Fork 2k
multiline: add built-in json parser for JSON multiline objects #12264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lecaros
wants to merge
5
commits into
master
Choose a base branch
from
lecaros-json-ml-builtin-parser
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b624f39
tests: multiline: add tests for json multiline
lecaros f21887b
in_tail: add json builtin parser to list
lecaros fd04c53
multiline: add built-in json parser for ml objects
lecaros a94e2c8
filter_multiline: mention json in multiline.parser help
lecaros 171ea21
tests: multiline: add json invalid boundary fixtures
lecaros File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ | ||
|
|
||
| /* Fluent Bit | ||
| * ========== | ||
| * Copyright (C) 2015-2026 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. | ||
| */ | ||
|
|
||
| #include <fluent-bit/flb_info.h> | ||
| #include <fluent-bit/multiline/flb_ml.h> | ||
| #include <fluent-bit/multiline/flb_ml_rule.h> | ||
| #include <fluent-bit/multiline/flb_ml_parser.h> | ||
|
|
||
| #define rule flb_ml_rule_create | ||
|
|
||
| static void rule_error(struct flb_ml_parser *ml_parser) | ||
| { | ||
| int id; | ||
|
|
||
| id = mk_list_size(&ml_parser->regex_rules); | ||
| flb_error("[multiline: json] rule #%i could not be created", id); | ||
| flb_ml_parser_destroy(ml_parser); | ||
| } | ||
|
|
||
| /* | ||
| * Built-in multiline mode for pretty-printed JSON objects. | ||
| * | ||
| * Tail reads line-by-line, so a formatted JSON object is not valid JSON per | ||
| * line. This parser groups lines from an opening '{' until the next object | ||
| * starts or flush_timeout expires. | ||
| * | ||
| * Do not attach a JSON parser here: per-line JSON parsing fails on partial | ||
| * lines (see ml_append_try_parser_type_text). Use filter_parser on the | ||
| * assembled 'log' field after grouping. | ||
| */ | ||
| struct flb_ml_parser *flb_ml_parser_json(struct flb_config *config, char *key) | ||
| { | ||
| int ret; | ||
| struct flb_ml_parser *mlp; | ||
|
|
||
| mlp = flb_ml_parser_create(config, /* Fluent Bit context */ | ||
| "json", /* name */ | ||
| FLB_ML_REGEX, /* type */ | ||
| NULL, /* match_str */ | ||
| FLB_FALSE, /* negate */ | ||
| FLB_ML_FLUSH_TIMEOUT, /* flush_ms */ | ||
| key, /* key_content */ | ||
| NULL, /* key_group */ | ||
| NULL, /* key_pattern */ | ||
| NULL, /* parser ctx */ | ||
| NULL); /* parser name */ | ||
|
|
||
| if (!mlp) { | ||
| flb_error("[multiline] could not create 'json mode'"); | ||
| return NULL; | ||
| } | ||
|
|
||
| ret = rule(mlp, | ||
| "start_state", | ||
| "/^\\{.*/", | ||
| "cont", NULL); | ||
| if (ret != 0) { | ||
| rule_error(mlp); | ||
| return NULL; | ||
| } | ||
|
|
||
| ret = rule(mlp, | ||
| "cont", | ||
| "/^([^\\S\\r\\n].*|})$/", | ||
|
lecaros marked this conversation as resolved.
|
||
| "cont", NULL); | ||
| if (ret != 0) { | ||
| rule_error(mlp); | ||
| return NULL; | ||
| } | ||
|
|
||
| ret = flb_ml_parser_init(mlp); | ||
| if (ret != 0) { | ||
| flb_error("[multiline: json] error on mapping rules"); | ||
| flb_ml_parser_destroy(mlp); | ||
| return NULL; | ||
| } | ||
|
|
||
| return mlp; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.