Make elapsed-since-last an explicit param extractor, not a Duration-kind special case (ticket 038)

Previously a Value-role param with kind: Duration silently meant "elapsed
time since the previous same-labeled event" instead of parsed text — the
same MetricPrimitiveKind::Duration variant meant two unrelated things
depending on which list (label_params vs value_param) referenced it.

Add ParamDeclaration::elapsed_since_last, a third extraction source
alongside capture_group/json_field_path: when set, the param is computed
(via a new resolve_param_value, used uniformly for label_params and
value_param) rather than text-extracted, and declare_param validates it's
paired with kind: Duration. apply_metric_to_log no longer branches on
(role, kind) — it dispatches purely on what each resolved param declares.

Migrate existing profiles: migrate_duration_value_params converts any
Duration-kind param referenced as a metric's value_param into the new
extractor, and resolve_or_create_legacy_param (ticket 037's pre-existing
migration) now carries the same meaning forward for pre-037 inline
Value-role Duration matchers.

Params.tsx offers "Elapsed since previous event" as its own Extraction
option (hides the now-irrelevant message-match-regex/capture-group
fields and forces kind to duration); Metrics.tsx labels such params
accordingly wherever they're listed as label/value param choices.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XFUNZwCvz3kcod3352ZR2o
This commit is contained in:
Hugh Kaznowski
2026-09-05 23:01:32 +01:00
co-authored by Claude Sonnet 5
parent 375aa38ef8
commit 5e30e02a78
8 changed files with 352 additions and 105 deletions
+3 -3
View File
@@ -254,7 +254,7 @@ export function Metrics({ range, activePreset }: { range: TimeRange | null; acti
onChange={() => toggleLabelParam(p.name)}
/>
<span className="font-mono">{p.name}</span>
<span className="text-text-faint">({p.kind})</span>
<span className="text-text-faint">({p.elapsed_since_last ? 'elapsed since previous' : p.kind})</span>
</label>
))
)}
@@ -263,7 +263,7 @@ export function Metrics({ range, activePreset }: { range: TimeRange | null; acti
<div>
<label
className="mb-1 block text-xs text-text-subtle"
title="At most one param becomes the counter's per-event captured value (used by histogram()/gauge()) instead of a label. A 'duration'-kind value param is derived automatically — elapsed time since the previous match with the same labels — rather than parsed from its capture group."
title="At most one param becomes the counter's per-event captured value (used by histogram()/gauge()) instead of a label. An 'elapsed since previous event' param (declared in the Params panel) is computed automatically — elapsed time since the previous match with the same labels — rather than parsed from its capture group."
>
Value param
</label>
@@ -275,7 +275,7 @@ export function Metrics({ range, activePreset }: { range: TimeRange | null; acti
<option value="">(none — occurrence count)</option>
{params.map((p) => (
<option key={p.name} value={p.name}>
{p.name} ({p.kind})
{p.name} ({p.elapsed_since_last ? 'elapsed since previous' : p.kind})
</option>
))}
</select>
+63 -35
View File
@@ -13,7 +13,11 @@ import { RegexHelpIcon } from './RegexHelpIcon'
const EMPTY_KIND: ParamDeclaration['kind'] = 'string'
type ExtractionSource = 'regex' | 'json_field'
// 'elapsed_since_last' is always offered, regardless of what the message match regex looks like —
// unlike 'json_field' (which only makes sense once the sample line looks like a JSON object), this
// param's value is never extracted from text at all: it's elapsed time since the previous event
// sharing the same label set (ticket 038).
type ExtractionSource = 'regex' | 'json_field' | 'elapsed_since_last'
/// A JSON *object* (not an array/scalar) parses cleanly and pretty-prints with braces — the same
/// shape a JSON field path can be read from, so this is the "line looks like JSON" gate that
@@ -41,23 +45,39 @@ export function Params({ params, onDeclared }: { params: ParamDeclaration[]; onD
const [collapsed, setCollapsed] = useSectionCollapsed('params')
const isBoolean = kind === 'boolean'
const isElapsedSinceLast = source === 'elapsed_since_last'
// The message match regex doubles as a sample line here so a pasted JSON log line unlocks the
// "JSON field" extraction option — regex-based params are unaffected either way.
// "JSON field" extraction option — regex-based params are unaffected either way. Not consulted at
// all for "elapsed since previous event", which is always offered.
const detectedJson = useMemo(() => looksLikeJsonObject(regex), [regex])
const effectiveSource: ExtractionSource = detectedJson && !isBoolean ? source : 'regex'
const effectiveSource: ExtractionSource = isElapsedSinceLast
? 'elapsed_since_last'
: detectedJson && !isBoolean
? source
: 'regex'
function handleSourceChange(next: ExtractionSource) {
setSource(next)
// "Elapsed since previous event" always produces a Duration value — never text-extracted — so
// there's no separate kind to pick; force it rather than leaving a stale/mismatched kind selected.
if (next === 'elapsed_since_last') setKind('duration')
}
async function handleSubmit(e: React.FormEvent) {
e.preventDefault()
setSubmitError(null)
setSubmitting(true)
try {
// A boolean-kind param never uses a capture group — its value is just whether the regex matched.
// A boolean-kind param never uses a capture group — its value is just whether the regex
// matched. An elapsed-since-last param never uses a capture group, JSON field path, or even
// its own message match regex — its value is computed, not extracted from text at all.
await declareParam({
name,
message_match_regex: regex,
capture_group: isBoolean || effectiveSource === 'json_field' ? '' : captureGroup,
message_match_regex: effectiveSource === 'elapsed_since_last' ? '' : regex,
capture_group: isBoolean || effectiveSource !== 'regex' ? '' : captureGroup,
json_field_path: effectiveSource === 'json_field' ? jsonFieldPath : null,
kind,
kind: effectiveSource === 'elapsed_since_last' ? 'duration' : kind,
elapsed_since_last: effectiveSource === 'elapsed_since_last',
})
setName('')
setRegex('')
@@ -104,33 +124,39 @@ export function Params({ params, onDeclared }: { params: ParamDeclaration[]; onD
required
/>
</div>
<div>
<label className="mb-1 flex items-center gap-1 text-xs text-text-subtle">
Message match regex
<RegexHelpIcon label="Help: Message match regex syntax" />
</label>
<input
className="w-full rounded border border-border-soft bg-surface-1 px-2 py-1 font-mono text-sm text-text placeholder:text-text-subtle focus:border-border-strong focus:outline-none"
value={regex}
onChange={(e) => setRegex(e.target.value)}
placeholder={String.raw`request path=(?P<path>\S+)`}
required
/>
</div>
{detectedJson && !isBoolean && (
{!isElapsedSinceLast && (
<div>
<label className="mb-1 block text-xs text-text-subtle">Extraction</label>
<select
className="w-full rounded border border-border-soft bg-surface-1 px-2 py-1 text-xs text-text focus:border-border-strong focus:outline-none"
value={source}
onChange={(e) => setSource(e.target.value as ExtractionSource)}
>
<option value="regex">Regex capture group</option>
<option value="json_field">JSON field</option>
</select>
<label className="mb-1 flex items-center gap-1 text-xs text-text-subtle">
Message match regex
<RegexHelpIcon label="Help: Message match regex syntax" />
</label>
<input
className="w-full rounded border border-border-soft bg-surface-1 px-2 py-1 font-mono text-sm text-text placeholder:text-text-subtle focus:border-border-strong focus:outline-none"
value={regex}
onChange={(e) => setRegex(e.target.value)}
placeholder={String.raw`request path=(?P<path>\S+)`}
required
/>
</div>
)}
{effectiveSource === 'json_field' ? (
<div>
<label className="mb-1 block text-xs text-text-subtle">Extraction</label>
<select
className="w-full rounded border border-border-soft bg-surface-1 px-2 py-1 text-xs text-text focus:border-border-strong focus:outline-none"
value={source}
onChange={(e) => handleSourceChange(e.target.value as ExtractionSource)}
>
<option value="regex">Regex capture group</option>
{detectedJson && !isBoolean && <option value="json_field">JSON field</option>}
<option value="elapsed_since_last">Elapsed since previous event</option>
</select>
</div>
{isElapsedSinceLast ? (
<p className="text-xs text-text-faint">
Computed automatically — elapsed time since the previous event that shares the same
labels wherever this param is used, not parsed from the log line.
</p>
) : effectiveSource === 'json_field' ? (
<div className="flex items-end gap-1">
<div className="min-w-0 flex-1">
<label className="mb-1 block text-xs text-text-subtle">JSON field path</label>
@@ -199,12 +225,14 @@ export function Params({ params, onDeclared }: { params: ParamDeclaration[]; onD
<span
key={p.name}
className="rounded border border-border-soft px-2 py-1 text-xs text-text-secondary"
title={p.message_match_regex}
title={p.elapsed_since_last ? undefined : p.message_match_regex}
>
{p.name} ({p.kind}
{p.kind === 'boolean'
? ''
: `, ${p.json_field_path ? `json field ${p.json_field_path}` : `group ${p.capture_group}`}`}
{p.elapsed_since_last
? ', elapsed since previous'
: p.kind === 'boolean'
? ''
: `, ${p.json_field_path ? `json field ${p.json_field_path}` : `group ${p.capture_group}`}`}
)
</span>
))}
+2
View File
@@ -30,6 +30,8 @@ export interface ParamDeclaration {
/** Alternative to `capture_group`: a dot-separated JSON field path (e.g. "foo.bar"), read from lines that parse as a JSON object. When set (non-empty), takes precedence over `capture_group`. */
json_field_path?: string | null
kind: 'string' | 'number' | 'timestamp' | 'duration' | 'boolean'
/** A third extraction source, distinct from `capture_group`/`json_field_path`: when true, this param's value is never parsed from text — it's computed as elapsed time since the previous event sharing the same label set. Only meaningful paired with `kind: 'duration'`. */
elapsed_since_last?: boolean
}
export interface TimelineBucket {