Skip to content

Commit b3de56a

Browse files
committed
imp(lib): add Default trait implementation
1 parent a786933 commit b3de56a

File tree

2 files changed

+76
-3
lines changed

2 files changed

+76
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ path = "examples/internal/rbspy-connect.rs"
5353
[dependencies]
5454
thiserror ="1.0"
5555
log = "0.4"
56+
names = "0.13.0"
5657
reqwest = { version = "0.11", features = ["blocking"]}
5758
libc = "^0.2.124"
5859

src/pyroscope.rs

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const LOG_TAG: &str = "Pyroscope::Agent";
2828
/// use pyroscope::pyroscope::PyroscopeConfig;
2929
/// let config = PyroscopeConfig::new("http://localhost:8080", "my-app");
3030
/// ```
31-
#[derive(Clone, Debug, Default)]
31+
#[derive(Clone, Debug)]
3232
pub struct PyroscopeConfig {
3333
/// Pyroscope Server Address
3434
pub url: String,
@@ -44,6 +44,22 @@ pub struct PyroscopeConfig {
4444
pub auth_token: Option<String>,
4545
}
4646

47+
impl Default for PyroscopeConfig {
48+
fn default() -> Self {
49+
Self {
50+
url: "http://localhost:4040".to_string(),
51+
application_name: names::Generator::default()
52+
.next()
53+
.unwrap_or_else(|| "unassigned.app".to_string())
54+
.replace('-', "."),
55+
tags: HashMap::new(),
56+
sample_rate: 100u32,
57+
spy_name: "undefined".to_string(),
58+
auth_token: None,
59+
}
60+
}
61+
}
62+
4763
impl PyroscopeConfig {
4864
/// Create a new PyroscopeConfig object. url and application_name are required.
4965
/// tags and sample_rate are optional. If sample_rate is not specified, it will default to 100.
@@ -63,6 +79,22 @@ impl PyroscopeConfig {
6379
}
6480
}
6581

82+
// Set the Pyroscope Server URL
83+
pub fn url(self, url: impl AsRef<str>) -> Self {
84+
Self {
85+
url: url.as_ref().to_owned(),
86+
..self
87+
}
88+
}
89+
90+
// Set the Application Name
91+
pub fn application_name(self, application_name: impl AsRef<str>) -> Self {
92+
Self {
93+
application_name: application_name.as_ref().to_owned(),
94+
..self
95+
}
96+
}
97+
6698
/// Set the Sample rate.
6799
pub fn sample_rate(self, sample_rate: u32) -> Self {
68100
Self {
@@ -126,6 +158,15 @@ pub struct PyroscopeAgentBuilder {
126158
config: PyroscopeConfig,
127159
}
128160

161+
impl Default for PyroscopeAgentBuilder {
162+
fn default() -> Self {
163+
Self {
164+
backend: void_backend(VoidConfig::default()),
165+
config: PyroscopeConfig::default(),
166+
}
167+
}
168+
}
169+
129170
impl PyroscopeAgentBuilder {
130171
/// Create a new PyroscopeAgentBuilder object. url and application_name are required.
131172
/// tags and sample_rate are optional.
@@ -141,14 +182,45 @@ impl PyroscopeAgentBuilder {
141182
}
142183
}
143184

185+
/// Set the Pyroscope Server URL. This can be used if the Builder was initialized with the default
186+
/// trait. Default is "http://localhost:4040".
187+
///
188+
/// # Example
189+
/// ```ignore
190+
/// let builder = PyroscopeAgentBuilder::default()
191+
/// .url("http://localhost:8080")
192+
/// .build()?;
193+
/// ```
194+
pub fn url(self, url: impl AsRef<str>) -> Self {
195+
Self {
196+
config: self.config.url(url),
197+
..self
198+
}
199+
}
200+
201+
/// Set the Application Name. This can be used if the Builder was initialized with the default
202+
/// trait. Default is a randomly generated name.
203+
///
204+
/// # Example
205+
/// ```ignore
206+
/// let builder = PyroscopeAgentBuilder::default()
207+
/// .application_name("my-app")
208+
/// .build()?;
209+
/// ```
210+
pub fn application_name(self, application_name: impl AsRef<str>) -> Self {
211+
Self {
212+
config: self.config.application_name(application_name),
213+
..self
214+
}
215+
}
216+
144217
/// Set the agent backend. Default is void-backend.
145218
///
146219
/// # Example
147220
/// ```ignore
148221
/// let builder = PyroscopeAgentBuilder::new("http://localhost:8080", "my-app")
149222
/// .backend(PprofConfig::new().sample_rate(100))
150-
/// .build()
151-
/// ?;
223+
/// .build()?;
152224
/// ```
153225
pub fn backend(self, backend: BackendImpl<BackendUninitialized>) -> Self {
154226
Self { backend, ..self }

0 commit comments

Comments
 (0)