Skip to content

Commit eb39937

Browse files
committed
fix: Wrap network collector in ComponentResult for consistent error handling
The network collector silently swallowed interface listing errors by returning empty data. Now it returns Result so the orchestrator wraps it in ComponentResult, matching the pattern used by other fallible collectors. Errors appear in JSON output instead of being silently lost.
1 parent aedcc1d commit eb39937

File tree

2 files changed

+32
-33
lines changed

2 files changed

+32
-33
lines changed

src/system_information/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub struct SystemInformation {
1515
pub os: Option<os::OperatingSystem>,
1616
pub current_user: Option<ComponentResult<user::User>>,
1717
pub disks: Option<Vec<disk::Disk>>,
18-
pub network: Option<network::SystemNetworkInfo>,
18+
pub network: Option<ComponentResult<network::SystemNetworkInfo>>,
1919
// TODO:
2020
// Current time
2121
// SElinux/AppArmor
@@ -70,7 +70,10 @@ impl SystemInformation {
7070
user::User::collect_current(&ctx.system),
7171
)),
7272
disks: Some(disk::Disk::collect_all()),
73-
network: Some(network::SystemNetworkInfo::collect().await),
73+
network: Some(ComponentResult::report_from_result(
74+
"SystemNetworkInfo::collect",
75+
network::SystemNetworkInfo::collect().await,
76+
)),
7477
// ..Default::default()
7578
};
7679

src/system_information/network.rs

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use hickory_resolver::{
33
};
44
use local_ip_address::list_afinet_netifas;
55
use serde::Serialize;
6+
use snafu::{ResultExt, Snafu};
67
use std::{
78
collections::{BTreeSet, HashMap},
89
net::IpAddr,
@@ -11,6 +12,12 @@ use std::{
1112
};
1213
use tokio::task::JoinSet;
1314

15+
#[derive(Debug, Snafu)]
16+
pub enum Error {
17+
#[snafu(display("failed to list network interfaces"))]
18+
ListInterfaces { source: local_ip_address::Error },
19+
}
20+
1421
static GLOBAL_DNS_RESOLVER: LazyLock<Option<TokioResolver>> = LazyLock::new(|| {
1522
let (resolver_config, mut resolver_opts) = match read_system_conf() {
1623
Ok(conf) => conf,
@@ -42,44 +49,33 @@ pub struct SystemNetworkInfo {
4249

4350
impl SystemNetworkInfo {
4451
#[tracing::instrument(name = "SystemNetworkInfo::collect")]
45-
pub async fn collect() -> SystemNetworkInfo {
46-
let interfaces = match list_afinet_netifas() {
47-
Ok(netifs) => {
48-
let mut interface_map = std::collections::HashMap::new();
52+
pub async fn collect() -> Result<SystemNetworkInfo, Error> {
53+
let netifs = list_afinet_netifas().context(ListInterfacesSnafu)?;
54+
let mut interfaces = HashMap::new();
4955

50-
// Iterate over the network interfaces and group them by name
51-
// An interface may appear multiple times if it has multiple IP addresses (e.g. IPv4 and IPv6)
52-
for (name, ip_addr) in netifs {
53-
tracing::info!(
54-
network.interface.name = name,
55-
network.interface.address = %ip_addr,
56-
"found network interface"
57-
);
58-
interface_map
59-
.entry(name)
60-
.or_insert_with(Vec::new)
61-
.push(ip_addr);
62-
}
63-
interface_map
64-
}
65-
Err(error) => {
66-
tracing::error!(
67-
error = &error as &dyn std::error::Error,
68-
"failed to list network interfaces"
69-
);
70-
HashMap::new()
71-
}
72-
};
56+
// Iterate over the network interfaces and group them by name
57+
// An interface may appear multiple times if it has multiple IP addresses (e.g. IPv4 and IPv6)
58+
for (name, ip_addr) in netifs {
59+
tracing::info!(
60+
network.interface.name = name,
61+
network.interface.address = %ip_addr,
62+
"found network interface"
63+
);
64+
interfaces
65+
.entry(name)
66+
.or_insert_with(Vec::new)
67+
.push(ip_addr);
68+
}
7369

7470
let ips: BTreeSet<IpAddr> = interfaces.values().flatten().copied().collect();
7571
tracing::info!(network.addresses.ip = ?ips, "ip addresses");
7672

7773
let Some(resolver) = GLOBAL_DNS_RESOLVER.as_ref() else {
78-
return SystemNetworkInfo {
74+
return Ok(SystemNetworkInfo {
7975
interfaces,
8076
reverse_lookups: HashMap::new(),
8177
forward_lookups: HashMap::new(),
82-
};
78+
});
8379
};
8480

8581
let mut reverse_lookup_tasks = JoinSet::new();
@@ -139,10 +135,10 @@ impl SystemNetworkInfo {
139135
})
140136
.collect();
141137

142-
SystemNetworkInfo {
138+
Ok(SystemNetworkInfo {
143139
interfaces,
144140
reverse_lookups,
145141
forward_lookups,
146-
}
142+
})
147143
}
148144
}

0 commit comments

Comments
 (0)