Skip to content

Commit 90abfac

Browse files
committed
docs(all): write preliminary docs
1 parent 3cfb0a7 commit 90abfac

File tree

6 files changed

+173
-11
lines changed

6 files changed

+173
-11
lines changed

src/backends/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ use std::fmt::Debug;
1111
/// Backend State
1212
#[derive(Debug, Clone, Copy, PartialEq)]
1313
pub enum State {
14+
/// Backend is uninitialized.
1415
Uninitialized,
16+
/// Backend is ready to be used.
1517
Ready,
18+
/// Backend is running.
1619
Running,
1720
}
1821

@@ -24,10 +27,15 @@ impl Default for State {
2427

2528
/// Backend Trait
2629
pub trait Backend: Send + Debug {
30+
/// Get the backend state.
2731
fn get_state(&self) -> State;
32+
/// Initialize the backend.
2833
fn initialize(&mut self, sample_rate: i32) -> Result<()>;
34+
/// Start the backend.
2935
fn start(&mut self) -> Result<()>;
36+
/// Stop the backend.
3037
fn stop(&mut self) -> Result<()>;
38+
/// Generate profiling report
3139
fn report(&mut self) -> Result<Vec<u8>>;
3240
}
3341

src/pyroscope.rs

Lines changed: 114 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,19 @@ use crate::session::SessionManager;
1919
use crate::session::SessionSignal;
2020
use 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)]
2429
pub 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

3845
impl 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+
/// ```
79110
pub struct PyroscopeAgentBuilder {
80111
/// Profiler backend
81112
backend: Arc<Mutex<dyn Backend>>,
@@ -84,8 +115,13 @@ pub struct PyroscopeAgentBuilder {
84115
}
85116

86117
impl 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)]
153210
pub 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

194252
impl 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

src/session.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,22 @@ use crate::utils::merge_tags_with_app_name;
1515
use crate::Result;
1616

1717
/// Session Signal
18+
///
19+
/// This enum is used to send data to the session thread. It can also kill the session thread.
1820
#[derive(Debug)]
1921
pub enum SessionSignal {
22+
/// Send session data to the session thread.
2023
Session(Session),
24+
/// Kill the session thread.
2125
Kill,
2226
}
2327

24-
/// SessionManager
28+
/// Manage sessions and send data to the server.
2529
#[derive(Debug)]
2630
pub struct SessionManager {
31+
/// The SessionManager thread.
2732
pub handle: Option<JoinHandle<Result<()>>>,
33+
/// Channel to send data to the SessionManager thread.
2834
pub tx: SyncSender<SessionSignal>,
2935
}
3036

@@ -71,6 +77,8 @@ impl SessionManager {
7177
}
7278

7379
/// Pyroscope Session
80+
///
81+
/// Used to contain the session data, and send it to the server.
7482
#[derive(Clone, Debug)]
7583
pub struct Session {
7684
pub config: PyroscopeConfig,
@@ -80,6 +88,14 @@ pub struct Session {
8088
}
8189

8290
impl Session {
91+
/// Create a new Session
92+
/// # Example
93+
/// ```ignore
94+
/// let config = PyroscopeConfig::new("https://localhost:8080", "my-app");
95+
/// let report = vec![1, 2, 3];
96+
/// let until = 154065120;
97+
/// let session = Session::new(until, config, report).unwrap();
98+
/// ```
8399
pub fn new(mut until: u64, config: PyroscopeConfig, report: Vec<u8>) -> Result<Self> {
84100
log::info!("Session - Creating Session");
85101
// Session interrupted (0 signal), determine the time
@@ -103,6 +119,15 @@ impl Session {
103119
})
104120
}
105121

122+
/// Send the session to the server and consumes the session object.
123+
/// # Example
124+
/// ```ignore
125+
/// let config = PyroscopeConfig::new("https://localhost:8080", "my-app");
126+
/// let report = vec![1, 2, 3];
127+
/// let until = 154065120;
128+
/// let session = Session::new(until, config, report).unwrap();
129+
/// session.send().unwrap();
130+
/// ```
106131
pub fn send(self) -> Result<()> {
107132
log::info!("Session - Sending Session");
108133

src/timer/epoll.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ use std::sync::{
1313
};
1414
use std::{thread, thread::JoinHandle};
1515

16+
/// A thread that sends a notification every 10th second
17+
///
18+
/// Timer will send an event to attached listeners (mpsc::Sender) every 10th
19+
/// second (...10, ...20, ...)
20+
///
21+
/// The Timer thread will run continously until all Senders are dropped.
22+
/// The Timer thread will be joined when all Senders are dropped.
23+
1624
#[derive(Debug, Default)]
1725
pub struct Timer {
1826
/// A vector to store listeners (mpsc::Sender)
@@ -23,6 +31,7 @@ pub struct Timer {
2331
}
2432

2533
impl Timer {
34+
/// Initialize Timer and run a thread to send events to attached listeners
2635
pub fn initialize(self) -> Result<Self> {
2736
let txs = Arc::clone(&self.txs);
2837

@@ -136,6 +145,7 @@ impl Timer {
136145
Ok(epoll_fd)
137146
}
138147

148+
/// Wait for an event on the epoll file descriptor
139149
fn epoll_wait(timer_fd: libc::c_int, epoll_fd: libc::c_int) -> Result<()> {
140150
// vector to store events
141151
let mut events = Vec::with_capacity(1);

0 commit comments

Comments
 (0)