@@ -11,6 +11,7 @@ use crate::append_only_zks::{Azks, InsertMode};
1111use crate :: ecvrf:: { VRFKeyStorage , VRFPublicKey } ;
1212use crate :: errors:: { AkdError , DirectoryError , StorageError } ;
1313use crate :: helper_structs:: LookupInfo ;
14+ use crate :: log:: { error, info} ;
1415use crate :: storage:: manager:: StorageManager ;
1516use crate :: storage:: types:: { DbRecord , ValueState , ValueStateRetrievalFlag } ;
1617use crate :: storage:: Database ;
@@ -23,11 +24,12 @@ use crate::VersionFreshness;
2324use akd_core:: configuration:: Configuration ;
2425use akd_core:: utils:: get_marker_versions;
2526use akd_core:: verify:: history:: HistoryParams ;
26- use log:: { error, info} ;
2727use std:: collections:: { HashMap , HashSet } ;
2828use std:: marker:: PhantomData ;
2929use std:: sync:: Arc ;
3030use tokio:: sync:: RwLock ;
31+ #[ cfg( feature = "tracing" ) ]
32+ use tracing:: Instrument ;
3133
3234/// The representation of a auditable key directory
3335pub struct Directory < TC , S : Database , V > {
6466 /// Creates a new (stateless) instance of a auditable key directory.
6567 /// Takes as input a pointer to the storage being used for this instance.
6668 /// The state is stored in the storage.
69+ #[ cfg_attr( feature = "tracing" , tracing:: instrument( skip_all) ) ]
6770 pub async fn new ( storage : StorageManager < S > , vrf : V ) -> Result < Self , AkdError > {
6871 let azks = Directory :: < TC , S , V > :: get_azks_from_storage ( & storage, false ) . await ;
6972
9093 ///
9194 /// Note that the vector of label-value pairs should not contain any entries with duplicate labels. This
9295 /// condition is explicitly checked, and an error will be returned if this is the case.
96+ #[ cfg_attr( feature = "tracing" , tracing:: instrument( skip_all, fields( num_updates = updates. len( ) ) ) ) ]
9397 pub async fn publish ( & self , updates : Vec < ( AkdLabel , AkdValue ) > ) -> Result < EpochHash , AkdError > {
94- // The guard will be dropped at the end of the publish
98+ // The guard will be dropped at the end of the publish operation
9599 let _guard = self . cache_lock . read ( ) . await ;
96100
97101 // Check for duplicate labels and return an error if any are encountered
@@ -254,6 +258,7 @@ where
254258 ///
255259 /// Returns [Ok((LookupProof, EpochHash))] upon successful generation for the latest version
256260 /// of the target label's state. [Err(_)] otherwise
261+ #[ cfg_attr( feature = "tracing" , tracing:: instrument( skip_all) ) ]
257262 pub async fn lookup ( & self , akd_label : AkdLabel ) -> Result < ( LookupProof , EpochHash ) , AkdError > {
258263 // The guard will be dropped at the end of the proof generation
259264 let _guard = self . cache_lock . read ( ) . await ;
@@ -281,6 +286,7 @@ where
281286 /// from bulk lookup proof generation, as it has its own preloading operation
282287 ///
283288 /// Returns [Ok(LookupProof)] if the proof generation succeeded, [Err(_)] otherwise
289+ #[ cfg_attr( feature = "tracing" , tracing:: instrument( skip_all) ) ]
284290 async fn lookup_with_info (
285291 & self ,
286292 current_azks : & Azks ,
@@ -351,6 +357,7 @@ where
351357
352358 // TODO(eoz): Call proof generations async
353359 /// Allows efficient batch lookups by preloading necessary nodes for the lookups.
360+ #[ cfg_attr( feature = "tracing" , tracing:: instrument( skip_all) ) ]
354361 pub async fn batch_lookup (
355362 & self ,
356363 akd_labels : & [ AkdLabel ] ,
@@ -392,6 +399,7 @@ where
392399 Ok ( ( lookup_proofs, root_hash) )
393400 }
394401
402+ #[ cfg_attr( feature = "tracing" , tracing:: instrument( skip_all) ) ]
395403 async fn build_lookup_info ( & self , latest_st : & ValueState ) -> Result < LookupInfo , AkdError > {
396404 let akd_label = & latest_st. username ;
397405 // Need to account for the case where the latest state is
@@ -419,6 +427,7 @@ where
419427 } )
420428 }
421429
430+ #[ cfg_attr( feature = "tracing" , tracing:: instrument( skip_all) ) ]
422431 async fn get_lookup_info (
423432 & self ,
424433 akd_label : AkdLabel ,
@@ -449,13 +458,21 @@ where
449458 /// this function returns all the values ever associated with it,
450459 /// and the epoch at which each value was first committed to the server state.
451460 /// It also returns the proof of the latest version being served at all times.
461+ #[ cfg_attr( feature = "tracing" , tracing:: instrument( skip_all) ) ]
452462 pub async fn key_history (
453463 & self ,
454464 akd_label : & AkdLabel ,
455465 params : HistoryParams ,
456466 ) -> Result < ( HistoryProof , EpochHash ) , AkdError > {
457467 // The guard will be dropped at the end of the proof generation
468+ #[ cfg( not( feature = "tracing" ) ) ]
458469 let _guard = self . cache_lock . read ( ) . await ;
470+ #[ cfg( feature = "tracing" ) ]
471+ let _guard = self
472+ . cache_lock
473+ . read ( )
474+ . instrument ( tracing:: trace_span!( "cache_lock.read" ) )
475+ . await ;
459476
460477 let current_azks = self . retrieve_azks ( ) . await ?;
461478 let current_epoch = current_azks. get_latest_epoch ( ) ;
@@ -616,9 +633,24 @@ where
616633 {
617634 // acquire a singleton lock prior to flushing the cache to assert that no
618635 // cache accesses are underway (i.e. publish/proof generations/etc)
636+ #[ cfg( not( feature = "tracing" ) ) ]
619637 let _guard = self . cache_lock . write ( ) . await ;
638+ #[ cfg( feature = "tracing" ) ]
639+ let _guard = self
640+ . cache_lock
641+ . write ( )
642+ . instrument ( tracing:: trace_span!( "cache_lock.write" ) )
643+ . await ;
644+
620645 // flush the cache in its entirety
646+ #[ cfg( not( feature = "tracing" ) ) ]
621647 self . storage . flush_cache ( ) . await ;
648+ #[ cfg( feature = "tracing" ) ]
649+ self . storage
650+ . flush_cache ( )
651+ . instrument ( tracing:: trace_span!( "flush_cache" ) )
652+ . await ;
653+
622654 // re-fetch the azks to load it into cache so when we release the cache lock
623655 // others will see the new AZKS loaded up and ready
624656 last =
@@ -643,13 +675,21 @@ where
643675
644676 /// Returns an [AppendOnlyProof] for the leaves inserted into the underlying tree between
645677 /// the epochs `audit_start_ep` and `audit_end_ep`.
678+ #[ cfg_attr( feature = "tracing" , tracing:: instrument( skip_all, fields( start_epoch = audit_start_ep, end_epoch = audit_end_ep) ) ) ]
646679 pub async fn audit (
647680 & self ,
648681 audit_start_ep : u64 ,
649682 audit_end_ep : u64 ,
650683 ) -> Result < AppendOnlyProof , AkdError > {
651684 // The guard will be dropped at the end of the proof generation
685+ #[ cfg( not( feature = "tracing" ) ) ]
652686 let _guard = self . cache_lock . read ( ) . await ;
687+ #[ cfg( feature = "tracing" ) ]
688+ let _guard = self
689+ . cache_lock
690+ . read ( )
691+ . instrument ( tracing:: trace_span!( "cache_lock.read" ) )
692+ . await ;
653693
654694 let current_azks = self . retrieve_azks ( ) . await ?;
655695 let current_epoch = current_azks. get_latest_epoch ( ) ;
@@ -673,10 +713,12 @@ where
673713 }
674714
675715 /// Retrieves the [Azks]
716+ #[ cfg_attr( feature = "tracing" , tracing:: instrument( skip_all) ) ]
676717 pub ( crate ) async fn retrieve_azks ( & self ) -> Result < Azks , crate :: errors:: AkdError > {
677718 Directory :: < TC , S , V > :: get_azks_from_storage ( & self . storage , false ) . await
678719 }
679720
721+ #[ cfg_attr( feature = "tracing" , tracing:: instrument( skip_all, fields( ignore_cache = ignore_cache) ) ) ]
680722 async fn get_azks_from_storage (
681723 storage : & StorageManager < S > ,
682724 ignore_cache : bool ,
@@ -704,10 +746,12 @@ where
704746 /// HELPERS ///
705747
706748 /// Use this function to retrieve the [VRFPublicKey] for this AKD.
749+ #[ cfg_attr( feature = "tracing" , tracing:: instrument( skip_all) ) ]
707750 pub async fn get_public_key ( & self ) -> Result < VRFPublicKey , AkdError > {
708751 Ok ( self . vrf . get_vrf_public_key ( ) . await ?)
709752 }
710753
754+ #[ cfg_attr( feature = "tracing" , tracing:: instrument( skip_all) ) ]
711755 async fn create_single_update_proof (
712756 & self ,
713757 akd_label : & AkdLabel ,
@@ -770,6 +814,7 @@ where
770814 }
771815
772816 /// Gets the root hash at the current epoch.
817+ #[ cfg_attr( feature = "tracing" , tracing:: instrument( skip_all) ) ]
773818 pub async fn get_epoch_hash ( & self ) -> Result < EpochHash , AkdError > {
774819 let current_azks = self . retrieve_azks ( ) . await ?;
775820 let latest_epoch = current_azks. get_latest_epoch ( ) ;
@@ -823,11 +868,13 @@ where
823868 }
824869
825870 /// Read-only access to [Directory::lookup](Directory::lookup).
871+ #[ cfg_attr( feature = "tracing" , tracing:: instrument( skip_all) ) ]
826872 pub async fn lookup ( & self , uname : AkdLabel ) -> Result < ( LookupProof , EpochHash ) , AkdError > {
827873 self . 0 . lookup ( uname) . await
828874 }
829875
830876 /// Read-only access to [Directory::batch_lookup](Directory::batch_lookup).
877+ #[ cfg_attr( feature = "tracing" , tracing:: instrument( skip_all) ) ]
831878 pub async fn batch_lookup (
832879 & self ,
833880 unames : & [ AkdLabel ] ,
@@ -836,6 +883,7 @@ where
836883 }
837884
838885 /// Read-only access to [Directory::key_history](Directory::key_history).
886+ #[ cfg_attr( feature = "tracing" , tracing:: instrument( skip_all) ) ]
839887 pub async fn key_history (
840888 & self ,
841889 uname : & AkdLabel ,
@@ -845,6 +893,7 @@ where
845893 }
846894
847895 /// Read-only access to [Directory::poll_for_azks_changes](Directory::poll_for_azks_changes).
896+ #[ cfg_attr( feature = "tracing" , tracing:: instrument( skip_all) ) ]
848897 pub async fn poll_for_azks_changes (
849898 & self ,
850899 period : tokio:: time:: Duration ,
@@ -854,6 +903,7 @@ where
854903 }
855904
856905 /// Read-only access to [Directory::audit](Directory::audit).
906+ #[ cfg_attr( feature = "tracing" , tracing:: instrument( skip_all) ) ]
857907 pub async fn audit (
858908 & self ,
859909 audit_start_ep : u64 ,
@@ -863,11 +913,13 @@ where
863913 }
864914
865915 /// Read-only access to [Directory::get_epoch_hash].
916+ #[ cfg_attr( feature = "tracing" , tracing:: instrument( skip_all) ) ]
866917 pub async fn get_epoch_hash ( & self ) -> Result < EpochHash , AkdError > {
867918 self . 0 . get_epoch_hash ( ) . await
868919 }
869920
870921 /// Read-only access to [Directory::get_public_key](Directory::get_public_key).
922+ #[ cfg_attr( feature = "tracing" , tracing:: instrument( skip_all) ) ]
871923 pub async fn get_public_key ( & self ) -> Result < VRFPublicKey , AkdError > {
872924 self . 0 . get_public_key ( ) . await
873925 }
0 commit comments