Base: codex-main originale (prima del fork aKode)
This commit is contained in:
45
codex-rs/otel/src/auth_storage/originator.rs
Normal file
45
codex-rs/otel/src/auth_storage/originator.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
//! Bounded attribution for auth-storage observations, scoped to one task.
|
||||
//! Spawned tasks must explicitly carry this context across the task boundary.
|
||||
|
||||
use std::future::Future;
|
||||
|
||||
tokio::task_local! {
|
||||
static CURRENT: AuthStorageOriginator;
|
||||
}
|
||||
|
||||
/// A bounded client identity used only by credential-storage telemetry.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct AuthStorageOriginator(&'static str);
|
||||
|
||||
impl AuthStorageOriginator {
|
||||
pub fn from_client_name(name: &str) -> Self {
|
||||
let name = match name {
|
||||
"Codex Desktop" => "codex_desktop",
|
||||
_ => name,
|
||||
};
|
||||
Self(crate::bounded_originator_tag_value(name))
|
||||
}
|
||||
|
||||
/// Unattributed work must not borrow another connection's identity.
|
||||
pub fn current() -> Self {
|
||||
CURRENT
|
||||
.try_with(|originator| *originator)
|
||||
.unwrap_or(Self("none"))
|
||||
}
|
||||
|
||||
pub fn as_str(self) -> &'static str {
|
||||
self.0
|
||||
}
|
||||
|
||||
pub async fn scope<F: Future>(self, future: F) -> F::Output {
|
||||
CURRENT.scope(self, future).await
|
||||
}
|
||||
|
||||
pub fn sync_scope<R>(self, operation: impl FnOnce() -> R) -> R {
|
||||
CURRENT.sync_scope(self, operation)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "originator_tests.rs"]
|
||||
mod tests;
|
||||
49
codex-rs/otel/src/auth_storage/originator_tests.rs
Normal file
49
codex-rs/otel/src/auth_storage/originator_tests.rs
Normal file
@@ -0,0 +1,49 @@
|
||||
//! Verifies attribution isolation and explicit propagation across task boundaries.
|
||||
|
||||
use super::AuthStorageOriginator;
|
||||
use pretty_assertions::assert_eq;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::Barrier;
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn concurrent_clients_keep_their_originator_across_awaits_and_blocking_work() {
|
||||
let barrier = Arc::new(Barrier::new(/*n*/ 3));
|
||||
let mut tasks = Vec::new();
|
||||
for name in ["Codex Desktop", "codex_vscode", "private-client-name"] {
|
||||
let barrier = Arc::clone(&barrier);
|
||||
let originator = AuthStorageOriginator::from_client_name(name);
|
||||
tasks.push(tokio::spawn(originator.scope(async move {
|
||||
barrier.wait().await;
|
||||
tokio::task::yield_now().await;
|
||||
let before = AuthStorageOriginator::current();
|
||||
let nested = AuthStorageOriginator::from_client_name("codex_cli_rs")
|
||||
.scope(async {
|
||||
tokio::task::yield_now().await;
|
||||
AuthStorageOriginator::current().as_str()
|
||||
})
|
||||
.await;
|
||||
let blocking = tokio::task::spawn_blocking(move || {
|
||||
before.sync_scope(AuthStorageOriginator::current)
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
(
|
||||
before.as_str(),
|
||||
nested,
|
||||
blocking.as_str(),
|
||||
AuthStorageOriginator::current().as_str(),
|
||||
)
|
||||
})));
|
||||
}
|
||||
let mut actual = Vec::new();
|
||||
for task in tasks {
|
||||
actual.push(task.await.unwrap());
|
||||
}
|
||||
assert_eq!(
|
||||
actual,
|
||||
["codex_desktop", "codex_vscode", "other"]
|
||||
.map(|originator| (originator, "codex_cli_rs", originator, originator))
|
||||
.to_vec()
|
||||
);
|
||||
assert_eq!(AuthStorageOriginator::current().as_str(), "none");
|
||||
}
|
||||
Reference in New Issue
Block a user