#[inline]
pub fn applied_protocol(base: &str) -> String {
#[cfg(feature = "session-isolation")]
{
if let Some(id) = active_session_id() {
return format!("{base}/{id}");
}
}
base.to_owned()
}
#[cfg(feature = "session-isolation")]
fn active_session_id() -> Option<&'static str> {
use std::sync::OnceLock;
static CACHE: OnceLock<Option<String>> = OnceLock::new();
CACHE
.get_or_init(|| {
std::env::var("THESPAN_SESSION_ID")
.ok()
.filter(|s| !s.is_empty())
})
.as_deref()
}
#[cfg(test)]
mod tests {
use super::applied_protocol;
#[cfg(feature = "session-isolation")]
#[test]
fn applied_protocol_appends_session_id_when_env_set() {
unsafe { std::env::set_var("THESPAN_SESSION_ID", "test-isolated-42") };
let out = applied_protocol("/piying/messaging/1.0.0");
assert!(
out.starts_with("/piying/messaging/1.0.0/"),
"expected suffixed protocol id, got {out:?}",
);
}
#[cfg(not(feature = "session-isolation"))]
#[test]
fn applied_protocol_is_pass_through_without_feature() {
unsafe { std::env::set_var("THESPAN_SESSION_ID", "ignored-when-feature-off") };
let out = applied_protocol("/piying/messaging/1.0.0");
assert_eq!(out, "/piying/messaging/1.0.0");
}
}