Merge ticket/018-boolean-param-kind and ticket/019-docs-isError-use-case

This commit is contained in:
Hugh Kaznowski
2026-09-05 19:39:32 +01:00
7 changed files with 86 additions and 8 deletions
+5
View File
@@ -33,6 +33,11 @@ pub enum MetricPrimitiveKind {
Timestamp, Timestamp,
/// On a Value-role param this is derived as elapsed time since the previous event (not parsed from text); otherwise it's a literal i64 millisecond count. /// On a Value-role param this is derived as elapsed time since the previous event (not parsed from text); otherwise it's a literal i64 millisecond count.
Duration, Duration,
/// A standalone param declared with this kind doesn't require (or use) a capture group: its value is
/// simply whether `message_match_regex` matches the line, case-insensitively or per the regex's own
/// flags — see `crate::engine::derive_log_params`. Unlike other kinds it's present as `true`/`false`
/// on every line rather than omitted when unmatched.
Boolean,
} }
/// Value type paired with [`crate::index_key::IndexKey`] in the engine's primary index. /// Value type paired with [`crate::index_key::IndexKey`] in the engine's primary index.
+49 -2
View File
@@ -207,7 +207,10 @@ fn declare_param(
) -> Result<(), String> { ) -> Result<(), String> {
let re = Regex::new(&request.message_match_regex) let re = Regex::new(&request.message_match_regex)
.map_err(|err| format!("invalid regex: {err}"))?; .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!( return Err(format!(
"regex has no capture group named '{}'", "regex has no capture group named '{}'",
request.capture_group 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. // 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), 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:?}"))?; .map_err(|_| format!("expected a numeric value, got {raw:?}"))?;
(*ms as f64).partial_cmp(&v) (*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))) Ok(ordering.is_some_and(|ordering| apply_compare_op(ordering, op)))
} }
@@ -707,13 +721,20 @@ fn compiled_param_declarations(
.collect() .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( fn derive_log_params(
text: &str, text: &str,
declarations: &[(String, Regex, String, MetricPrimitiveKind)], declarations: &[(String, Regex, String, MetricPrimitiveKind)],
) -> BTreeMap<String, MetricPrimitive> { ) -> BTreeMap<String, MetricPrimitive> {
let mut params = BTreeMap::new(); let mut params = BTreeMap::new();
for (name, regex, capture_group, kind) in declarations { 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 { let Some(captures) = regex.captures(text) else {
continue; continue;
}; };
@@ -939,6 +960,13 @@ fn coerce_to_f64(value: &MetricPrimitive) -> f64 {
MetricPrimitive::Timestamp(ts) => ts.as_millis() as f64, MetricPrimitive::Timestamp(ts) => ts.as_millis() as f64,
MetricPrimitive::Duration(ms) => *ms as f64, MetricPrimitive::Duration(ms) => *ms as f64,
MetricPrimitive::String(_) => 1.0, 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()); 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] #[test]
fn timeline_query_applies_filter_before_bucketing() { fn timeline_query_applies_filter_before_bucketing() {
let mut index = BTreeMap::new(); let mut index = BTreeMap::new();
+5
View File
@@ -12,6 +12,8 @@ pub enum MetricPrimitive {
Timestamp(Timestamp), Timestamp(Timestamp),
/// Milliseconds elapsed; distinct from `Number` so it reads unambiguously in JSON/label form. /// Milliseconds elapsed; distinct from `Number` so it reads unambiguously in JSON/label form.
Duration(i64), Duration(i64),
/// A `Boolean`-kind param's value: whether `message_match_regex` matched the line. See `MetricPrimitiveKind::Boolean`.
Boolean(bool),
} }
impl PartialEq for MetricPrimitive { impl PartialEq for MetricPrimitive {
@@ -35,6 +37,7 @@ impl Ord for MetricPrimitive {
MetricPrimitive::Number(_) => 1, MetricPrimitive::Number(_) => 1,
MetricPrimitive::Timestamp(_) => 2, MetricPrimitive::Timestamp(_) => 2,
MetricPrimitive::Duration(_) => 3, MetricPrimitive::Duration(_) => 3,
MetricPrimitive::Boolean(_) => 4,
} }
} }
match (self, other) { match (self, other) {
@@ -42,6 +45,7 @@ impl Ord for MetricPrimitive {
(MetricPrimitive::Number(a), MetricPrimitive::Number(b)) => a.cmp(b), (MetricPrimitive::Number(a), MetricPrimitive::Number(b)) => a.cmp(b),
(MetricPrimitive::Timestamp(a), MetricPrimitive::Timestamp(b)) => a.cmp(b), (MetricPrimitive::Timestamp(a), MetricPrimitive::Timestamp(b)) => a.cmp(b),
(MetricPrimitive::Duration(a), MetricPrimitive::Duration(b)) => a.cmp(b), (MetricPrimitive::Duration(a), MetricPrimitive::Duration(b)) => a.cmp(b),
(MetricPrimitive::Boolean(a), MetricPrimitive::Boolean(b)) => a.cmp(b),
(a, b) => rank(a).cmp(&rank(b)), (a, b) => rank(a).cmp(&rank(b)),
} }
} }
@@ -56,6 +60,7 @@ impl fmt::Display for MetricPrimitive {
MetricPrimitive::Number(Number::Float(n)) => write!(f, "{n}"), MetricPrimitive::Number(Number::Float(n)) => write!(f, "{n}"),
MetricPrimitive::Timestamp(ts) => write!(f, "{}", ts.as_millis()), MetricPrimitive::Timestamp(ts) => write!(f, "{}", ts.as_millis()),
MetricPrimitive::Duration(ms) => write!(f, "{ms}"), MetricPrimitive::Duration(ms) => write!(f, "{ms}"),
MetricPrimitive::Boolean(b) => write!(f, "{b}"),
} }
} }
} }
+4
View File
@@ -30,6 +30,8 @@ pub enum ProfileMetricPrimitiveKind {
Timestamp, Timestamp,
/// Mirrors `MetricPrimitiveKind::Duration`. /// Mirrors `MetricPrimitiveKind::Duration`.
Duration, Duration,
/// Mirrors `MetricPrimitiveKind::Boolean`.
Boolean,
} }
impl From<MetricPrimitiveKind> for ProfileMetricPrimitiveKind { impl From<MetricPrimitiveKind> for ProfileMetricPrimitiveKind {
@@ -39,6 +41,7 @@ impl From<MetricPrimitiveKind> for ProfileMetricPrimitiveKind {
MetricPrimitiveKind::Number => ProfileMetricPrimitiveKind::Number, MetricPrimitiveKind::Number => ProfileMetricPrimitiveKind::Number,
MetricPrimitiveKind::Timestamp => ProfileMetricPrimitiveKind::Timestamp, MetricPrimitiveKind::Timestamp => ProfileMetricPrimitiveKind::Timestamp,
MetricPrimitiveKind::Duration => ProfileMetricPrimitiveKind::Duration, MetricPrimitiveKind::Duration => ProfileMetricPrimitiveKind::Duration,
MetricPrimitiveKind::Boolean => ProfileMetricPrimitiveKind::Boolean,
} }
} }
} }
@@ -50,6 +53,7 @@ impl From<ProfileMetricPrimitiveKind> for MetricPrimitiveKind {
ProfileMetricPrimitiveKind::Number => MetricPrimitiveKind::Number, ProfileMetricPrimitiveKind::Number => MetricPrimitiveKind::Number,
ProfileMetricPrimitiveKind::Timestamp => MetricPrimitiveKind::Timestamp, ProfileMetricPrimitiveKind::Timestamp => MetricPrimitiveKind::Timestamp,
ProfileMetricPrimitiveKind::Duration => MetricPrimitiveKind::Duration, ProfileMetricPrimitiveKind::Duration => MetricPrimitiveKind::Duration,
ProfileMetricPrimitiveKind::Boolean => MetricPrimitiveKind::Boolean,
} }
} }
} }
+8
View File
@@ -178,6 +178,14 @@ const SECTIONS: DocSection[] = [
a metric (see <em>Filtering</em> above — bare params aren't queryable by name on their own, only metric a metric (see <em>Filtering</em> above — bare params aren't queryable by name on their own, only metric
labels/values are). labels/values are).
</p> </p>
<p>
Given the same kind of lines, declaring a param named <Code>IsError</Code> with regex{' '}
<Code>{String.raw`(?i)error`}</Code> and type <Code>boolean</Code> — no capture group needed — shows{' '}
<Code>IsError</Code> as <Code>true</Code> or <Code>false</Code> on every line in the log table, based on
whether the regex matches that line's text case-insensitively. Unlike <Code>string</Code>/<Code>number</Code>
/<Code>timestamp</Code>/<Code>duration</Code> params, a <Code>boolean</Code> param is never omitted for a
non-matching line.
</p>
</div> </div>
), ),
}, },
+12 -5
View File
@@ -21,12 +21,15 @@ export function Params({ params, onDeclared }: { params: ParamDeclaration[]; onD
const [submitting, setSubmitting] = useState(false) const [submitting, setSubmitting] = useState(false)
const [collapsed, setCollapsed] = useSectionCollapsed('params') const [collapsed, setCollapsed] = useSectionCollapsed('params')
const isBoolean = kind === 'boolean'
async function handleSubmit(e: React.FormEvent) { async function handleSubmit(e: React.FormEvent) {
e.preventDefault() e.preventDefault()
setSubmitError(null) setSubmitError(null)
setSubmitting(true) setSubmitting(true)
try { try {
await declareParam({ name, message_match_regex: regex, capture_group: captureGroup, kind }) // A boolean-kind param never uses a capture group — its value is just whether the regex matched.
await declareParam({ name, message_match_regex: regex, capture_group: isBoolean ? '' : captureGroup, kind })
setName('') setName('')
setRegex('') setRegex('')
setCaptureGroup('') setCaptureGroup('')
@@ -82,13 +85,16 @@ export function Params({ params, onDeclared }: { params: ParamDeclaration[]; onD
</div> </div>
<div className="flex items-end gap-1"> <div className="flex items-end gap-1">
<div className="min-w-0 flex-1"> <div className="min-w-0 flex-1">
<label className="mb-1 block text-xs text-text-subtle">Capture group</label> <label className="mb-1 block text-xs text-text-subtle">
Capture group{isBoolean && ' (unused for boolean)'}
</label>
<input <input
className="w-full min-w-0 rounded border border-border-soft bg-surface-1 px-1.5 py-1 text-xs text-text placeholder:text-text-subtle focus:border-border-strong focus:outline-none" className="w-full min-w-0 rounded border border-border-soft bg-surface-1 px-1.5 py-1 text-xs text-text placeholder:text-text-subtle focus:border-border-strong focus:outline-none disabled:opacity-50"
value={captureGroup} value={captureGroup}
onChange={(e) => setCaptureGroup(e.target.value)} onChange={(e) => setCaptureGroup(e.target.value)}
placeholder="path" placeholder="path"
required required={!isBoolean}
disabled={isBoolean}
/> />
</div> </div>
<select <select
@@ -100,6 +106,7 @@ export function Params({ params, onDeclared }: { params: ParamDeclaration[]; onD
<option value="number">number</option> <option value="number">number</option>
<option value="timestamp">timestamp</option> <option value="timestamp">timestamp</option>
<option value="duration">duration</option> <option value="duration">duration</option>
<option value="boolean">boolean</option>
</select> </select>
</div> </div>
{submitError && <div className="rounded border border-danger-border bg-danger-bg/50 px-2 py-1 text-xs text-danger-text">{submitError}</div>} {submitError && <div className="rounded border border-danger-border bg-danger-bg/50 px-2 py-1 text-xs text-danger-text">{submitError}</div>}
@@ -122,7 +129,7 @@ export function Params({ params, onDeclared }: { params: ParamDeclaration[]; onD
className="rounded border border-border-soft px-2 py-1 text-xs text-text-secondary" className="rounded border border-border-soft px-2 py-1 text-xs text-text-secondary"
title={p.message_match_regex} title={p.message_match_regex}
> >
{p.name} ({p.kind}, group {p.capture_group}) {p.name} ({p.kind}{p.kind === 'boolean' ? '' : `, group ${p.capture_group}`})
</span> </span>
))} ))}
</div> </div>
+3 -1
View File
@@ -4,11 +4,13 @@ export type MetricPrimitive =
| { Number: { Int: number } | { Float: number } } | { Number: { Int: number } | { Float: number } }
| { Timestamp: number } | { Timestamp: number }
| { Duration: number } | { Duration: number }
| { Boolean: boolean }
export function metricPrimitiveToDisplayString(value: MetricPrimitive): string { export function metricPrimitiveToDisplayString(value: MetricPrimitive): string {
if ('String' in value) return value.String if ('String' in value) return value.String
if ('Timestamp' in value) return String(value.Timestamp) if ('Timestamp' in value) return String(value.Timestamp)
if ('Duration' in value) return `${value.Duration}ms` if ('Duration' in value) return `${value.Duration}ms`
if ('Boolean' in value) return String(value.Boolean)
if ('Int' in value.Number) return String(value.Number.Int) if ('Int' in value.Number) return String(value.Number.Int)
return String(value.Number.Float) return String(value.Number.Float)
} }
@@ -25,7 +27,7 @@ export interface ParamDeclaration {
name: string name: string
message_match_regex: string message_match_regex: string
capture_group: string capture_group: string
kind: 'string' | 'number' | 'timestamp' | 'duration' kind: 'string' | 'number' | 'timestamp' | 'duration' | 'boolean'
} }
export interface TimelineBucket { export interface TimelineBucket {