Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bd-log-matcher/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ version = "1.0.0"
doctest = false

[dependencies]
ahash.workspace = true
anyhow.workspace = true
bd-log-primitives.path = "../bd-log-primitives"
bd-proto.path = "../bd-proto"
Expand Down
92 changes: 91 additions & 1 deletion bd-log-matcher/src/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ use base_log_matcher::tag_match::Value_match::{
DoubleValueMatch,
IntValueMatch,
IsSetMatch,
JsonValueMatch,
SemVerValueMatch,
StringValueMatch,
};
use bd_log_primitives::tiny_set::TinyMap;
use bd_log_primitives::{FieldsRef, LogLevel, LogMessage};
use bd_log_primitives::{DataValue, FieldsRef, LogLevel, LogMessage};
use bd_proto::protos::config::v1::config::log_matcher::base_log_matcher::StringMatchType;
use bd_proto::protos::config::v1::config::log_matcher::{
BaseLogMatcher as LegacyBaseLogMatcher,
Expand All @@ -39,6 +40,10 @@ use bd_proto::protos::log_matcher::log_matcher;
use bd_proto::protos::logging::payload::LogType;
use bd_proto::protos::state::scope::StateScope;
use bd_proto::protos::value_matcher::value_matcher::Operator;
use bd_proto::protos::value_matcher::value_matcher::json_path_value_match::{
KeyOrIndex,
key_or_index,
};
use bd_state::Scope;
use log_matcher::LogMatcher;
use log_matcher::log_matcher::{BaseLogMatcher, Matcher, base_log_matcher};
Expand Down Expand Up @@ -162,6 +167,14 @@ impl Tree {
.get(message, fields, state)
.is_some_and(|input| criteria.evaluate(input.as_ref())),
Leaf::IsSetValue(input) => input.get(message, fields, state).is_some(),
Leaf::JsonPathValue {
field_key,
path,
matcher,
} => fields
.field(field_key)
.and_then(|value| resolve_json_path(value, path))
.is_some_and(|input| matcher.evaluate(input.as_ref(), extracted_fields)),
Leaf::Any => true,
},
Self::Or(or_matchers) => or_matchers.iter().any(|matcher| {
Expand Down Expand Up @@ -259,10 +272,24 @@ pub enum Leaf {
/// Whether a given tag is set or not.
IsSetValue(InputType),

/// Uses a destructured JSON path to match against a string value within a Map structure stored in
/// a tag.
JsonPathValue {
field_key: String,
path: Vec<JsonPathToken>,
matcher: StringMatch,
},

/// Always true.
Any,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum JsonPathToken {
Key(String),
Index(i32),
}

impl Leaf {
fn new_legacy(log_matcher: &LegacyBaseLogMatcher) -> Result<Self> {
fn map_string_value(value: &str, match_type: StringMatchType) -> Result<StringMatch> {
Expand Down Expand Up @@ -436,8 +463,71 @@ impl Leaf {
version::VersionMatch::from_proto(sem_ver_value_match)?,
),
IsSetMatch(_) => Self::IsSetValue(InputType::Field(tag_match.tag_key.clone())),
JsonValueMatch(json_value_match) => {
let path = json_value_match
.key_or_index
.iter()
.map(parse_json_path)
.collect::<Result<Vec<_>>>()?;
Self::JsonPathValue {
field_key: tag_match.tag_key.clone(),
path,
matcher: StringMatch::new(
json_value_match.operator,
ValueOrSavedFieldId::Value(json_value_match.match_value.clone()),
)?,
}
},
},
},
)
}
}

fn parse_json_path(key_or_index: &KeyOrIndex) -> Result<JsonPathToken> {
match key_or_index
.key_or_index
.as_ref()
.ok_or_else(|| anyhow!("missing json path key or index"))?
{
key_or_index::Key_or_index::Key(key) => Ok(JsonPathToken::Key(key.clone())),
key_or_index::Key_or_index::Index(index) => Ok(JsonPathToken::Index(*index)),
}
}

fn resolve_json_path<'a>(value: &'a DataValue, path: &[JsonPathToken]) -> Option<Cow<'a, str>> {
let mut current = value;
for token in path {
match token {
JsonPathToken::Key(key) => {
let DataValue::Map(map_data) = current else {
return None;
};
current = map_data.entries().get(key)?;
},
JsonPathToken::Index(index) => {
let DataValue::Array(array_data) = current else {
return None;
};
let items = array_data.items();
let len = i32::try_from(items.len()).ok()?;
let index = if *index < 0 { len + *index } else { *index };
let index: usize = index.try_into().ok()?;
current = items.get(index)?;
},
}
}

match current {
DataValue::String(value) => Some(Cow::Borrowed(value.as_str())),
DataValue::SharedString(value) => Some(Cow::Borrowed(value.as_ref())),
DataValue::StaticString(value) => Some(Cow::Borrowed(value)),
DataValue::Bytes(_)
| DataValue::Boolean(_)
| DataValue::U64(_)
| DataValue::I64(_)
| DataValue::Double(_)
| DataValue::Map(_)
| DataValue::Array(_) => None,
}
}
Loading
Loading