Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

refactor(sentry): share dsn and environment resolution across crates

Steven Enamakel5afe8345b2ed
diff --git a/crates/openhuman-cli/src/main.rs b/crates/openhuman-cli/src/main.rs
index ff7f1c154e..2cafe71f5f 100644
--- a/crates/openhuman-cli/src/main.rs
+++ b/crates/openhuman-cli/src/main.rs
@@ -53,12 +53,15 @@ fn restore_default_sigpipe() {}
#[cfg(feature = "crash-reporting")]
fn init_sentry() -> process::sentry::sdk::ClientInitGuard {
let config = process::sentry::SentryConfig::new(
- sentry_dsn(),
+ process::sentry::core_dsn(
+ option_env!("OPENHUMAN_CORE_SENTRY_DSN"),
+ option_env!("OPENHUMAN_SENTRY_DSN"),
+ ),
process::sentry::release_tag(
env!("CARGO_PKG_VERSION"),
option_env!("OPENHUMAN_BUILD_SHA"),
),
- resolve_environment(std::env::var("OPENHUMAN_APP_ENV").ok()),
+ process::sentry::resolve_environment(std::env::var("OPENHUMAN_APP_ENV").ok()),
);
log::debug!(
"[cli] sentry init environment={} dsn_set={}",
@@ -68,36 +71,6 @@ fn init_sentry() -> process::sentry::sdk::ClientInitGuard {
process::sentry::sdk::init(process::sentry::client_options(config))
}
-/// The core DSN: `OPENHUMAN_CORE_SENTRY_DSN`, then the legacy
-/// `OPENHUMAN_SENTRY_DSN`, at runtime and then baked in at compile time.
-/// `None` gives a client that sends nothing.
-#[cfg(feature = "crash-reporting")]
-fn sentry_dsn() -> Option<String> {
- process::sentry::first_non_blank([
- std::env::var("OPENHUMAN_CORE_SENTRY_DSN").ok(),
- std::env::var("OPENHUMAN_SENTRY_DSN").ok(),
- option_env!("OPENHUMAN_CORE_SENTRY_DSN").map(str::to_owned),
- option_env!("OPENHUMAN_SENTRY_DSN").map(str::to_owned),
- ])
-}
-
-/// The deployment environment: `OPENHUMAN_APP_ENV` (lower-cased) when set,
-/// else `development` for debug builds and `production` for release builds.
-#[cfg(feature = "crash-reporting")]
-fn resolve_environment(app_env: Option<String>) -> String {
- if let Some(value) = app_env {
- let trimmed = value.trim().to_ascii_lowercase();
- if !trimmed.is_empty() {
- return trimmed;
- }
- }
- if cfg!(debug_assertions) {
- "development".to_string()
- } else {
- "production".to_string()
- }
-}
-
#[cfg(all(test, feature = "crash-reporting"))]
#[path = "main_tests.rs"]
mod tests;
diff --git a/crates/openhuman-cli/src/main_tests.rs b/crates/openhuman-cli/src/main_tests.rs
index 904901d356..6368c0c5b5 100644
--- a/crates/openhuman-cli/src/main_tests.rs
+++ b/crates/openhuman-cli/src/main_tests.rs
@@ -1,21 +1,5 @@
use super::*;
-#[test]
-fn environment_prefers_app_env_lowercased() {
- assert_eq!(resolve_environment(Some(" Staging ".into())), "staging");
-}
-
-#[test]
-fn environment_falls_back_on_blank_or_missing() {
- let expected = if cfg!(debug_assertions) {
- "development"
- } else {
- "production"
- };
- assert_eq!(resolve_environment(None), expected);
- assert_eq!(resolve_environment(Some(" ".into())), expected);
-}
-
#[test]
fn release_tag_uses_the_crate_version() {
let tag = process::sentry::release_tag(env!("CARGO_PKG_VERSION"), None);
diff --git a/crates/openhuman-embed/src/process_sentry.rs b/crates/openhuman-embed/src/process_sentry.rs
index 8df4eed4ea..998bd0d49b 100644
--- a/crates/openhuman-embed/src/process_sentry.rs
+++ b/crates/openhuman-embed/src/process_sentry.rs
@@ -69,6 +69,41 @@ pub fn first_non_blank(candidates: impl IntoIterator<Item = Option<String>>) ->
.find(|value| !value.is_empty())
}
+/// The core binaries' DSN: `OPENHUMAN_CORE_SENTRY_DSN`, then the legacy
+/// `OPENHUMAN_SENTRY_DSN`, read at runtime and then from the values the
+/// calling crate baked in at compile time.
+///
+/// `baked_core` / `baked_legacy` are the caller's
+/// `option_env!("OPENHUMAN_CORE_SENTRY_DSN")` / `option_env!("OPENHUMAN_SENTRY_DSN")`:
+/// they must be expanded in the host crate, because `option_env!` reads the
+/// environment of the crate being compiled. `None` gives a client that sends
+/// nothing.
+pub fn core_dsn(baked_core: Option<&str>, baked_legacy: Option<&str>) -> Option<String> {
+ first_non_blank([
+ std::env::var("OPENHUMAN_CORE_SENTRY_DSN").ok(),
+ std::env::var("OPENHUMAN_SENTRY_DSN").ok(),
+ baked_core.map(str::to_owned),
+ baked_legacy.map(str::to_owned),
+ ])
+}
+
+/// The deployment environment: `app_env` (the host's `OPENHUMAN_APP_ENV`,
+/// trimmed and lower-cased) when non-blank, else `development` for debug
+/// builds and `production` for release builds.
+pub fn resolve_environment(app_env: Option<String>) -> String {
+ if let Some(value) = app_env {
+ let trimmed = value.trim().to_ascii_lowercase();
+ if !trimmed.is_empty() {
+ return trimmed;
+ }
+ }
+ if cfg!(debug_assertions) {
+ "development".to_string()
+ } else {
+ "production".to_string()
+ }
+}
+
/// The signed-in user's id from the core's credential identity slot — the
/// CLI's fallback, and the default for [`SentryConfig::user_id`].
pub fn credential_user_id() -> Option<String> {
diff --git a/crates/openhuman-embed/src/process_sentry_tests.rs b/crates/openhuman-embed/src/process_sentry_tests.rs
index 447540ab71..46e489c62d 100644
--- a/crates/openhuman-embed/src/process_sentry_tests.rs
+++ b/crates/openhuman-embed/src/process_sentry_tests.rs
@@ -234,3 +234,32 @@ fn client_options_wire_the_chain_and_the_transport() {
let before_send = options.before_send.expect("chain installed");
assert!(before_send(event("Failed to request http://localhost:1/x: e", &[])).is_none());
}
+
+#[test]
+fn environment_prefers_app_env_lowercased() {
+ assert_eq!(resolve_environment(Some(" Staging ".into())), "staging");
+}
+
+#[test]
+fn environment_falls_back_on_blank_or_missing() {
+ let expected = if cfg!(debug_assertions) {
+ "development"
+ } else {
+ "production"
+ };
+ assert_eq!(resolve_environment(None), expected);
+ assert_eq!(resolve_environment(Some(" ".into())), expected);
+}
+
+#[test]
+fn core_dsn_falls_back_to_the_baked_values_in_order() {
+ // Only meaningful when the runtime variables are unset; otherwise the
+ // runtime value wins, which `first_non_blank` already covers.
+ if std::env::var_os("OPENHUMAN_CORE_SENTRY_DSN").is_none()
+ && std::env::var_os("OPENHUMAN_SENTRY_DSN").is_none()
+ {
+ assert_eq!(core_dsn(Some(" a "), Some("b")).as_deref(), Some("a"));
+ assert_eq!(core_dsn(None, Some("b")).as_deref(), Some("b"));
+ assert_eq!(core_dsn(Some(" "), None), None);
+ }
+}
diff --git a/crates/openhuman-tui/src/crash_reporting.rs b/crates/openhuman-tui/src/crash_reporting.rs
index 9f3c8a9360..630cf80138 100644
--- a/crates/openhuman-tui/src/crash_reporting.rs
+++ b/crates/openhuman-tui/src/crash_reporting.rs
@@ -20,9 +20,12 @@ pub fn init_crash_reporting() -> sentry::sdk::ClientInitGuard {
let _ = process::load_dotenv_for_cli();
let config = sentry::SentryConfig::new(
- sentry_dsn(),
+ sentry::core_dsn(
+ option_env!("OPENHUMAN_CORE_SENTRY_DSN"),
+ option_env!("OPENHUMAN_SENTRY_DSN"),
+ ),
build_release_tag(),
- resolve_environment(std::env::var("OPENHUMAN_APP_ENV").ok()),
+ sentry::resolve_environment(std::env::var("OPENHUMAN_APP_ENV").ok()),
);
sentry::sdk::init(sentry::client_options(config))
}
@@ -30,18 +33,6 @@ pub fn init_crash_reporting() -> sentry::sdk::ClientInitGuard {
#[cfg(not(feature = "crash-reporting"))]
pub fn init_crash_reporting() {}
-/// `OPENHUMAN_CORE_SENTRY_DSN`, then the legacy `OPENHUMAN_SENTRY_DSN`, at
-/// runtime and then baked in at compile time.
-#[cfg(feature = "crash-reporting")]
-fn sentry_dsn() -> Option<String> {
- sentry::first_non_blank([
- std::env::var("OPENHUMAN_CORE_SENTRY_DSN").ok(),
- std::env::var("OPENHUMAN_SENTRY_DSN").ok(),
- option_env!("OPENHUMAN_CORE_SENTRY_DSN").map(str::to_owned),
- option_env!("OPENHUMAN_SENTRY_DSN").map(str::to_owned),
- ])
-}
-
#[cfg(feature = "crash-reporting")]
fn build_release_tag() -> String {
sentry::release_tag(
@@ -50,21 +41,6 @@ fn build_release_tag() -> String {
)
}
-#[cfg(feature = "crash-reporting")]
-fn resolve_environment(app_env: Option<String>) -> String {
- if let Some(value) = app_env {
- let value = value.trim().to_ascii_lowercase();
- if !value.is_empty() {
- return value;
- }
- }
- if cfg!(debug_assertions) {
- "development".to_string()
- } else {
- "production".to_string()
- }
-}
-
#[cfg(all(test, feature = "crash-reporting"))]
#[path = "crash_reporting_tests.rs"]
mod tests;
diff --git a/crates/openhuman-tui/src/crash_reporting_tests.rs b/crates/openhuman-tui/src/crash_reporting_tests.rs
index 5677642e63..9914d6c528 100644
--- a/crates/openhuman-tui/src/crash_reporting_tests.rs
+++ b/crates/openhuman-tui/src/crash_reporting_tests.rs
@@ -13,14 +13,3 @@ fn release_tag_uses_the_package_version_without_a_build_sha() {
);
}
}
-
-#[test]
-fn environment_prefers_app_env_lowercased() {
- assert_eq!(resolve_environment(Some(" Staging ".into())), "staging");
- let fallback = if cfg!(debug_assertions) {
- "development"
- } else {
- "production"
- };
- assert_eq!(resolve_environment(Some(" ".into())), fallback);
-}