Skip to content

Commit 465deca

Browse files
committed
refactor: Use BTreeMap for deterministic JSON key ordering
HashMap produces non-deterministic JSON output, making it hard to diff containerdebug output across runs. BTreeMap sorts keys consistently.
1 parent eb39937 commit 465deca

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/system_information/network.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use local_ip_address::list_afinet_netifas;
55
use serde::Serialize;
66
use snafu::{ResultExt, Snafu};
77
use std::{
8-
collections::{BTreeSet, HashMap},
8+
collections::{BTreeMap, BTreeSet},
99
net::IpAddr,
1010
sync::LazyLock,
1111
time::Duration,
@@ -42,16 +42,16 @@ static GLOBAL_DNS_RESOLVER: LazyLock<Option<TokioResolver>> = LazyLock::new(|| {
4242
/// and the results of reverse and forward DNS lookups.
4343
#[derive(Debug, Serialize)]
4444
pub struct SystemNetworkInfo {
45-
pub interfaces: HashMap<String, Vec<IpAddr>>,
46-
pub reverse_lookups: HashMap<IpAddr, Vec<String>>,
47-
pub forward_lookups: HashMap<String, Vec<IpAddr>>,
45+
pub interfaces: BTreeMap<String, Vec<IpAddr>>,
46+
pub reverse_lookups: BTreeMap<IpAddr, Vec<String>>,
47+
pub forward_lookups: BTreeMap<String, Vec<IpAddr>>,
4848
}
4949

5050
impl SystemNetworkInfo {
5151
#[tracing::instrument(name = "SystemNetworkInfo::collect")]
5252
pub async fn collect() -> Result<SystemNetworkInfo, Error> {
5353
let netifs = list_afinet_netifas().context(ListInterfacesSnafu)?;
54-
let mut interfaces = HashMap::new();
54+
let mut interfaces = BTreeMap::new();
5555

5656
// Iterate over the network interfaces and group them by name
5757
// An interface may appear multiple times if it has multiple IP addresses (e.g. IPv4 and IPv6)
@@ -73,16 +73,16 @@ impl SystemNetworkInfo {
7373
let Some(resolver) = GLOBAL_DNS_RESOLVER.as_ref() else {
7474
return Ok(SystemNetworkInfo {
7575
interfaces,
76-
reverse_lookups: HashMap::new(),
77-
forward_lookups: HashMap::new(),
76+
reverse_lookups: BTreeMap::new(),
77+
forward_lookups: BTreeMap::new(),
7878
});
7979
};
8080

8181
let mut reverse_lookup_tasks = JoinSet::new();
8282
for ip in ips {
8383
reverse_lookup_tasks.spawn(async move { (ip, resolver.reverse_lookup(ip).await) });
8484
}
85-
let reverse_lookups: HashMap<IpAddr, Vec<String>> = reverse_lookup_tasks
85+
let reverse_lookups: BTreeMap<IpAddr, Vec<String>> = reverse_lookup_tasks
8686
.join_all()
8787
.await
8888
.into_iter()
@@ -114,7 +114,7 @@ impl SystemNetworkInfo {
114114
forward_lookup_tasks
115115
.spawn(async move { (hostname.clone(), resolver.lookup_ip(hostname).await) });
116116
}
117-
let forward_lookups: HashMap<String, Vec<IpAddr>> = forward_lookup_tasks
117+
let forward_lookups: BTreeMap<String, Vec<IpAddr>> = forward_lookup_tasks
118118
.join_all()
119119
.await
120120
.into_iter()

0 commit comments

Comments
 (0)