1
1
use super :: { CacheError , Weighted } ;
2
2
use ahash:: RandomState ;
3
3
use chroma_error:: ChromaError ;
4
+ use chroma_tracing:: util:: Stopwatch ;
4
5
use clap:: Parser ;
5
6
use foyer:: {
6
7
CacheBuilder , DirectFsDeviceOptions , Engine , FifoConfig , FifoPicker , HybridCacheBuilder ,
@@ -286,24 +287,6 @@ impl Default for FoyerCacheConfig {
286
287
}
287
288
}
288
289
289
- struct Stopwatch < ' a > (
290
- & ' a opentelemetry:: metrics:: Histogram < u64 > ,
291
- std:: time:: Instant ,
292
- ) ;
293
-
294
- impl < ' a > Stopwatch < ' a > {
295
- fn new ( histogram : & ' a opentelemetry:: metrics:: Histogram < u64 > ) -> Self {
296
- Self ( histogram, std:: time:: Instant :: now ( ) )
297
- }
298
- }
299
-
300
- impl Drop for Stopwatch < ' _ > {
301
- fn drop ( & mut self ) {
302
- let elapsed = self . 1 . elapsed ( ) . as_micros ( ) as u64 ;
303
- self . 0 . record ( elapsed, & [ ] ) ;
304
- }
305
- }
306
-
307
290
#[ derive( Clone ) ]
308
291
pub struct FoyerHybridCache < K , V >
309
292
where
@@ -457,7 +440,7 @@ where
457
440
V : Clone + Send + Sync + StorageValue + Weighted + ' static ,
458
441
{
459
442
async fn get ( & self , key : & K ) -> Result < Option < V > , CacheError > {
460
- let _stopwatch = Stopwatch :: new ( & self . get_latency ) ;
443
+ let _stopwatch = Stopwatch :: new ( & self . get_latency , & [ ] ) ;
461
444
let res = self . cache . get ( key) . await ?. map ( |v| v. value ( ) . clone ( ) ) ;
462
445
if res. is_some ( ) {
463
446
self . cache_hit . add ( 1 , & [ ] ) ;
@@ -468,22 +451,22 @@ where
468
451
}
469
452
470
453
async fn insert ( & self , key : K , value : V ) {
471
- let _stopwatch = Stopwatch :: new ( & self . insert_latency ) ;
454
+ let _stopwatch = Stopwatch :: new ( & self . insert_latency , & [ ] ) ;
472
455
self . cache . insert ( key, value) ;
473
456
}
474
457
475
458
async fn remove ( & self , key : & K ) {
476
- let _stopwatch = Stopwatch :: new ( & self . remove_latency ) ;
459
+ let _stopwatch = Stopwatch :: new ( & self . remove_latency , & [ ] ) ;
477
460
self . cache . remove ( key) ;
478
461
}
479
462
480
463
async fn clear ( & self ) -> Result < ( ) , CacheError > {
481
- let _stopwatch = Stopwatch :: new ( & self . clear_latency ) ;
464
+ let _stopwatch = Stopwatch :: new ( & self . clear_latency , & [ ] ) ;
482
465
Ok ( self . cache . clear ( ) . await ?)
483
466
}
484
467
485
468
async fn obtain ( & self , key : K ) -> Result < Option < V > , CacheError > {
486
- let _stopwatch = Stopwatch :: new ( & self . obtain_latency ) ;
469
+ let _stopwatch = Stopwatch :: new ( & self . obtain_latency , & [ ] ) ;
487
470
let res = self . cache . obtain ( key) . await ?. map ( |v| v. value ( ) . clone ( ) ) ;
488
471
if res. is_some ( ) {
489
472
self . cache_hit . add ( 1 , & [ ] ) ;
@@ -622,7 +605,7 @@ where
622
605
V : Clone + Send + Sync + Weighted + ' static ,
623
606
{
624
607
async fn get ( & self , key : & K ) -> Result < Option < V > , CacheError > {
625
- let _stopwatch = Stopwatch :: new ( & self . get_latency ) ;
608
+ let _stopwatch = Stopwatch :: new ( & self . get_latency , & [ ] ) ;
626
609
let res = self . cache . get ( key) . map ( |v| v. value ( ) . clone ( ) ) ;
627
610
if res. is_some ( ) {
628
611
self . cache_hit . add ( 1 , & [ ] ) ;
@@ -633,23 +616,23 @@ where
633
616
}
634
617
635
618
async fn insert ( & self , key : K , value : V ) {
636
- let _stopwatch = Stopwatch :: new ( & self . insert_latency ) ;
619
+ let _stopwatch = Stopwatch :: new ( & self . insert_latency , & [ ] ) ;
637
620
self . cache . insert ( key, value) ;
638
621
}
639
622
640
623
async fn remove ( & self , key : & K ) {
641
- let _stopwatch = Stopwatch :: new ( & self . remove_latency ) ;
624
+ let _stopwatch = Stopwatch :: new ( & self . remove_latency , & [ ] ) ;
642
625
self . cache . remove ( key) ;
643
626
}
644
627
645
628
async fn clear ( & self ) -> Result < ( ) , CacheError > {
646
- let _stopwatch = Stopwatch :: new ( & self . clear_latency ) ;
629
+ let _stopwatch = Stopwatch :: new ( & self . clear_latency , & [ ] ) ;
647
630
self . cache . clear ( ) ;
648
631
Ok ( ( ) )
649
632
}
650
633
651
634
async fn obtain ( & self , key : K ) -> Result < Option < V > , CacheError > {
652
- let _stopwatch = Stopwatch :: new ( & self . obtain_latency ) ;
635
+ let _stopwatch = Stopwatch :: new ( & self . obtain_latency , & [ ] ) ;
653
636
let res = self . cache . get ( & key) . map ( |v| v. value ( ) . clone ( ) ) ;
654
637
if res. is_some ( ) {
655
638
self . cache_hit . add ( 1 , & [ ] ) ;
0 commit comments