Skip to content

Commit

Permalink
Merge pull request #2788 from lann/summarize-runtime-config
Browse files Browse the repository at this point in the history
Add summarize_runtime_config
  • Loading branch information
lann authored Aug 29, 2024
2 parents fe39e66 + 55e5b89 commit 09de9c3
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/runtime-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ pub struct ResolvedRuntimeConfig<T> {
///
/// `None` is used for an "unset" log directory.
pub log_dir: Option<PathBuf>,
/// The input TOML, for informational summaries.
pub toml: toml::Table,
}

impl<T> ResolvedRuntimeConfig<T>
Expand Down Expand Up @@ -140,6 +142,7 @@ where
sqlite_resolver: sqlite_config_resolver,
state_dir: toml_resolver.state_dir()?,
log_dir: toml_resolver.log_dir()?,
toml,
})
}

Expand Down
1 change: 1 addition & 0 deletions crates/trigger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ spin-runtime-config = { path = "../runtime-config" }
spin-telemetry = { path = "../telemetry" }
terminal = { path = "../terminal" }
tokio = { version = "1.23", features = ["fs", "rt"] }
toml = "0.8"
tracing = { workspace = true }

[dev-dependencies]
Expand Down
8 changes: 5 additions & 3 deletions crates/trigger/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ use spin_core::async_trait;
use spin_factors_executor::{ComponentLoader, FactorsExecutor};
use spin_runtime_config::{ResolvedRuntimeConfig, UserProvidedPath};
use sqlite_statements::SqlStatementExecutorHook;
use summary::{KeyValueDefaultStoreSummaryHook, SqliteDefaultStoreSummaryHook};
use summary::{
summarize_runtime_config, KeyValueDefaultStoreSummaryHook, SqliteDefaultStoreSummaryHook,
};

use crate::factors::{TriggerFactors, TriggerFactorsRuntimeConfig};
use crate::stdio::{FollowComponents, StdioLoggingExecutorHooks};
Expand Down Expand Up @@ -341,6 +343,8 @@ impl<T: Trigger> TriggerAppBuilder<T> {
use_gpu,
)?;

summarize_runtime_config(&runtime_config, runtime_config_path);

runtime_config
.set_initial_key_values(&options.initial_key_values)
.await?;
Expand Down Expand Up @@ -421,8 +425,6 @@ impl<T: Trigger> TriggerAppBuilder<T> {
options.follow_components,
log_dir,
));
// TODO:
// builder.hooks(SummariseRuntimeConfigHook::new(&self.runtime_config_file));
executor.add_hooks(KeyValueDefaultStoreSummaryHook);
executor.add_hooks(SqliteDefaultStoreSummaryHook);
executor.add_hooks(SqlStatementExecutorHook::new(options.sqlite_statements));
Expand Down
42 changes: 42 additions & 0 deletions crates/trigger/src/cli/summary.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,52 @@
use std::path::Path;

use spin_common::ui::quoted_path;
use spin_core::async_trait;
use spin_factor_key_value::KeyValueFactor;
use spin_factor_sqlite::SqliteFactor;
use spin_factors_executor::ExecutorHooks;
use spin_runtime_config::ResolvedRuntimeConfig;
use toml::Value;

use crate::factors::TriggerFactors;

pub fn summarize_runtime_config<T>(
runtime_config: &ResolvedRuntimeConfig<T>,
runtime_config_path: Option<&Path>,
) {
let toml = &runtime_config.toml;
let summarize_labeled_typed_tables = |key| {
let mut summaries = vec![];
if let Some(tables) = toml.get(key).and_then(Value::as_table) {
for (label, config) in tables {
if let Some(ty) = config.get("type").and_then(Value::as_str) {
summaries.push(format!("[{key}.{label}: {ty}]"))
}
}
}
summaries
};

let mut summaries = vec![];
// [key_value_store.<label>: <type>]
summaries.extend(summarize_labeled_typed_tables("key_value_store"));
// [sqlite_database.<label>: <type>]
summaries.extend(summarize_labeled_typed_tables("sqlite_database"));
// [llm_compute: <type>]
if let Some(table) = toml.get("llm_compute").and_then(Value::as_table) {
if let Some(ty) = table.get("type").and_then(Value::as_str) {
summaries.push(format!("[llm_compute: {ty}"));
}
}
if !summaries.is_empty() {
let summaries = summaries.join(", ");
let from_path = runtime_config_path
.map(|path| format!("from {}", quoted_path(path)))
.unwrap_or_default();
println!("Using runtime config {summaries} {from_path}");
}
}

/// An [`ExecutorHooks`] that prints information about the default KV store.
pub struct KeyValueDefaultStoreSummaryHook;

Expand Down
1 change: 1 addition & 0 deletions examples/spin-timer/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 09de9c3

Please sign in to comment.