From 40d3f53aeeae2dddbf4d43f158f914e6018d8217 Mon Sep 17 00:00:00 2001 From: Marco Neumann Date: Wed, 8 Sep 2021 15:38:55 +0200 Subject: [PATCH] feat: add DB and table name to job metrics --- server/src/job.rs | 12 ++++++-- server/src/lib.rs | 78 ++++++++++++----------------------------------- 2 files changed, 29 insertions(+), 61 deletions(-) diff --git a/server/src/job.rs b/server/src/job.rs index 41163186b6..bc39d29189 100644 --- a/server/src/job.rs +++ b/server/src/job.rs @@ -164,7 +164,7 @@ impl JobRegistryMetrics { let metadata = job.metadata(); let status = job.get_status(); - metric::Attributes::from(&[ + let mut attributes = metric::Attributes::from(&[ ("description", metadata.description()), ( "status", @@ -173,7 +173,15 @@ impl JobRegistryMetrics { .map(|result| result.name()) .unwrap_or_else(|| status.name()), ), - ]) + ]); + if let Some(db_name) = metadata.db_name() { + attributes.insert("db_name", db_name.to_string()); + } + if let Some(table) = metadata.table_name() { + attributes.insert("table", table.to_string()); + } + + attributes } fn process_completed_job(&mut self, job: &TaskTracker) { diff --git a/server/src/lib.rs b/server/src/lib.rs index 649041e236..db875e5417 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -2439,7 +2439,7 @@ mod tests { let wait_nanos = 1000; let job = application .job_registry() - .spawn_dummy_job(vec![wait_nanos], None); + .spawn_dummy_job(vec![wait_nanos], Some(Arc::from("some_db"))); job.join().await; @@ -2453,77 +2453,37 @@ mod tests { server.join().await.unwrap(); // ========== influxdb_iox_job_count ========== - let observations: Vec<_> = reporter - .observations() - .iter() - .filter(|observation| observation.metric_name == "influxdb_iox_job_count") - .collect(); - assert_eq!(observations.len(), 1); - - let gauge = observations[0]; - assert_eq!(gauge.kind, metric::MetricKind::U64Gauge); - - let observations: Vec<_> = gauge - .observations - .iter() - .filter(|(attributes, _)| { - attributes - .iter() - .any(|(k, v)| (k == &"status") && (v == "Success")) - }) - .collect(); - assert_eq!(observations.len(), 1); - - let (attributes, observation) = &observations[0]; - assert_eq!( - attributes, - &metric::Attributes::from(&[ + let metric = reporter.metric("influxdb_iox_job_count").unwrap(); + assert_eq!(metric.kind, metric::MetricKind::U64Gauge); + let observation = metric + .observation(&[ ("description", "Dummy Job, for testing"), ("status", "Success"), + ("db_name", "some_db"), ]) - ); + .unwrap(); assert_eq!(observation, &metric::Observation::U64Gauge(1)); // ========== influxdb_iox_job_completed_cpu ========== - let observations: Vec<_> = reporter - .observations() - .iter() - .filter(|observation| observation.metric_name == "influxdb_iox_job_completed_cpu") - .collect(); - assert_eq!(observations.len(), 1); - - let histogram = observations[0]; - assert_eq!(histogram.kind, metric::MetricKind::DurationHistogram); - assert_eq!(histogram.observations.len(), 1); - - let (attributes, _) = &histogram.observations[0]; - assert_eq!( - attributes, - &metric::Attributes::from(&[ + let metric = reporter.metric("influxdb_iox_job_completed_cpu").unwrap(); + assert_eq!(metric.kind, metric::MetricKind::DurationHistogram); + metric + .observation(&[ ("description", "Dummy Job, for testing"), ("status", "Success"), + ("db_name", "some_db"), ]) - ); + .unwrap(); // ========== influxdb_iox_job_completed_wall ========== - let observations: Vec<_> = reporter - .observations() - .iter() - .filter(|observation| observation.metric_name == "influxdb_iox_job_completed_wall") - .collect(); - assert_eq!(observations.len(), 1); - - let histogram = observations[0]; - assert_eq!(histogram.kind, metric::MetricKind::DurationHistogram); - assert_eq!(histogram.observations.len(), 1); - - let (attributes, _) = &histogram.observations[0]; - assert_eq!( - attributes, - &metric::Attributes::from(&[ + let metric = reporter.metric("influxdb_iox_job_completed_wall").unwrap(); + assert_eq!(metric.kind, metric::MetricKind::DurationHistogram); + metric + .observation(&[ ("description", "Dummy Job, for testing"), ("status", "Success"), + ("db_name", "some_db"), ]) - ); + .unwrap(); } }