-
-
Notifications
You must be signed in to change notification settings - Fork 157
feat: detect and extract json from log-lines #1248
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
6e77c04
feat: detect and extract json from log-lines
6d8afa5
fix: copy `/resources`
d234c1c
refactor: flatten code
f8247b9
doc+test: improve readability and testing
231cc0d
refactor: `check_or_extract`
bf0ecc2
fix: accept from custom sources
bb37551
style: error message
159dd5d
fix: consider single capture groups
186a586
fix: patterns are distinct
a37d368
feat: update log source entry
199c4d4
fix: allow empty
090e491
ci: deepsource
98fe200
refactor: clippy suggestions
0db29f1
fix: don't accept unknown log format
7ff8ed2
test: fix expectation
637f33c
fix: untag custom log sources
4b6209b
Merge remote-tracking branch 'origin/main' into schema-detect
f756639
ensure lowercase
85e13cc
Merge remote-tracking branch 'origin/main' into schema-detect
547c09e
test: fix `test_v5_v6_unknown_log_source`
2364b39
feat: add more known formats
fd65c35
update regex for syslog, add to ignore header
nikhilsinhaparseable 652b593
Merge branch 'main' into schema-detect
nikhilsinhaparseable 2d67c26
Update formats.json
nikhilsinhaparseable 73d9938
Update formats.json
nikhilsinhaparseable 382897d
update error message
nikhilsinhaparseable 571c528
Merge branch 'main' into schema-detect
nikhilsinhaparseable 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,157 @@ | ||
| use std::collections::HashMap; | ||
|
|
||
| use once_cell::sync::Lazy; | ||
| use regex::Regex; | ||
| use serde::Deserialize; | ||
| use serde_json::{Map, Value}; | ||
| use tracing::{error, warn}; | ||
|
|
||
| const FORMATS_JSON: &str = include_str!("../../../resources/formats.json"); | ||
|
|
||
| // Schema definition with pattern matching | ||
| pub static KNOWN_SCHEMA_LIST: Lazy<EventProcessor> = Lazy::new(|| { | ||
| let mut processor = EventProcessor { | ||
| schema_definitions: HashMap::new(), | ||
| }; | ||
|
|
||
| // Register known schemas | ||
| processor.register_schema(); | ||
|
|
||
| processor | ||
| }); | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct SchemaDefinition { | ||
| pattern: Option<Regex>, | ||
| field_mappings: Vec<String>, // Maps field names to regex capture groups | ||
| } | ||
|
|
||
| impl SchemaDefinition { | ||
| pub fn extract(&self, event: &str) -> Option<Map<String, Value>> { | ||
| if let Some(pattern) = &self.pattern { | ||
| if let Some(captures) = pattern.captures(event) { | ||
| let mut extracted_fields = Map::new(); | ||
|
|
||
| // With named capture groups, you can iterate over the field names | ||
| for field_name in self.field_mappings.iter() { | ||
| if let Some(value) = captures.name(field_name) { | ||
| extracted_fields.insert( | ||
| field_name.to_owned(), | ||
| Value::String(value.as_str().to_string()), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| return Some(extracted_fields); | ||
| } | ||
| } | ||
|
|
||
| None | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, Deserialize)] | ||
| struct Format { | ||
| name: String, | ||
| regex: Vec<Pattern>, | ||
| } | ||
| #[derive(Debug, Deserialize)] | ||
| struct Pattern { | ||
| pattern: Option<String>, | ||
| fields: Vec<String>, | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct EventProcessor { | ||
| pub schema_definitions: HashMap<String, SchemaDefinition>, | ||
| } | ||
|
|
||
| impl EventProcessor { | ||
| fn register_schema(&mut self) { | ||
| let json_data: serde_json::Value = serde_json::from_str(FORMATS_JSON).unwrap(); | ||
| let formats: Vec<Format> = | ||
| serde_json::from_value(json_data).expect("Failed to parse formats.json"); | ||
|
|
||
| for format in formats { | ||
| for pattern in &format.regex { | ||
| if let Some(pattern_str) = &pattern.pattern { | ||
| // Compile the regex pattern | ||
| match Regex::new(pattern_str) { | ||
| Ok(exp) => { | ||
| let field_mappings = pattern | ||
| .fields | ||
| .iter() | ||
| .map(|field| field.to_string()) | ||
| .collect(); | ||
|
|
||
| self.schema_definitions.insert( | ||
| format.name.clone(), | ||
| SchemaDefinition { | ||
| pattern: Some(exp), | ||
| field_mappings, | ||
| }, | ||
| ); | ||
| } | ||
| Err(e) => { | ||
| error!("Error compiling regex pattern: {e}; Pattern: {pattern_str}"); | ||
| } | ||
| } | ||
| } else { | ||
| let field_mappings = pattern | ||
| .fields | ||
| .iter() | ||
| .map(|field| field.to_string()) | ||
| .collect(); | ||
|
|
||
| self.schema_definitions.insert( | ||
| format.name.clone(), | ||
| SchemaDefinition { | ||
| pattern: None, | ||
| field_mappings, | ||
| }, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| pub fn extract_from_inline_log( | ||
| mut json: Value, | ||
| log_source: &str, | ||
| extract_log: Option<&str>, | ||
| ) -> Value { | ||
| let Some(schema) = KNOWN_SCHEMA_LIST.schema_definitions.get(log_source) else { | ||
| warn!("Unknown log format: {log_source}"); | ||
| return json; | ||
| }; | ||
|
|
||
| match &mut json { | ||
| Value::Array(list) => { | ||
| for event in list { | ||
| let Value::Object(event) = event else { | ||
| continue; | ||
| }; | ||
| per_event_extraction(event, schema, extract_log) | ||
| } | ||
| } | ||
| Value::Object(event) => per_event_extraction(event, schema, extract_log), | ||
| _ => unreachable!("We don't accept events of the form: {json}"), | ||
| } | ||
|
|
||
| json | ||
| } | ||
|
|
||
| pub fn per_event_extraction( | ||
| obj: &mut Map<String, Value>, | ||
| schema: &SchemaDefinition, | ||
| extract_log: Option<&str>, | ||
| ) { | ||
| if let Some(event) = extract_log | ||
| .and_then(|field| obj.get(field)) | ||
| .and_then(|s| s.as_str()) | ||
| { | ||
| if let Some(additional) = schema.extract(event) { | ||
| obj.extend(additional); | ||
| } | ||
| } | ||
| } | ||
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
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.