Add Boolean param kind that is always present true/false (ticket 018)
MetricPrimitiveKind gains Boolean, serialized as "boolean". A param declared with kind Boolean skips capture-group validation and, instead of being omitted when message_match_regex doesn't match, is present on every log line as true/false based purely on whether the regex matches (case-insensitively or per the regex's own flags). Params.tsx lets you pick boolean as a kind and disables the now-unused capture group input. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XFUNZwCvz3kcod3352ZR2o
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
e6e5bc8f50
commit
a9855de3ad
+49
-2
@@ -207,7 +207,10 @@ fn declare_param(
|
||||
) -> Result<(), String> {
|
||||
let re = Regex::new(&request.message_match_regex)
|
||||
.map_err(|err| format!("invalid regex: {err}"))?;
|
||||
if !re.capture_names().flatten().any(|name| name == request.capture_group) {
|
||||
// A Boolean-kind param never uses a capture group — its value is just whether the regex matched.
|
||||
if request.kind != MetricPrimitiveKind::Boolean
|
||||
&& !re.capture_names().flatten().any(|name| name == request.capture_group)
|
||||
{
|
||||
return Err(format!(
|
||||
"regex has no capture group named '{}'",
|
||||
request.capture_group
|
||||
@@ -294,6 +297,10 @@ fn parse_metric_primitive(raw: &str, kind: MetricPrimitiveKind) -> Option<Metric
|
||||
}
|
||||
// A Value-role param never reaches this branch (see apply_metric_to_log); reached only for a Label-role param or standalone ParamDeclaration, parsed as a literal millisecond count.
|
||||
MetricPrimitiveKind::Duration => raw.parse::<i64>().ok().map(MetricPrimitive::Duration),
|
||||
// A standalone Boolean param never reaches this branch (see derive_log_params, which derives its
|
||||
// value from whether the regex matched rather than parsing captured text); reached only if a metric
|
||||
// matcher references a Boolean-kind param, where it's parsed as a literal "true"/"false" capture.
|
||||
MetricPrimitiveKind::Boolean => raw.parse::<bool>().ok().map(MetricPrimitive::Boolean),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -616,6 +623,13 @@ fn compare_metric_primitive(
|
||||
.map_err(|_| format!("expected a numeric value, got {raw:?}"))?;
|
||||
(*ms as f64).partial_cmp(&v)
|
||||
}
|
||||
(MetricPrimitive::Boolean(b), FilterValue::Raw(raw)) => {
|
||||
let v: bool = raw
|
||||
.parse()
|
||||
.map_err(|_| format!("expected true or false, got {raw:?}"))?;
|
||||
b.partial_cmp(&v)
|
||||
}
|
||||
(MetricPrimitive::Boolean(_), FilterValue::Number(_)) => None,
|
||||
};
|
||||
Ok(ordering.is_some_and(|ordering| apply_compare_op(ordering, op)))
|
||||
}
|
||||
@@ -707,13 +721,20 @@ fn compiled_param_declarations(
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// A param is omitted (not included as null/empty) when its regex/capture group doesn't match or the captured text fails to parse — see `LogEntryDto::params`.
|
||||
/// A param is omitted (not included as null/empty) when its regex/capture group doesn't match or the
|
||||
/// captured text fails to parse — see `LogEntryDto::params`. The one exception is a `Boolean`-kind param,
|
||||
/// which never uses a capture group and is instead present as `true`/`false` on every line based purely on
|
||||
/// whether `message_match_regex` matches (case-insensitively or per the regex's own flags).
|
||||
fn derive_log_params(
|
||||
text: &str,
|
||||
declarations: &[(String, Regex, String, MetricPrimitiveKind)],
|
||||
) -> BTreeMap<String, MetricPrimitive> {
|
||||
let mut params = BTreeMap::new();
|
||||
for (name, regex, capture_group, kind) in declarations {
|
||||
if *kind == MetricPrimitiveKind::Boolean {
|
||||
params.insert(name.clone(), MetricPrimitive::Boolean(regex.is_match(text)));
|
||||
continue;
|
||||
}
|
||||
let Some(captures) = regex.captures(text) else {
|
||||
continue;
|
||||
};
|
||||
@@ -939,6 +960,13 @@ fn coerce_to_f64(value: &MetricPrimitive) -> f64 {
|
||||
MetricPrimitive::Timestamp(ts) => ts.as_millis() as f64,
|
||||
MetricPrimitive::Duration(ms) => *ms as f64,
|
||||
MetricPrimitive::String(_) => 1.0,
|
||||
MetricPrimitive::Boolean(b) => {
|
||||
if *b {
|
||||
1.0
|
||||
} else {
|
||||
0.0
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1978,6 +2006,25 @@ mod tests {
|
||||
assert!(entries[1].params.get("level").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn boolean_param_declares_without_capture_group_and_is_always_present() {
|
||||
let mut index = BTreeMap::new();
|
||||
let mut req = request("IsError", r"(?i)error", "");
|
||||
req.kind = MetricPrimitiveKind::Boolean;
|
||||
assert!(declare_param(&mut index, req).is_ok());
|
||||
|
||||
ingest_log(&mut index, 0, "an ERROR occurred");
|
||||
ingest_log(&mut index, 1, "everything is fine");
|
||||
|
||||
let EngineResponse::Logs(entries) = handle_query(&index, logs_query(None), 0) else {
|
||||
panic!("expected Logs response");
|
||||
};
|
||||
|
||||
assert_eq!(entries.len(), 2);
|
||||
assert_eq!(entries[0].params.get("IsError"), Some(&MetricPrimitive::Boolean(true)));
|
||||
assert_eq!(entries[1].params.get("IsError"), Some(&MetricPrimitive::Boolean(false)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn timeline_query_applies_filter_before_bucketing() {
|
||||
let mut index = BTreeMap::new();
|
||||
|
||||
Reference in New Issue
Block a user