Skip to content

Commit 493bfab

Browse files
committed
overwork labels
1 parent a65f2e4 commit 493bfab

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

src/commands/backup.rs

+29-9
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,22 @@ impl BackupCmd {
343343

344344
#[cfg(feature = "prometheus")]
345345
if config.global.is_prometheus_configured() {
346-
if let Err(err) = publish_metrics(&snap, self.prometheus_job, self.prometheus_labels) {
347-
warn!("error pushing prometheus metrics: {err}");
346+
#[cfg(feature = "prometheus")]
347+
{
348+
// Merge global prometheus labels
349+
conflate::btreemap::append_or_ignore(
350+
&mut self.prometheus_labels,
351+
config.global.prometheus_labels.clone(),
352+
);
353+
if let Err(err) =
354+
publish_metrics(&snap, self.prometheus_job, self.prometheus_labels)
355+
{
356+
warn!("error pushing prometheus metrics: {err}");
357+
}
358+
}
359+
#[cfg(not(feature = "prometheus"))]
360+
{
361+
warn!("not pushing prometheus metrics - this rustic version is compiled without prometheus support");
348362
}
349363
}
350364

@@ -357,7 +371,7 @@ impl BackupCmd {
357371
fn publish_metrics(
358372
snap: &SnapshotFile,
359373
job_name: Option<String>,
360-
extra_labels: BTreeMap<String, String>,
374+
mut labels: BTreeMap<String, String>,
361375
) -> Result<()> {
362376
use prometheus::register_gauge;
363377

@@ -498,12 +512,18 @@ fn publish_metrics(
498512
.context("registering prometheus gauge")?;
499513
metric_total_duration.set(summary.total_duration);
500514

501-
let mut labels = Vec::new();
502-
labels.push(("paths".to_owned(), format!("{}", snap.paths)));
503-
labels.push(("hostname".to_owned(), snap.hostname.clone()));
504-
labels.push(("snapshot_label".to_string(), snap.label.clone()));
505-
labels.push(("tags".to_string(), format!("{}", snap.tags)));
506-
labels.extend(extra_labels);
515+
_ = labels
516+
.entry("paths".to_string())
517+
.or_insert_with(|| format!("{}", snap.paths));
518+
_ = labels
519+
.entry("hostname".to_owned())
520+
.or_insert_with(|| snap.hostname.clone());
521+
_ = labels
522+
.entry("snapshot_label".to_string())
523+
.or_insert_with(|| snap.label.clone());
524+
_ = labels
525+
.entry("tags".to_string())
526+
.or_insert_with(|| format!("{}", snap.tags));
507527

508528
let job_name = job_name.as_deref().unwrap_or("rustic_backup");
509529
RUSTIC_APP.config().global.push_metrics(job_name, labels)

src/config.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ pub struct GlobalOptions {
213213
/// Additional labels to set to generated Prometheus metrics
214214
#[clap(skip)]
215215
#[merge(strategy=conflate::btreemap::append_or_ignore)]
216-
prometheus_labels: BTreeMap<String, String>,
216+
pub prometheus_labels: BTreeMap<String, String>,
217217
}
218218

219219
pub fn parse_labels(s: &str) -> Result<BTreeMap<String, String>> {
@@ -237,7 +237,7 @@ impl GlobalOptions {
237237
self.prometheus.is_some()
238238
}
239239

240-
pub fn push_metrics(&self, job_name: &str, extra_labels: Vec<(String, String)>) -> Result<()> {
240+
pub fn push_metrics(&self, job_name: &str, labels: BTreeMap<String, String>) -> Result<()> {
241241
use prometheus::{Encoder, ProtobufEncoder};
242242
use reqwest::{blocking::Client, header::CONTENT_TYPE, StatusCode};
243243

@@ -246,14 +246,6 @@ impl GlobalOptions {
246246
return Ok(());
247247
};
248248

249-
// Merge labels and extra labels
250-
let labels = self
251-
.prometheus_labels
252-
.iter()
253-
.map(|x| (x.0.as_str(), x.1.as_str()))
254-
.chain(extra_labels.iter().map(|(a, b)| (a.as_str(), b.as_str())))
255-
.collect();
256-
257249
let (full_url, encoded_metrics) = make_url_and_encoded_metrics(url, job_name, labels)?;
258250
debug!("using url: {full_url}");
259251

@@ -289,7 +281,7 @@ impl GlobalOptions {
289281
fn make_url_and_encoded_metrics(
290282
url: &Url,
291283
job: &str,
292-
grouping: BTreeMap<&str, &str>,
284+
grouping: BTreeMap<String, String>,
293285
) -> Result<(Url, Vec<u8>)> {
294286
use base64::prelude::*;
295287
use prometheus::{Encoder, ProtobufEncoder};
@@ -441,6 +433,7 @@ mod tests {
441433
("nogroup", ""),
442434
]
443435
.into_iter()
436+
.map(|(a, b)| (a.to_string(), b.to_string()))
444437
.collect();
445438
let (url, _) =
446439
make_url_and_encoded_metrics(&Url::from_str("http://host")?, "test_job", grouping)?;

0 commit comments

Comments
 (0)