diff --git a/crates/obs/src/metrics/collectors/bucket.rs b/crates/obs/src/metrics/collectors/bucket.rs index 189381601f..f78ad6250a 100644 --- a/crates/obs/src/metrics/collectors/bucket.rs +++ b/crates/obs/src/metrics/collectors/bucket.rs @@ -50,28 +50,22 @@ pub fn collect_bucket_metrics(buckets: &[BucketStats]) -> Vec return Vec::new(); } + let size_metric = PrometheusMetric::prepare_static_descriptor(&BUCKET_USAGE_BYTES_MD); + let objects_metric = PrometheusMetric::prepare_static_descriptor(&BUCKET_OBJECTS_TOTAL_MD); + let quota_metric = PrometheusMetric::prepare_static_descriptor(&BUCKET_QUOTA_BYTES_MD); let mut metrics = Vec::with_capacity(buckets.len() * 3); for bucket in buckets { let bucket_label: Cow<'static, str> = Cow::Owned(bucket.name.clone()); if let Some(size_bytes) = bucket.size_bytes { - metrics.push( - PrometheusMetric::from_descriptor(&BUCKET_USAGE_BYTES_MD, size_bytes as f64) - .with_label("bucket", bucket_label.clone()), - ); + metrics.push(size_metric(size_bytes as f64).with_label("bucket", bucket_label.clone())); } if let Some(objects_count) = bucket.objects_count { - metrics.push( - PrometheusMetric::from_descriptor(&BUCKET_OBJECTS_TOTAL_MD, objects_count as f64) - .with_label("bucket", bucket_label.clone()), - ); + metrics.push(objects_metric(objects_count as f64).with_label("bucket", bucket_label.clone())); } - metrics.push( - PrometheusMetric::from_descriptor(&BUCKET_QUOTA_BYTES_MD, bucket.quota_bytes as f64) - .with_label("bucket", bucket_label), - ); + metrics.push(quota_metric(bucket.quota_bytes as f64).with_label("bucket", bucket_label)); } metrics diff --git a/crates/obs/src/metrics/collectors/cluster_usage.rs b/crates/obs/src/metrics/collectors/cluster_usage.rs index 76ccd6cf5d..52d23a6168 100644 --- a/crates/obs/src/metrics/collectors/cluster_usage.rs +++ b/crates/obs/src/metrics/collectors/cluster_usage.rs @@ -119,36 +119,32 @@ pub fn usage_since_last_update_seconds(last_update: Option, now: Sys /// /// Returns a vector of Prometheus metrics for bucket usage. pub fn collect_bucket_usage_metrics(stats: &[BucketUsageStats]) -> Vec { - let mut metrics = Vec::new(); + if stats.is_empty() { + return Vec::new(); + } + + let total_bytes = PrometheusMetric::prepare_static_descriptor(&USAGE_BUCKET_TOTAL_BYTES_MD); + let objects = PrometheusMetric::prepare_static_descriptor(&USAGE_BUCKET_OBJECTS_TOTAL_MD); + let versions = PrometheusMetric::prepare_static_descriptor(&USAGE_BUCKET_VERSIONS_COUNT_MD); + let delete_markers = PrometheusMetric::prepare_static_descriptor(&USAGE_BUCKET_DELETE_MARKERS_COUNT_MD); + let quota = PrometheusMetric::prepare_static_descriptor(&USAGE_BUCKET_QUOTA_TOTAL_BYTES_MD); + let size_distribution = PrometheusMetric::prepare_static_descriptor(&USAGE_BUCKET_OBJECT_SIZE_DISTRIBUTION_MD); + let version_distribution = PrometheusMetric::prepare_static_descriptor(&USAGE_BUCKET_OBJECT_VERSION_COUNT_DISTRIBUTION_MD); + let mut metrics = Vec::with_capacity(stats.len() * 5); for stat in stats { let bucket_label = stat.bucket.clone(); - metrics.push( - PrometheusMetric::from_descriptor(&USAGE_BUCKET_TOTAL_BYTES_MD, stat.total_bytes as f64) - .with_label_owned(BUCKET_LABEL, bucket_label.clone()), - ); - metrics.push( - PrometheusMetric::from_descriptor(&USAGE_BUCKET_OBJECTS_TOTAL_MD, stat.objects_count as f64) - .with_label_owned(BUCKET_LABEL, bucket_label.clone()), - ); - metrics.push( - PrometheusMetric::from_descriptor(&USAGE_BUCKET_VERSIONS_COUNT_MD, stat.versions_count as f64) - .with_label_owned(BUCKET_LABEL, bucket_label.clone()), - ); - metrics.push( - PrometheusMetric::from_descriptor(&USAGE_BUCKET_DELETE_MARKERS_COUNT_MD, stat.delete_markers_count as f64) - .with_label_owned(BUCKET_LABEL, bucket_label.clone()), - ); - metrics.push( - PrometheusMetric::from_descriptor(&USAGE_BUCKET_QUOTA_TOTAL_BYTES_MD, stat.quota_bytes as f64) - .with_label_owned(BUCKET_LABEL, bucket_label.clone()), - ); + metrics.push(total_bytes(stat.total_bytes as f64).with_label_owned(BUCKET_LABEL, bucket_label.clone())); + metrics.push(objects(stat.objects_count as f64).with_label_owned(BUCKET_LABEL, bucket_label.clone())); + metrics.push(versions(stat.versions_count as f64).with_label_owned(BUCKET_LABEL, bucket_label.clone())); + metrics.push(delete_markers(stat.delete_markers_count as f64).with_label_owned(BUCKET_LABEL, bucket_label.clone())); + metrics.push(quota(stat.quota_bytes as f64).with_label_owned(BUCKET_LABEL, bucket_label.clone())); // Object size distribution per bucket for (range, count) in &stat.object_size_distribution { metrics.push( - PrometheusMetric::from_descriptor(&USAGE_BUCKET_OBJECT_SIZE_DISTRIBUTION_MD, *count as f64) + size_distribution(*count as f64) .with_label_owned(RANGE_LABEL, range.clone()) .with_label_owned(BUCKET_LABEL, bucket_label.clone()), ); @@ -157,7 +153,7 @@ pub fn collect_bucket_usage_metrics(stats: &[BucketUsageStats]) -> Vec impl Fn(f64) -> Self { + let name = intern_string(&NAME_CACHE, &descriptor.get_full_metric_name()); + let kind = descriptor.metric_type; + let help = descriptor.help.as_str(); + move |value| Self::new(name, kind, help, value) + } + #[inline] pub fn with_label(mut self, key: &'static str, value: impl Into>) -> Self { self.labels.push((key, value.into()));