Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(host_metrics source): add a new collector for tcp stats #22057

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -380,13 +380,20 @@ heim = { git = "https://github.com/vectordotdev/heim.git", branch = "update-nix"
# make sure to update the external docs when the Lua version changes
mlua = { version = "0.10.2", default-features = false, features = ["lua54", "send", "vendored", "macros"], optional = true }
sysinfo = "0.32.1"
byteorder = "1.5.0"

[target.'cfg(windows)'.dependencies]
windows-service = "0.7.0"

[target.'cfg(unix)'.dependencies]
nix = { version = "0.26.2", default-features = false, features = ["socket", "signal"] }

[target.'cfg(target_os = "linux")'.dependencies]
netlink-packet-utils = "0.5.2"
netlink-packet-sock-diag = "0.4.2"
netlink-packet-core = "0.7.0"
netlink-sys = { version = "0.8.7", features = ["tokio_socket"] }

[build-dependencies]
prost-build = { workspace = true, optional = true }
tonic-build = { workspace = true, optional = true }
Expand Down
4 changes: 4 additions & 0 deletions LICENSE-3rdparty.csv
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,10 @@ mongodb,https://github.com/mongodb/mongo-rust-driver,Apache-2.0,"Saghm Rossi <sa
multer,https://github.com/rousan/multer-rs,MIT,Rousan Ali <hello@rousan.io>
native-tls,https://github.com/sfackler/rust-native-tls,MIT OR Apache-2.0,Steven Fackler <sfackler@gmail.com>
ndk-context,https://github.com/rust-windowing/android-ndk-rs,MIT OR Apache-2.0,The Rust Windowing contributors
netlink-packet-core,https://github.com/rust-netlink/netlink-packet-core,MIT,Corentin Henry <corentinhenry@gmail.com>
netlink-packet-sock-diag,https://github.com/rust-netlink/netlink-packet-sock-diag,MIT,"Flier Lu <flier.lu@gmail.com>, Corentin Henry <corentinhenry@gmail.com>"
netlink-packet-utils,https://github.com/rust-netlink/netlink-packet-utils,MIT,Corentin Henry <corentinhenry@gmail.com>
netlink-sys,https://github.com/rust-netlink/netlink-sys,MIT,Corentin Henry <corentinhenry@gmail.com>
nibble_vec,https://github.com/michaelsproul/rust_nibble_vec,MIT,Michael Sproul <micsproul@gmail.com>
nix,https://github.com/nix-rust/nix,MIT,The nix-rust Project Developers
nkeys,https://github.com/wasmcloud/nkeys,Apache-2.0,wasmCloud Team
Expand Down
13 changes: 13 additions & 0 deletions changelog.d/21972-add-tcp-collector-host-metrics.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
The `host_metrics` source has a new collector, `tcp`. The `tcp`
collector exposes three metrics related to the TCP stack of the
system:
* `tcp_connections_total`: The total number of TCP connections. It
includes the `state` of the connection as a tag.
* `tcp_tx_queued_bytes_total`: The sum of the number of bytes in the
send queue across all connections.
* `tcp_rx_queued_bytes_total`: The sum of the number of bytes in the
receive queue across all connections.

This collector is enabled only on Linux systems.

authors: aryan9600
1 change: 1 addition & 0 deletions license-tool.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# `ring` has a custom license that is mostly "ISC-style" but parts of it also fall under OpenSSL licensing.
"ring-0.16.20" = { license = "ISC AND Custom" }
"ring-0.17.5" = { license = "ISC AND Custom" }
"ring-0.17.8" = { license = "ISC AND Custom" }

# `rustls-webpki` doesn't specify their license in the metadata, but the file contains the ISC terms.
"rustls-webpki-0.100.1" = { license = "ISC" }
Expand Down
28 changes: 28 additions & 0 deletions src/api/schema/metrics/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,26 @@ impl DiskMetrics {
}
}

pub struct TCPMetrics(Vec<Metric>);

#[Object]
impl TCPMetrics {
/// Total TCP connections
async fn tcp_conns_total(&self) -> f64 {
filter_host_metric(&self.0, "tcp_connections_total")
}

/// Total bytes in the send queue across all connections.
async fn tcp_tx_queued_bytes_total(&self) -> f64 {
filter_host_metric(&self.0, "tcp_tx_queued_bytes_total")
}

/// Total bytes in the receive queue across all connections.
async fn tcp_rx_queued_bytes_total(&self) -> f64 {
filter_host_metric(&self.0, "tcp_rx_queued_bytes_total")
}
}

pub struct HostMetrics(host_metrics::HostMetrics);

impl HostMetrics {
Expand Down Expand Up @@ -324,6 +344,14 @@ impl HostMetrics {
self.0.disk_metrics(&mut buffer).await;
DiskMetrics(buffer.metrics)
}

#[cfg(target_os = "linux")]
/// TCP metrics
async fn tcp(&self) -> TCPMetrics {
let mut buffer = self.0.buffer();
self.0.tcp_metrics(&mut buffer).await;
TCPMetrics(buffer.metrics)
}
}

/// Filters a [`Vec<Metric>`] by name, returning the inner `value` or 0.00 if not found
Expand Down
10 changes: 10 additions & 0 deletions src/sources/host_metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ mod filesystem;
mod memory;
mod network;
mod process;
#[cfg(target_os = "linux")]
mod tcp;

/// Collector types.
#[serde_as]
Expand Down Expand Up @@ -70,6 +72,9 @@ pub enum Collector {

/// Metrics related to network utilization.
Network,

/// Metrics related to TCP connections.
TCP,
}

/// Filtering configuration.
Expand Down Expand Up @@ -201,6 +206,7 @@ fn default_collectors() -> Option<Vec<Collector>> {
Collector::Memory,
Collector::Network,
Collector::Process,
Collector::TCP,
];

#[cfg(target_os = "linux")]
Expand Down Expand Up @@ -399,6 +405,10 @@ impl HostMetrics {
if self.config.has_collector(Collector::Network) {
self.network_metrics(&mut buffer).await;
}
#[cfg(target_os = "linux")]
if self.config.has_collector(Collector::TCP) {
self.tcp_metrics(&mut buffer).await;
}

let metrics = buffer.metrics;
self.events_received.emit(CountByteSize(
Expand Down
Loading