kerniq_telemetry/
resource.rs1use opentelemetry::KeyValue;
5
6pub const SERVICE_NAME: &str = "service.name";
8pub const SERVICE_VERSION: &str = "service.version";
10pub const SERVICE_NAMESPACE: &str = "service.namespace";
12pub const SERVICE_INSTANCE_ID: &str = "service.instance.id";
14pub const DEPLOYMENT_ENVIRONMENT: &str = "deployment.environment.name";
16
17#[derive(Debug, Clone, Default)]
24pub struct ServiceInfo {
25 pub name: Option<String>,
27 pub version: Option<String>,
29 pub namespace: Option<String>,
31 pub environment: Option<String>,
33 pub instance_id: Option<String>,
36 pub attributes: Vec<KeyValue>,
38}
39
40impl ServiceInfo {
41 pub fn to_attributes(&self) -> Vec<KeyValue> {
44 let mut out = Vec::with_capacity(4 + self.attributes.len());
45 if let Some(v) = &self.version {
46 out.push(KeyValue::new(SERVICE_VERSION, v.clone()));
47 }
48 if let Some(v) = &self.namespace {
49 out.push(KeyValue::new(SERVICE_NAMESPACE, v.clone()));
50 }
51 if let Some(v) = &self.environment {
52 out.push(KeyValue::new(DEPLOYMENT_ENVIRONMENT, v.clone()));
53 }
54 if let Some(v) = &self.instance_id {
55 out.push(KeyValue::new(SERVICE_INSTANCE_ID, v.clone()));
56 }
57 out.extend(self.attributes.iter().cloned());
58 out
59 }
60}
61
62pub fn default_instance_id() -> String {
65 let host = std::env::var("HOSTNAME")
66 .or_else(|_| std::env::var("COMPUTERNAME"))
67 .unwrap_or_else(|_| "localhost".to_owned());
68 let started = std::time::SystemTime::now()
69 .duration_since(std::time::UNIX_EPOCH)
70 .map(|d| d.as_millis())
71 .unwrap_or_default();
72 format!("{host}-{}-{started:x}", std::process::id())
73}
74
75#[cfg(test)]
76mod tests {
77 use super::*;
78
79 #[test]
80 fn attributes() {
81 let info = ServiceInfo {
82 name: Some("payment-api".into()),
83 version: Some("1.0.0".into()),
84 environment: Some("production".into()),
85 attributes: vec![KeyValue::new("cloud.region", "eu-west-1")],
86 ..ServiceInfo::default()
87 };
88 let attrs = info.to_attributes();
89 let keys: Vec<_> = attrs.iter().map(|kv| kv.key.as_str()).collect();
90 assert_eq!(
91 keys,
92 [SERVICE_VERSION, DEPLOYMENT_ENVIRONMENT, "cloud.region"]
93 );
94 }
95
96 #[test]
97 fn instance_id_contains_pid() {
98 assert!(default_instance_id().contains(&std::process::id().to_string()));
99 }
100}