tracing_modality/common/
options.rs

1use auxon_sdk::api::AttrVal;
2use std::net::SocketAddr;
3
4/// Initialization options.
5#[derive(Clone)]
6pub struct Options {
7    pub(crate) auth: Option<Vec<u8>>,
8    pub(crate) metadata: Vec<(String, AttrVal)>,
9    pub(crate) server_addr: SocketAddr,
10}
11
12impl Options {
13    pub fn new() -> Options {
14        let auth = Self::resolve_auth_token();
15        let server_addr = ([127, 0, 0, 1], 14182).into();
16        Options {
17            auth,
18            metadata: Vec::new(),
19            server_addr,
20        }
21    }
22
23    fn resolve_auth_token() -> Option<Vec<u8>> {
24        if let Some(from_env) = std::env::var("MODALITY_AUTH_TOKEN")
25            .ok()
26            .and_then(|t| hex::decode(t).ok())
27        {
28            return Some(from_env);
29        }
30
31        dirs::config_dir()
32            .and_then(|config| {
33                let file_path = config.join("modality_cli").join(".user_auth_token");
34                std::fs::read_to_string(file_path).ok()
35            })
36            .and_then(|t| hex::decode(t.trim()).ok())
37    }
38
39    /// Set an auth token to be provided to modality. Tokens should be a hex stringish value.
40    pub fn set_auth<S: AsRef<[u8]>>(&mut self, auth: S) {
41        self.auth = hex::decode(auth).ok();
42    }
43    /// A chainable version of [set_auth](Self::set_auth).
44    pub fn with_auth<S: AsRef<[u8]>>(mut self, auth: S) -> Self {
45        self.auth = hex::decode(auth).ok();
46        self
47    }
48
49    /// Set the name for the root timeline. By default this will be the name of the main thread as
50    /// provided by the OS.
51    pub fn set_name<S: AsRef<str>>(&mut self, name: S) {
52        self.metadata.push((
53            "timeline.name".to_string(),
54            AttrVal::String(name.as_ref().to_string().into()),
55        ));
56    }
57    /// A chainable version of [set_name](Self::set_name).
58    pub fn with_name<S: AsRef<str>>(mut self, name: S) -> Self {
59        self.metadata.push((
60            "timeline.name".to_string(),
61            AttrVal::String(name.as_ref().to_string().into()),
62        ));
63        self
64    }
65
66    /// Add arbitrary metadata to the root timeline.
67    ///
68    /// This can be called multiple times.
69    pub fn add_metadata<K: AsRef<str>, V: Into<AttrVal>>(&mut self, key: K, value: V) {
70        let key = key.as_ref();
71        let key = if key.starts_with("timeline.") {
72            key.to_string()
73        } else {
74            format!("timeline.{}", key)
75        };
76
77        self.metadata.push((key, value.into()));
78    }
79    /// A chainable version of [add_metadata](Self::add_metadata).
80    pub fn with_metadata<K: AsRef<str>, V: Into<AttrVal>>(mut self, key: K, value: V) -> Self {
81        let key = key.as_ref();
82        let key = if key.starts_with("timeline.") {
83            key.to_string()
84        } else {
85            format!("timeline.{}", key)
86        };
87
88        self.metadata.push((key, value.into()));
89        self
90    }
91
92    /// Set the address of modalityd or a modality reflector where trace data should be sent.
93    ///
94    /// Defaults to `localhost:default_port`
95    pub fn set_server_address(&mut self, addr: SocketAddr) {
96        self.server_addr = addr;
97    }
98    /// A chainable version of [set_server_address](Self::set_server_address).
99    pub fn with_server_address(mut self, addr: SocketAddr) -> Self {
100        self.server_addr = addr;
101        self
102    }
103}
104
105impl Default for Options {
106    fn default() -> Options {
107        Options::new()
108    }
109}