@@ -15,6 +15,8 @@ use crate::backends::pprof::Pprof;
1515use crate :: backends:: Backend ;
1616use crate :: error:: Result ;
1717use crate :: session:: Session ;
18+ use crate :: session:: SessionManager ;
19+ use crate :: session:: SessionSignal ;
1820use crate :: timer:: Timer ;
1921
2022/// Represent PyroscopeAgent Configuration
@@ -93,7 +95,9 @@ impl PyroscopeAgentBuilder {
9395
9496 /// Set the agent backend. Default is pprof.
9597 pub fn backend < T : ' static > ( self , backend : T ) -> Self
96- where T : Backend {
98+ where
99+ T : Backend ,
100+ {
97101 Self {
98102 backend : Arc :: new ( Mutex :: new ( backend) ) ,
99103 ..self
@@ -121,15 +125,22 @@ impl PyroscopeAgentBuilder {
121125 // Initiliaze the backend
122126 let backend = Arc :: clone ( & self . backend ) ;
123127 backend. lock ( ) ?. initialize ( self . config . sample_rate ) ?;
128+ log:: trace!( "PyroscopeAgent - Backend initialized" ) ;
124129
125130 // Start Timer
126131 let timer = Timer :: default ( ) . initialize ( ) ?;
132+ log:: trace!( "PyroscopeAgent - Timer initialized" ) ;
133+
134+ // Start the SessionManager
135+ let session_manager = SessionManager :: new ( ) ?;
136+ log:: trace!( "PyroscopeAgent - SessionManager initialized" ) ;
127137
128138 // Return PyroscopeAgent
129139 Ok ( PyroscopeAgent {
130140 backend : self . backend ,
131141 config : self . config ,
132142 timer,
143+ session_manager,
133144 tx : None ,
134145 handle : None ,
135146 running : Arc :: new ( ( Mutex :: new ( false ) , Condvar :: new ( ) ) ) ,
@@ -142,6 +153,7 @@ impl PyroscopeAgentBuilder {
142153pub struct PyroscopeAgent {
143154 pub backend : Arc < Mutex < dyn Backend > > ,
144155 timer : Timer ,
156+ session_manager : SessionManager ,
145157 tx : Option < Sender < u64 > > ,
146158 handle : Option < JoinHandle < Result < ( ) > > > ,
147159 running : Arc < ( Mutex < bool > , Condvar ) > ,
@@ -153,9 +165,29 @@ pub struct PyroscopeAgent {
153165impl Drop for PyroscopeAgent {
154166 /// Properly shutdown the agent.
155167 fn drop ( & mut self ) {
168+ log:: debug!( "PyroscopeAgent::drop()" ) ;
169+
156170 // Stop Timer
157171 self . timer . drop_listeners ( ) . unwrap ( ) ; // Drop listeners
172+ log:: trace!( "PyroscopeAgent - Dropped timer listeners" ) ;
158173 self . timer . handle . take ( ) . unwrap ( ) . join ( ) . unwrap ( ) . unwrap ( ) ; // Wait for the Timer thread to finish
174+ log:: trace!( "PyroscopeAgent - Dropped timer thread" ) ;
175+
176+ // Stop the SessionManager
177+ self . session_manager . push ( SessionSignal :: Kill ) . unwrap ( ) ;
178+ log:: trace!( "PyroscopeAgent - Sent kill signal to SessionManager" ) ;
179+ self . session_manager
180+ . handle
181+ . take ( )
182+ . unwrap ( )
183+ . join ( )
184+ . unwrap ( )
185+ . unwrap ( ) ;
186+ log:: trace!( "PyroscopeAgent - Dropped SessionManager thread" ) ;
187+
188+ // Wait for main thread to finish
189+ self . handle . take ( ) . unwrap ( ) . join ( ) . unwrap ( ) . unwrap ( ) ;
190+ log:: trace!( "PyroscopeAgent - Dropped main thread" ) ;
159191 }
160192}
161193
@@ -168,6 +200,8 @@ impl PyroscopeAgent {
168200
169201 /// Start profiling and sending data. The agent will keep running until stopped.
170202 pub fn start ( & mut self ) -> Result < ( ) > {
203+ log:: debug!( "PyroscopeAgent - Starting" ) ;
204+
171205 // Create a clone of Backend
172206 let backend = Arc :: clone ( & self . backend ) ;
173207 // Call start()
@@ -188,13 +222,27 @@ impl PyroscopeAgent {
188222
189223 let config = self . config . clone ( ) ;
190224
225+ let stx = self . session_manager . tx . clone ( ) ;
226+
191227 self . handle = Some ( std:: thread:: spawn ( move || {
228+ log:: trace!( "PyroscopeAgent - Main Thread started" ) ;
229+
192230 while let Ok ( time) = rx. recv ( ) {
231+ log:: trace!( "PyroscopeAgent - Sending session {}" , time) ;
232+
233+ // Generate report from backend
193234 let report = backend. lock ( ) ?. report ( ) ?;
194- // start a new session
195- Session :: new ( time, config. clone ( ) , report) ?. send ( ) ?;
235+
236+ // Send new Session to SessionManager
237+ stx. send ( SessionSignal :: Session ( Session :: new (
238+ time,
239+ config. clone ( ) ,
240+ report,
241+ ) ?) ) ?;
196242
197243 if time == 0 {
244+ log:: trace!( "PyroscopeAgent - Session Killed" ) ;
245+
198246 let ( lock, cvar) = & * pair;
199247 let mut running = lock. lock ( ) ?;
200248 * running = false ;
@@ -212,6 +260,7 @@ impl PyroscopeAgent {
212260
213261 /// Stop the agent.
214262 pub fn stop ( & mut self ) -> Result < ( ) > {
263+ log:: debug!( "PyroscopeAgent - Stopping" ) ;
215264 // get tx and send termination signal
216265 self . tx . take ( ) . unwrap ( ) . send ( 0 ) ?;
217266
@@ -230,6 +279,7 @@ impl PyroscopeAgent {
230279
231280 /// Add tags. This will restart the agent.
232281 pub fn add_tags ( & mut self , tags : & [ ( & str , & str ) ] ) -> Result < ( ) > {
282+ log:: debug!( "PyroscopeAgent - Adding tags" ) ;
233283 // Stop Agent
234284 self . stop ( ) ?;
235285
@@ -251,6 +301,7 @@ impl PyroscopeAgent {
251301
252302 /// Remove tags. This will restart the agent.
253303 pub fn remove_tags ( & mut self , tags : & [ & str ] ) -> Result < ( ) > {
304+ log:: debug!( "PyroscopeAgent - Removing tags" ) ;
254305 // Stop Agent
255306 self . stop ( ) ?;
256307
0 commit comments