diff --git a/.site/spi/spk.spec b/.site/spi/spk.spec index 4b393aa766..635315d590 100644 --- a/.site/spi/spk.spec +++ b/.site/spi/spk.spec @@ -35,9 +35,6 @@ Package manager and a software runtime for studio environments export SPDEV_CONFIG_FILE=.site/spi/.spdev.yaml dev toolchain install source ~/.bashrc -export SENTRY_DSN=http://4506b47108ac4b648fdf18a8d803f403@sentry.spimageworks.com/25 -export SENTRY_ENVIRONMENT=production -export SENTRY_USERNAME_OVERRIDE_VAR=GITLAB_USER_LOGIN # Include `--all` to also build spk-launcher dev env -- cargo build --release --features "migration-to-components,sentry,spfs/protobuf-src,statsd,fuse-backend-rhel-7-6" --all diff --git a/Cargo.lock b/Cargo.lock index b6a2115d9d..c93ec65ba6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3493,6 +3493,7 @@ dependencies = [ "serde_yaml", "spfs", "spk-build", + "spk-config", "spk-exec", "spk-schema", "spk-solve", diff --git a/crates/spk-cli/common/Cargo.toml b/crates/spk-cli/common/Cargo.toml index 8384be06d1..6c806d4b9f 100644 --- a/crates/spk-cli/common/Cargo.toml +++ b/crates/spk-cli/common/Cargo.toml @@ -7,6 +7,7 @@ version = { workspace = true } [features] migration-to-components = [ "spk-build/migration-to-components", + "spk-config/migration-to-components", "spk-exec/migration-to-components", "spk-solve/migration-to-components", "spk-schema/migration-to-components", @@ -32,6 +33,7 @@ sentry-anyhow = { workspace = true, optional = true } sentry-tracing = { workspace = true, optional = true } spfs = { version = '0.34.6', path = "../../spfs" } spk-build = { path = '../../spk-build' } +spk-config = { path = '../../spk-config' } spk-exec = { path = '../../spk-exec' } spk-solve = { path = '../../spk-solve' } spk-schema = { path = '../../spk-schema' } diff --git a/crates/spk-cli/common/src/env.rs b/crates/spk-cli/common/src/env.rs index 8fc63a8605..69b31d6bde 100644 --- a/crates/spk-cli/common/src/env.rs +++ b/crates/spk-cli/common/src/env.rs @@ -102,18 +102,31 @@ pub async fn current_env() -> crate::Result { #[cfg(feature = "sentry")] pub fn configure_sentry() -> Option { - // Call this before `sentry::init` to avoid potential `SIGSEGV`. - let username = get_username_for_sentry(); + let Ok(config) = spk_config::get_config() else { + return None; + }; + + let username = config + .sentry + .username_override_var + .clone() + .unwrap_or_else(|| { + // Call this before `sentry::init` to avoid potential `SIGSEGV`. + whoami::username() + }); // When using the sentry feature it is expected that the DSN // and other configuration is provided at *compile* time. let guard = match catch_unwind(|| { sentry::init(( - option_env!("SENTRY_DSN"), + config.sentry.dsn.as_str(), sentry::ClientOptions { release: sentry::release_name!(), - environment: option_env!("SENTRY_ENVIRONMENT") - .map(ToString::to_string) + environment: config + .sentry + .environment + .as_ref() + .map(ToOwned::to_owned) .map(std::borrow::Cow::Owned), before_send: Some(std::sync::Arc::new(|mut event| { // Remove ansi color codes from the event message @@ -169,23 +182,6 @@ pub fn configure_sentry() -> Option { Some(guard) } -#[cfg(feature = "sentry")] -fn get_username_for_sentry() -> String { - // If this is being run from an automated process run by a - // non-human user, e.g. gitlab CI job, then use the configured env - // var name to get the username of the person that triggered the - // job. Otherwise get the username of the person who ran this spk - // instance. - let username_override_var = option_env!("SENTRY_USERNAME_OVERRIDE_VAR"); - username_override_var - .map(std::env::var) - .and_then(Result::ok) - .unwrap_or_else(|| { - // Call this before `sentry::init` to avoid potential `SIGSEGV`. - whoami::username() - }) -} - #[cfg(feature = "sentry")] fn get_spk_context() -> ( String, diff --git a/crates/spk-config/src/config.rs b/crates/spk-config/src/config.rs index d9f0bd1c08..69fa2f215f 100644 --- a/crates/spk-config/src/config.rs +++ b/crates/spk-config/src/config.rs @@ -15,6 +15,22 @@ mod config_test; static CONFIG: OnceCell>> = OnceCell::new(); +#[derive(Clone, Default, Debug, Deserialize, Serialize)] +#[serde(default)] +pub struct Sentry { + /// Sentry DSN + pub dsn: String, + + /// Sentry environment + pub environment: Option, + + /// Environment variable name to use as sentry username, if set. + /// + /// This is useful in CI if the CI system has a variable that contains + /// the username of the person who triggered the build. + pub username_override_var: Option, +} + #[derive(Clone, Default, Debug, Deserialize, Serialize)] #[serde(default)] pub struct Solver { @@ -84,6 +100,7 @@ pub struct Config { // These sub-types should aim to only have one level of // values within them, otherwise they become impossible to address // with environment variables. + pub sentry: Sentry, pub solver: Solver, pub statsd: Statsd, } diff --git a/crates/spk-launcher/bin/create-spk-platform b/crates/spk-launcher/bin/create-spk-platform index 1071936da4..7a9e4f6bde 100755 --- a/crates/spk-launcher/bin/create-spk-platform +++ b/crates/spk-launcher/bin/create-spk-platform @@ -21,9 +21,6 @@ mkdir -p build export SPDEV_CONFIG_FILE=.site/spi/.spdev.yaml dev toolchain install Rust source ~/.bashrc -export SENTRY_DSN=http://4506b47108ac4b648fdf18a8d803f403@sentry.spimageworks.com/25 -export SENTRY_ENVIRONMENT=production -export SENTRY_USERNAME_OVERRIDE_VAR=GITLAB_USER_LOGIN dev env -- cargo build --release --features "sentry, migration-to-components, statsd" # 2. create a new spfs layer