use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use tracing::{Span, field};
pub fn hash_key(key: &str) -> u64 {
let mut hasher = DefaultHasher::new();
key.hash(&mut hasher);
hasher.finish()
}
pub fn add_hashed_key(key: &str) {
let hash = hash_key(key);
Span::current().record("secret_key_hash", field::display(hash));
}
#[macro_export]
macro_rules! instrument_secret_op {
(name = $name:expr) => {
#[tracing::instrument(
name = $name,
skip_all,
fields(
secret_op = $name,
secret_key_hash = tracing::field::Empty,
outcome = tracing::field::Empty
)
)]
};
}
pub fn record_success() {
Span::current().record("outcome", field::display("success"));
}
pub fn record_error(error: &crate::secret_provider::errors::SecretError) {
Span::current().record("outcome", field::display("error"));
match error {
crate::secret_provider::errors::SecretError::NotFound(_) => {
Span::current().record("error_type", field::display("not_found"));
}
crate::secret_provider::errors::SecretError::Backend(_) => {
Span::current().record("error_type", field::display("backend"));
}
crate::secret_provider::errors::SecretError::Configuration(_) => {
Span::current().record("error_type", field::display("configuration"));
}
crate::secret_provider::errors::SecretError::UnsupportedOperation(_) => {
Span::current().record("error_type", field::display("unsupported"));
}
}
}