@@ -10,7 +10,8 @@ use std::cell::RefCell;
1010use std:: collections:: BTreeMap ;
1111use std:: ffi:: CStr ;
1212use std:: rc:: Rc ;
13- use std:: time:: { Duration , Instant } ;
13+ use std:: time:: { Duration , Instant , SystemTime , UNIX_EPOCH } ;
14+ use sysinfo:: System ;
1415
1516include ! ( concat!( env!( "OUT_DIR" ) , "/bindings.rs" ) ) ;
1617
@@ -29,17 +30,17 @@ where
2930}
3031
3132#[ serde_as]
32- #[ derive( Debug , Default , Serialize ) ]
33+ #[ derive( Debug , Serialize ) ]
3334pub struct Context {
3435 #[ serde_as( as = "Hex" ) ]
3536 #[ serde( rename = "context" ) ]
3637 pub id : ContextId ,
3738 #[ serde_as( as = "Hex" ) ]
3839 pub origin : Vec < u8 > ,
39- #[ serde_as( as = "serde_with::DurationNanoSeconds<u64 >" ) ]
40- pub start : Duration ,
41- #[ serde_as( as = "serde_with::DurationNanoSeconds<u64 >" ) ]
42- pub end : Duration ,
40+ #[ serde_as( as = "serde_with::TimestampSecondsWithFrac<f64 >" ) ]
41+ pub start : SystemTime ,
42+ #[ serde_as( as = "serde_with::TimestampSecondsWithFrac<f64 >" ) ]
43+ pub end : SystemTime ,
4344 pub events : BTreeMap < String , EventData > ,
4445 #[ serde( skip_serializing_if = "BTreeMap::is_empty" ) ]
4546 #[ serde( serialize_with = "only_values" ) ]
@@ -50,13 +51,19 @@ pub struct Context {
5051pub struct ContextTracker {
5152 all_contexts : BTreeMap < ContextId , Rc < RefCell < Context > > > ,
5253 root_contexts : Vec < ( Instant , Rc < RefCell < Context > > ) > ,
54+ boot_time : SystemTime ,
5355}
5456
5557impl ContextTracker {
56- pub fn new ( ) -> Self {
58+ pub fn new ( boot_time : Option < SystemTime > ) -> Self {
5759 Self {
5860 all_contexts : BTreeMap :: new ( ) ,
5961 root_contexts : Vec :: new ( ) ,
62+ boot_time : boot_time. unwrap_or_else ( || {
63+ UNIX_EPOCH
64+ . checked_add ( Duration :: from_secs ( System :: boot_time ( ) ) )
65+ . unwrap ( )
66+ } ) ,
6067 }
6168 }
6269
@@ -79,6 +86,14 @@ impl ContextTracker {
7986 }
8087
8188 pub fn handle_event_group ( & mut self , group : & EventGroup ) -> usize {
89+ let start = self
90+ . boot_time
91+ . checked_add ( group. start ( ) )
92+ . unwrap_or ( UNIX_EPOCH ) ;
93+ let end = self
94+ . boot_time
95+ . checked_add ( group. end ( ) )
96+ . unwrap_or ( UNIX_EPOCH ) ;
8297 let mut count = 0 ;
8398 for event in group. events ( ) {
8499 match event {
@@ -89,9 +104,10 @@ impl ContextTracker {
89104 let context = Rc :: new ( RefCell :: new ( Context {
90105 id : * group. context ( ) ,
91106 origin : origin. to_owned ( ) ,
92- start : group. start ( ) ,
93- end : group. end ( ) ,
94- ..Default :: default ( )
107+ start,
108+ end,
109+ events : Default :: default ( ) ,
110+ spans : Default :: default ( ) ,
95111 } ) ) ;
96112 if let Some ( parent) = self . all_contexts . get ( & parent_context[ ..] ) {
97113 parent
@@ -112,9 +128,11 @@ impl ContextTracker {
112128 // this message.
113129 let context_obj = Rc :: new ( RefCell :: new ( Context {
114130 id : * group. context ( ) ,
115- start : group. start ( ) ,
116- end : group. end ( ) ,
117- ..Default :: default ( )
131+ origin : Default :: default ( ) ,
132+ start,
133+ end,
134+ events : Default :: default ( ) ,
135+ spans : Default :: default ( ) ,
118136 } ) ) ;
119137 self . root_contexts
120138 . push ( ( Instant :: now ( ) , context_obj. clone ( ) ) ) ;
0 commit comments