From b004032e7b12a3c01e3416e2b742487e519ccfe9 Mon Sep 17 00:00:00 2001 From: Sanskar Jaiswal Date: Fri, 20 Dec 2024 23:57:50 +0530 Subject: [PATCH] chore(host_metrics): add linux config directive for tcp code Signed-off-by: Sanskar Jaiswal --- src/sources/host_metrics/mod.rs | 2 +- src/sources/host_metrics/network.rs | 48 +++++++++++++++++------------ 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/src/sources/host_metrics/mod.rs b/src/sources/host_metrics/mod.rs index 5f5da3da42acb1..1ffcb58eacd65a 100644 --- a/src/sources/host_metrics/mod.rs +++ b/src/sources/host_metrics/mod.rs @@ -33,7 +33,7 @@ mod cpu; mod disk; mod filesystem; mod memory; -// #[cfg(target_os = "linux")] +#[cfg(target_os = "linux")] mod netlink_tcp; mod network; mod process; diff --git a/src/sources/host_metrics/network.rs b/src/sources/host_metrics/network.rs index 60461e4880ca20..d2607b0acc7b51 100644 --- a/src/sources/host_metrics/network.rs +++ b/src/sources/host_metrics/network.rs @@ -9,6 +9,7 @@ use vector_lib::metric_tags; use crate::internal_events::HostMetricsScrapeDetailError; +#[cfg(target_os = "linux")] use super::netlink_tcp; use super::{ default_all_devices, example_devices, filter_result, FilterList, HostMetrics, MetricTags, @@ -102,6 +103,7 @@ impl HostMetrics { } } + #[cfg(target_os = "linux")] match netlink_tcp::build_tcp_stats().await { Ok(stats) => { output.name = "tcp"; @@ -192,26 +194,32 @@ mod tests { // Assert that the metrics buffer contains the TCP related metrics // and the network_tcp_connections_total has the "state" tag. - let mut n_tx_queued_bytes_metric = 0; - let mut n_rx_queued_bytes_metric = 0; - - metrics.iter().for_each(|metric| { - if metric.name() == NETWORK_TCP_CONNS_TOTAL { - let tags = metric.tags().unwrap(); - assert!( - tags.contains_key(TCP_CONN_STATE), - "Metric tcp_connections_total must have a mode tag" - ); - } else if metric.name() == NETWORK_TCP_TX_QUEUED_BYTES_TOTAL { - n_tx_queued_bytes_metric += 1; - } else if metric.name() == NETWORK_TCP_RX_QUEUED_BYTES_TOTAL { - n_rx_queued_bytes_metric += 1; - } else { - panic!("unrecognized metric name"); - } - }); - assert_eq!(n_tx_queued_bytes_metric, 1); - assert_eq!(n_rx_queued_bytes_metric, 1); + #[cfg(target_os = "linux")] + { + let mut n_tcp_conns_total_metric = 0; + let mut n_tx_queued_bytes_metric = 0; + let mut n_rx_queued_bytes_metric = 0; + + metrics.iter().for_each(|metric| { + if metric.name() == NETWORK_TCP_CONNS_TOTAL { + n_tcp_conns_total_metric += 1; + let tags = metric.tags().unwrap(); + assert!( + tags.contains_key(TCP_CONN_STATE), + "Metric tcp_connections_total must have a mode tag" + ); + } else if metric.name() == NETWORK_TCP_TX_QUEUED_BYTES_TOTAL { + n_tx_queued_bytes_metric += 1; + } else if metric.name() == NETWORK_TCP_RX_QUEUED_BYTES_TOTAL { + n_rx_queued_bytes_metric += 1; + } else { + return; + } + }); + assert_eq!(n_tcp_conns_total_metric, 1); + assert_eq!(n_tx_queued_bytes_metric, 1); + assert_eq!(n_rx_queued_bytes_metric, 1); + } } #[tokio::test]