From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <pdm-devel-bounces@lists.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
	by lore.proxmox.com (Postfix) with ESMTPS id 5FF2A1FF164
	for <inbox@lore.proxmox.com>; Fri, 14 Feb 2025 14:07:47 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id 2265C19D75;
	Fri, 14 Feb 2025 14:07:48 +0100 (CET)
From: Lukas Wagner <l.wagner@proxmox.com>
To: pdm-devel@lists.proxmox.com
Date: Fri, 14 Feb 2025 14:06:41 +0100
Message-Id: <20250214130653.283012-17-l.wagner@proxmox.com>
X-Mailer: git-send-email 2.39.5
In-Reply-To: <20250214130653.283012-1-l.wagner@proxmox.com>
References: <20250214130653.283012-1-l.wagner@proxmox.com>
MIME-Version: 1.0
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.010 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pdm-devel] [PATCH proxmox-datacenter-manager v2 16/28] metric
 collection: record remote response time in metric database
X-BeenThere: pdm-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Datacenter Manager development discussion
 <pdm-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pdm-devel>, 
 <mailto:pdm-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pdm-devel/>
List-Post: <mailto:pdm-devel@lists.proxmox.com>
List-Help: <mailto:pdm-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel>, 
 <mailto:pdm-devel-request@lists.proxmox.com?subject=subscribe>
Reply-To: Proxmox Datacenter Manager development discussion
 <pdm-devel@lists.proxmox.com>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: pdm-devel-bounces@lists.proxmox.com
Sender: "pdm-devel" <pdm-devel-bounces@lists.proxmox.com>

This gives us the ability to retrieve max/avg response times for a given
time window.

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
---
 .../src/metric_collection/collection_task.rs  | 13 +++++++-
 server/src/metric_collection/rrd_task.rs      | 31 +++++++++++++++++++
 2 files changed, 43 insertions(+), 1 deletion(-)

diff --git a/server/src/metric_collection/collection_task.rs b/server/src/metric_collection/collection_task.rs
index f1027ccd..b58bf2fc 100644
--- a/server/src/metric_collection/collection_task.rs
+++ b/server/src/metric_collection/collection_task.rs
@@ -336,6 +336,7 @@ impl MetricCollectionTask {
         let (result_tx, result_rx) = oneshot::channel();
 
         let now = proxmox_time::epoch_i64();
+        let start = Instant::now();
 
         let res: Result<RrdStoreResult, Error> = async {
             match remote.ty {
@@ -349,11 +350,16 @@ impl MetricCollectionTask {
                         )
                         .await?;
 
+                    let duration = start.elapsed();
+
                     sender
                         .send(RrdStoreRequest::Pve {
                             remote: remote.id.clone(),
                             metrics,
                             channel: result_tx,
+                            // TODO: use as_millis_f64 once stabilized
+                            response_time: duration.as_secs_f64() * 1000.,
+                            request_at: now,
                         })
                         .await?;
                 }
@@ -363,15 +369,20 @@ impl MetricCollectionTask {
                         .metrics(Some(true), Some(status.most_recent_datapoint))
                         .await?;
 
+                    let duration = start.elapsed();
+
                     sender
                         .send(RrdStoreRequest::Pbs {
                             remote: remote.id.clone(),
                             metrics,
                             channel: result_tx,
+                            // TODO: use as_millis_f64 once stabilized
+                            response_time: duration.as_secs_f64() * 1000.,
+                            request_at: now,
                         })
                         .await?;
                 }
-            }
+            };
 
             result_rx.await.map_err(Error::from)
         }
diff --git a/server/src/metric_collection/rrd_task.rs b/server/src/metric_collection/rrd_task.rs
index c2a41d30..a8e48e89 100644
--- a/server/src/metric_collection/rrd_task.rs
+++ b/server/src/metric_collection/rrd_task.rs
@@ -20,6 +20,10 @@ pub(super) enum RrdStoreRequest {
         metrics: ClusterMetrics,
         /// Oneshot channel to return the [`RrdStoreResult`].
         channel: oneshot::Sender<RrdStoreResult>,
+        /// Reponse time in ms for the API request.
+        response_time: f64,
+        /// Timestamp at which the request was done (UNIX epoch).
+        request_at: i64,
     },
     /// Store PBS metrics.
     Pbs {
@@ -29,6 +33,10 @@ pub(super) enum RrdStoreRequest {
         metrics: Metrics,
         /// Oneshot channel to return the [`RrdStoreResult`].
         channel: oneshot::Sender<RrdStoreResult>,
+        /// Reponse time in ms for the API request.
+        response_time: f64,
+        /// Timestamp at which the request was done (UNIX epoch).
+        request_at: i64,
     },
 }
 
@@ -54,11 +62,14 @@ pub(super) async fn store_in_rrd_task(
                     remote,
                     metrics,
                     channel,
+                    response_time,
+                    request_at,
                 } => {
                     for data_point in metrics.data {
                         most_recent_timestamp = most_recent_timestamp.max(data_point.timestamp);
                         store_metric_pve(&cache_clone, &remote, &data_point);
                     }
+                    store_response_time(&cache_clone, &remote, response_time, request_at);
 
                     channel
                 }
@@ -66,11 +77,14 @@ pub(super) async fn store_in_rrd_task(
                     remote,
                     metrics,
                     channel,
+                    response_time,
+                    request_at,
                 } => {
                     for data_point in metrics.data {
                         most_recent_timestamp = most_recent_timestamp.max(data_point.timestamp);
                         store_metric_pbs(&cache_clone, &remote, &data_point);
                     }
+                    store_response_time(&cache_clone, &remote, response_time, request_at);
 
                     channel
                 }
@@ -137,6 +151,12 @@ fn store_metric_pbs(cache: &RrdCache, remote_name: &str, data_point: &MetricData
     );
 }
 
+fn store_response_time(cache: &RrdCache, remote_name: &str, response_time: f64, timestamp: i64) {
+    let name = format!("local/metric-collection/remotes/{remote_name}/response-time");
+
+    cache.update_value(&name, response_time, timestamp, DataSourceType::Gauge);
+}
+
 #[cfg(test)]
 mod tests {
     use proxmox_rrd_api_types::{RrdMode, RrdTimeframe};
@@ -199,6 +219,8 @@ mod tests {
             remote: "some-remote".into(),
             metrics,
             channel: tx_back,
+            response_time: 10.0,
+            request_at: now,
         };
 
         // Act
@@ -224,6 +246,15 @@ mod tests {
             assert!(data.data.iter().any(Option::is_some));
         }
 
+        if let Some(data) = cache.extract_data(
+            "local/metric-collection/remotes/some-remote",
+            "response-time",
+            RrdTimeframe::Hour,
+            RrdMode::Max,
+        )? {
+            assert!(data.data.iter().any(Option::is_some));
+        }
+
         Ok(())
     }
 }
-- 
2.39.5



_______________________________________________
pdm-devel mailing list
pdm-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel