Skip to main content

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
39// Write metrics.
40lazy_static! {
41    /// Global write buffer size in bytes.
42    pub static ref WRITE_BUFFER_BYTES: IntGauge =
43        register_int_gauge!("greptime_mito_write_buffer_bytes", "mito write buffer bytes").unwrap();
44    /// Global memtable dictionary size in bytes.
45    pub static ref MEMTABLE_DICT_BYTES: IntGauge =
46        register_int_gauge!("greptime_mito_memtable_dict_bytes", "mito memtable dictionary size in bytes").unwrap();
47    /// Gauge for open regions in each worker.
48    pub static ref REGION_COUNT: IntGaugeVec =
49        register_int_gauge_vec!(
50            "greptime_mito_region_count",
51            "mito region count in each worker",
52            &[WORKER_LABEL],
53        ).unwrap();
54    /// Elapsed time to handle requests.
55    pub static ref HANDLE_REQUEST_ELAPSED: HistogramVec = register_histogram_vec!(
56            "greptime_mito_handle_request_elapsed",
57            "mito handle request elapsed",
58            &[TYPE_LABEL],
59            // 0.01 ~ 10000
60            exponential_buckets(0.01, 10.0, 7).unwrap(),
61        )
62        .unwrap();
63
64    // ------ Flush related metrics
65    /// Counter of scheduled flush requests.
66    /// Note that the flush scheduler may merge some flush requests.
67    pub static ref FLUSH_REQUESTS_TOTAL: IntCounterVec = register_int_counter_vec!(
68            "greptime_mito_flush_requests_total",
69            "mito flush requests total",
70            &[FLUSH_REASON]
71        )
72        .unwrap();
73    /// Counter of scheduled failed flush jobs.
74    pub static ref FLUSH_FAILURE_TOTAL: IntCounter =
75        register_int_counter!("greptime_mito_flush_failure_total", "mito flush failure total").unwrap();
76    /// Elapsed time of a flush job.
77    pub static ref FLUSH_ELAPSED: HistogramVec = register_histogram_vec!(
78            "greptime_mito_flush_elapsed",
79            "mito flush elapsed",
80            &[TYPE_LABEL],
81            // 1 ~ 625
82            exponential_buckets(1.0, 5.0, 6).unwrap(),
83        )
84        .unwrap();
85    /// Histogram of flushed bytes.
86    pub static ref FLUSH_BYTES_TOTAL: IntCounter =
87        register_int_counter!("greptime_mito_flush_bytes_total", "mito flush bytes total").unwrap();
88    /// Gauge for inflight flush tasks.
89    pub static ref INFLIGHT_FLUSH_COUNT: IntGauge =
90        register_int_gauge!(
91            "greptime_mito_inflight_flush_count",
92            "inflight flush count",
93        ).unwrap();
94    // ------ End of flush related metrics
95
96
97    // ------ Write related metrics
98    //
99    /// Counter of rejected write requests.
100    pub static ref WRITE_REJECT_TOTAL: IntCounter =
101        register_int_counter!("greptime_mito_write_reject_total", "mito write reject total").unwrap();
102    /// Elapsed time of each write stage.
103    pub static ref WRITE_STAGE_ELAPSED: HistogramVec = register_histogram_vec!(
104            "greptime_mito_write_stage_elapsed",
105            "mito write stage elapsed",
106            &[STAGE_LABEL],
107            // 0.01 ~ 1000
108            exponential_buckets(0.01, 10.0, 6).unwrap(),
109        )
110        .unwrap();
111    /// Counter of rows to write.
112    pub static ref WRITE_ROWS_TOTAL: IntCounterVec = register_int_counter_vec!(
113        "greptime_mito_write_rows_total",
114        "mito write rows total",
115        &[TYPE_LABEL]
116    )
117    .unwrap();
118}
119
120// Compaction metrics.
121lazy_static! {
122    /// Timer of different stages in compaction.
123    /// - pick
124    /// - merge (in parallel)
125    ///   - iter_source
126    ///   - write_batch
127    ///   - update_index
128    ///   - upload_parquet
129    ///   - upload puffin
130    /// - write_manifest
131    pub static ref COMPACTION_STAGE_ELAPSED: HistogramVec = register_histogram_vec!(
132        "greptime_mito_compaction_stage_elapsed",
133        "mito compaction stage elapsed",
134        &[STAGE_LABEL],
135        // 1 ~ 100000
136        exponential_buckets(1.0, 10.0, 6).unwrap(),
137    )
138    .unwrap();
139    /// Timer of whole compaction task.
140    pub static ref COMPACTION_ELAPSED_TOTAL: Histogram =
141        register_histogram!(
142        "greptime_mito_compaction_total_elapsed",
143        "mito compaction total elapsed",
144        // 1 ~ 100000
145        exponential_buckets(1.0, 10.0, 6).unwrap(),
146    ).unwrap();
147    /// Counter of all requested compaction task.
148    pub static ref COMPACTION_REQUEST_COUNT: IntCounter =
149        register_int_counter!("greptime_mito_compaction_requests_total", "mito compaction requests total").unwrap();
150    /// Counter of failed compaction task.
151    pub static ref COMPACTION_FAILURE_COUNT: IntCounter =
152        register_int_counter!("greptime_mito_compaction_failure_total", "mito compaction failure total").unwrap();
153
154    /// Gauge for inflight compaction tasks.
155    pub static ref INFLIGHT_COMPACTION_COUNT: IntGauge =
156        register_int_gauge!(
157            "greptime_mito_inflight_compaction_count",
158            "inflight compaction count",
159        ).unwrap();
160
161    /// Bytes reserved by compaction memory manager.
162    pub static ref COMPACTION_MEMORY_IN_USE: IntGauge =
163        register_int_gauge!(
164            "greptime_mito_compaction_memory_in_use_bytes",
165            "bytes currently reserved for compaction tasks",
166        )
167        .unwrap();
168    /// Configured compaction memory limit.
169    pub static ref COMPACTION_MEMORY_LIMIT: IntGauge =
170        register_int_gauge!(
171            "greptime_mito_compaction_memory_limit_bytes",
172            "maximum bytes allowed for compaction tasks",
173        )
174        .unwrap();
175    /// Wait time to obtain compaction memory.
176    pub static ref COMPACTION_MEMORY_WAIT: Histogram = register_histogram!(
177        "greptime_mito_compaction_memory_wait_seconds",
178        "time waiting for compaction memory",
179        // 0.01s ~ ~10s
180        exponential_buckets(0.01, 2.0, 10).unwrap(),
181    ).unwrap();
182    /// Counter of rejected compaction memory allocations.
183    pub static ref COMPACTION_MEMORY_REJECTED: IntCounterVec =
184        register_int_counter_vec!(
185            "greptime_mito_compaction_memory_rejected_total",
186            "number of compaction tasks rejected due to memory limit",
187            &[TYPE_LABEL]
188        ).unwrap();
189}
190
191// Query metrics.
192lazy_static! {
193    /// Timer of different stages in query.
194    pub static ref READ_STAGE_ELAPSED: HistogramVec = register_histogram_vec!(
195        "greptime_mito_read_stage_elapsed",
196        "mito read stage elapsed",
197        &[STAGE_LABEL],
198        // 0.01 ~ 10000
199        exponential_buckets(0.01, 10.0, 7).unwrap(),
200    )
201    .unwrap();
202    pub static ref READ_STAGE_FETCH_PAGES: Histogram = READ_STAGE_ELAPSED.with_label_values(&["fetch_pages"]);
203    /// Number of in-progress scan per partition.
204    pub static ref IN_PROGRESS_SCAN: IntGaugeVec = register_int_gauge_vec!(
205        "greptime_mito_in_progress_scan",
206        "mito in progress scan per partition",
207        &[TYPE_LABEL, PARTITION_LABEL]
208    )
209    .unwrap();
210    /// Counter of rows read from different source.
211    pub static ref READ_ROWS_TOTAL: IntCounterVec =
212        register_int_counter_vec!("greptime_mito_read_rows_total", "mito read rows total", &[TYPE_LABEL]).unwrap();
213    /// Counter of filtered rows during merge.
214    pub static ref MERGE_FILTER_ROWS_TOTAL: IntCounterVec =
215        register_int_counter_vec!("greptime_mito_merge_filter_rows_total", "mito merge filter rows total", &[TYPE_LABEL]).unwrap();
216    /// Counter of row groups read.
217    pub static ref READ_ROW_GROUPS_TOTAL: IntCounterVec =
218        register_int_counter_vec!("greptime_mito_read_row_groups_total", "mito read row groups total", &[TYPE_LABEL]).unwrap();
219    /// Counter of filtered rows by precise filter.
220    pub static ref PRECISE_FILTER_ROWS_TOTAL: IntCounterVec =
221        register_int_counter_vec!("greptime_mito_precise_filter_rows_total", "mito precise filter rows total", &[TYPE_LABEL]).unwrap();
222    pub static ref READ_ROWS_IN_ROW_GROUP_TOTAL: IntCounterVec =
223        register_int_counter_vec!("greptime_mito_read_rows_in_row_group_total", "mito read rows in row group total", &[TYPE_LABEL]).unwrap();
224    /// Histogram for the number of SSTs to scan per query.
225    pub static ref READ_SST_COUNT: Histogram = register_histogram!(
226        "greptime_mito_read_sst_count",
227        "Number of SSTs to scan in a scan task",
228        vec![1.0, 4.0, 8.0, 16.0, 32.0, 64.0, 256.0, 1024.0],
229    ).unwrap();
230    /// Histogram for the number of rows returned per query.
231    pub static ref READ_ROWS_RETURN: Histogram = register_histogram!(
232        "greptime_mito_read_rows_return",
233        "Number of rows returned in a scan task",
234        exponential_buckets(100.0, 10.0, 8).unwrap(),
235    ).unwrap();
236    /// Histogram for the number of batches returned per query.
237    pub static ref READ_BATCHES_RETURN: Histogram = register_histogram!(
238        "greptime_mito_read_batches_return",
239        "Number of rows returned in a scan task",
240        exponential_buckets(100.0, 10.0, 7).unwrap(),
241    ).unwrap();
242    /// Gauge for scan memory usage in bytes.
243    pub static ref SCAN_MEMORY_USAGE_BYTES: IntGauge = register_int_gauge!(
244        "greptime_mito_scan_memory_usage_bytes",
245        "current scan memory usage in bytes"
246    ).unwrap();
247    /// Counter of scan allocation attempts that could not acquire memory immediately.
248    pub static ref SCAN_MEMORY_EXHAUSTED_TOTAL: IntCounter = register_int_counter!(
249        "greptime_mito_scan_memory_exhausted_total",
250        "total number of times scan memory was unavailable for immediate acquisition"
251    ).unwrap();
252    /// Counter of scan requests that ultimately failed due to memory pressure.
253    pub static ref SCAN_REQUESTS_REJECTED_TOTAL: IntCounter = register_int_counter!(
254        "greptime_mito_scan_requests_rejected_total",
255        "total number of scan requests that ultimately failed due to memory limit"
256    ).unwrap();
257    /// Gauge for active file range builders in the pruner.
258    pub static ref PRUNER_ACTIVE_BUILDERS: IntGauge = register_int_gauge!(
259        "greptime_mito_pruner_active_builders",
260        "number of active file range builders in the pruner"
261    ).unwrap();
262}
263
264// Cache metrics.
265lazy_static! {
266    /// Cache hit counter.
267    pub static ref CACHE_HIT: IntCounterVec = register_int_counter_vec!(
268        "greptime_mito_cache_hit",
269        "mito cache hit",
270        &[TYPE_LABEL]
271    )
272    .unwrap();
273    /// Cache miss counter.
274    pub static ref CACHE_MISS: IntCounterVec = register_int_counter_vec!(
275        "greptime_mito_cache_miss",
276        "mito cache miss",
277        &[TYPE_LABEL]
278    )
279    .unwrap();
280    /// Cache size in bytes.
281    pub static ref CACHE_BYTES: IntGaugeVec = register_int_gauge_vec!(
282        "greptime_mito_cache_bytes",
283        "mito cache bytes",
284        &[TYPE_LABEL]
285    )
286    .unwrap();
287    /// Download bytes counter in the write cache.
288    pub static ref WRITE_CACHE_DOWNLOAD_BYTES_TOTAL: IntCounter = register_int_counter!(
289        "mito_write_cache_download_bytes_total",
290        "mito write cache download bytes total",
291    ).unwrap();
292    /// Timer of the downloading task in the write cache.
293    pub static ref WRITE_CACHE_DOWNLOAD_ELAPSED: HistogramVec = register_histogram_vec!(
294        "mito_write_cache_download_elapsed",
295        "mito write cache download elapsed",
296        &[TYPE_LABEL],
297        // 0.1 ~ 10000
298        exponential_buckets(0.1, 10.0, 6).unwrap(),
299    ).unwrap();
300    /// Number of inflight download tasks.
301    pub static ref WRITE_CACHE_INFLIGHT_DOWNLOAD: IntGauge = register_int_gauge!(
302        "mito_write_cache_inflight_download_count",
303        "mito write cache inflight download tasks",
304    ).unwrap();
305    /// Upload bytes counter.
306    pub static ref UPLOAD_BYTES_TOTAL: IntCounter = register_int_counter!(
307        "mito_upload_bytes_total",
308        "mito upload bytes total",
309    )
310    .unwrap();
311    /// Cache eviction counter, labeled with cache type and eviction reason.
312    pub static ref CACHE_EVICTION: IntCounterVec = register_int_counter_vec!(
313        "greptime_mito_cache_eviction",
314        "mito cache eviction",
315        &[TYPE_LABEL, CACHE_EVICTION_CAUSE]
316    ).unwrap();
317}
318
319// Index metrics.
320lazy_static! {
321    /// Approximate published index bytes in open regions, refreshed by maintenance.
322    pub static ref SERIES_INDEX_DISK_BYTES: IntGauge = register_int_gauge!(
323        "greptime_mito_series_index_disk_bytes", "estimated series and range index bytes in open regions"
324    ).unwrap();
325    /// Maintenance passes deferred by the estimated disk usage.
326    pub static ref SERIES_INDEX_CAPACITY_DEFERRED: IntCounter = register_int_counter!(
327        "greptime_mito_series_index_capacity_deferred_total", "series-index capacity deferrals"
328    ).unwrap();
329    // Index metrics.
330    /// Outcomes of series-index reconciliation passes.
331    pub static ref SERIES_INDEX_RECONCILE_TOTAL: IntCounterVec = register_int_counter_vec!(
332        "greptime_mito_series_index_reconcile_total",
333        "series-index reconciliation passes",
334        &["result"],
335    ).unwrap();
336    /// Elapsed time of series-index reconciliation phases.
337    pub static ref SERIES_INDEX_RECONCILE_ELAPSED: HistogramVec = register_histogram_vec!(
338        "greptime_mito_series_index_reconcile_elapsed",
339        "series-index reconciliation elapsed time",
340        &["phase"],
341        exponential_buckets(0.01, 10.0, 7).unwrap(),
342    ).unwrap();
343    /// Series and range index file operations.
344    pub static ref SERIES_INDEX_FILE_OPERATION_TOTAL: IntCounterVec = register_int_counter_vec!(
345        "greptime_mito_series_index_file_operation_total",
346        "series-index file operations",
347        &["index_type", "operation", "result"],
348    ).unwrap();
349    /// Number of stale index publications rejected at each publication stage.
350    pub static ref INDEX_PUBLICATION_STALE_TOTAL: IntCounterVec =
351        register_int_counter_vec!(
352            "greptime_mito_index_publication_stale_total",
353            "stale index publications rejected",
354            &[STAGE_LABEL],
355        ).unwrap();
356    /// Number of failures while cleaning local state for stale index artifacts.
357    pub static ref INDEX_ARTIFACT_CLEANUP_FAILURE_TOTAL: IntCounter =
358        register_int_counter!(
359            "greptime_mito_index_artifact_cleanup_failure_total",
360            "failures while cleaning local state for stale index artifacts",
361        ).unwrap();
362    /// Timer of index application.
363    pub static ref INDEX_APPLY_ELAPSED: HistogramVec = register_histogram_vec!(
364        "greptime_index_apply_elapsed",
365        "index apply elapsed",
366        &[TYPE_LABEL],
367        // 0.01 ~ 1000
368        exponential_buckets(0.01, 10.0, 6).unwrap(),
369    )
370    .unwrap();
371    /// Gauge of index apply memory usage.
372    pub static ref INDEX_APPLY_MEMORY_USAGE: IntGauge = register_int_gauge!(
373        "greptime_index_apply_memory_usage",
374        "index apply memory usage",
375    )
376    .unwrap();
377    /// Timer of index creation.
378    pub static ref INDEX_CREATE_ELAPSED: HistogramVec = register_histogram_vec!(
379        "greptime_index_create_elapsed",
380        "index create elapsed",
381        &[STAGE_LABEL, TYPE_LABEL],
382        // 0.1 ~ 10000
383        exponential_buckets(0.1, 10.0, 6).unwrap(),
384    )
385    .unwrap();
386    /// Counter of rows indexed.
387    pub static ref INDEX_CREATE_ROWS_TOTAL: IntCounterVec = register_int_counter_vec!(
388        "greptime_index_create_rows_total",
389        "index create rows total",
390        &[TYPE_LABEL],
391    )
392    .unwrap();
393    /// Counter of created index bytes.
394    pub static ref INDEX_CREATE_BYTES_TOTAL: IntCounterVec = register_int_counter_vec!(
395        "greptime_index_create_bytes_total",
396        "index create bytes total",
397        &[TYPE_LABEL],
398    )
399    .unwrap();
400    /// Gauge of index create memory usage.
401    pub static ref INDEX_CREATE_MEMORY_USAGE: IntGaugeVec = register_int_gauge_vec!(
402        "greptime_index_create_memory_usage",
403        "index create memory usage",
404        &[TYPE_LABEL],
405    ).unwrap();
406    /// Counter of r/w bytes on index related IO operations.
407    pub static ref INDEX_IO_BYTES_TOTAL: IntCounterVec = register_int_counter_vec!(
408        "greptime_index_io_bytes_total",
409        "index io bytes total",
410        &[TYPE_LABEL, FILE_TYPE_LABEL]
411    )
412    .unwrap();
413    /// Counter of read bytes on puffin files.
414    pub static ref INDEX_PUFFIN_READ_BYTES_TOTAL: IntCounter = INDEX_IO_BYTES_TOTAL
415        .with_label_values(&["read", "puffin"]);
416    /// Counter of write bytes on puffin files.
417    pub static ref INDEX_PUFFIN_WRITE_BYTES_TOTAL: IntCounter = INDEX_IO_BYTES_TOTAL
418        .with_label_values(&["write", "puffin"]);
419    /// Counter of read bytes on intermediate files.
420    pub static ref INDEX_INTERMEDIATE_READ_BYTES_TOTAL: IntCounter = INDEX_IO_BYTES_TOTAL
421        .with_label_values(&["read", "intermediate"]);
422    /// Counter of write bytes on intermediate files.
423    pub static ref INDEX_INTERMEDIATE_WRITE_BYTES_TOTAL: IntCounter = INDEX_IO_BYTES_TOTAL
424        .with_label_values(&["write", "intermediate"]);
425
426    /// Counter of r/w operations on index related IO operations, e.g. read, write, seek and flush.
427    pub static ref INDEX_IO_OP_TOTAL: IntCounterVec = register_int_counter_vec!(
428        "greptime_index_io_op_total",
429        "index io op total",
430        &[TYPE_LABEL, FILE_TYPE_LABEL]
431    )
432    .unwrap();
433    /// Counter of read operations on puffin files.
434    pub static ref INDEX_PUFFIN_READ_OP_TOTAL: IntCounter = INDEX_IO_OP_TOTAL
435        .with_label_values(&["read", "puffin"]);
436    /// Counter of seek operations on puffin files.
437    pub static ref INDEX_PUFFIN_SEEK_OP_TOTAL: IntCounter = INDEX_IO_OP_TOTAL
438        .with_label_values(&["seek", "puffin"]);
439    /// Counter of write operations on puffin files.
440    pub static ref INDEX_PUFFIN_WRITE_OP_TOTAL: IntCounter = INDEX_IO_OP_TOTAL
441        .with_label_values(&["write", "puffin"]);
442    /// Counter of flush operations on puffin files.
443    pub static ref INDEX_PUFFIN_FLUSH_OP_TOTAL: IntCounter = INDEX_IO_OP_TOTAL
444        .with_label_values(&["flush", "puffin"]);
445    /// Counter of read operations on intermediate files.
446    pub static ref INDEX_INTERMEDIATE_READ_OP_TOTAL: IntCounter = INDEX_IO_OP_TOTAL
447        .with_label_values(&["read", "intermediate"]);
448    /// Counter of seek operations on intermediate files.
449    pub static ref INDEX_INTERMEDIATE_SEEK_OP_TOTAL: IntCounter = INDEX_IO_OP_TOTAL
450        .with_label_values(&["seek", "intermediate"]);
451    /// Counter of write operations on intermediate files.
452    pub static ref INDEX_INTERMEDIATE_WRITE_OP_TOTAL: IntCounter = INDEX_IO_OP_TOTAL
453        .with_label_values(&["write", "intermediate"]);
454    /// Counter of flush operations on intermediate files.
455    pub static ref INDEX_INTERMEDIATE_FLUSH_OP_TOTAL: IntCounter = INDEX_IO_OP_TOTAL
456        .with_label_values(&["flush", "intermediate"]);
457}
458
459lazy_static! {
460    // Manifest related metrics:
461
462    /// Elapsed time of manifest operation. Labeled with "op".
463    pub static ref MANIFEST_OP_ELAPSED: HistogramVec = register_histogram_vec!(
464        "greptime_manifest_op_elapsed",
465        "mito manifest operation elapsed",
466        &["op"],
467        // 0.01 ~ 1000
468        exponential_buckets(0.01, 10.0, 6).unwrap(),
469    ).unwrap();
470
471
472    pub static ref REGION_WORKER_HANDLE_WRITE_ELAPSED: HistogramVec = register_histogram_vec!(
473        "greptime_region_worker_handle_write",
474        "elapsed time for handling writes in region worker loop",
475        &["stage"],
476        exponential_buckets(0.001, 10.0, 5).unwrap()
477    ).unwrap();
478
479}
480
481lazy_static! {
482    /// Counter for compaction input file size.
483    pub static ref COMPACTION_INPUT_BYTES: Counter = register_counter!(
484        "greptime_mito_compaction_input_bytes",
485        "mito compaction input file size",
486        ).unwrap();
487
488    /// Counter for compaction output file size.
489    pub static ref COMPACTION_OUTPUT_BYTES: Counter = register_counter!(
490        "greptime_mito_compaction_output_bytes",
491        "mito compaction output file size",
492        ).unwrap();
493
494    /// Active series count in TimeSeriesMemtable
495    pub static ref MEMTABLE_ACTIVE_SERIES_COUNT: IntGauge = register_int_gauge!(
496        "greptime_mito_memtable_active_series_count",
497        "active time series count in TimeSeriesMemtable",
498        ).unwrap();
499
500    /// Active field builder count in TimeSeriesMemtable
501    pub static ref MEMTABLE_ACTIVE_FIELD_BUILDER_COUNT: IntGauge = register_int_gauge!(
502        "greptime_mito_memtable_field_builder_count",
503        "active field builder count in TimeSeriesMemtable",
504        ).unwrap();
505
506    /// Number of stalling write requests in each worker.
507    pub static ref WRITE_STALLING: IntGaugeVec = register_int_gauge_vec!(
508            "greptime_mito_write_stalling_count",
509            "mito stalled write request in each worker",
510            &[WORKER_LABEL]
511        ).unwrap();
512    /// Number of ref files
513    pub static ref GC_REF_FILE_CNT: IntGauge = register_int_gauge!(
514            "greptime_gc_ref_file_count",
515            "gc ref file count",
516        ).unwrap();
517    /// Total number of stalled write requests.
518    pub static ref WRITE_STALL_TOTAL: IntCounter = register_int_counter!(
519        "greptime_mito_write_stall_total",
520        "Total number of stalled write requests"
521    ).unwrap();
522    /// Time waiting for requests to be handled by the region worker.
523    pub static ref REQUEST_WAIT_TIME: HistogramVec = register_histogram_vec!(
524            "greptime_mito_request_wait_time",
525            "mito request wait time before being handled by region worker",
526            &[WORKER_LABEL],
527            // 0.001 ~ 10000
528            exponential_buckets(0.001, 10.0, 8).unwrap(),
529        )
530        .unwrap();
531
532    /// Counter for the number of files deleted by the GC worker.
533    pub static ref GC_DELETE_FILE_CNT: IntCounter =
534        register_int_counter!(
535            "greptime_mito_gc_delete_file_count",
536            "mito gc deleted file count",
537        ).unwrap();
538
539    /// Counter for the number of unparsable files skipped by GC.
540    pub static ref GC_SKIPPED_UNPARSABLE_FILES: IntCounter =
541        register_int_counter!(
542            "greptime_mito_gc_skipped_unparsable_files",
543            "mito gc skipped unparsable files count",
544        ).unwrap();
545
546    /// Counter for the number of orphaned index files found by GC.
547    pub static ref GC_ORPHANED_INDEX_FILES: IntCounter =
548        register_int_counter!(
549            "greptime_mito_gc_orphaned_index_files",
550            "mito gc orphaned index files count",
551        ).unwrap();
552
553    /// Histogram for GC operation duration by stage.
554    pub static ref GC_DURATION_SECONDS: HistogramVec = register_histogram_vec!(
555        "greptime_mito_gc_duration_seconds",
556        "GC operation duration by stage",
557        &[STAGE_LABEL],
558        exponential_buckets(0.01, 10.0, 6).unwrap(),
559    ).unwrap();
560
561    /// Counter for GC runs by mode.
562    pub static ref GC_RUNS_TOTAL: IntCounterVec = register_int_counter_vec!(
563        "greptime_mito_gc_runs_total",
564        "Total GC runs by mode",
565        &["mode"],
566    ).unwrap();
567
568    /// Counter for GC errors by type.
569    pub static ref GC_ERRORS_TOTAL: IntCounterVec = register_int_counter_vec!(
570        "greptime_mito_gc_errors_total",
571        "Total GC errors by type",
572        &["error_type"],
573    ).unwrap();
574
575    /// Counter for total files deleted by GC, labeled by file type.
576    pub static ref GC_FILES_DELETED_TOTAL: IntCounterVec = register_int_counter_vec!(
577        "greptime_mito_gc_files_deleted_total",
578        "Total files deleted by GC",
579        &[FILE_TYPE_LABEL],
580    ).unwrap();
581
582    /// Total number of files downloaded during cache fill on region open.
583    pub static ref CACHE_FILL_DOWNLOADED_FILES: IntCounter = register_int_counter!(
584        "mito_cache_fill_downloaded_files",
585        "mito cache fill downloaded files count",
586    ).unwrap();
587
588    /// Number of files pending download during cache fill on region open.
589    pub static ref CACHE_FILL_PENDING_FILES: IntGauge = register_int_gauge!(
590        "mito_cache_fill_pending_files",
591        "mito cache fill pending files count",
592    ).unwrap();
593
594    /// Counter of flush files.
595    pub static ref FLUSH_FILE_TOTAL: IntCounter =
596        register_int_counter!("greptime_mito_flush_file_total", "mito flushed file count").unwrap();
597}
598
599/// Stager notifier to collect metrics.
600pub struct StagerMetrics {
601    cache_hit: IntCounter,
602    cache_miss: IntCounter,
603    staging_cache_bytes: IntGauge,
604    recycle_cache_bytes: IntGauge,
605    cache_eviction: IntCounter,
606    staging_miss_read: Histogram,
607}
608
609impl StagerMetrics {
610    /// Creates a new stager notifier.
611    pub fn new() -> Self {
612        Self {
613            cache_hit: CACHE_HIT.with_label_values(&[STAGING_TYPE]),
614            cache_miss: CACHE_MISS.with_label_values(&[STAGING_TYPE]),
615            staging_cache_bytes: CACHE_BYTES.with_label_values(&[STAGING_TYPE]),
616            recycle_cache_bytes: CACHE_BYTES.with_label_values(&[RECYCLE_TYPE]),
617            cache_eviction: CACHE_EVICTION.with_label_values(&[STAGING_TYPE, "size"]),
618            staging_miss_read: READ_STAGE_ELAPSED.with_label_values(&["staging_miss_read"]),
619        }
620    }
621}
622
623impl Default for StagerMetrics {
624    fn default() -> Self {
625        Self::new()
626    }
627}
628
629impl StagerNotifier for StagerMetrics {
630    fn on_cache_hit(&self, _size: u64) {
631        self.cache_hit.inc();
632    }
633
634    fn on_cache_miss(&self, _size: u64) {
635        self.cache_miss.inc();
636    }
637
638    fn on_cache_insert(&self, size: u64) {
639        self.staging_cache_bytes.add(size as i64);
640    }
641
642    fn on_load_dir(&self, duration: Duration) {
643        self.staging_miss_read.observe(duration.as_secs_f64());
644    }
645
646    fn on_load_blob(&self, duration: Duration) {
647        self.staging_miss_read.observe(duration.as_secs_f64());
648    }
649
650    fn on_cache_evict(&self, size: u64) {
651        self.cache_eviction.inc();
652        self.staging_cache_bytes.sub(size as i64);
653    }
654
655    fn on_recycle_insert(&self, size: u64) {
656        self.recycle_cache_bytes.add(size as i64);
657    }
658
659    fn on_recycle_clear(&self, size: u64) {
660        self.recycle_cache_bytes.sub(size as i64);
661    }
662}