public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Wolfgang Bumiller <w.bumiller@proxmox.com>
To: Dominik Csapak <d.csapak@proxmox.com>
Cc: pbs-devel@lists.proxmox.com
Subject: Re: [pbs-devel] [PATCH proxmox-backup v7 3/8] pbs-config: add metrics config class
Date: Fri, 3 Jun 2022 12:16:59 +0200	[thread overview]
Message-ID: <20220603101659.xokej3cppb362oi5@wobu-vie.proxmox.com> (raw)
In-Reply-To: <20220602121811.3472729-4-d.csapak@proxmox.com>

On Thu, Jun 02, 2022 at 02:18:06PM +0200, Dominik Csapak wrote:
(...)
> @@ -0,0 +1,115 @@
> +use std::collections::HashMap;
> +
> +use anyhow::Error;
> +use lazy_static::lazy_static;
> +
> +use proxmox_metrics::{influxdb_http, influxdb_udp, Metrics};
> +use proxmox_schema::*;
> +use proxmox_section_config::{SectionConfig, SectionConfigData, SectionConfigPlugin};
> +
> +use pbs_api_types::{InfluxDbHttp, InfluxDbUdp, METRIC_SERVER_ID_SCHEMA};
> +
> +use crate::{open_backup_lockfile, BackupLockGuard};
> +
> +lazy_static! {
> +    pub static ref CONFIG: SectionConfig = init();
> +}
> +
> +fn init() -> SectionConfig {
> +    let mut config = SectionConfig::new(&METRIC_SERVER_ID_SCHEMA);
> +
> +    let udp_schema = match InfluxDbUdp::API_SCHEMA {
> +        Schema::Object(ref object_schema) => object_schema,
> +        _ => unreachable!(),
> +    };

New code should prefer using the `unwrap_<type>_schema()` const fns,
defining a const for the schema, in order to get some compile time
checks there.

    const UDP_SCHEMA: &ObjectSchema = InfluxDbUdp::API_SCHEMA.unwrap_object_schema();

Eg. if you ever `#[serde(flatten)]` some struct into the type the schema
type will change to `AllOfSchema`. See prune.rs as an example for this.
(We should also switch the remaining files probably.)

> +
> +    let udp_plugin = SectionConfigPlugin::new(
> +        "influxdb-udp".to_string(),
> +        Some("name".to_string()),
> +        udp_schema,
> +    );
> +    config.register_plugin(udp_plugin);
> +
> +    let http_schema = match InfluxDbHttp::API_SCHEMA {
> +        Schema::Object(ref object_schema) => object_schema,
> +        _ => unreachable!(),
> +    };
> +
> +    let http_plugin = SectionConfigPlugin::new(
> +        "influxdb-http".to_string(),
> +        Some("name".to_string()),
> +        http_schema,
> +    );
> +
> +    config.register_plugin(http_plugin);
> +
> +    config
> +}
> +
> +pub const METRIC_SERVER_CFG_FILENAME: &str = "/etc/proxmox-backup/metricserver.cfg";
> +pub const METRIC_SERVER_CFG_LOCKFILE: &str = "/etc/proxmox-backup/.metricserver.lck";
> +
> +/// Get exclusive lock
> +pub fn lock_config() -> Result<BackupLockGuard, Error> {
> +    open_backup_lockfile(METRIC_SERVER_CFG_LOCKFILE, None, true)
> +}
> +
> +pub fn config() -> Result<(SectionConfigData, [u8; 32]), Error> {
> +    let content = proxmox_sys::fs::file_read_optional_string(METRIC_SERVER_CFG_FILENAME)?
> +        .unwrap_or_else(|| "".to_string());

Easier on the compiler (and a `const fn`):

    .unwrap_or_else(String::new)

or even

    .unwrap_or_default()

> +
> +    let digest = openssl::sha::sha256(content.as_bytes());
> +    let data = CONFIG.parse(METRIC_SERVER_CFG_FILENAME, &content)?;
> +    Ok((data, digest))
> +}
> +
> +pub fn save_config(config: &SectionConfigData) -> Result<(), Error> {
> +    let raw = CONFIG.write(METRIC_SERVER_CFG_FILENAME, config)?;
> +    crate::replace_backup_config(METRIC_SERVER_CFG_FILENAME, raw.as_bytes())
> +}
> +
> +// shell completion helper
> +pub fn complete_remote_name(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
> +    match config() {
> +        Ok((data, _digest)) => data.sections.iter().map(|(id, _)| id.to_string()).collect(),

I think you could simplify this to `.keys().cloned().collect()`




  reply	other threads:[~2022-06-03 10:17 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-02 12:18 [pbs-devel] [PATCH proxmox-backup v7 0/8] add metrics server capability Dominik Csapak
2022-06-02 12:18 ` [pbs-devel] [PATCH proxmox-backup v7 1/8] use 'fs_info' from proxmox-sys Dominik Csapak
2022-06-07  6:51   ` [pbs-devel] applied: " Thomas Lamprecht
2022-06-02 12:18 ` [pbs-devel] [PATCH proxmox-backup v7 2/8] pbs-api-types: add metrics api types Dominik Csapak
2022-06-02 12:18 ` [pbs-devel] [PATCH proxmox-backup v7 3/8] pbs-config: add metrics config class Dominik Csapak
2022-06-03 10:16   ` Wolfgang Bumiller [this message]
2022-06-02 12:18 ` [pbs-devel] [PATCH proxmox-backup v7 4/8] backup-proxy: decouple stats gathering from rrd update Dominik Csapak
2022-06-02 14:09   ` Matthias Heiserer
2022-06-02 12:18 ` [pbs-devel] [PATCH proxmox-backup v7 5/8] proxmox-backup-proxy: send metrics to configured metrics server Dominik Csapak
2022-06-02 12:18 ` [pbs-devel] [PATCH proxmox-backup v7 6/8] api: add metricserver endpoints Dominik Csapak
2022-06-07 12:44   ` Thomas Lamprecht
2022-06-08  8:23     ` Dominik Csapak
2022-06-02 12:18 ` [pbs-devel] [PATCH proxmox-backup v7 7/8] ui: add window/InfluxDbEdit Dominik Csapak
2022-06-02 12:18 ` [pbs-devel] [PATCH proxmox-backup v7 8/8] ui: add MetricServerView and use it Dominik Csapak
2022-06-07 12:55   ` Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220603101659.xokej3cppb362oi5@wobu-vie.proxmox.com \
    --to=w.bumiller@proxmox.com \
    --cc=d.csapak@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal