From 2a549af46f3cc4f4b6f3737c292a534ca3cc389c Mon Sep 17 00:00:00 2001 From: Teddy Astie Date: Mon, 6 Jan 2025 14:27:29 +0100 Subject: [PATCH] Reorganize xcp-metrics/xcp-metrics-common Remove protocol v3 replaced by newer "xcp-metrics protocol v1". Remove XAPI RPC. Remove xcp-rrdd compatibility layers. Only support "xcp-metrics protocol" for plugins/daemon communication. --- xcp-metrics-common/Cargo.toml | 40 ++- xcp-metrics-common/build.rs | 1 + xcp-metrics-common/src/lib.rs | 7 +- xcp-metrics-common/src/metrics.rs | 48 ++- xcp-metrics-common/src/openmetrics/convert.rs | 58 ++-- xcp-metrics-common/src/openmetrics/test.rs | 155 ++++----- xcp-metrics-common/src/openmetrics/text.rs | 192 +++++------ xcp-metrics-common/src/protocol.rs | 170 +++++++++ xcp-metrics-common/src/protocol_v3.rs | 236 ------------- xcp-metrics-common/src/rrdd/mod.rs | 1 - .../src/rrdd/protocol_common.rs | 33 +- xcp-metrics-common/src/rrdd/rrd_updates.rs | 157 --------- xcp-metrics-common/src/test.rs | 166 +-------- xcp-metrics-common/src/utils/delta.rs | 8 +- xcp-metrics-common/src/utils/mapping.rs | 43 +-- xcp-metrics-common/src/utils/mod.rs | 1 + xcp-metrics/Cargo.toml | 26 +- xcp-metrics/src/forwarded/mod.rs | 94 ----- xcp-metrics/src/forwarded/request.rs | 89 ----- xcp-metrics/src/forwarded/response.rs | 48 --- xcp-metrics/src/forwarded/routes.rs | 97 ------ xcp-metrics/src/hub.rs | 190 +++++----- xcp-metrics/src/main.rs | 102 +----- xcp-metrics/src/mappings.rs | 34 -- xcp-metrics/src/providers/mod.rs | 11 - xcp-metrics/src/providers/protocol_v2.rs | 254 -------------- xcp-metrics/src/providers/protocol_v3.rs | 132 ------- xcp-metrics/src/publishers/mod.rs | 3 - xcp-metrics/src/publishers/openmetrics.rs | 69 ---- xcp-metrics/src/publishers/rrdd/entry.rs | 60 ---- xcp-metrics/src/publishers/rrdd/mod.rs | 88 ----- .../src/publishers/rrdd/round_robin.rs | 184 ---------- xcp-metrics/src/publishers/rrdd/server.rs | 324 ------------------ xcp-metrics/src/rpc.rs | 81 +++++ xcp-metrics/src/rpc/mod.rs | 103 ------ xcp-metrics/src/rpc/routes/deregister.rs | 28 -- xcp-metrics/src/rpc/routes/get_formats.rs | 22 -- xcp-metrics/src/rpc/routes/mod.rs | 50 --- xcp-metrics/src/rpc/routes/next_reading.rs | 19 - xcp-metrics/src/rpc/routes/register.rs | 66 ---- xcp-metrics/src/rpc/routes/register_v3.rs | 54 --- 41 files changed, 647 insertions(+), 2897 deletions(-) create mode 100644 xcp-metrics-common/src/protocol.rs delete mode 100644 xcp-metrics-common/src/protocol_v3.rs delete mode 100644 xcp-metrics-common/src/rrdd/rrd_updates.rs delete mode 100644 xcp-metrics/src/forwarded/mod.rs delete mode 100644 xcp-metrics/src/forwarded/request.rs delete mode 100644 xcp-metrics/src/forwarded/response.rs delete mode 100644 xcp-metrics/src/forwarded/routes.rs delete mode 100644 xcp-metrics/src/mappings.rs delete mode 100644 xcp-metrics/src/providers/mod.rs delete mode 100644 xcp-metrics/src/providers/protocol_v2.rs delete mode 100644 xcp-metrics/src/providers/protocol_v3.rs delete mode 100644 xcp-metrics/src/publishers/mod.rs delete mode 100644 xcp-metrics/src/publishers/openmetrics.rs delete mode 100644 xcp-metrics/src/publishers/rrdd/entry.rs delete mode 100644 xcp-metrics/src/publishers/rrdd/mod.rs delete mode 100644 xcp-metrics/src/publishers/rrdd/round_robin.rs delete mode 100644 xcp-metrics/src/publishers/rrdd/server.rs create mode 100644 xcp-metrics/src/rpc.rs delete mode 100644 xcp-metrics/src/rpc/mod.rs delete mode 100644 xcp-metrics/src/rpc/routes/deregister.rs delete mode 100644 xcp-metrics/src/rpc/routes/get_formats.rs delete mode 100644 xcp-metrics/src/rpc/routes/mod.rs delete mode 100644 xcp-metrics/src/rpc/routes/next_reading.rs delete mode 100644 xcp-metrics/src/rpc/routes/register.rs delete mode 100644 xcp-metrics/src/rpc/routes/register_v3.rs diff --git a/xcp-metrics-common/Cargo.toml b/xcp-metrics-common/Cargo.toml index e24e956..99a9e38 100644 --- a/xcp-metrics-common/Cargo.toml +++ b/xcp-metrics-common/Cargo.toml @@ -9,13 +9,12 @@ rust-version = "1.70" # we need only 1.66 but our deps want 1.70 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -crc32fast = "1.4" -serde_json = "1.0" anyhow = "1.0" -prost = "0.13" -prost-types = "0.13" maplit = "1.0" -json5 = "0.4.1" +smol_str = { version = "0.3", features = ["serde"] } + +# xcp-metrics protocol v1 +ciborium = "0.2" [dependencies.serde] version = "1.0" @@ -25,19 +24,40 @@ features = ["std", "derive"] version = "1.11" features = ["std", "serde", "v4", "fast-rng"] -[dependencies.indexmap] -version = "2.7" -features = ["serde"] - [dependencies.tokio] version = "1" features = ["io-util"] +# OpenMetrics support +[dependencies.prost] +version = "0.13" +optional = true + +[dependencies.prost-types] +version = "0.13" +optional = true + +# RRDD Compatibility dependencies +[dependencies.crc32fast] +version = "1.4" +optional = true + +[dependencies.serde_json] +version = "1.0" +optional = true + +[dependencies.indexmap] +version = "2.7" +features = ["serde"] +optional = true + [build-dependencies] -prost-build = "0.13" +prost-build = { version = "0.13", optional = true } [features] default = [] +rrdd_compat = ["dep:crc32fast", "dep:serde_json", "dep:indexmap"] +openmetrics = ["dep:prost", "dep:prost-types", "dep:prost-build"] test = ["tokio/full"] [dev-dependencies] diff --git a/xcp-metrics-common/build.rs b/xcp-metrics-common/build.rs index 5fc717a..11c53f4 100644 --- a/xcp-metrics-common/build.rs +++ b/xcp-metrics-common/build.rs @@ -1,3 +1,4 @@ fn main() { + #[cfg(feature = "openmetrics")] prost_build::compile_protos(&["src/openmetrics_data_model.proto"], &["src/"]).unwrap(); } diff --git a/xcp-metrics-common/src/lib.rs b/xcp-metrics-common/src/lib.rs index c73ba0c..b81177e 100644 --- a/xcp-metrics-common/src/lib.rs +++ b/xcp-metrics-common/src/lib.rs @@ -1,9 +1,12 @@ //! xcp-metrics common library pub mod metrics; +pub mod protocol; +pub mod utils; + +#[cfg(feature = "openmetrics")] pub mod openmetrics; -pub mod protocol_v3; +#[cfg(feature = "rrdd_compat")] pub mod rrdd; -pub mod utils; #[cfg(test)] mod test; diff --git a/xcp-metrics-common/src/metrics.rs b/xcp-metrics-common/src/metrics.rs index b252eb2..2edf4cc 100644 --- a/xcp-metrics-common/src/metrics.rs +++ b/xcp-metrics-common/src/metrics.rs @@ -1,23 +1,28 @@ //! Common metrics data structures, mostly modelled after OpenMetrics. use std::{collections::HashMap, time::SystemTime}; +use serde::{Deserialize, Serialize}; +use smol_str::SmolStr; + /// Top level metric data structure. #[derive(Clone, Default, PartialEq, Debug)] pub struct MetricSet { - pub families: HashMap, MetricFamily>, + pub families: HashMap, } /// A family of [Metric] sharing a [MetricType] and `unit`. #[derive(Clone, Default, PartialEq, Debug)] pub struct MetricFamily { + // Number of references to this family. + pub reference_count: usize, pub metric_type: MetricType, - pub unit: Box, - pub help: Box, + pub unit: SmolStr, + pub help: SmolStr, pub metrics: HashMap, } -#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Debug)] +#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Debug, Serialize, Deserialize)] pub enum MetricType { #[default] Unknown, @@ -45,28 +50,19 @@ impl std::fmt::Display for MetricType { } } -#[derive(Clone, PartialEq, Debug, Eq, Hash)] -pub struct Label( - /// Label name - pub Box, - /// Label value - pub Box, -); +#[derive(Clone, PartialEq, Debug, Eq, Hash, Serialize, Deserialize)] +pub struct Label { + pub name: SmolStr, + pub value: SmolStr, +} -#[derive(Clone, Default, PartialEq, Debug)] +#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)] pub struct Metric { pub labels: Box<[Label]>, - pub metrics_point: Box<[MetricPoint]>, -} - -#[derive(Clone, PartialEq, Debug)] -pub struct MetricPoint { - /// *Its type should match with MetricFamily's MetricType for text export.* pub value: MetricValue, - pub timestamp: SystemTime, } -#[derive(Clone, PartialEq, Debug)] +#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)] pub enum MetricValue { Unknown(NumberValue), Gauge(NumberValue), @@ -105,33 +101,33 @@ impl MetricValue { } } -#[derive(Clone, Default, PartialEq, Debug)] +#[derive(Clone, Default, PartialEq, Debug, Serialize, Deserialize)] pub struct Bucket { pub count: u64, pub upper_bound: f64, pub exemplar: Option>, } -#[derive(Clone, Default, PartialEq, Debug)] +#[derive(Clone, Default, PartialEq, Debug, Serialize, Deserialize)] pub struct Exemplar { pub value: f64, pub timestamp: Option, pub labels: Box<[Label]>, } -#[derive(Clone, Default, PartialEq, Debug)] +#[derive(Clone, Default, PartialEq, Debug, Serialize, Deserialize)] pub struct State { pub enabled: bool, - pub name: Box, + pub name: SmolStr, } -#[derive(Clone, Copy, Default, PartialEq, Debug)] +#[derive(Clone, Copy, Default, PartialEq, Debug, Serialize, Deserialize)] pub struct Quantile { pub quantile: f64, pub value: f64, } -#[derive(Clone, Copy, Default, PartialEq, Debug)] +#[derive(Clone, Copy, Default, PartialEq, Debug, Serialize, Deserialize)] pub enum NumberValue { Double(f64), Int64(i64), diff --git a/xcp-metrics-common/src/openmetrics/convert.rs b/xcp-metrics-common/src/openmetrics/convert.rs index 74aa1cd..4b9e195 100644 --- a/xcp-metrics-common/src/openmetrics/convert.rs +++ b/xcp-metrics-common/src/openmetrics/convert.rs @@ -3,8 +3,8 @@ use std::time::SystemTime; use crate::metrics::{ - Bucket, Exemplar, Label, Metric, MetricFamily, MetricPoint, MetricSet, MetricType, MetricValue, - NumberValue, Quantile, State, + Bucket, Exemplar, Label, Metric, MetricFamily, MetricSet, MetricType, MetricValue, NumberValue, + Quantile, State, }; use super::{ @@ -59,17 +59,20 @@ impl From for MetricType { /// Convert a [Label] into a [openmetrics::Label]. impl From