typed_store/
metrics.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
use once_cell::sync::OnceCell;
use prometheus::{
    register_histogram_vec_with_registry, register_int_counter_vec_with_registry,
    register_int_gauge_vec_with_registry, HistogramVec, IntCounterVec, IntGaugeVec, Registry,
};
use rocksdb::perf::set_perf_stats;
use rocksdb::{PerfContext, PerfMetric, PerfStatsLevel};
use std::cell::RefCell;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::Duration;
use tap::TapFallible;
use tracing::warn;

thread_local! {
    static PER_THREAD_ROCKS_PERF_CONTEXT: std::cell::RefCell<rocksdb::PerfContext>  = RefCell::new(PerfContext::default());
}

const LATENCY_SEC_BUCKETS: &[f64] = &[
    0.00001, 0.00005, // 10 mcs, 50 mcs
    0.0001, 0.0002, 0.0003, 0.0004, 0.0005, // 100..500 mcs
    0.001, 0.002, 0.003, 0.004, 0.005, // 1..5ms
    0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1., 2.5, 5., 10.,
];

#[derive(Debug, Clone)]
// A struct for sampling based on number of operations or duration.
// Sampling happens if the duration expires and after number of operations
pub struct SamplingInterval {
    // Sample once every time duration
    pub once_every_duration: Duration,
    // Sample once every number of operations
    pub after_num_ops: u64,
    // Counter for keeping track of previous sample
    pub counter: Arc<AtomicU64>,
}

impl Default for SamplingInterval {
    fn default() -> Self {
        // Enabled with 60 second interval
        SamplingInterval::new(Duration::from_secs(60), 0)
    }
}

impl SamplingInterval {
    pub fn new(once_every_duration: Duration, after_num_ops: u64) -> Self {
        let counter = Arc::new(AtomicU64::new(1));
        if !once_every_duration.is_zero() {
            let counter = counter.clone();
            tokio::task::spawn(async move {
                loop {
                    if counter.load(Ordering::SeqCst) > after_num_ops {
                        counter.store(0, Ordering::SeqCst);
                    }
                    tokio::time::sleep(once_every_duration).await;
                }
            });
        }
        SamplingInterval {
            once_every_duration,
            after_num_ops,
            counter,
        }
    }
    pub fn new_from_self(&self) -> SamplingInterval {
        SamplingInterval::new(self.once_every_duration, self.after_num_ops)
    }
    pub fn sample(&self) -> bool {
        if self.once_every_duration.is_zero() {
            self.counter.fetch_add(1, Ordering::Relaxed) % (self.after_num_ops + 1) == 0
        } else {
            self.counter.fetch_add(1, Ordering::Relaxed) == 0
        }
    }
}

#[derive(Debug)]
pub struct ColumnFamilyMetrics {
    pub rocksdb_total_sst_files_size: IntGaugeVec,
    pub rocksdb_total_blob_files_size: IntGaugeVec,
    pub rocksdb_total_num_files: IntGaugeVec,
    pub rocksdb_num_level0_files: IntGaugeVec,
    pub rocksdb_current_size_active_mem_tables: IntGaugeVec,
    pub rocksdb_size_all_mem_tables: IntGaugeVec,
    pub rocksdb_num_snapshots: IntGaugeVec,
    pub rocksdb_oldest_snapshot_time: IntGaugeVec,
    pub rocksdb_actual_delayed_write_rate: IntGaugeVec,
    pub rocksdb_is_write_stopped: IntGaugeVec,
    pub rocksdb_block_cache_capacity: IntGaugeVec,
    pub rocksdb_block_cache_usage: IntGaugeVec,
    pub rocksdb_block_cache_pinned_usage: IntGaugeVec,
    pub rocksdb_estimate_table_readers_mem: IntGaugeVec,
    pub rocksdb_num_immutable_mem_tables: IntGaugeVec,
    pub rocksdb_mem_table_flush_pending: IntGaugeVec,
    pub rocksdb_compaction_pending: IntGaugeVec,
    pub rocksdb_estimate_pending_compaction_bytes: IntGaugeVec,
    pub rocksdb_num_running_compactions: IntGaugeVec,
    pub rocksdb_num_running_flushes: IntGaugeVec,
    pub rocksdb_estimate_oldest_key_time: IntGaugeVec,
    pub rocksdb_background_errors: IntGaugeVec,
    pub rocksdb_estimated_num_keys: IntGaugeVec,
    pub rocksdb_base_level: IntGaugeVec,
}

