@@ -19,12 +19,19 @@ use crate::session::SessionManager;
1919use crate :: session:: SessionSignal ;
2020use crate :: timer:: Timer ;
2121
22- /// Represent PyroscopeAgent Configuration
22+ /// Pyroscope Agent Configuration. This is the configuration that is passed to the agent.
23+ /// # Example
24+ /// ```
25+ /// use pyroscope::pyroscope::PyroscopeConfig;
26+ /// let config = PyroscopeConfig::new("http://localhost:8080", "my-app");
27+ /// ```
2328#[ derive( Clone , Debug ) ]
2429pub struct PyroscopeConfig {
30+ /// Pyroscope Server Address
2531 pub url : String ,
2632 /// Application Name
2733 pub application_name : String ,
34+ /// Tags
2835 pub tags : HashMap < String , String > ,
2936 /// Sample rate used in Hz
3037 pub sample_rate : i32 ,
@@ -37,7 +44,11 @@ pub struct PyroscopeConfig {
3744
3845impl PyroscopeConfig {
3946 /// Create a new PyroscopeConfig object. url and application_name are required.
40- /// tags and sample_rate are optional.
47+ /// tags and sample_rate are optional. If sample_rate is not specified, it will default to 100.
48+ /// # Example
49+ /// ```ignore
50+ /// let config = PyroscopeConfig::new("http://localhost:8080", "my-app");
51+ /// ```
4152 pub fn new < S : AsRef < str > > ( url : S , application_name : S ) -> Self {
4253 Self {
4354 url : url. as_ref ( ) . to_owned ( ) ,
@@ -48,14 +59,27 @@ impl PyroscopeConfig {
4859 }
4960
5061 /// Set the Sample rate
62+ /// # Example
63+ /// ```ignore
64+ /// let mut config = PyroscopeConfig::new("http://localhost:8080", "my-app");
65+ /// config.set_sample_rate(10)
66+ /// .unwrap();
67+ /// ```
5168 pub fn sample_rate ( self , sample_rate : i32 ) -> Self {
5269 Self {
5370 sample_rate,
5471 ..self
5572 }
5673 }
5774
58- /// Set Tags
75+ /// Set the tags
76+ /// # Example
77+ /// ```ignore
78+ /// use pyroscope::pyroscope::PyroscopeConfig;
79+ /// let config = PyroscopeConfig::new("http://localhost:8080", "my-app")
80+ /// .tags(vec![("env", "dev")])
81+ /// .unwrap();
82+ /// ```
5983 pub fn tags ( self , tags : & [ ( & str , & str ) ] ) -> Self {
6084 // Convert &[(&str, &str)] to HashMap(String, String)
6185 let tags_hashmap: HashMap < String , String > = tags
@@ -76,6 +100,13 @@ impl PyroscopeConfig {
76100///
77101/// Alternatively, you can use PyroscopeAgent::build() which is a short-hand
78102/// for calling PyroscopeAgentBuilder::new()
103+ ///
104+ /// # Example
105+ /// ```ignore
106+ /// use pyroscope::pyroscope::PyroscopeAgentBuilder;
107+ /// let builder = PyroscopeAgentBuilder::new("http://localhost:8080", "my-app");
108+ /// let agent = builder.build().unwrap();
109+ /// ```
79110pub struct PyroscopeAgentBuilder {
80111 /// Profiler backend
81112 backend : Arc < Mutex < dyn Backend > > ,
@@ -84,8 +115,13 @@ pub struct PyroscopeAgentBuilder {
84115}
85116
86117impl PyroscopeAgentBuilder {
87- /// Create a new PyroscopeConfig object. url and application_name are required.
118+ /// Create a new PyroscopeAgentBuilder object. url and application_name are required.
88119 /// tags and sample_rate are optional.
120+ ///
121+ /// # Example
122+ /// ```ignore
123+ /// let builder = PyroscopeAgentBuilder::new("http://localhost:8080", "my-app");
124+ /// ```
89125 pub fn new < S : AsRef < str > > ( url : S , application_name : S ) -> Self {
90126 Self {
91127 backend : Arc :: new ( Mutex :: new ( Pprof :: default ( ) ) ) , // Default Backend
@@ -94,6 +130,13 @@ impl PyroscopeAgentBuilder {
94130 }
95131
96132 /// Set the agent backend. Default is pprof.
133+ /// # Example
134+ /// ```ignore
135+ /// let builder = PyroscopeAgentBuilder::new("http://localhost:8080", "my-app")
136+ /// .backend(Pprof::default())
137+ /// .build()
138+ /// .unwrap();
139+ /// ```
97140 pub fn backend < T : ' static > ( self , backend : T ) -> Self
98141 where
99142 T : Backend ,
@@ -105,6 +148,13 @@ impl PyroscopeAgentBuilder {
105148 }
106149
107150 /// Set the Sample rate. Default value is 100.
151+ /// # Example
152+ /// ```ignore
153+ /// let builder = PyroscopeAgentBuilder::new("http://localhost:8080", "my-app")
154+ /// .sample_rate(99)
155+ /// .build()
156+ /// .unwrap();
157+ /// ```
108158 pub fn sample_rate ( self , sample_rate : i32 ) -> Self {
109159 Self {
110160 config : self . config . sample_rate ( sample_rate) ,
@@ -113,6 +163,13 @@ impl PyroscopeAgentBuilder {
113163 }
114164
115165 /// Set tags. Default is empty.
166+ /// # Example
167+ /// ```ignore
168+ /// let builder = PyroscopeAgentBuilder::new("http://localhost:8080", "my-app")
169+ /// .tags(vec![("env", "dev")])
170+ /// .build()
171+ /// .unwrap();
172+ /// ```
116173 pub fn tags ( self , tags : & [ ( & str , & str ) ] ) -> Self {
117174 Self {
118175 config : self . config . tags ( tags) ,
@@ -148,17 +205,18 @@ impl PyroscopeAgentBuilder {
148205 }
149206}
150207
151- /// PyroscopeAgent
208+ /// PyroscopeAgent is the main object of the library. It is used to start and stop the profiler, schedule the timer, and send the profiler data to the server.
152209#[ derive( Debug ) ]
153210pub struct PyroscopeAgent {
154- pub backend : Arc < Mutex < dyn Backend > > ,
155211 timer : Timer ,
156212 session_manager : SessionManager ,
157213 tx : Option < Sender < u64 > > ,
158214 handle : Option < JoinHandle < Result < ( ) > > > ,
159215 running : Arc < ( Mutex < bool > , Condvar ) > ,
160216
161- // Session Data
217+ /// Profiler backend
218+ pub backend : Arc < Mutex < dyn Backend > > ,
219+ /// Configuration Object
162220 pub config : PyroscopeConfig ,
163221}
164222
@@ -192,13 +250,22 @@ impl Drop for PyroscopeAgent {
192250}
193251
194252impl PyroscopeAgent {
195- /// Short-hand for PyroscopeAgentBuilder::build()
253+ /// Short-hand for PyroscopeAgentBuilder::build(). This is a convenience method.
254+ /// # Example
255+ /// ```ignore
256+ /// let agent = PyroscopeAgent::builder("http://localhost:8080", "my-app").build().unwrap();
257+ /// ```
196258 pub fn builder < S : AsRef < str > > ( url : S , application_name : S ) -> PyroscopeAgentBuilder {
197259 // Build PyroscopeAgent
198260 PyroscopeAgentBuilder :: new ( url, application_name)
199261 }
200262
201- /// Start profiling and sending data. The agent will keep running until stopped.
263+ /// Start profiling and sending data. The agent will keep running until stopped. The agent will send data to the server every 10s secondy.
264+ /// # Example
265+ /// ```ignore
266+ /// let agent = PyroscopeAgent::builder("http://localhost:8080", "my-app").build().unwrap();
267+ /// agent.start().unwrap();
268+ /// ```
202269 pub fn start ( & mut self ) -> Result < ( ) > {
203270 log:: debug!( "PyroscopeAgent - Starting" ) ;
204271
@@ -258,7 +325,14 @@ impl PyroscopeAgent {
258325 Ok ( ( ) )
259326 }
260327
261- /// Stop the agent.
328+ /// Stop the agent. The agent will stop profiling and send a last report to the server.
329+ /// # Example
330+ /// ```ignore
331+ /// let agent = PyroscopeAgent::builder("http://localhost:8080", "my-app").build().unwrap();
332+ /// agent.start().unwrap();
333+ /// // Expensive operation
334+ /// agent.stop().unwrap();
335+ /// ```
262336 pub fn stop ( & mut self ) -> Result < ( ) > {
263337 log:: debug!( "PyroscopeAgent - Stopping" ) ;
264338 // get tx and send termination signal
@@ -278,8 +352,22 @@ impl PyroscopeAgent {
278352 }
279353
280354 /// Add tags. This will restart the agent.
355+ /// # Example
356+ /// ```ignore
357+ /// let agent = PyroscopeAgent::builder("http://localhost:8080", "my-app").build().unwrap();
358+ /// agent.start().unwrap();
359+ /// // Expensive operation
360+ /// agent.add_tags(vec!["tag", "value"]).unwrap();
361+ /// // Tagged operation
362+ /// agent.stop().unwrap();
363+ /// ```
281364 pub fn add_tags ( & mut self , tags : & [ ( & str , & str ) ] ) -> Result < ( ) > {
282365 log:: debug!( "PyroscopeAgent - Adding tags" ) ;
366+ // Check that tags are not empty
367+ if tags. is_empty ( ) {
368+ return Ok ( ( ) ) ;
369+ }
370+
283371 // Stop Agent
284372 self . stop ( ) ?;
285373
@@ -300,8 +388,24 @@ impl PyroscopeAgent {
300388 }
301389
302390 /// Remove tags. This will restart the agent.
391+ /// # Example
392+ /// ```ignore
393+ /// let agent = PyroscopeAgent::builder("http://localhost:8080", "my-app")
394+ /// .tags(vec![("tag", "value")])
395+ /// .build().unwrap();
396+ /// agent.start().unwrap();
397+ /// // Expensive operation
398+ /// agent.remove_tags(vec!["tag"]).unwrap();
399+ /// // Un-Tagged operation
400+ /// agent.stop().unwrap();
303401 pub fn remove_tags ( & mut self , tags : & [ & str ] ) -> Result < ( ) > {
304402 log:: debug!( "PyroscopeAgent - Removing tags" ) ;
403+
404+ // Check that tags are not empty
405+ if tags. is_empty ( ) {
406+ return Ok ( ( ) ) ;
407+ }
408+
305409 // Stop Agent
306410 self . stop ( ) ?;
307411
0 commit comments