From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <pbs-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 25F251FF17B
	for <inbox@lore.proxmox.com>; Tue, 15 Oct 2024 09:27:31 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id 1254EFCE8;
	Tue, 15 Oct 2024 09:28:03 +0200 (CEST)
Message-ID: <10e46225-24a7-4bc5-a8a3-f171363fc900@proxmox.com>
Date: Tue, 15 Oct 2024 09:27:29 +0200
MIME-Version: 1.0
User-Agent: Mozilla Thunderbird
To: Wolfgang Bumiller <w.bumiller@proxmox.com>
References: <20241011105137.131530-1-l.wagner@proxmox.com>
 <20241011105137.131530-14-l.wagner@proxmox.com>
 <ot4axdhe4a2trhpmyr3rbipwpy5sfkzmswqfytnmappap3c4ej@c6qnoh2qhtrr>
Content-Language: de-AT, en-US
From: Lukas Wagner <l.wagner@proxmox.com>
In-Reply-To: <ot4axdhe4a2trhpmyr3rbipwpy5sfkzmswqfytnmappap3c4ej@c6qnoh2qhtrr>
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.143 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
 POISEN_SPAM_PILL          0.1 Meta: its spam
 POISEN_SPAM_PILL_1        0.1 random spam to be learned in bayes
 POISEN_SPAM_PILL_3        0.1 random spam to be learned in bayes
 RCVD_IN_VALIDITY_CERTIFIED_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to
 Validity was blocked. See
 https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more
 information.
 RCVD_IN_VALIDITY_RPBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to
 Validity was blocked. See
 https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more
 information.
 RCVD_IN_VALIDITY_SAFE_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to
 Validity was blocked. See
 https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more
 information.
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [mod.rs, status.rs, metrics.rs]
Subject: Re: [pbs-devel] [PATCH proxmox-backup 13/13] api: add
 /status/metrics API
X-BeenThere: pbs-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Backup Server development discussion
 <pbs-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pbs-devel/>
List-Post: <mailto:pbs-devel@lists.proxmox.com>
List-Help: <mailto:pbs-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=subscribe>
Reply-To: Proxmox Backup Server development discussion
 <pbs-devel@lists.proxmox.com>
Cc: pbs-devel@lists.proxmox.com
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: pbs-devel-bounces@lists.proxmox.com
Sender: "pbs-devel" <pbs-devel-bounces@lists.proxmox.com>

On  2024-10-14 12:02, Wolfgang Bumiller wrote:
> On Fri, Oct 11, 2024 at 12:51:37PM GMT, Lukas Wagner wrote:
>> This one is modelled exactly as the one in PVE (there it
>> is available under /cluster/metrics/export).
>>
>> The returned data format is quite simple, being an array of
>> metric records, including a value, a metric name, an id to identify
>> the object (e.g. datastore/foo, host), a timestamp and a type
>> ('gauge', 'derive', ...). The latter property makes the format
>> self-describing and aids the metric collector in choosing a
>> representation for storing the metric data.
>>
>> [
>>     ...
>>     {
>> 	"metric": "cpu_avg1",
>> 	"value": 0.12,
>> 	"timestamp": 170053205,
>> 	"id": "host",
>> 	"type": "gauge"
>>     },
>>     ...
>> ]
>>
>> In terms of permissions, the new endpoint requires Sys.Audit
>> on /system/status for metrics of the 'host' object,
>> and Datastore.Audit on /datastore/{store} for 'datastore/{store}'
>> metric objects.
>>
>> Via the 'history' and 'start-time' parameters one can query
>> the last 30mins of metric history. If these parameters
>> are not provided, only the most recent metric generation
>> is returned.
>>
>> Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
>> ---
>>  src/api2/mod.rs                              |  1 +
>>  src/api2/status.rs                           | 13 ++++--
>>  src/server/metric_collection/mod.rs          |  4 +-
>>  src/server/metric_collection/pull_metrics.rs | 45 ++++++++++++++++++++
>>  4 files changed, 57 insertions(+), 6 deletions(-)
>>
>> diff --git a/src/api2/mod.rs b/src/api2/mod.rs
>> index a83e4c20..f485ae53 100644
>> --- a/src/api2/mod.rs
>> +++ b/src/api2/mod.rs
>> @@ -9,6 +9,7 @@ pub mod admin;
>>  pub mod backup;
>>  pub mod config;
>>  pub mod helpers;
>> +pub mod metrics;
> 
> This file is missing (and also wouldn't be where I'd expect it, see
> below).
> 

Argh, sorry.
I might need to reconfigure Git to also show untracked files again in `git status`.


