@@ -66,7 +66,7 @@ impl std::fmt::Display for DatagramSource {
6666
6767pub struct Session {
6868 /// Unique identifier for the session.
69- pub id : u64 ,
69+ pub trace_id : String ,
7070 /// The network type, representing either TCP or UDP.
7171 pub network : Network ,
7272 /// The socket address of the remote peer of an inbound connection.
@@ -93,7 +93,7 @@ pub struct Session {
9393impl Clone for Session {
9494 fn clone ( & self ) -> Self {
9595 Session {
96- id : self . id ,
96+ trace_id : self . trace_id . clone ( ) ,
9797 network : self . network ,
9898 source : self . source ,
9999 local_addr : self . local_addr ,
@@ -110,10 +110,17 @@ impl Clone for Session {
110110
111111impl Default for Session {
112112 fn default ( ) -> Self {
113- use std:: sync:: atomic:: { AtomicU64 , Ordering } ;
114- static SESSION_ID_COUNTER : AtomicU64 = AtomicU64 :: new ( 1 ) ;
113+ use rand:: Rng ;
114+ let mut rng = rand:: thread_rng ( ) ;
115+ let trace_id: String = ( 0 ..8 )
116+ . map ( |_| {
117+ const CHARS : & [ u8 ] = b"abcdefghijklmnopqrstuvwxyz0123456789" ;
118+ let idx = rng. gen_range ( 0 ..CHARS . len ( ) ) ;
119+ CHARS [ idx] as char
120+ } )
121+ . collect ( ) ;
115122 Session {
116- id : SESSION_ID_COUNTER . fetch_add ( 1 , Ordering :: Relaxed ) ,
123+ trace_id ,
117124 network : Network :: Tcp ,
118125 source : * crate :: option:: UNSPECIFIED_BIND_ADDR ,
119126 local_addr : * crate :: option:: UNSPECIFIED_BIND_ADDR ,
@@ -130,7 +137,7 @@ impl Default for Session {
130137
131138impl Session {
132139 pub fn create_span ( & self ) -> tracing:: Span {
133- tracing:: info_span!( "session" , id = self . id )
140+ tracing:: info_span!( "session" , trace_id = self . trace_id )
134141 }
135142}
136143
0 commit comments