mito2/
metrics.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
15use std::time::Duration;
16
17use lazy_static::lazy_static;
18use prometheus::*;
19use puffin::puffin_manager::stager::StagerNotifier;
20
21/// Stage label.
22pub const STAGE_LABEL: &str = "stage";
23/// Type label.
24pub const TYPE_LABEL: &str = "type";
25const CACHE_EVICTION_CAUSE: &str = "cause";
26/// Reason to flush.
27pub const FLUSH_REASON: &str = "reason";
28/// File type label.
29pub const FILE_TYPE_LABEL: &str = "file_type";
30/// Region worker id label.
31pub const WORKER_LABEL: &str = "worker";
32/// Partition label.
33pub const PARTITION_LABEL: &str = "partition";
34/// Staging dir type label.
35pub const STAGING_TYPE: &str = "index_staging";
36/// Recycle bin type label.
37pub const RECYCLE_TYPE: &str = "recycle_bin";
38
39lazy_static! {
40    /// Global write buffer size in bytes.
41    pub static ref WRITE_BUFFER_BYTES: IntGauge =
42        register_int_gauge!("greptime_mito_write_buffer_bytes", "mito write buffer bytes").unwrap();
43    /// Global memtable dictionary size in bytes.
44    pub static ref MEMTABLE_DICT_BYTES: IntGauge =
45        register_int_gauge!("greptime_mito_memtable_dict_bytes", "mito memtable dictionary size in bytes").unwrap();
46    /// Gauge for open regions in each worker.
47    pub static ref REGION_COUNT: IntGaugeVec =
48        register_int_gauge_vec!(
49            "greptime_mito_region_count",
50            "mito region count in each worker",
51            &[WORKER_LABEL],
52        ).unwrap();
53    /// Elapsed time to handle requests.
54    pub static ref HANDLE_REQUEST_ELAPSED: HistogramVec = register_histogram_vec!(
55            "greptime_mito_handle_request_elapsed",
56            "mito handle request elapsed",
57            &[TYPE_LABEL],
58            // 0.01 ~ 10000
59            exponential_buckets(0.01, 10.0, 7).unwrap(),
60        )
61        .unwrap();
62
63    // ------ Flush related metrics
64    /// Counter of scheduled flush requests.
65    /// Note that the flush scheduler may merge some flush requests.
66    pub static ref FLUSH_REQUESTS_TOTAL: IntCounterVec = register_int_counter_vec!(
67            "greptime_mito_flush_requests_total",
68            "mito flush requests total",
69            &[FLUSH_REASON]
70        )
71        .unwrap();
72    /// Counter of scheduled failed flush jobs.
73    pub static ref FLUSH_FAILURE_TOTAL: IntCounter =
74        register_int_counter!("greptime_mito_flush_failure_total", "mito flush failure total").unwrap();
75    /// Elapsed time of a flush job.
76    pub static ref FLUSH_ELAPSED: HistogramVec = register_histogram_vec!(
77            "greptime_mito_flush_elapsed",
78            "mito flush elapsed",
79            &[TYPE_LABEL],
80            // 1 ~ 625
81            exponential_buckets(1.0, 5.0, 6).unwrap(),
82        )
83        .unwrap();
84    /// Histogram of flushed bytes.
85    pub static ref FLUSH_BYTES_TOTAL: IntCounter =
86        register_int_counter!("greptime_mito_flush_bytes_total", "mito flush bytes total").unwrap();
87    /// Gauge for inflight flush tasks.
88    pub static ref INFLIGHT_FLUSH_COUNT: IntGauge =
89        register_int_gauge!(
90            "greptime_mito_inflight_flush_count",
91            "inflight flush count",
92        ).unwrap();
93    // ------ End of flush related metrics
94
95
96    // ------ Write related metrics
97    /// Number of stalled write requests in each worker.
98    pub static ref WRITE_STALL_TOTAL: IntGaugeVec = register_int_gauge_vec!(
99            "greptime_mito_write_stall_total",
100            "mito stalled write request in each worker",
101            &[WORKER_LABEL]
102        ).unwrap();
103    /// Counter of rejected write requests.
104    pub static ref WRITE_REJECT_TOTAL: IntCounter =
105        register_int_counter!("greptime_mito_write_reject_total", "mito write reject total").unwrap();
106    /// Elapsed time of each write stage.
107    pub static ref WRITE_STAGE_ELAPSED: HistogramVec = register_histogram_vec!(
108            "greptime_mito_write_stage_elapsed",
109            "mito write stage elapsed",
110            &[STAGE_LABEL],
111            // 0.01 ~ 1000
112            exponential_buckets(0.01, 10.0, 6).unwrap(),
113        )
114        .unwrap();
115    /// Counter of rows to write.
116    pub static ref WRITE_ROWS_TOTAL: IntCounterVec = register_int_counter_vec!(
117        "greptime_mito_write_rows_total",
118        "mito write rows total",
119        &[TYPE_LABEL]
120    )
121    .unwrap();
122    // ------ End of write related metrics
123
124
125    // Compaction metrics
126    /// Timer of different stages in compaction.
127    pub static ref COMPACTION_STAGE_ELAPSED: HistogramVec = register_histogram_vec!(
128        "greptime_mito_compaction_stage_elapsed",
129        "mito compaction stage elapsed",
130        &[STAGE_LABEL],
131        // 1 ~ 100000
132        exponential_buckets(1.0, 10.0, 6).unwrap(),
133    )
134    .unwrap();
135    /// Timer of whole compaction task.
136    pub static ref COMPACTION_ELAPSED_TOTAL: Histogram =
137        register_histogram!(
138        "greptime_mito_compaction_total_elapsed",
139        "mito compaction total elapsed",
140        // 1 ~ 100000
141        exponential_buckets(1.0, 10.0, 6).unwrap(),
142    ).unwrap();
143    /// Counter of all requested compaction task.
144    pub static ref COMPACTION_REQUEST_COUNT: IntCounter =
145        register_int_counter!("greptime_mito_compaction_requests_total", "mito compaction requests total").unwrap();
146    /// Counter of failed compaction task.
147    pub static ref COMPACTION_FAILURE_COUNT: IntCounter =
148        register_int_counter!("greptime_mito_compaction_failure_total", "mito compaction failure total").unwrap();
149
150    /// Gauge for inflight compaction tasks.
151    pub static ref INFLIGHT_COMPACTION_COUNT: IntGauge =
152        register_int_gauge!(
153            "greptime_mito_inflight_compaction_count",
154            "inflight compaction count",
155        ).unwrap();
156
157    // Query metrics.
158    /// Timer of different stages in query.
159    pub static ref READ_STAGE_ELAPSED: HistogramVec = register_histogram_vec!(
160        "greptime_mito_read_stage_elapsed",
161        "mito read stage elapsed",
162        &[STAGE_LABEL],
163        // 0.01 ~ 10000
164        exponential_buckets(0.01, 10.0, 7).unwrap(),
165    )
166    .unwrap();
167    pub static ref READ_STAGE_FETCH_PAGES: Histogram = READ_STAGE_ELAPSED.with_label_values(&["fetch_pages"]);
168    /// Number of in-progress scan per partition.
169    pub static ref IN_PROGRESS_SCAN: IntGaugeVec = register_int_gauge_vec!(
170        "greptime_mito_in_progress_scan",
171        "mito in progress scan per partition",
172        &[TYPE_LABEL, PARTITION_LABEL]
173    )
174    .unwrap();
175    /// Counter of rows read from different source.
176    pub static ref READ_ROWS_TOTAL: IntCounterVec =
177        register_int_counter_vec!("greptime_mito_read_rows_total", "mito read rows total", &[TYPE_LABEL]).unwrap();
178    /// Counter of filtered rows during merge.
179    pub static ref MERGE_FILTER_ROWS_TOTAL: IntCounterVec =
180        register_int_counter_vec!("greptime_mito_merge_filter_rows_total", "mito merge filter rows total", &[TYPE_LABEL]).unwrap();
181    /// Counter of row groups read.
182    pub static ref READ_ROW_GROUPS_TOTAL: IntCounterVec =
183        register_int_counter_vec!("greptime_mito_read_row_groups_total", "mito read row groups total", &[TYPE_LABEL]).unwrap();
184    /// Counter of filtered rows by precise filter.
185    pub static ref PRECISE_FILTER_ROWS_TOTAL: IntCounterVec =
186        register_int_counter_vec!("greptime_mito_precise_filter_rows_total", "mito precise filter rows total", &[TYPE_LABEL]).unwrap();
187    pub static ref READ_ROWS_IN_ROW_GROUP_TOTAL: IntCounterVec =
188        register_int_counter_vec!("greptime_mito_read_rows_in_row_group_total", "mito read rows in row group total", &[TYPE_LABEL]).unwrap();
189    /// Histogram for the number of SSTs to scan per query.
190    pub static ref READ_SST_COUNT: Histogram = register_histogram!(
191        "greptime_mito_read_sst_count",
192        "Number of SSTs to scan in a scan task",
193        vec![1.0, 4.0, 8.0, 16.0, 32.0, 64.0, 256.0, 1024.0],
194    ).unwrap();
195    /// Histogram for the number of rows returned per query.
196    pub static ref READ_ROWS_RETURN: Histogram = register_histogram!(
197        "greptime_mito_read_rows_return",
198        "Number of rows returned in a scan task",
199        exponential_buckets(100.0, 10.0, 8).unwrap(),
200    ).unwrap();
201    /// Histogram for the number of batches returned per query.
202    pub static ref READ_BATCHES_RETURN: Histogram = register_histogram!(
203        "greptime_mito_read_batches_return",
204        "Number of rows returned in a scan task",
205        exponential_buckets(100.0, 10.0, 7).unwrap(),
206    ).unwrap();
207    // ------- End of query metrics.
208
209    // Cache related metrics.
210    /// Cache hit counter.
211    pub static ref CACHE_HIT: IntCounterVec = register_int_counter_vec!(
212        "greptime_mito_cache_hit",
213        "mito cache hit",
214        &[TYPE_LABEL]
215    )
216    .unwrap();
217    /// Cache miss counter.
218    pub static ref CACHE_MISS: IntCounterVec = register_int_counter_vec!(
219        "greptime_mito_cache_miss",
220        "mito cache miss",
221        &[TYPE_LABEL]
222    )
223    .unwrap();
224    /// Cache size in bytes.
225    pub static ref CACHE_BYTES: IntGaugeVec = register_int_gauge_vec!(
226        "greptime_mito_cache_bytes",
227        "mito cache bytes",
228        &[TYPE_LABEL]
229    )
230    .unwrap();
231    /// Download bytes counter in the write cache.
232    pub static ref WRITE_CACHE_DOWNLOAD_BYTES_TOTAL: IntCounter = register_int_counter!(
233        "mito_write_cache_download_bytes_total",
234        "mito write cache download bytes total",
235    ).unwrap();
236    /// Timer of the downloading task in the write cache.
237    pub static ref WRITE_CACHE_DOWNLOAD_ELAPSED: HistogramVec = register_histogram_vec!(
238        "mito_write_cache_download_elapsed",
239        "mito write cache download elapsed",
240        &[TYPE_LABEL],
241        // 0.1 ~ 10000
242        exponential_buckets(0.1, 10.0, 6).unwrap(),
243    ).unwrap();
244    /// Number of inflight download tasks.
245    pub static ref WRITE_CACHE_INFLIGHT_DOWNLOAD: IntGauge = register_int_gauge!(
246        "mito_write_cache_inflight_download_count",
247        "mito write cache inflight download tasks",
248    ).unwrap();
249    /// Upload bytes counter.
250    pub static ref UPLOAD_BYTES_TOTAL: IntCounter = register_int_counter!(
251        "mito_upload_bytes_total",
252        "mito upload bytes total",
253    )
254    .unwrap();
255    /// Cache eviction counter, labeled with cache type and eviction reason.
256    pub static ref CACHE_EVICTION: IntCounterVec = register_int_counter_vec!(
257        "greptime_mito_cache_eviction",
258        "mito cache eviction",
259        &[TYPE_LABEL, CACHE_EVICTION_CAUSE]
260    ).unwrap();
261    // ------- End of cache metrics.
262
263    // Index metrics.
264    /// Timer of index application.
265    pub static ref INDEX_APPLY_ELAPSED: HistogramVec = register_histogram_vec!(
266        "greptime_index_apply_elapsed",
267        "index apply elapsed",
268        &[TYPE_LABEL],
269        // 0.01 ~ 1000
270        exponential_buckets(0.01, 10.0, 6).unwrap(),
271    )
272    .unwrap();
273    /// Gauge of index apply memory usage.
274    pub static ref INDEX_APPLY_MEMORY_USAGE: IntGauge = register_int_gauge!(
275        "greptime_index_apply_memory_usage",
276        "index apply memory usage",
277    )
278    .unwrap();
279    /// Timer of index creation.
280    pub static ref INDEX_CREATE_ELAPSED: HistogramVec = register_histogram_vec!(
281        "greptime_index_create_elapsed",
282        "index create elapsed",
283        &[STAGE_LABEL, TYPE_LABEL],
284        // 0.1 ~ 10000
285        exponential_buckets(0.1, 10.0, 6).unwrap(),
286    )
287    .unwrap();
288    /// Counter of rows indexed.
289    pub static ref INDEX_CREATE_ROWS_TOTAL: IntCounterVec = register_int_counter_vec!(
290        "greptime_index_create_rows_total",
291        "index create rows total",
292        &[TYPE_LABEL],
293    )
294    .unwrap();
295    /// Counter of created index bytes.
296    pub static ref INDEX_CREATE_BYTES_TOTAL: IntCounterVec = register_int_counter_vec!(
297        "greptime_index_create_bytes_total",
298        "index create bytes total",
299        &[TYPE_LABEL],
300    )
301    .unwrap();
302    /// Gauge of index create memory usage.
303    pub static ref INDEX_CREATE_MEMORY_USAGE: IntGaugeVec = register_int_gauge_vec!(
304        "greptime_index_create_memory_usage",
305        "index create memory usage",
306        &[TYPE_LABEL],
307    ).unwrap();
308    /// Counter of r/w bytes on index related IO operations.
309    pub static ref INDEX_IO_BYTES_TOTAL: IntCounterVec = register_int_counter_vec!(
310        "greptime_index_io_bytes_total",
311        "index io bytes total",
312        &[TYPE_LABEL, FILE_TYPE_LABEL]
313    )
314    .unwrap();
315    /// Counter of read bytes on puffin files.
316    pub static ref INDEX_PUFFIN_READ_BYTES_TOTAL: IntCounter = INDEX_IO_BYTES_TOTAL
317        .with_label_values(&["read", "puffin"]);
318    /// Counter of write bytes on puffin files.
319    pub static ref INDEX_PUFFIN_WRITE_BYTES_TOTAL: IntCounter = INDEX_IO_BYTES_TOTAL
320        .with_label_values(&["write", "puffin"]);
321    /// Counter of read bytes on intermediate files.
322    pub static ref INDEX_INTERMEDIATE_READ_BYTES_TOTAL: IntCounter = INDEX_IO_BYTES_TOTAL
323        .with_label_values(&["read", "intermediate"]);
324    /// Counter of write bytes on intermediate files.
325    pub static ref INDEX_INTERMEDIATE_WRITE_BYTES_TOTAL: IntCounter = INDEX_IO_BYTES_TOTAL
326        .with_label_values(&["write", "intermediate"]);
327
328    /// Counter of r/w operations on index related IO operations, e.g. read, write, seek and flush.
329    pub static ref INDEX_IO_OP_TOTAL: IntCounterVec = register_int_counter_vec!(
330        "greptime_index_io_op_total",
331        "index io op total",
332        &[TYPE_LABEL, FILE_TYPE_LABEL]
333    )
334    .unwrap();
335    /// Counter of read operations on puffin files.
336    pub static ref INDEX_PUFFIN_READ_OP_TOTAL: IntCounter = INDEX_IO_OP_TOTAL
337        .with_label_values(&["read", "puffin"]);
338    /// Counter of seek operations on puffin files.
339    pub static ref INDEX_PUFFIN_SEEK_OP_TOTAL: IntCounter = INDEX_IO_OP_TOTAL
340        .with_label_values(&["seek", "puffin"]);
341    /// Counter of write operations on puffin files.
342    pub static ref INDEX_PUFFIN_WRITE_OP_TOTAL: IntCounter = INDEX_IO_OP_TOTAL
343        .with_label_values(&["write", "puffin"]);
344    /// Counter of flush operations on puffin files.
345    pub static ref INDEX_PUFFIN_FLUSH_OP_TOTAL: IntCounter = INDEX_IO_OP_TOTAL
346        .with_label_values(&["flush", "puffin"]);
347    /// Counter of read operations on intermediate files.
348    pub static ref INDEX_INTERMEDIATE_READ_OP_TOTAL: IntCounter = INDEX_IO_OP_TOTAL
349        .with_label_values(&["read", "intermediate"]);
350    /// Counter of seek operations on intermediate files.
351    pub static ref INDEX_INTERMEDIATE_SEEK_OP_TOTAL: IntCounter = INDEX_IO_OP_TOTAL
352        .with_label_values(&["seek", "intermediate"]);
353    /// Counter of write operations on intermediate files.
354    pub static ref INDEX_INTERMEDIATE_WRITE_OP_TOTAL: IntCounter = INDEX_IO_OP_TOTAL
355        .with_label_values(&["write", "intermediate"]);
356    /// Counter of flush operations on intermediate files.
357    pub static ref INDEX_INTERMEDIATE_FLUSH_OP_TOTAL: IntCounter = INDEX_IO_OP_TOTAL
358        .with_label_values(&["flush", "intermediate"]);
359    // ------- End of index metrics.
360
361    /// Partition tree memtable data buffer freeze metrics
362    pub static ref PARTITION_TREE_DATA_BUFFER_FREEZE_STAGE_ELAPSED: HistogramVec = register_histogram_vec!(
363        "greptime_partition_tree_buffer_freeze_stage_elapsed",
364        "mito partition tree data buffer freeze stage elapsed",
365        &[STAGE_LABEL],
366        // 0.01 ~ 1000
367        exponential_buckets(0.01, 10.0, 6).unwrap(),
368    )
369    .unwrap();
370
371    /// Partition tree memtable read path metrics
372    pub static ref PARTITION_TREE_READ_STAGE_ELAPSED: HistogramVec = register_histogram_vec!(
373        "greptime_partition_tree_read_stage_elapsed",
374        "mito partition tree read stage elapsed",
375        &[STAGE_LABEL],
376        // 0.01 ~ 1000
377        exponential_buckets(0.01, 10.0, 6).unwrap(),
378    )
379    .unwrap();
380
381    // ------- End of partition tree memtable metrics.
382
383
384    // Manifest related metrics:
385
386    /// Elapsed time of manifest operation. Labeled with "op".
387    pub static ref MANIFEST_OP_ELAPSED: HistogramVec = register_histogram_vec!(
388        "greptime_manifest_op_elapsed",
389        "mito manifest operation elapsed",
390        &["op"],
391        // 0.01 ~ 1000
392        exponential_buckets(0.01, 10.0, 6).unwrap(),
393    ).unwrap();
394
395
396    pub static ref REGION_WORKER_HANDLE_WRITE_ELAPSED: HistogramVec = register_histogram_vec!(
397        "greptime_region_worker_handle_write",
398        "elapsed time for handling writes in region worker loop",
399        &["stage"],
400        exponential_buckets(0.001, 10.0, 5).unwrap()
401    ).unwrap();
402
403}
404
405lazy_static! {
406    /// Counter for compaction input file size.
407    pub static ref COMPACTION_INPUT_BYTES: Counter = register_counter!(
408        "greptime_mito_compaction_input_bytes",
409        "mito compaction input file size",
410        ).unwrap();
411
412    /// Counter for compaction output file size.
413    pub static ref COMPACTION_OUTPUT_BYTES: Counter = register_counter!(
414        "greptime_mito_compaction_output_bytes",
415        "mito compaction output file size",
416        ).unwrap();
417}
418
419/// Stager notifier to collect metrics.
420pub struct StagerMetrics {
421    cache_hit: IntCounter,
422    cache_miss: IntCounter,
423    staging_cache_bytes: IntGauge,
424    recycle_cache_bytes: IntGauge,
425    cache_eviction: IntCounter,
426    staging_miss_read: Histogram,
427}
428
429impl StagerMetrics {
430    /// Creates a new stager notifier.
431    pub fn new() -> Self {
432        Self {
433            cache_hit: CACHE_HIT.with_label_values(&[STAGING_TYPE]),
434            cache_miss: CACHE_MISS.with_label_values(&[STAGING_TYPE]),
435            staging_cache_bytes: CACHE_BYTES.with_label_values(&[STAGING_TYPE]),
436            recycle_cache_bytes: CACHE_BYTES.with_label_values(&[RECYCLE_TYPE]),
437            cache_eviction: CACHE_EVICTION.with_label_values(&[STAGING_TYPE, "size"]),
438            staging_miss_read: READ_STAGE_ELAPSED.with_label_values(&["staging_miss_read"]),
439        }
440    }
441}
442
443impl Default for StagerMetrics {
444    fn default() -> Self {
445        Self::new()
446    }
447}
448
449impl StagerNotifier for StagerMetrics {
450    fn on_cache_hit(&self, _size: u64) {
451        self.cache_hit.inc();
452    }
453
454    fn on_cache_miss(&self, _size: u64) {
455        self.cache_miss.inc();
456    }
457
458    fn on_cache_insert(&self, size: u64) {
459        self.staging_cache_bytes.add(size as i64);
460    }
461
462    fn on_load_dir(&self, duration: Duration) {
463        self.staging_miss_read.observe(duration.as_secs_f64());
464    }
465
466    fn on_load_blob(&self, duration: Duration) {
467        self.staging_miss_read.observe(duration.as_secs_f64());
468    }
469
470    fn on_cache_evict(&self, size: u64) {
471        self.cache_eviction.inc();
472        self.staging_cache_bytes.sub(size as i64);
473    }
474
475    fn on_recycle_insert(&self, size: u64) {
476        self.recycle_cache_bytes.add(size as i64);
477    }
478
479    fn on_recycle_clear(&self, size: u64) {
480        self.recycle_cache_bytes.sub(size as i64);
481    }
482}