Extract shared declare/update-metric backfill and auto-param insertion helpers
declare_metric and update_metric duplicated the resolve-params + backfill_metric + insert-MetricDeclaration sequence; extracted into resolve_backfill_and_store_metric so the declare-vs-rename difference is expressed only in caller-side logic. Also extracted auto_declare_params's per-candidate insertion body into try_insert_auto_param so the candidate-building and insertion steps read as separate concerns. Reimplemented directly against current main (ticket 085) after the original worktree agent's branch was based on a stale pre-consolidation snapshot module layout that no longer matches main. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TeyhRfMYjMditek6geDRoQ
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
cdd781c904
commit
a47f771087
+23
-28
@@ -330,11 +330,18 @@ impl EngineSnapshot {
|
|||||||
pub(crate) fn declare_metric(&mut self, request: DeclareMetricRequest) -> Result<(), String> {
|
pub(crate) fn declare_metric(&mut self, request: DeclareMetricRequest) -> Result<(), String> {
|
||||||
let re = self.validate_metric_request(&request)?;
|
let re = self.validate_metric_request(&request)?;
|
||||||
self.metric_regex_cache.insert(request.name.clone(), re.clone());
|
self.metric_regex_cache.insert(request.name.clone(), re.clone());
|
||||||
let resolved_params = self.resolved_param_declarations();
|
self.resolve_backfill_and_store_metric(request, &re);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Shared by `declare_metric` and `update_metric`: resolves param declarations, backfills
|
||||||
|
/// the metric's buckets from existing log history, then writes the `MetricDeclaration` entry.
|
||||||
|
/// Callers differ only in what happens before this (rename bookkeeping for updates).
|
||||||
|
fn resolve_backfill_and_store_metric(&mut self, request: DeclareMetricRequest, re: &Regex) {
|
||||||
|
let resolved_params = self.resolved_param_declarations();
|
||||||
self.backfill_metric(
|
self.backfill_metric(
|
||||||
&request.name,
|
&request.name,
|
||||||
&re,
|
re,
|
||||||
&request.label_params,
|
&request.label_params,
|
||||||
request.value_param.as_deref(),
|
request.value_param.as_deref(),
|
||||||
&resolved_params,
|
&resolved_params,
|
||||||
@@ -342,7 +349,7 @@ impl EngineSnapshot {
|
|||||||
request.unmatched_labels.as_ref(),
|
request.unmatched_labels.as_ref(),
|
||||||
);
|
);
|
||||||
self.index.insert(
|
self.index.insert(
|
||||||
IndexKey::MetricDeclaration { name: request.name },
|
IndexKey::MetricDeclaration { name: request.name.clone() },
|
||||||
DataValue::MetricDeclaration {
|
DataValue::MetricDeclaration {
|
||||||
message_match_regex: request.message_match_regex,
|
message_match_regex: request.message_match_regex,
|
||||||
label_params: request.label_params,
|
label_params: request.label_params,
|
||||||
@@ -351,7 +358,6 @@ impl EngineSnapshot {
|
|||||||
unmatched_labels: request.unmatched_labels,
|
unmatched_labels: request.unmatched_labels,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn validate_update_metric(
|
pub(crate) fn validate_update_metric(
|
||||||
@@ -372,7 +378,6 @@ impl EngineSnapshot {
|
|||||||
|
|
||||||
pub(crate) fn update_metric(&mut self, existing_name: &str, request: DeclareMetricRequest) -> Result<(), String> {
|
pub(crate) fn update_metric(&mut self, existing_name: &str, request: DeclareMetricRequest) -> Result<(), String> {
|
||||||
let re = self.validate_update_metric(existing_name, &request)?;
|
let re = self.validate_update_metric(existing_name, &request)?;
|
||||||
let resolved_params = self.resolved_param_declarations();
|
|
||||||
|
|
||||||
self.clear_metric_data(existing_name);
|
self.clear_metric_data(existing_name);
|
||||||
if request.name != existing_name {
|
if request.name != existing_name {
|
||||||
@@ -381,25 +386,7 @@ impl EngineSnapshot {
|
|||||||
}
|
}
|
||||||
self.metric_regex_cache.insert(request.name.clone(), re.clone());
|
self.metric_regex_cache.insert(request.name.clone(), re.clone());
|
||||||
|
|
||||||
self.backfill_metric(
|
self.resolve_backfill_and_store_metric(request, &re);
|
||||||
&request.name,
|
|
||||||
&re,
|
|
||||||
&request.label_params,
|
|
||||||
request.value_param.as_deref(),
|
|
||||||
&resolved_params,
|
|
||||||
request.matched_labels.as_ref(),
|
|
||||||
request.unmatched_labels.as_ref(),
|
|
||||||
);
|
|
||||||
self.index.insert(
|
|
||||||
IndexKey::MetricDeclaration { name: request.name.clone() },
|
|
||||||
DataValue::MetricDeclaration {
|
|
||||||
message_match_regex: request.message_match_regex,
|
|
||||||
label_params: request.label_params,
|
|
||||||
value_param: request.value_param,
|
|
||||||
matched_labels: request.matched_labels,
|
|
||||||
unmatched_labels: request.unmatched_labels,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -698,9 +685,18 @@ impl EngineSnapshot {
|
|||||||
if new_this_line >= MAX_NEW_AUTO_PARAMS_PER_LINE || total >= MAX_AUTO_DECLARED_PARAMS {
|
if new_this_line >= MAX_NEW_AUTO_PARAMS_PER_LINE || total >= MAX_AUTO_DECLARED_PARAMS {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if self.try_insert_auto_param(candidate, &mut total) {
|
||||||
|
new_this_line += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Inserts `candidate` as an auto-derived `ParamDeclaration` unless a param with that name
|
||||||
|
/// is already declared, bumping `*total` and returning whether it inserted.
|
||||||
|
fn try_insert_auto_param(&mut self, candidate: AutoParamCandidate, total: &mut usize) -> bool {
|
||||||
let index_key = IndexKey::ParamDeclaration { name: candidate.name.clone() };
|
let index_key = IndexKey::ParamDeclaration { name: candidate.name.clone() };
|
||||||
if self.index.contains_key(&index_key) {
|
if self.index.contains_key(&index_key) {
|
||||||
continue;
|
return false;
|
||||||
}
|
}
|
||||||
if let Ok(re) = Regex::new(&candidate.message_match_regex) {
|
if let Ok(re) = Regex::new(&candidate.message_match_regex) {
|
||||||
self.param_regex_cache.insert(candidate.name.clone(), re);
|
self.param_regex_cache.insert(candidate.name.clone(), re);
|
||||||
@@ -718,9 +714,8 @@ impl EngineSnapshot {
|
|||||||
log_stream_gated: false,
|
log_stream_gated: false,
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
new_this_line += 1;
|
*total += 1;
|
||||||
total += 1;
|
true
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn derive_log_stream_params_from_row(
|
pub(crate) fn derive_log_stream_params_from_row(
|
||||||
|
|||||||
Reference in New Issue
Block a user