Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add summarize_runtime_config #2788

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading