@@ -7,7 +7,7 @@ use chroma_config::{registry, Configurable};
7
7
use chroma_error:: { ChromaError , ErrorCodes } ;
8
8
use chroma_log:: { LocalCompactionManager , LocalCompactionManagerConfig , Log } ;
9
9
use chroma_metering:: {
10
- CollectionForkContext , CollectionReadContext , CollectionWriteContext , Enterable ,
10
+ CollectionForkContext , CollectionReadContext , CollectionWriteContext , Enterable , FinishRequest ,
11
11
FtsQueryLength , LatestCollectionLogicalSizeBytes , LogSizeBytes , MetadataPredicateCount ,
12
12
MeterEvent , PulledLogSizeBytes , QueryEmbeddingCount , ReturnBytes , WriteAction ,
13
13
} ;
@@ -40,10 +40,13 @@ use chroma_types::{
40
40
} ;
41
41
use opentelemetry:: global;
42
42
use opentelemetry:: metrics:: Counter ;
43
- use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
44
43
use std:: sync:: Arc ;
45
44
use std:: time:: { SystemTime , UNIX_EPOCH } ;
46
45
use std:: { collections:: HashSet , time:: Duration } ;
46
+ use std:: {
47
+ sync:: atomic:: { AtomicUsize , Ordering } ,
48
+ time:: Instant ,
49
+ } ;
47
50
48
51
use super :: utils:: to_records;
49
52
@@ -637,9 +640,9 @@ impl ServiceBasedFrontend {
637
640
. set_collection_with_segments ( collection_and_segments)
638
641
. await ;
639
642
640
- // Attach the collection's latest logical size to the metering context
643
+ // Attach metadata to the metering context
641
644
chroma_metering:: with_current ( |context| {
642
- context. latest_collection_logical_size_bytes ( latest_collection_logical_size_bytes)
645
+ context. latest_collection_logical_size_bytes ( latest_collection_logical_size_bytes) ;
643
646
} ) ;
644
647
645
648
// TODO: Submit event after the response is sent
@@ -752,8 +755,11 @@ impl ServiceBasedFrontend {
752
755
. add_retries_counter
753
756
. add ( retries. load ( Ordering :: Relaxed ) as u64 , & [ ] ) ;
754
757
755
- // Attach the log size in bytes to the metering context
756
- chroma_metering:: with_current ( |context| context. log_size_bytes ( log_size_bytes) ) ;
758
+ // Attach metadata to the metering context
759
+ chroma_metering:: with_current ( |context| {
760
+ context. log_size_bytes ( log_size_bytes) ;
761
+ context. finish_request ( Instant :: now ( ) ) ;
762
+ } ) ;
757
763
758
764
// TODO: Submit event after the response is sent
759
765
match res {
@@ -834,8 +840,11 @@ impl ServiceBasedFrontend {
834
840
. update_retries_counter
835
841
. add ( retries. load ( Ordering :: Relaxed ) as u64 , & [ ] ) ;
836
842
837
- // Attach the log size in bytes to the metering context
838
- chroma_metering:: with_current ( |context| context. log_size_bytes ( log_size_bytes) ) ;
843
+ // Attach metadata to the metering context
844
+ chroma_metering:: with_current ( |context| {
845
+ context. log_size_bytes ( log_size_bytes) ;
846
+ context. finish_request ( Instant :: now ( ) ) ;
847
+ } ) ;
839
848
840
849
// TODO: Submit event after the response is sent
841
850
match res {
@@ -918,8 +927,11 @@ impl ServiceBasedFrontend {
918
927
. upsert_retries_counter
919
928
. add ( retries. load ( Ordering :: Relaxed ) as u64 , & [ ] ) ;
920
929
921
- // Attach the log size in bytes to the metering context
922
- chroma_metering:: with_current ( |context| context. log_size_bytes ( log_size_bytes) ) ;
930
+ // Attach metadata to the metering context
931
+ chroma_metering:: with_current ( |context| {
932
+ context. log_size_bytes ( log_size_bytes) ;
933
+ context. finish_request ( Instant :: now ( ) ) ;
934
+ } ) ;
923
935
924
936
// TODO: Submit event after the response is sent
925
937
match res {
@@ -1041,9 +1053,10 @@ impl ServiceBasedFrontend {
1041
1053
None
1042
1054
} ;
1043
1055
1044
- // NOTE(c-gamble): See note above for additional context, but this is a non-standard pattern
1045
- // and we are only implementing metering for delete in this manner because delete currently
1046
- // produces two metering events.
1056
+ if let Some ( event) = read_event {
1057
+ event. submit ( ) . await ;
1058
+ }
1059
+
1047
1060
let collection_write_context_container =
1048
1061
chroma_metering:: create :: < CollectionWriteContext > ( CollectionWriteContext :: new (
1049
1062
tenant_id. clone ( ) ,
@@ -1071,12 +1084,10 @@ impl ServiceBasedFrontend {
1071
1084
}
1072
1085
} ) ?;
1073
1086
1074
- if let Some ( event) = read_event {
1075
- event. submit ( ) . await ;
1076
- }
1077
-
1078
- // Attach the log size bytes to the write context
1079
- chroma_metering:: with_current ( |context| context. log_size_bytes ( log_size_bytes) ) ;
1087
+ // Attach metadata to the write context
1088
+ chroma_metering:: with_current ( |context| {
1089
+ context. log_size_bytes ( log_size_bytes) ;
1090
+ } ) ;
1080
1091
1081
1092
// TODO: Submit event after the response is sent
1082
1093
match chroma_metering:: close :: < CollectionWriteContext > ( ) {
@@ -1296,6 +1307,7 @@ impl ServiceBasedFrontend {
1296
1307
context. pulled_log_size_bytes ( get_result. pulled_log_bytes ) ;
1297
1308
context. latest_collection_logical_size_bytes ( latest_collection_logical_size_bytes) ;
1298
1309
context. return_bytes ( return_bytes) ;
1310
+ context. finish_request ( Instant :: now ( ) ) ;
1299
1311
} ) ;
1300
1312
1301
1313
// TODO: Submit event after the response is sent
@@ -1424,6 +1436,7 @@ impl ServiceBasedFrontend {
1424
1436
context. pulled_log_size_bytes ( query_result. pulled_log_bytes ) ;
1425
1437
context. latest_collection_logical_size_bytes ( latest_collection_logical_size_bytes) ;
1426
1438
context. return_bytes ( return_bytes) ;
1439
+ context. finish_request ( Instant :: now ( ) ) ;
1427
1440
} ) ;
1428
1441
1429
1442
// TODO: Submit event after the response is sent
0 commit comments