mirror of
https://github.com/rustfs/rustfs.git
synced 2026-12-02 14:18:01 +00:00
perf(obs): prepare fixed descriptors once per bucket collection
Co-Authored-By: heihutu <heihutu@gmail.com> Co-Authored-By: zhi22915 <qiuzgang@gmail.com> Co-Authored-By: qunjiazhu <qunjiazhu@gmail.com>
This commit is contained in:
@@ -50,28 +50,22 @@ pub fn collect_bucket_metrics(buckets: &[BucketStats]) -> Vec<PrometheusMetric>
|
||||
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
|
||||
|
||||
@@ -119,36 +119,32 @@ pub fn usage_since_last_update_seconds(last_update: Option<SystemTime>, now: Sys
|
||||
///
|
||||
/// Returns a vector of Prometheus metrics for bucket usage.
|
||||
pub fn collect_bucket_usage_metrics(stats: &[BucketUsageStats]) -> Vec<PrometheusMetric> {
|
||||
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<Prometheu
|
||||
// Version count distribution per bucket
|
||||
for (range, count) in &stat.version_count_distribution {
|
||||
metrics.push(
|
||||
PrometheusMetric::from_descriptor(&USAGE_BUCKET_OBJECT_VERSION_COUNT_DISTRIBUTION_MD, *count as f64)
|
||||
version_distribution(*count as f64)
|
||||
.with_label_owned(RANGE_LABEL, range.clone())
|
||||
.with_label_owned(BUCKET_LABEL, bucket_label.clone()),
|
||||
);
|
||||
@@ -225,8 +221,33 @@ mod tests {
|
||||
let metrics = collect_bucket_usage_metrics(&stats);
|
||||
report_metrics(&metrics);
|
||||
|
||||
// 5 base metrics + 1 size distribution + 1 version distribution = 7
|
||||
assert_eq!(metrics.len(), 7);
|
||||
let expected = [
|
||||
(&*USAGE_BUCKET_TOTAL_BYTES_MD, stats[0].total_bytes, None),
|
||||
(&*USAGE_BUCKET_OBJECTS_TOTAL_MD, stats[0].objects_count, None),
|
||||
(&*USAGE_BUCKET_VERSIONS_COUNT_MD, stats[0].versions_count, None),
|
||||
(&*USAGE_BUCKET_DELETE_MARKERS_COUNT_MD, stats[0].delete_markers_count, None),
|
||||
(&*USAGE_BUCKET_QUOTA_TOTAL_BYTES_MD, stats[0].quota_bytes, None),
|
||||
(&*USAGE_BUCKET_OBJECT_SIZE_DISTRIBUTION_MD, 500, Some("0-1KB")),
|
||||
(&*USAGE_BUCKET_OBJECT_VERSION_COUNT_DISTRIBUTION_MD, 800, Some("1")),
|
||||
];
|
||||
assert_eq!(metrics.len(), expected.len());
|
||||
for (metric, (descriptor, value, range)) in metrics.iter().zip(expected) {
|
||||
assert!(matches!(metric.name, std::borrow::Cow::Borrowed(_)));
|
||||
assert_eq!(metric.name, descriptor.get_full_metric_name());
|
||||
assert_eq!(metric.help, descriptor.help);
|
||||
assert_eq!(metric.metric_type, descriptor.metric_type);
|
||||
assert_eq!(metric.value, value as f64);
|
||||
assert_eq!(metric.labels.len(), if range.is_some() { 2 } else { 1 });
|
||||
assert!(
|
||||
metric
|
||||
.labels
|
||||
.iter()
|
||||
.any(|(key, value)| *key == BUCKET_LABEL && value == "test-bucket")
|
||||
);
|
||||
if let Some(range) = range {
|
||||
assert!(metric.labels.iter().any(|(key, value)| *key == RANGE_LABEL && value == range));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -144,6 +144,14 @@ impl PrometheusMetric {
|
||||
}
|
||||
}
|
||||
|
||||
/// Prepare a fixed descriptor once before emitting a family of samples.
|
||||
pub(crate) fn prepare_static_descriptor(descriptor: &'static MetricDescriptor) -> 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<Cow<'static, str>>) -> Self {
|
||||
self.labels.push((key, value.into()));
|
||||
|
||||
Reference in New Issue
Block a user