Create Structure for Querying Stats from Docker Socket

This commit is contained in:
2023-09-11 18:46:12 -03:00
parent a787bb36ee
commit 2b1fde68bd
12 changed files with 532 additions and 19 deletions

336
Cargo.lock generated
View File

@@ -136,7 +136,7 @@ checksum = "3b829e4e32b91e643de6eafe82b1d90675f5874230191a4ffbc1b336dec4d6bf"
dependencies = [
"async-trait",
"axum-core",
"bitflags",
"bitflags 1.3.2",
"bytes",
"futures-util",
"http",
@@ -210,6 +210,12 @@ version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bitflags"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635"
[[package]]
name = "bitvec"
version = "1.0.1"
@@ -398,6 +404,22 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
[[package]]
name = "core-foundation"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "194a7a9e6de53fa55116934067c844d9d749312f75c6f6d0980e8c252f8c2146"
dependencies = [
"core-foundation-sys",
"libc",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa"
[[package]]
name = "cpufeatures"
version = "0.2.9"
@@ -437,12 +459,48 @@ dependencies = [
"subtle",
]
[[package]]
name = "encoding_rs"
version = "0.8.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7268b386296a025e474d5140678f75d6de9493ae55a5d709eeb9dd08149945e1"
dependencies = [
"cfg-if",
]
[[package]]
name = "errno"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "136526188508e25c6fef639d7927dfb3e0e3084488bf202267829cf7fc23dbdd"
dependencies = [
"errno-dragonfly",
"libc",
"windows-sys",
]
[[package]]
name = "errno-dragonfly"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf"
dependencies = [
"cc",
"libc",
]
[[package]]
name = "fallible-iterator"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7"
[[package]]
name = "fastrand"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6999dc1837253364c2ebb0704ba97994bd874e8f195d665c50b7548f6ea92764"
[[package]]
name = "finl_unicode"
version = "1.2.0"
@@ -465,6 +523,21 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "foreign-types"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1"
dependencies = [
"foreign-types-shared",
]
[[package]]
name = "foreign-types-shared"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
[[package]]
name = "form_urlencoded"
version = "1.2.0"
@@ -684,6 +757,29 @@ dependencies = [
"want",
]
[[package]]
name = "hyper-tls"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905"
dependencies = [
"bytes",
"hyper",
"native-tls",
"tokio",
"tokio-native-tls",
]
[[package]]
name = "idna"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d20d6b07bfbc108882d88ed8e37d39636dcc260e15e30c45e6ba089610b917c"
dependencies = [
"unicode-bidi",
"unicode-normalization",
]
[[package]]
name = "indexmap"
version = "1.9.3"
@@ -694,6 +790,12 @@ dependencies = [
"hashbrown",
]
[[package]]
name = "ipnet"
version = "2.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28b29a3cd74f0f4598934efe3aeba42bae0eb4680554128851ebbecb02af14e6"
[[package]]
name = "iri-string"
version = "0.4.1"
@@ -730,6 +832,12 @@ version = "0.2.147"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3"
[[package]]
name = "linux-raw-sys"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a9bad9f94746442c783ca431b22403b519cd7fbeed0533fdd6328b2f2212128"
[[package]]
name = "lock_api"
version = "0.4.10"
@@ -809,6 +917,24 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "native-tls"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07226173c32f2926027b63cce4bcd8076c3552846cbe7925f3aaffeac0a3b92e"
dependencies = [
"lazy_static",
"libc",
"log",
"openssl",
"openssl-probe",
"openssl-sys",
"schannel",
"security-framework",
"security-framework-sys",
"tempfile",
]
[[package]]
name = "nom"
version = "7.1.3"
@@ -853,6 +979,50 @@ version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
[[package]]
name = "openssl"
version = "0.10.57"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bac25ee399abb46215765b1cb35bc0212377e58a061560d8b29b024fd0430e7c"
dependencies = [
"bitflags 2.4.0",
"cfg-if",
"foreign-types",
"libc",
"once_cell",
"openssl-macros",
"openssl-sys",
]
[[package]]
name = "openssl-macros"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.32",
]
[[package]]
name = "openssl-probe"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf"
[[package]]
name = "openssl-sys"
version = "0.9.93"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db4d56a4c0478783083cfafcc42493dd4a981d41669da64b4572a2a089b51b1d"
dependencies = [
"cc",
"libc",
"pkg-config",
"vcpkg",
]
[[package]]
name = "parking_lot"
version = "0.12.1"
@@ -932,6 +1102,12 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "pkg-config"
version = "0.3.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964"
[[package]]
name = "postgres"
version = "0.19.7"
@@ -1091,7 +1267,7 @@ version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "567664f262709473930a4bf9e51bf2ebf3348f2e748ccc50dea20646858f8f29"
dependencies = [
"bitflags",
"bitflags 1.3.2",
]
[[package]]
@@ -1103,6 +1279,43 @@ dependencies = [
"bytecheck",
]
[[package]]
name = "reqwest"
version = "0.11.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3e9ad3fe7488d7e34558a2033d45a0c90b72d97b4f80705666fea71472e2e6a1"
dependencies = [
"base64 0.21.4",
"bytes",
"encoding_rs",
"futures-core",
"futures-util",
"h2",
"http",
"http-body",
"hyper",
"hyper-tls",
"ipnet",
"js-sys",
"log",
"mime",
"native-tls",
"once_cell",
"percent-encoding",
"pin-project-lite",
"serde",
"serde_json",
"serde_urlencoded",
"tokio",
"tokio-native-tls",
"tower-service",
"url",
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"winreg",
]
[[package]]
name = "rkyv"
version = "0.7.42"
@@ -1164,6 +1377,19 @@ version = "0.1.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76"
[[package]]
name = "rustix"
version = "0.38.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7db8590df6dfcd144d22afd1b83b36c21a18d7cbc1dc4bb5295a8712e9eb662"
dependencies = [
"bitflags 2.4.0",
"errno",
"libc",
"linux-raw-sys",
"windows-sys",
]
[[package]]
name = "rustversion"
version = "1.0.14"
@@ -1176,6 +1402,15 @@ version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741"
[[package]]
name = "schannel"
version = "0.1.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c3733bf4cf7ea0880754e19cb5a462007c4a8c1914bff372ccc95b464f1df88"
dependencies = [
"windows-sys",
]
[[package]]
name = "scopeguard"
version = "1.2.0"
@@ -1188,6 +1423,29 @@ version = "4.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c107b6f4780854c8b126e228ea8869f4d7b71260f962fefb57b996b8959ba6b"
[[package]]
name = "security-framework"
version = "2.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05b64fb303737d99b81884b2c63433e9ae28abebe5eb5045dcdd175dc2ecf4de"
dependencies = [
"bitflags 1.3.2",
"core-foundation",
"core-foundation-sys",
"libc",
"security-framework-sys",
]
[[package]]
name = "security-framework-sys"
version = "2.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e932934257d3b408ed8f30db49d85ea163bfe74961f017f405b025af298f0c7a"
dependencies = [
"core-foundation-sys",
"libc",
]
[[package]]
name = "serde"
version = "1.0.188"
@@ -1245,20 +1503,24 @@ dependencies = [
name = "server_metrics"
version = "0.1.0"
dependencies = [
"async-trait",
"axum",
"clap",
"http",
"http-body",
"hyper",
"prometheus",
"reqwest",
"rust_decimal",
"rust_decimal_macros",
"serde",
"serde_json",
"slog",
"thiserror",
"tokio",
"tower",
"tower-http",
"url",
]
[[package]]
@@ -1302,6 +1564,12 @@ dependencies = [
"autocfg",
]
[[package]]
name = "slog"
version = "2.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8347046d4ebd943127157b94d63abb990fcf729dc4e9978927fdf4ac3c998d06"
[[package]]
name = "smallvec"
version = "1.11.0"
@@ -1385,6 +1653,19 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369"
[[package]]
name = "tempfile"
version = "3.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb94d2f3cc536af71caac6b6fcebf65860b347e7ce0cc9ebe8f70d3e521054ef"
dependencies = [
"cfg-if",
"fastrand",
"redox_syscall",
"rustix",
"windows-sys",
]
[[package]]
name = "thiserror"
version = "1.0.48"
@@ -1450,6 +1731,16 @@ dependencies = [
"syn 2.0.32",
]
[[package]]
name = "tokio-native-tls"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2"
dependencies = [
"native-tls",
"tokio",
]
[[package]]
name = "tokio-postgres"
version = "0.7.10"
@@ -1528,7 +1819,7 @@ checksum = "f873044bf02dd1e8239e9c1293ea39dad76dc594ec16185d0a1bf31d8dc8d858"
dependencies = [
"async-compression",
"base64 0.13.1",
"bitflags",
"bitflags 1.3.2",
"bytes",
"futures-core",
"futures-util",
@@ -1625,6 +1916,17 @@ dependencies = [
"tinyvec",
]
[[package]]
name = "url"
version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "143b538f18257fac9cad154828a57c6bf5157e1aa604d4816b5995bf6de87ae5"
dependencies = [
"form_urlencoded",
"idna",
"percent-encoding",
]
[[package]]
name = "utf8parse"
version = "0.2.1"
@@ -1640,6 +1942,12 @@ dependencies = [
"getrandom",
]
[[package]]
name = "vcpkg"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]]
name = "version_check"
version = "0.9.4"
@@ -1686,6 +1994,18 @@ dependencies = [
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-futures"
version = "0.4.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c02dbc21516f9f1f04f187958890d7e6026df8d16540b7ad9492bc34a67cea03"
dependencies = [
"cfg-if",
"js-sys",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.87"
@@ -1823,6 +2143,16 @@ version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
[[package]]
name = "winreg"
version = "0.50.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "524e57b2c537c0f9b1e69f1965311ec12182b4122e45035b1508cd24d2adadb1"
dependencies = [
"cfg-if",
"windows-sys",
]
[[package]]
name = "wyz"
version = "0.5.1"

View File

@@ -6,17 +6,21 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
async-trait = "0.1.61"
axum = "0.6.7"
clap = { version = "4.4.2", features = ["derive"] }
http = "0.2.8"
http-body = "0.4.5"
hyper = { version = "0.14.24", features = ["full"] }
prometheus = "0.13.3"
reqwest = { version = "0.11.14", features = ["json"] }
rust_decimal = { version = "1.29.1", features = ["serde", "db-postgres"] }
rust_decimal_macros = "1.27"
serde = { version = "1.0.152", features = ["derive"] }
serde_json = "1.0.91"
slog = "2.7.0"
thiserror = "1.0.38"
tokio = { version = "1.28.2", features = ["full"] }
tower = { version = "0.4.13", features = ["full"] }
tower-http = { version = "0.3.5", features = ["full"] }
tower-http = { version = "0.3.5", features = ["full"] }
url = "2.3.1"

View File

@@ -3,6 +3,8 @@ use serde::Deserialize;
pub mod collector;
pub mod metrics;
pub mod realm;
mod util;
#[derive(Debug, Deserialize)]

View File

@@ -0,0 +1,9 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GetContainersRequest {}
#[derive(Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GetContainersResponse {}

View File

@@ -0,0 +1,22 @@
use crate::{
docker::realm::{
containers::{
get_containers::{GetContainersRequest, GetContainersResponse},
DockerClientContainersTrait,
},
DockerClient,
},
error::SystemExporterResult,
};
use async_trait::async_trait;
#[async_trait]
impl DockerClientContainersTrait for DockerClient {
async fn get_containers(
&self,
req: GetContainersRequest,
) -> SystemExporterResult<GetContainersResponse> {
self.send_request(http::Method::GET, "containers/json?all=true", req)
.await
}
}

View File

@@ -0,0 +1,16 @@
use crate::{
docker::realm::containers::get_containers::{GetContainersRequest, GetContainersResponse},
error::SystemExporterResult,
};
use async_trait::async_trait;
pub mod get_containers;
mod implementation;
#[async_trait]
pub trait DockerClientContainersTrait {
async fn get_containers(
&self,
req: GetContainersRequest,
) -> SystemExporterResult<GetContainersResponse>;
}

65
src/docker/realm/mod.rs Normal file
View File

@@ -0,0 +1,65 @@
use crate::{
docker::realm::{containers::DockerClientContainersTrait, stats::DockerClientStatsTrait},
error::SystemExporterResult,
};
use async_trait::async_trait;
use http::Method;
use serde::{de::DeserializeOwned, Serialize};
use url::Url;
pub mod containers;
pub mod stats;
#[derive(Clone)]
pub struct DockerClient {
pub config: DockerClientConfiguration,
}
#[derive(Clone)]
pub struct DockerClientConfiguration {
pub socket_path: String,
}
impl DockerClient {
pub fn from_config(config: DockerClientConfiguration) -> Self {
DockerClient { config }
}
// todo(steve.sampson): seems reqwest doesn't support unix sockets yet... find another way
pub async fn send_request<T, U>(
&self,
method: Method,
url: &str,
body: U,
) -> SystemExporterResult<T>
where
T: DeserializeOwned,
U: Serialize,
{
let mut res = reqwest::Client::new()
.request(
method,
Url::parse(format!("unix://localhost/{url}").as_str())?,
)
.body(serde_json::to_string(&body)?)
.header("X-Unix-Socket", self.config.socket_path.as_str())
.header(reqwest::header::CONTENT_TYPE, "application/json");
let res = res.send().await?;
let json_value: serde_json::Value = res.json().await?;
println!("HERE: {}", json_value.to_string());
panic!("")
// match serde_json::from_value(json_value.clone()) {
// Ok(value) => Ok(value),
// Err(_) => Err(ExchangeClientError::RemoteServerError(
// serde_json::from_value(json_value)?,
// )),
// }
}
}
#[async_trait]
pub trait DockerClientTrait: DockerClientStatsTrait + DockerClientContainersTrait {}

View File

@@ -0,0 +1,11 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GetContainerStatsRequest {
pub id: String,
}
#[derive(Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GetContainerStatsResponse {}

View File

@@ -0,0 +1,21 @@
use crate::{
docker::realm::{
stats::{
get_stats::{GetContainerStatsRequest, GetContainerStatsResponse},
DockerClientStatsTrait,
},
DockerClient,
},
error::SystemExporterResult,
};
use async_trait::async_trait;
#[async_trait]
impl DockerClientStatsTrait for DockerClient {
async fn get_container_stats(
&self,
req: GetContainerStatsRequest,
) -> SystemExporterResult<GetContainerStatsResponse> {
todo!()
}
}

View File

@@ -0,0 +1,16 @@
use crate::{
docker::realm::stats::get_stats::{GetContainerStatsRequest, GetContainerStatsResponse},
error::SystemExporterResult,
};
use async_trait::async_trait;
pub mod get_stats;
mod implementation;
#[async_trait]
pub trait DockerClientStatsTrait {
async fn get_container_stats(
&self,
req: GetContainerStatsRequest,
) -> SystemExporterResult<GetContainerStatsResponse>;
}

View File

@@ -10,6 +10,10 @@ pub enum SystemExporterError {
Prometheus(#[from] prometheus::Error),
#[error(transparent)]
AddrParse(#[from] std::net::AddrParseError),
#[error(transparent)]
UrlParse(#[from] url::ParseError),
#[error(transparent)]
Reqwest(#[from] reqwest::Error),
}
pub type SystemExporterResult<T, E = SystemExporterError> = Result<T, E>;

View File

@@ -1,8 +1,12 @@
use crate::{error::SystemExporterResult, metrics::Metrics};
use crate::docker::realm::containers::get_containers::GetContainersRequest;
use crate::docker::realm::containers::DockerClientContainersTrait;
use crate::docker::realm::{DockerClient, DockerClientConfiguration};
use crate::{
docker::collector::DockerCollector, error::SystemExporterResult, metrics::Metrics,
server::Server,
};
use clap::Parser;
use std::{sync::Arc, time::Duration};
use crate::docker::collector::DockerCollector;
use crate::server::Server;
mod docker;
mod error;
@@ -28,18 +32,27 @@ pub struct Args {
#[tokio::main]
async fn main() -> SystemExporterResult<()> {
let args = Args::parse();
let metrics = Arc::new(Metrics::new(args.metrics_prefix.as_str())?);
let server = Server::new(&args, metrics.clone())?;
let collector = DockerCollector::new(metrics);
tokio::spawn(async move {
loop {
let _ = collector.poll();
tokio::time::sleep(Duration::from_secs(args.docker_stats_seconds)).await;
}
let client = DockerClient::from_config(DockerClientConfiguration {
socket_path: "/Users/stev/.docker/run/docker.sock".to_string(),
});
server.serve().await
let res = client.get_containers(GetContainersRequest {}).await;
println!("{:?}", res);
// let args = Args::parse();
//
// let metrics = Arc::new(Metrics::new(args.metrics_prefix.as_str())?);
// let server = Server::new(&args, metrics.clone())?;
// let collector = DockerCollector::new(metrics);
//
// tokio::spawn(async move {
// loop {
// let _ = collector.poll();
// tokio::time::sleep(Duration::from_secs(args.docker_stats_seconds)).await;
// }
// });
//
// server.serve().await
Ok(())
}