Skip to content

Commit 545b902

Browse files
refactored changed to from_file_with_schema
1 parent 4648a20 commit 545b902

2 files changed

Lines changed: 25 additions & 27 deletions

File tree

asap-planner-rs/src/lib.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -363,27 +363,11 @@ impl Controller {
363363
/// or when the schema is constructed in-process by the caller).
364364
pub fn from_file_with_schema(
365365
path: &Path,
366-
mut schema: PromQLSchema,
366+
schema: PromQLSchema,
367367
opts: RuntimeOptions,
368368
) -> Result<Self, ControllerError> {
369369
let yaml_str = std::fs::read_to_string(path)?;
370370
let config: ControllerConfig = serde_yaml::from_str(&yaml_str)?;
371-
// Fill in any metrics missing from the caller-supplied schema using the
372-
// config-file `metrics` hint (if present).
373-
if let Some(metric_hints) = &config.metrics {
374-
for hint in metric_hints {
375-
if !schema.config.contains_key(&hint.metric) {
376-
debug!(
377-
"Schema missing '{}'; falling back to config-file hint with labels {:?}",
378-
hint.metric, hint.labels
379-
);
380-
schema = schema.add_metric(
381-
hint.metric.clone(),
382-
KeyByLabelNames::new(hint.labels.clone()),
383-
);
384-
}
385-
}
386-
}
387371
Ok(Self {
388372
config,
389373
schema,

asap-planner-rs/src/main.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use asap_planner::{Controller, RuntimeOptions, SQLController, SQLRuntimeOptions, StreamingEngine};
22
use clap::Parser;
3+
use promql_utilities::data_model::KeyByLabelNames;
34
use sketch_db_common::enums::QueryLanguage;
45
use std::path::PathBuf;
56

@@ -56,6 +57,19 @@ enum EngineArg {
5657
Precompute,
5758
}
5859

60+
/// Build a `PromQLSchema` from the `metrics` hints in a controller config file.
61+
fn schema_from_config_file(path: &PathBuf) -> anyhow::Result<asap_planner::PromQLSchema> {
62+
let yaml_str = std::fs::read_to_string(path)?;
63+
let config: asap_planner::ControllerConfig = serde_yaml::from_str(&yaml_str)?;
64+
let mut schema = asap_planner::PromQLSchema::new();
65+
if let Some(metrics) = &config.metrics {
66+
for m in metrics {
67+
schema = schema.add_metric(m.metric.clone(), KeyByLabelNames::new(m.labels.clone()));
68+
}
69+
}
70+
Ok(schema)
71+
}
72+
5973
fn main() -> anyhow::Result<()> {
6074
let args = Args::parse();
6175

@@ -89,19 +103,19 @@ fn main() -> anyhow::Result<()> {
89103
(Some(config_path), None, Some(url)) => {
90104
Controller::from_file(&config_path, opts, &url)?
91105
}
92-
(Some(config_path), None, None) => Controller::from_file_with_schema(
93-
&config_path,
94-
asap_planner::PromQLSchema::new(),
95-
opts,
96-
)?,
106+
(Some(config_path), None, None) => {
107+
let schema = schema_from_config_file(&config_path)?;
108+
Controller::from_file_with_schema(&config_path, schema, opts)?
109+
}
97110
(None, Some(log_path), Some(url)) => {
98111
Controller::from_query_log(&log_path, opts, &url)?
99112
}
100-
(None, Some(log_path), None) => Controller::from_query_log_with_schema(
101-
&log_path,
102-
asap_planner::PromQLSchema::new(),
103-
opts,
104-
)?,
113+
(None, Some(_log_path), None) => {
114+
anyhow::bail!(
115+
"--prometheus-url is required when using --query-log \
116+
(query logs have no metrics hint to fall back on)"
117+
)
118+
}
105119
_ => anyhow::bail!(
106120
"exactly one of --input_config or --query-log must be provided for PromQL mode"
107121
),

0 commit comments

Comments
 (0)