Skip to main content

mito2/metrics/
file_metadata.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Scrape-time counts of registered SST metadata values.
16
17use std::sync::{Arc, Mutex, Weak};
18
19use lazy_static::lazy_static;
20use prometheus::core::{Collector, Desc};
21use prometheus::proto::MetricFamily;
22use prometheus::{IntGaugeVec, Opts};
23
24use crate::metrics::TYPE_LABEL;
25use crate::sst::file_metadata::FileMetadataInternerState;
26
27lazy_static! {
28    pub(crate) static ref FILE_METADATA_COLLECTOR: FileMetadataCollector = {
29        let collector = FileMetadataCollector::default();
30        prometheus::register(Box::new(collector.clone())).unwrap();
31        collector
32    };
33}
34
35/// Aggregates live interners without retaining them or adding work to intern/drop.
36/// Labels have fixed cardinality; entries are distinct values, not SST view counts.
37#[derive(Clone)]
38pub(crate) struct FileMetadataCollector {
39    sources: Arc<Mutex<Vec<Weak<FileMetadataInternerState>>>>,
40    entries: IntGaugeVec,
41}
42
43impl Default for FileMetadataCollector {
44    fn default() -> Self {
45        Self {
46            sources: Arc::new(Mutex::new(Vec::new())),
47            entries: IntGaugeVec::new(
48                Opts::new(
49                    "greptime_mito_sst_metadata_interner_entries",
50                    "Registered values in live SST metadata interners",
51                ),
52                &[TYPE_LABEL],
53            )
54            .unwrap(),
55        }
56    }
57}
58
59impl FileMetadataCollector {
60    pub(crate) fn register(&self, source: &Arc<FileMetadataInternerState>) {
61        let mut sources = self.sources.lock().unwrap();
62        sources.retain(|source| source.strong_count() > 0);
63        sources.push(Arc::downgrade(source));
64    }
65}
66
67impl Collector for FileMetadataCollector {
68    fn desc(&self) -> Vec<&Desc> {
69        self.entries.desc()
70    }
71
72    fn collect(&self) -> Vec<MetricFamily> {
73        // Serialize gathers so concurrent scrapes cannot overwrite each other's gauges.
74        // Registration never holds a shard lock; intern/drop never take this mutex.
75        let mut sources = self.sources.lock().unwrap();
76        let mut files = 0;
77        let mut expressions = 0;
78        sources.retain(|source| {
79            let Some(source) = source.upgrade() else {
80                return false;
81            };
82            let counts = source.entry_counts();
83            files += counts.0;
84            expressions += counts.1;
85            true
86        });
87        self.entries.with_label_values(&["file"]).set(files as i64);
88        self.entries
89            .with_label_values(&["partition_expr"])
90            .set(expressions as i64);
91        self.entries.collect()
92    }
93}