provide_telemetry/
consent.rs1use std::sync::{Mutex, OnceLock};
7
8#[derive(Clone, Copy, Debug, PartialEq, Eq)]
9pub enum ConsentLevel {
10 Full,
11 Functional,
12 Minimal,
13 None,
14}
15
16static CONSENT_LEVEL: OnceLock<Mutex<ConsentLevel>> = OnceLock::new();
17
18fn consent_level() -> &'static Mutex<ConsentLevel> {
19 CONSENT_LEVEL.get_or_init(|| Mutex::new(ConsentLevel::Full))
20}
21
22pub fn set_consent_level(level: ConsentLevel) {
23 *consent_level().lock().expect("consent lock poisoned") = level;
24}
25
26pub fn get_consent_level() -> ConsentLevel {
27 *consent_level().lock().expect("consent lock poisoned")
28}
29
30fn log_level_order(level: Option<&str>) -> usize {
31 match level.unwrap_or_default().to_ascii_uppercase().as_str() {
32 "TRACE" => 0,
33 "DEBUG" => 1,
34 "INFO" => 2,
35 "WARNING" | "WARN" => 3,
36 "ERROR" => 4,
37 "CRITICAL" => 5,
38 _ => 0,
39 }
40}
41
42pub fn should_allow(signal: &str, log_level: Option<&str>) -> bool {
43 match get_consent_level() {
44 ConsentLevel::Full => true,
45 ConsentLevel::None => false,
46 ConsentLevel::Functional => match signal {
47 "logs" => log_level_order(log_level) >= 3,
48 "context" => false,
49 _ => true,
50 },
51 ConsentLevel::Minimal => match signal {
52 "logs" => log_level_order(log_level) >= 4,
53 _ => false,
54 },
55 }
56}
57
58pub fn reset_consent_for_tests() {
59 set_consent_level(ConsentLevel::Full);
60}