Skip to content
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

fix(datadog grok): enable the DOTALL mode by default #1022

Merged
merged 7 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(datadog grok): support multiline logs
  • Loading branch information
vladimir-dd committed Sep 7, 2024
commit d0199410a2a433a536e1f91fd4ecad03e40a131f
15 changes: 7 additions & 8 deletions src/datadog/grok/parse_grok.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn apply_grok_rule(source: &str, grok_rule: &GrokRule) -> Result<Value, Error> {
if let Some(ref mut v) = value {
value = match apply_filter(v, filter) {
Ok(Value::Null) => None,
Ok(v ) if v.is_object() => Some(parse_keys_as_path(v)),
Ok(v) if v.is_object() => Some(parse_keys_as_path(v)),
Ok(v) => Some(v),
Err(error) => {
warn!(message = "Error applying filter", field = %field, filter = %filter, %error);
Expand Down Expand Up @@ -287,7 +287,7 @@ mod tests {
parse_grok_rules(&["%{unknown}".to_string()], BTreeMap::new())
.unwrap_err()
.to_string(),
r#"failed to parse grok expression '\A%{unknown}\z': The given pattern definition name "unknown" could not be found in the definition map"#
r#"failed to parse grok expression '(?m)\A%{unknown}\z': The given pattern definition name "unknown" could not be found in the definition map"#
);
}

Expand Down Expand Up @@ -657,7 +657,7 @@ mod tests {
Ok(Value::Array(vec!["1".into(), "2".into()])),
),
(
r#"(?m)%{data:field:array("[]","\\n")}"#,
r#"%{data:field:array("[]","\\n")}"#,
"[1\n2]",
Ok(Value::Array(vec!["1".into(), "2".into()])),
),
Expand Down Expand Up @@ -1021,23 +1021,22 @@ mod tests {
#[test]
fn parses_with_new_lines() {
test_full_grok(vec![
// multi-line mode is enabled by default
(
"(?m)%{data:field}",
"%{data:field}",
"a\nb",
Ok(Value::from(btreemap! {
"field" => "a\nb"
})),
),
(
"(?m)%{data:line1}\n%{data:line2}",
"%{data:line1}\n%{data:line2}",
"a\nb",
Ok(Value::from(btreemap! {
"line1" => "a",
"line2" => "b"
})),
),
// no DOTALL mode by default
("%{data:field}", "a\nb", Err(Error::NoMatch)),
// (?s) is not supported by the underlying regex engine(onig) - it uses (?m) instead, so we convert it silently
(
"(?s)%{data:field}",
Expand Down Expand Up @@ -1106,7 +1105,7 @@ mod tests {
#[test]
fn supports_xml_filter() {
test_grok_pattern(vec![(
"(?s)%{data:field:xml}", // (?s) enables DOTALL mode to include newlines
"%{data:field:xml}",
r#"<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
Expand Down
2 changes: 1 addition & 1 deletion src/datadog/grok/parse_grok_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ fn parse_pattern(
parse_grok_rule(pattern, context)?;
let mut pattern = String::new();
// \A, \z - parses from the beginning to the end of string, not line(until \n)
pattern.push_str(r"\A");
pattern.push_str(r"(?m)\A"); // (?m) enables multiline mode
pattern.push_str(&context.regex);
pattern.push_str(r"\z");

Expand Down
Loading