pub(crate) mod git;
#[cfg(feature = "host-details")]
mod host_details;
use std::env;
use std::path::PathBuf;
use flareapp_otlp::{KeyValue, Resource};
use crate::config::Config;
use crate::{SDK_NAME, SDK_VERSION};
const RUSTC_VERSION: &str = env!("FLARE_RUSTC_VERSION");
pub(crate) fn detect(config: &Config) -> Resource {
let executable_name = crate::process::executable_name();
let mut attributes = vec![
KeyValue::new("service.name", service_name(config, executable_name)),
KeyValue::new("telemetry.sdk.name", SDK_NAME),
KeyValue::new("telemetry.sdk.language", "rust"),
KeyValue::new("telemetry.sdk.version", SDK_VERSION),
KeyValue::new("process.runtime.name", "rustc"),
KeyValue::new("process.runtime.version", RUSTC_VERSION),
KeyValue::new("flare.language.name", "rust"),
KeyValue::new("flare.language.version", RUSTC_VERSION),
KeyValue::new("process.pid", i64::from(std::process::id())),
KeyValue::new("process.command_args", crate::command_args().to_vec()),
KeyValue::new(
"host.name",
gethostname::gethostname().to_string_lossy().into_owned(),
),
KeyValue::new("host.arch", host_arch(env::consts::ARCH)),
KeyValue::new("os.type", os_type(env::consts::OS)),
];
#[cfg(feature = "host-details")]
attributes.extend(host_details::attributes());
attributes.extend(optional_attributes(config));
attributes.extend(executable_attributes(executable_name));
Resource::new(attributes)
}
fn service_name(config: &Config, executable_name: Option<&str>) -> String {
config
.service_name
.clone()
.unwrap_or_else(|| match executable_name {
Some(name) => format!("unknown_service:{name}"),
None => "unknown_service".to_owned(),
})
}
fn optional_attributes(config: &Config) -> Vec<KeyValue> {
let mut attributes = Vec::new();
if let Some(version) = &config.service_version {
attributes.push(KeyValue::new("service.version", version.clone()));
}
if let Some(hash) = &config.git_hash {
attributes.push(KeyValue::new("git.hash", hash.clone()));
}
if let Some(environment) = &config.environment {
attributes.push(KeyValue::new(
"deployment.environment.name",
environment.clone(),
));
attributes.push(KeyValue::new("service.stage", environment.clone()));
}
if let Some((name, version)) = &config.framework {
attributes.extend(framework_attributes(name.clone(), version.clone()));
}
attributes
}
fn executable_attributes(executable_name: Option<&str>) -> Vec<KeyValue> {
let mut attributes = Vec::new();
if let Some(name) = executable_name {
attributes.push(KeyValue::new("process.executable.name", name.to_owned()));
}
if let Some(path) = env::current_exe().ok().map(PathBuf::into_os_string) {
attributes.push(KeyValue::new(
"process.executable.path",
path.to_string_lossy().into_owned(),
));
}
attributes
}
pub(crate) fn framework_attributes(name: String, version: Option<String>) -> Vec<KeyValue> {
let mut attributes = vec![KeyValue::new("flare.framework.name", name)];
if let Some(version) = version {
attributes.push(KeyValue::new("flare.framework.version", version));
}
attributes
}
fn host_arch(arch: &'static str) -> &'static str {
match arch {
"x86_64" => "amd64",
"aarch64" => "arm64",
"arm" => "arm32",
"x86" => "x86",
"powerpc" => "ppc32",
"powerpc64" => "ppc64",
other => other,
}
}
fn os_type(os: &'static str) -> &'static str {
match os {
"macos" => "darwin",
"dragonfly" => "dragonflybsd",
other => other,
}
}
#[cfg(test)]
mod tests {
use flareapp_otlp::AnyValue;
use super::*;
use crate::test_support::attribute;
fn value<'a>(resource: &'a Resource, key: &str) -> Option<&'a AnyValue> {
attribute(&resource.attributes, key)
}
fn string(value: &str) -> AnyValue {
AnyValue::String(value.to_owned())
}
#[test]
fn maps_architectures_to_semantic_conventions() {
assert_eq!(host_arch("x86_64"), "amd64");
assert_eq!(host_arch("aarch64"), "arm64");
assert_eq!(host_arch("riscv64"), "riscv64");
}
#[test]
fn maps_operating_systems_to_semantic_conventions() {
assert_eq!(os_type("macos"), "darwin");
assert_eq!(os_type("linux"), "linux");
assert_eq!(os_type("windows"), "windows");
}
#[test]
fn uses_the_configured_service() {
let config = Config::new()
.service_name("shop")
.service_version("1.2.3")
.environment("production");
let resource = detect(&config);
assert_eq!(value(&resource, "service.name"), Some(&string("shop")));
assert_eq!(value(&resource, "service.version"), Some(&string("1.2.3")));
assert_eq!(
value(&resource, "deployment.environment.name"),
Some(&string("production"))
);
assert_eq!(
value(&resource, "service.stage"),
Some(&string("production"))
);
}
#[test]
fn sends_the_git_hash() {
let resource = detect(&Config::new().git_hash(" 4f2a9c1e "));
assert_eq!(value(&resource, "git.hash"), Some(&string("4f2a9c1e")));
}
#[test]
fn leaves_out_a_blank_git_hash() {
let resource = detect(&Config::new().git_hash("4f2a9c1e").git_hash(""));
assert_eq!(value(&resource, "git.hash"), None);
}
#[test]
fn falls_back_to_unknown_service() {
let resource = detect(&Config::new());
let name = value(&resource, "service.name").and_then(AnyValue::as_str);
assert!(
name.is_some_and(|name| name.starts_with("unknown_service")),
"{name:?}"
);
}
#[test]
fn sends_no_service_version_without_a_version() {
let resource = detect(&Config::new());
assert_eq!(value(&resource, "service.version"), None);
}
const HOST_DETAILS: [&str; 4] = ["host.ip", "os.name", "os.version", "os.description"];
#[test]
fn always_sends_the_host_name_architecture_and_os_type() {
let resource = detect(&Config::new());
for key in ["host.name", "host.arch", "os.type"] {
assert!(value(&resource, key).is_some(), "{key} is missing");
}
}
#[cfg(not(feature = "host-details"))]
#[test]
fn leaves_out_host_details_without_the_feature() {
let resource = detect(&Config::new());
for key in HOST_DETAILS {
assert_eq!(value(&resource, key), None, "{key} is sent");
}
}
#[cfg(feature = "host-details")]
#[test]
fn sends_the_host_ip_as_one_string() {
let resource = detect(&Config::new());
let Some(AnyValue::String(ip)) = value(&resource, "host.ip") else {
panic!("host.ip is missing or not a string");
};
let ip: std::net::IpAddr = ip.parse().expect("host.ip is not an IP address");
assert!(!ip.is_loopback(), "host.ip is a loopback address");
}
#[cfg(feature = "host-details")]
#[test]
fn sends_no_empty_host_details() {
let resource = detect(&Config::new());
for key in HOST_DETAILS {
match value(&resource, key) {
None => {}
Some(AnyValue::String(text)) => assert!(!text.trim().is_empty(), "{key} is empty"),
Some(other) => panic!("{key} is not a string: {other:?}"),
}
}
}
#[test]
fn identifies_the_sdk() {
let resource = detect(&Config::new());
assert_eq!(
value(&resource, "telemetry.sdk.name"),
Some(&string("flareapp"))
);
assert_eq!(
value(&resource, "telemetry.sdk.language"),
Some(&string("rust"))
);
assert_eq!(
value(&resource, "process.runtime.name"),
Some(&string("rustc"))
);
assert_ne!(
value(&resource, "process.runtime.version"),
Some(&string("unknown"))
);
assert_eq!(
value(&resource, "flare.language.name"),
Some(&string("rust"))
);
assert_eq!(
value(&resource, "flare.language.version"),
value(&resource, "process.runtime.version")
);
}
}