>>  pub mod node;
>>  pub mod ping;
>>  pub mod pull;
>> diff --git a/src/api2/status.rs b/src/api2/status.rs
>> index e46fc1ae..f217a31d 100644
>> --- a/src/api2/status.rs
>> +++ b/src/api2/status.rs
>> @@ -7,6 +7,7 @@ use proxmox_router::list_subdirs_api_method;
>>  use proxmox_router::{ApiMethod, Permission, Router, RpcEnvironment, SubdirMap};
>>  use proxmox_rrd_api_types::{RrdMode, RrdTimeframe};
>>  use proxmox_schema::api;
>> +use proxmox_sortable_macro::sortable;
>>  
>>  use pbs_api_types::{
>>      Authid, DataStoreStatusListItem, Operation, PRIV_DATASTORE_AUDIT, PRIV_DATASTORE_BACKUP,
>> @@ -137,10 +138,14 @@ pub async fn datastore_status(
>>      Ok(list)
>>  }
>>  
>> -const SUBDIRS: SubdirMap = &[(
>> -    "datastore-usage",
>> -    &Router::new().get(&API_METHOD_DATASTORE_STATUS),
>> -)];
>> +#[sortable]
>> +const SUBDIRS: SubdirMap = &sorted!([
>> +    (
>> +        "datastore-usage",
>> +        &Router::new().get(&API_METHOD_DATASTORE_STATUS),
>> +    ),
>> +    ("metrics", &super::metrics::ROUTER),
> 
> The API modules should ideally be structured like the paths - this would
> be the first time we go up a level to get to a router. Better move
> `status.rs` to `status/mod.rs` and add the missing file as
> `status/metrics.rs`.

Thx, will do.

> 
>> +]);
>>  
>>  pub const ROUTER: Router = Router::new()
>>      .get(&list_subdirs_api_method!(SUBDIRS))
>> diff --git a/src/server/metric_collection/mod.rs b/src/server/metric_collection/mod.rs
>> index e6e04c5b..3cbd7425 100644
>> --- a/src/server/metric_collection/mod.rs
>> +++ b/src/server/metric_collection/mod.rs
>> @@ -17,8 +17,8 @@ use proxmox_sys::{
>>  use crate::tools::disks::{zfs_dataset_stats, BlockDevStat, DiskManage};
>>  
>>  mod metric_server;
>> -mod pull_metrics;
>> -pub mod rrd;
>> +pub(crate) mod pull_metrics;
>> +pub(crate) mod rrd;
>>  
>>  const METRIC_COLLECTION_INTERVAL: Duration = Duration::from_secs(10);
>>  
>> diff --git a/src/server/metric_collection/pull_metrics.rs b/src/server/metric_collection/pull_metrics.rs
>> index f4b506cf..1b5f3777 100644
>> --- a/src/server/metric_collection/pull_metrics.rs
>> +++ b/src/server/metric_collection/pull_metrics.rs
>> @@ -39,6 +39,51 @@ pub(super) fn init() -> Result<(), Error> {
>>      Ok(())
>>  }
>>  
>> +/// Return most recent metrics
>> +///
>> +/// If the metric collection loop has no produced any metrics yet, an empty
>> +/// `Vec` is returned. Returns an error if the cache could not be accessed.
>> +pub fn get_most_recent_metrics() -> Result<Vec<MetricDataPoint>, Error> {
>> +    let cached_datapoints: Option<MetricDataPoints> = get_cache()?.get()?;
>> +    let mut points = cached_datapoints.map(|r| r.datapoints).unwrap_or_default();
>> +
>> +    points.sort_unstable_by_key(|p| p.timestamp);
>> +
>> +    Ok(points)
>> +}
>> +
>> +/// Return all cached metrics with a `timestamp > start_time`
>> +///
>> +/// If the metric collection loop has no produced any metrics yet, an empty
>> +/// `Vec` is returned. Returns an error if the cache could not be accessed.
>> +pub fn get_all_metrics(start_time: i64) -> Result<Vec<MetricDataPoint>, Error> {
>> +    let now = proxmox_time::epoch_i64();
>> +
>> +    let delta = now - start_time;
>> +
>> +    if delta < 0 {
>> +        // start-time in the future, no metrics for you
>> +        return Ok(Vec::new());
>> +    }
>> +
>> +    let generations = delta / (METRIC_COLLECTION_INTERVAL.as_secs() as i64);
>> +    let generations = generations.clamp(0, STORED_METRIC_GENERATIONS as i64);
>> +
>> +    let cached_datapoints: Vec<MetricDataPoints> = get_cache()?.get_last(generations as u32)?;
>> +
>> +    let mut points = Vec::new();
>> +
>> +    for gen in cached_datapoints {
>> +        if gen.timestamp > start_time {
>> +            points.extend(gen.datapoints);
>> +        }
>> +    }
>> +
>> +    points.sort_unstable_by_key(|p| p.timestamp);
>> +
>> +    Ok(points)
>> +}
>> +
>>  /// Convert `DiskStat` `HostStat` into a universal metric data point and cache
>>  /// them for a later retrieval.
>>  pub(super) fn update_metrics(
>> -- 
>> 2.39.5

-- 
- Lukas


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