impl ColumnFamilyMetrics {
    pub(crate) fn new(registry: &Registry) -> Self {
        ColumnFamilyMetrics {
            rocksdb_total_sst_files_size: register_int_gauge_vec_with_registry!(
                "rocksdb_total_sst_files_size",
                "The storage size occupied by the sst files in the column family",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            rocksdb_total_blob_files_size: register_int_gauge_vec_with_registry!(
                "rocksdb_total_blob_files_size",
                "The storage size occupied by the blob files in the column family",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            rocksdb_total_num_files: register_int_gauge_vec_with_registry!(
                "rocksdb_total_num_files",
                "Total number of files used in the column family",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            rocksdb_num_level0_files: register_int_gauge_vec_with_registry!(
                "rocksdb_num_level0_files",
                "Number of level 0 files in the column family",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            rocksdb_current_size_active_mem_tables: register_int_gauge_vec_with_registry!(
                "rocksdb_current_size_active_mem_tables",
                "The current approximate size of active memtable (bytes).",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            rocksdb_size_all_mem_tables: register_int_gauge_vec_with_registry!(
                "rocksdb_size_all_mem_tables",
                "The memory size occupied by the column family's in-memory buffer",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            rocksdb_num_snapshots: register_int_gauge_vec_with_registry!(
                "rocksdb_num_snapshots",
                "Number of snapshots held for the column family",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            rocksdb_oldest_snapshot_time: register_int_gauge_vec_with_registry!(
                "rocksdb_oldest_snapshot_time",
                "Unit timestamp of the oldest unreleased snapshot",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            rocksdb_actual_delayed_write_rate: register_int_gauge_vec_with_registry!(
                "rocksdb_actual_delayed_write_rate",
                "The current actual delayed write rate. 0 means no delay",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            rocksdb_is_write_stopped: register_int_gauge_vec_with_registry!(
                "rocksdb_is_write_stopped",
                "A flag indicating whether writes are stopped on this column family. 1 indicates writes have been stopped.",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            rocksdb_block_cache_capacity: register_int_gauge_vec_with_registry!(
                "rocksdb_block_cache_capacity",
                "The block cache capacity of the column family.",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            rocksdb_block_cache_usage: register_int_gauge_vec_with_registry!(
                "rocksdb_block_cache_usage",
                "The memory size used by the column family in the block cache.",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            rocksdb_block_cache_pinned_usage: register_int_gauge_vec_with_registry!(
                "rocksdb_block_cache_pinned_usage",
                "The memory size used by the column family in the block cache where entries are pinned",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            rocksdb_estimate_table_readers_mem: register_int_gauge_vec_with_registry!(
                "rocksdb_estimate_table_readers_mem",
                "The estimated memory size used for reading SST tables in this column
                family such as filters and index blocks. Note that this number does not
                include the memory used in block cache.",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            rocksdb_num_immutable_mem_tables: register_int_gauge_vec_with_registry!(
                "rocksdb_num_immutable_mem_tables",
                "The number of immutable memtables that have not yet been flushed.",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            rocksdb_mem_table_flush_pending: register_int_gauge_vec_with_registry!(
                "rocksdb_mem_table_flush_pending",
                "A 1 or 0 flag indicating whether a memtable flush is pending.
                If this number is 1, it means a memtable is waiting for being flushed,
                but there might be too many L0 files that prevents it from being flushed.",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            rocksdb_compaction_pending: register_int_gauge_vec_with_registry!(
                "rocksdb_compaction_pending",
                "A 1 or 0 flag indicating whether a compaction job is pending.
                If this number is 1, it means some part of the column family requires
                compaction in order to maintain shape of LSM tree, but the compaction
                is pending because the desired compaction job is either waiting for
                other dependent compactions to be finished or waiting for an available
                compaction thread.",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            rocksdb_estimate_pending_compaction_bytes: register_int_gauge_vec_with_registry!(
                "rocksdb_estimate_pending_compaction_bytes",
                "Estimated total number of bytes compaction needs to rewrite to get all levels down
                to under target size. Not valid for other compactions than level-based.",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            rocksdb_num_running_compactions: register_int_gauge_vec_with_registry!(
                "rocksdb_num_running_compactions",
                "The number of compactions that are currently running for the column family.",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            rocksdb_num_running_flushes: register_int_gauge_vec_with_registry!(
                "rocksdb_num_running_flushes",
                "The number of flushes that are currently running for the column family.",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            rocksdb_estimate_oldest_key_time: register_int_gauge_vec_with_registry!(
                "rocksdb_estimate_oldest_key_time",
                "Estimation of the oldest key timestamp in the DB. Only available
                for FIFO compaction with compaction_options_fifo.allow_compaction = false.",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            rocksdb_estimated_num_keys: register_int_gauge_vec_with_registry!(
                "rocksdb_estimated_num_keys",
                "The estimated number of keys in the table",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            rocksdb_background_errors: register_int_gauge_vec_with_registry!(
                "rocksdb_background_errors",
                "The accumulated number of RocksDB background errors.",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            rocksdb_base_level: register_int_gauge_vec_with_registry!(
                "rocksdb_base_level",
                "The number of level to which L0 data will be compacted.",
                &["cf_name"],
                registry,
            )
            .unwrap(),
        }
    }
}

#[derive(Debug)]
pub struct OperationMetrics {
    pub rocksdb_iter_latency_seconds: HistogramVec,
    pub rocksdb_iter_bytes: HistogramVec,
    pub rocksdb_iter_keys: HistogramVec,
    pub rocksdb_get_latency_seconds: HistogramVec,
    pub rocksdb_get_bytes: HistogramVec,
    pub rocksdb_multiget_latency_seconds: HistogramVec,
    pub rocksdb_multiget_bytes: HistogramVec,
    pub rocksdb_put_latency_seconds: HistogramVec,
    pub rocksdb_put_bytes: HistogramVec,
    pub rocksdb_batch_put_bytes: HistogramVec,
    pub rocksdb_delete_latency_seconds: HistogramVec,
    pub rocksdb_deletes: IntCounterVec,
    pub rocksdb_batch_commit_latency_seconds: HistogramVec,
    pub rocksdb_batch_commit_bytes: HistogramVec,
    pub rocksdb_num_active_db_handles: IntGaugeVec,
    pub rocksdb_very_slow_batch_writes_count: IntCounterVec,
    pub rocksdb_very_slow_batch_writes_duration_ms: IntCounterVec,
    pub rocksdb_very_slow_puts_count: IntCounterVec,
    pub rocksdb_very_slow_puts_duration_ms: IntCounterVec,
}

impl OperationMetrics {
    pub(crate) fn new(registry: &Registry) -> Self {
        OperationMetrics {
            rocksdb_iter_latency_seconds: register_histogram_vec_with_registry!(
                "rocksdb_iter_latency_seconds",
                "Rocksdb iter latency in seconds",
                &["cf_name"],
                LATENCY_SEC_BUCKETS.to_vec(),
                registry,
            )
            .unwrap(),
            rocksdb_iter_bytes: register_histogram_vec_with_registry!(
                "rocksdb_iter_bytes",
                "Rocksdb iter size in bytes",
                &["cf_name"],
                prometheus::exponential_buckets(1.0, 4.0, 15)
                    .unwrap()
                    .to_vec(),
                registry,
            )
            .unwrap(),
            rocksdb_iter_keys: register_histogram_vec_with_registry!(
                "rocksdb_iter_keys",
                "Rocksdb iter num keys",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            rocksdb_get_latency_seconds: register_histogram_vec_with_registry!(
                "rocksdb_get_latency_seconds",
                "Rocksdb get latency in seconds",
                &["cf_name"],
                LATENCY_SEC_BUCKETS.to_vec(),
                registry,
            )
            .unwrap(),
            rocksdb_get_bytes: register_histogram_vec_with_registry!(
                "rocksdb_get_bytes",
                "Rocksdb get call returned data size in bytes",
                &["cf_name"],
                prometheus::exponential_buckets(1.0, 4.0, 15)
                    .unwrap()
                    .to_vec(),
                registry
            )
            .unwrap(),
            rocksdb_multiget_latency_seconds: register_histogram_vec_with_registry!(
                "rocksdb_multiget_latency_seconds",
                "Rocksdb multiget latency in seconds",
                &["cf_name"],
                LATENCY_SEC_BUCKETS.to_vec(),
                registry,
            )
            .unwrap(),
            rocksdb_multiget_bytes: register_histogram_vec_with_registry!(
                "rocksdb_multiget_bytes",
                "Rocksdb multiget call returned data size in bytes",
                &["cf_name"],
                prometheus::exponential_buckets(1.0, 4.0, 15)
                    .unwrap()
                    .to_vec(),
                registry,
            )
            .unwrap(),
            rocksdb_put_latency_seconds: register_histogram_vec_with_registry!(
                "rocksdb_put_latency_seconds",
                "Rocksdb put latency in seconds",
                &["cf_name"],
                LATENCY_SEC_BUCKETS.to_vec(),
                registry,
            )
            .unwrap(),
            rocksdb_put_bytes: register_histogram_vec_with_registry!(
                "rocksdb_put_bytes",
                "Rocksdb put call puts data size in bytes",
                &["cf_name"],
                prometheus::exponential_buckets(1.0, 4.0, 15)
                    .unwrap()
                    .to_vec(),
                registry,
            )
            .unwrap(),
            rocksdb_batch_put_bytes: register_histogram_vec_with_registry!(
                "rocksdb_batch_put_bytes",
                "Rocksdb batch put call puts data size in bytes",
                &["cf_name"],
                prometheus::exponential_buckets(1.0, 4.0, 15)
                    .unwrap()
                    .to_vec(),
                registry,
            )
            .unwrap(),
            rocksdb_delete_latency_seconds: register_histogram_vec_with_registry!(
                "rocksdb_delete_latency_seconds",
                "Rocksdb delete latency in seconds",
                &["cf_name"],
                LATENCY_SEC_BUCKETS.to_vec(),
                registry,
            )
            .unwrap(),
            rocksdb_deletes: register_int_counter_vec_with_registry!(
                "rocksdb_deletes",
                "Rocksdb delete calls",
                &["cf_name"],
                registry
            )
            .unwrap(),
            rocksdb_batch_commit_latency_seconds: register_histogram_vec_with_registry!(
                "rocksdb_write_batch_commit_latency_seconds",
                "Rocksdb schema batch commit latency in seconds",
                &["db_name"],
                LATENCY_SEC_BUCKETS.to_vec(),
                registry,
            )
            .unwrap(),
            rocksdb_batch_commit_bytes: register_histogram_vec_with_registry!(
                "rocksdb_batch_commit_bytes",
                "Rocksdb schema batch commit size in bytes",
                &["db_name"],
                prometheus::exponential_buckets(1.0, 4.0, 15)
                    .unwrap()
                    .to_vec(),
                registry,
            )
            .unwrap(),
            rocksdb_num_active_db_handles: register_int_gauge_vec_with_registry!(
                "rocksdb_num_active_db_handles",
                "Number of active db handles",
                &["db_name"],
                registry,
            )
            .unwrap(),
            rocksdb_very_slow_batch_writes_count: register_int_counter_vec_with_registry!(
                "rocksdb_num_very_slow_batch_writes",
                "Number of batch writes that took more than 1 second",
                &["db_name"],
                registry,
            )
            .unwrap(),
            rocksdb_very_slow_batch_writes_duration_ms: register_int_counter_vec_with_registry!(
                "rocksdb_very_slow_batch_writes_duration",
                "Total duration of batch writes that took more than 1 second",
                &["db_name"],
                registry,
            )
            .unwrap(),
            rocksdb_very_slow_puts_count: register_int_counter_vec_with_registry!(
                "rocksdb_num_very_slow_puts",
                "Number of puts that took more than 1 second",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            rocksdb_very_slow_puts_duration_ms: register_int_counter_vec_with_registry!(
                "rocksdb_very_slow_puts_duration",
                "Total duration of puts that took more than 1 second",
                &["cf_name"],
                registry,
            )
            .unwrap(),
        }
    }
}

pub struct RocksDBPerfContext;

impl Default for RocksDBPerfContext {
    fn default() -> Self {
        set_perf_stats(PerfStatsLevel::EnableTime);
        PER_THREAD_ROCKS_PERF_CONTEXT.with(|perf_context| {
            perf_context.borrow_mut().reset();
        });
        RocksDBPerfContext {}
    }
}

impl Drop for RocksDBPerfContext {
    fn drop(&mut self) {
        set_perf_stats(PerfStatsLevel::Disable);
    }
}

#[derive(Debug)]
pub struct ReadPerfContextMetrics {
    pub user_key_comparison_count: IntCounterVec,
    pub block_cache_hit_count: IntCounterVec,
    pub block_read_count: IntCounterVec,
    pub block_read_byte: IntCounterVec,
    pub block_read_nanos: IntCounterVec,
    pub block_checksum_nanos: IntCounterVec,
    pub block_decompress_nanos: IntCounterVec,
    pub get_read_bytes: IntCounterVec,
    pub multiget_read_bytes: IntCounterVec,
    pub get_snapshot_nanos: IntCounterVec,
    pub get_from_memtable_nanos: IntCounterVec,
    pub get_from_memtable_count: IntCounterVec,
    pub get_post_process_nanos: IntCounterVec,
    pub get_from_output_files_nanos: IntCounterVec,
    pub db_mutex_lock_nanos: IntCounterVec,
    pub db_condition_wait_nanos: IntCounterVec,
    pub merge_operator_nanos: IntCounterVec,
    pub read_index_block_nanos: IntCounterVec,
    pub read_filter_block_nanos: IntCounterVec,
    pub new_table_block_iter_nanos: IntCounterVec,
    pub block_seek_nanos: IntCounterVec,
    pub find_table_nanos: IntCounterVec,
    pub bloom_memtable_hit_count: IntCounterVec,
    pub bloom_memtable_miss_count: IntCounterVec,
    pub bloom_sst_hit_count: IntCounterVec,
    pub bloom_sst_miss_count: IntCounterVec,
    pub key_lock_wait_time: IntCounterVec,
    pub key_lock_wait_count: IntCounterVec,
    pub internal_delete_skipped_count: IntCounterVec,
    pub internal_skipped_count: IntCounterVec,
}

impl ReadPerfContextMetrics {
    pub(crate) fn new(registry: &Registry) -> Self {
        ReadPerfContextMetrics {
            user_key_comparison_count: register_int_counter_vec_with_registry!(
                "user_key_comparison_count",
                "Helps us figure out whether too many comparisons in binary search can be a problem,
                especially when a more expensive comparator is used. Moreover, since number of comparisons
                is usually uniform based on the memtable size, the SST file size for Level 0 and size of other
                levels, an significant increase of the counter can indicate unexpected LSM-tree shape.
                You may want to check whether flush/compaction can keep up with the write speed",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            block_cache_hit_count: register_int_counter_vec_with_registry!(
                "block_cache_hit_count",
                "Tells us how many times we read data blocks from block cache, and block_read_count tells us how many
                times we have to read blocks from the file system (either block cache is disabled or it is a cache miss).
                We can evaluate the block cache efficiency by looking at the two counters over time.",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            block_read_count: register_int_counter_vec_with_registry!(
                "block_read_count",
                "Tells us how many times we have to read blocks from the file system (either block cache is disabled or it is a cache miss)",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            block_read_byte: register_int_counter_vec_with_registry!(
                "block_read_byte",
                "Tells us how many total bytes we read from the file system. It can tell us whether a slow query can be caused by reading
                large blocks from the file system. Index and bloom filter blocks are usually large blocks. A large block can also be the result
                of a very large key or value",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            block_read_nanos: register_int_counter_vec_with_registry!(
                "block_read_nanos",
                "Total nanos spent on block reads",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            block_checksum_nanos: register_int_counter_vec_with_registry!(
                "block_checksum_nanos",
                "Total nanos spent on verifying block checksum",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            block_decompress_nanos: register_int_counter_vec_with_registry!(
                "block_decompress_nanos",
                "Total nanos spent on decompressing a block",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            get_read_bytes: register_int_counter_vec_with_registry!(
                "get_read_bytes",
                "Total bytes for values returned by Get",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            multiget_read_bytes: register_int_counter_vec_with_registry!(
                "multiget_read_bytes",
                "Total bytes for values returned by MultiGet.",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            get_snapshot_nanos: register_int_counter_vec_with_registry!(
                "get_snapshot_nanos",
                "Time spent in getting snapshot.",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            get_from_memtable_nanos: register_int_counter_vec_with_registry!(
                "get_from_memtable_nanos",
                "Time spent on reading data from memtable.",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            get_from_memtable_count: register_int_counter_vec_with_registry!(
                "get_from_memtable_count",
                "Number of memtables queried",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            get_post_process_nanos: register_int_counter_vec_with_registry!(
                "get_post_process_nanos",
                "Total nanos spent after Get() finds a key",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            get_from_output_files_nanos: register_int_counter_vec_with_registry!(
                "get_from_output_files_nanos",
                "Total nanos reading from output files",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            db_mutex_lock_nanos: register_int_counter_vec_with_registry!(
                "db_mutex_lock_nanos",
                "Time spent on acquiring db mutex",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            db_condition_wait_nanos: register_int_counter_vec_with_registry!(
                "db_condition_wait_nanos",
                "Time spent waiting with a condition variable created with DB Mutex.",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            merge_operator_nanos: register_int_counter_vec_with_registry!(
                "merge_operator_nanos",
                "Time spent on merge operator.",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            read_index_block_nanos: register_int_counter_vec_with_registry!(
                "read_index_block_nanos",
                "Time spent on reading index block from block cache or SST file",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            read_filter_block_nanos: register_int_counter_vec_with_registry!(
                "read_filter_block_nanos",
                "Time spent on reading filter block from block cache or SST file",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            new_table_block_iter_nanos: register_int_counter_vec_with_registry!(
                "new_table_block_iter_nanos",
                "Time spent on creating data block iterator",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            block_seek_nanos: register_int_counter_vec_with_registry!(
                "block_seek_nanos",
                "Time spent on seeking a key in data/index blocks",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            find_table_nanos: register_int_counter_vec_with_registry!(
                "find_table_nanos",
                "Time spent on finding or creating a table reader",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            bloom_memtable_hit_count: register_int_counter_vec_with_registry!(
                "bloom_memtable_hit_count",
                "Total number of mem table bloom hits",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            bloom_memtable_miss_count: register_int_counter_vec_with_registry!(
                "bloom_memtable_miss_count",
                "Total number of mem table bloom misses",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            bloom_sst_hit_count: register_int_counter_vec_with_registry!(
                "bloom_sst_hit_count",
                "Total number of SST table bloom hits",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            bloom_sst_miss_count: register_int_counter_vec_with_registry!(
                "bloom_sst_miss_count",
                "Total number of SST table bloom misses",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            key_lock_wait_time: register_int_counter_vec_with_registry!(
                "key_lock_wait_time",
                "Time spent waiting on key locks in transaction lock manager",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            key_lock_wait_count: register_int_counter_vec_with_registry!(
                "key_lock_wait_count",
                "Number of times acquiring a lock was blocked by another transaction",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            internal_delete_skipped_count: register_int_counter_vec_with_registry!(
                "internal_delete_skipped_count",
                "Total number of deleted keys skipped during iteration",
                &["cf_name"],
                registry,
            )
                .unwrap(),
            internal_skipped_count: register_int_counter_vec_with_registry!(
                "internal_skipped_count",
                "Totall number of internal keys skipped during iteration",
                &["cf_name"],
                registry,
            )
                .unwrap(),
        }
    }

    pub fn report_metrics(&self, cf_name: &str) {
        PER_THREAD_ROCKS_PERF_CONTEXT.with(|perf_context_cell| {
            set_perf_stats(PerfStatsLevel::Disable);
            let perf_context = perf_context_cell.borrow();
            self.user_key_comparison_count
                .with_label_values(&[cf_name])
                .inc_by(perf_context.metric(PerfMetric::UserKeyComparisonCount));
            self.block_cache_hit_count
                .with_label_values(&[cf_name])
                .inc_by(perf_context.metric(PerfMetric::BlockCacheHitCount));
            self.block_read_count
                .with_label_values(&[cf_name])
                .inc_by(perf_context.metric(PerfMetric::BlockReadCount));
            self.block_read_byte
                .with_label_values(&[cf_name])
                .inc_by(perf_context.metric(PerfMetric::BlockReadByte));
            self.block_read_nanos
                .with_label_values(&[cf_name])
                .inc_by(perf_context.metric(PerfMetric::BlockReadTime));
            self.block_read_count
                .with_label_values(&[cf_name])
                .inc_by(perf_context.metric(PerfMetric::BlockReadCount));
            self.block_checksum_nanos
                .with_label_values(&[cf_name])
                .inc_by(perf_context.metric(PerfMetric::BlockChecksumTime));
            self.block_decompress_nanos
                .with_label_values(&[cf_name])
                .inc_by(perf_context.metric(PerfMetric::BlockDecompressTime));
            self.get_read_bytes
                .with_label_values(&[cf_name])
                .inc_by(perf_context.metric(PerfMetric::GetReadBytes));
            self.multiget_read_bytes
                .with_label_values(&[cf_name])
                .inc_by(perf_context.metric(PerfMetric::MultigetReadBytes));
            self.get_snapshot_nanos
                .with_label_values(&[cf_name])
                .inc_by(perf_context.metric(PerfMetric::GetSnapshotTime));
            self.get_from_memtable_nanos
                .with_label_values(&[cf_name])
                .inc_by(perf_context.metric(PerfMetric::GetFromMemtableTime));
            self.get_from_memtable_count
                .with_label_values(&[cf_name])
                .inc_by(perf_context.metric(PerfMetric::GetFromMemtableCount));
            self.get_post_process_nanos
                .with_label_values(&[cf_name])
                .inc_by(perf_context.metric(PerfMetric::GetPostProcessTime));
            self.get_from_output_files_nanos
                .with_label_values(&[cf_name])
                .inc_by(perf_context.metric(PerfMetric::GetFromOutputFilesTime));
            self.db_mutex_lock_nanos
                .with_label_values(&[cf_name])
                .inc_by(perf_context.metric(PerfMetric::DbMutexLockNanos));
            self.db_condition_wait_nanos
                .with_label_values(&[cf_name])
                .inc_by(perf_context.metric(PerfMetric::DbConditionWaitNanos));
            self.merge_operator_nanos
                .with_label_values(&[cf_name])
                .inc_by(perf_context.metric(PerfMetric::MergeOperatorTimeNanos));
            self.read_index_block_nanos
                .with_label_values(&[cf_name])
                .inc_by(perf_context.metric(PerfMetric::ReadIndexBlockNanos));
            self.read_filter_block_nanos
                .with_label_values(&[cf_name])
                .inc_by(perf_context.metric(PerfMetric::ReadFilterBlockNanos));
            self.new_table_block_iter_nanos
                .with_label_values(&[cf_name])
                .inc_by(perf_context.metric(PerfMetric::NewTableBlockIterNanos));
            self.block_seek_nanos
                .with_label_values(&[cf_name])
                .inc_by(perf_context.metric(PerfMetric::BlockSeekNanos));
            self.find_table_nanos
                .with_label_values(&[cf_name])
                .inc_by(perf_context.metric(PerfMetric::FindTableNanos));
            self.bloom_memtable_hit_count
                .with_label_values(&[cf_name])
                .inc_by(perf_context.metric(PerfMetric::BloomMemtableHitCount));
            self.bloom_memtable_miss_count
                .with_label_values(&[cf_name])
                .inc_by(perf_context.metric(PerfMetric::BloomMemtableMissCount));
            self.bloom_sst_hit_count
                .with_label_values(&[cf_name])
                .inc_by(perf_context.metric(PerfMetric::BloomSstHitCount));
            self.bloom_sst_miss_count
                .with_label_values(&[cf_name])
                .inc_by(perf_context.metric(PerfMetric::BloomSstMissCount));
            self.key_lock_wait_time
                .with_label_values(&[cf_name])
                .inc_by(perf_context.metric(PerfMetric::KeyLockWaitTime));
            self.key_lock_wait_count
                .with_label_values(&[cf_name])
                .inc_by(perf_context.metric(PerfMetric::KeyLockWaitCount));
            self.internal_delete_skipped_count
                .with_label_values(&[cf_name])
                .inc_by(perf_context.metric(PerfMetric::InternalDeleteSkippedCount));
            self.internal_skipped_count
                .with_label_values(&[cf_name])
                .inc_by(perf_context.metric(PerfMetric::InternalKeySkippedCount));
        });
    }
}

#[derive(Debug)]
pub struct WritePerfContextMetrics {
    pub write_wal_nanos: IntCounterVec,
    pub write_memtable_nanos: IntCounterVec,
    pub write_delay_nanos: IntCounterVec,
    pub write_pre_and_post_process_nanos: IntCounterVec,
    pub write_db_mutex_lock_nanos: IntCounterVec,
    pub write_db_condition_wait_nanos: IntCounterVec,
    pub write_key_lock_wait_nanos: IntCounterVec,
    pub write_key_lock_wait_count: IntCounterVec,
}

impl WritePerfContextMetrics {
    pub(crate) fn new(registry: &Registry) -> Self {
        WritePerfContextMetrics {
            write_wal_nanos: register_int_counter_vec_with_registry!(
                "write_wal_nanos",
                "Total nanos spent on writing to WAL",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            write_memtable_nanos: register_int_counter_vec_with_registry!(
                "write_memtable_nanos",
                "Total nanos spent on writing to memtable",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            write_delay_nanos: register_int_counter_vec_with_registry!(
                "write_delay_nanos",
                "Total nanos spent on delaying or throttling write",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            write_pre_and_post_process_nanos: register_int_counter_vec_with_registry!(
                "write_pre_and_post_process_nanos",
                "Total nanos spent on writing a record, excluding the above four things",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            write_db_mutex_lock_nanos: register_int_counter_vec_with_registry!(
                "write_db_mutex_lock_nanos",
                "Time spent on acquiring db mutex",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            write_db_condition_wait_nanos: register_int_counter_vec_with_registry!(
                "write_db_condition_wait_nanos",
                "Time spent waiting with a condition variable created with DB Mutex.",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            write_key_lock_wait_nanos: register_int_counter_vec_with_registry!(
                "write_key_lock_wait_time",
                "Time spent waiting on key locks in transaction lock manager",
                &["cf_name"],
                registry,
            )
            .unwrap(),
            write_key_lock_wait_count: register_int_counter_vec_with_registry!(
                "write_key_lock_wait_count",
                "Number of times acquiring a lock was blocked by another transaction",
                &["cf_name"],
                registry,
            )
            .unwrap(),
        }
    }
    pub fn report_metrics(&self, db_name: &str) {
        PER_THREAD_ROCKS_PERF_CONTEXT.with(|perf_context_cell| {
            set_perf_stats(PerfStatsLevel::Disable);
            let perf_context = perf_context_cell.borrow();
            self.write_wal_nanos
                .with_label_values(&[db_name])
                .inc_by(perf_context.metric(PerfMetric::WriteWalTime));
            self.write_memtable_nanos
                .with_label_values(&[db_name])
                .inc_by(perf_context.metric(PerfMetric::WriteMemtableTime));
            self.write_delay_nanos
                .with_label_values(&[db_name])
                .inc_by(perf_context.metric(PerfMetric::WriteDelayTime));
            self.write_pre_and_post_process_nanos
                .with_label_values(&[db_name])
                .inc_by(perf_context.metric(PerfMetric::WritePreAndPostProcessTime));
            self.write_db_mutex_lock_nanos
                .with_label_values(&[db_name])
                .inc_by(perf_context.metric(PerfMetric::DbMutexLockNanos));
            self.write_db_condition_wait_nanos
                .with_label_values(&[db_name])
                .inc_by(perf_context.metric(PerfMetric::DbConditionWaitNanos));
            self.write_key_lock_wait_nanos
                .with_label_values(&[db_name])
                .inc_by(perf_context.metric(PerfMetric::KeyLockWaitTime));
            self.write_key_lock_wait_count
                .with_label_values(&[db_name])
                .inc_by(perf_context.metric(PerfMetric::KeyLockWaitCount));
        });
    }
}

#[derive(Debug)]
pub struct DBMetrics {
    pub op_metrics: OperationMetrics,
    pub cf_metrics: ColumnFamilyMetrics,
    pub read_perf_ctx_metrics: ReadPerfContextMetrics,
    pub write_perf_ctx_metrics: WritePerfContextMetrics,
}

static ONCE: OnceCell<Arc<DBMetrics>> = OnceCell::new();

impl DBMetrics {
    fn new(registry: &Registry) -> Self {
        DBMetrics {
            op_metrics: OperationMetrics::new(registry),
            cf_metrics: ColumnFamilyMetrics::new(registry),
            read_perf_ctx_metrics: ReadPerfContextMetrics::new(registry),
            write_perf_ctx_metrics: WritePerfContextMetrics::new(registry),
        }
    }
    pub fn init(registry: &Registry) -> &'static Arc<DBMetrics> {
        // Initialize this before creating any instance of DBMap
        // TODO: Remove static initialization because this basically means we can
        // only ever initialize db metrics once with a registry whereas
        // in the code we might want to initialize it with different
        // registries. The problem is underlying metrics cannot be re-initialized
        // or prometheus complains. We essentially need to pass in DBMetrics
        // everywhere we create DBMap as the right fix
        let _ = ONCE
            .set(Arc::new(DBMetrics::new(registry)))
            // this happens many times during tests
            .tap_err(|_| warn!("DBMetrics registry overwritten"));
        ONCE.get().unwrap()
    }
    pub fn increment_num_active_dbs(&self, db_name: &str) {
        self.op_metrics
            .rocksdb_num_active_db_handles
            .with_label_values(&[db_name])
            .inc();
    }
    pub fn decrement_num_active_dbs(&self, db_name: &str) {
        self.op_metrics
            .rocksdb_num_active_db_handles
            .with_label_values(&[db_name])
            .dec();
    }
    pub fn get() -> &'static Arc<DBMetrics> {
        ONCE.get()
            .unwrap_or_else(|| DBMetrics::init(prometheus::default_registry()))
    }
}