Skip to content

Commit 7ab6760

Browse files
committed
imp(regex): initial regex implementation
1 parent fe196d1 commit 7ab6760

File tree

4 files changed

+99
-1
lines changed

4 files changed

+99
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ names = "0.13.0"
5757
reqwest = { version = "0.11", features = ["blocking", "rustls-tls-native-roots"]}
5858
url = "2.2.2"
5959
libc = "^0.2.124"
60+
regex = "1"
6061

6162
[dev-dependencies]
6263
tokio = { version = "1.18", features = ["full"] }

examples/regex.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
extern crate pyroscope;
2+
3+
use pyroscope::{PyroscopeAgent, Result};
4+
use pyroscope_pprofrs::{pprof_backend, PprofConfig};
5+
use std::hash::{Hash, Hasher};
6+
7+
fn hash_rounds(n: u64) -> u64 {
8+
let hash_str = "Some string to hash";
9+
let mut default_hasher = std::collections::hash_map::DefaultHasher::new();
10+
11+
for _ in 0..n {
12+
for _ in 0..1000 {
13+
default_hasher.write(hash_str.as_bytes());
14+
}
15+
hash_str.hash(&mut default_hasher);
16+
}
17+
18+
n
19+
}
20+
21+
fn main() -> Result<()> {
22+
let agent = PyroscopeAgent::builder("http://localhost:4040", "example.regex")
23+
.backend(pprof_backend(PprofConfig::new().sample_rate(100)))
24+
.tags([("TagA", "ValueA"), ("TagB", "ValueB")].to_vec())
25+
.regex(regex::Regex::new(r"std::").unwrap())
26+
.build()?;
27+
28+
// Show start time
29+
let start = std::time::SystemTime::now()
30+
.duration_since(std::time::UNIX_EPOCH)
31+
.unwrap()
32+
.as_secs();
33+
println!("Start Time: {}", start);
34+
35+
// Start Agent
36+
let agent_running = agent.start()?;
37+
38+
let _result = hash_rounds(300_000);
39+
40+
// Show stop time
41+
let stop = std::time::SystemTime::now()
42+
.duration_since(std::time::UNIX_EPOCH)
43+
.unwrap()
44+
.as_secs();
45+
println!("Stop Time: {}", stop);
46+
47+
// Stop Agent
48+
let agent_ready = agent_running.stop()?;
49+
50+
// Shutdown the Agent
51+
agent_ready.shutdown();
52+
53+
// Show program exit time
54+
let exit = std::time::SystemTime::now()
55+
.duration_since(std::time::UNIX_EPOCH)
56+
.unwrap()
57+
.as_secs();
58+
println!("Exit Time: {}", exit);
59+
60+
Ok(())
61+
}

src/pyroscope.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use regex::Regex;
12
use std::{
23
collections::HashMap,
34
marker::PhantomData,
@@ -42,6 +43,8 @@ pub struct PyroscopeConfig {
4243
pub spy_name: String,
4344
/// Authentication Token
4445
pub auth_token: Option<String>,
46+
/// Regex to apply
47+
pub regex: Option<Regex>,
4548
}
4649

4750
impl Default for PyroscopeConfig {
@@ -56,6 +59,7 @@ impl Default for PyroscopeConfig {
5659
sample_rate: 100u32,
5760
spy_name: "undefined".to_string(),
5861
auth_token: None,
62+
regex: None,
5963
}
6064
}
6165
}
@@ -76,6 +80,7 @@ impl PyroscopeConfig {
7680
sample_rate: 100u32, // Default sample rate
7781
spy_name: String::from("undefined"), // Spy Name should be set by the backend
7882
auth_token: None, // No authentication token
83+
regex: None, // No regex
7984
}
8085
}
8186

@@ -116,6 +121,14 @@ impl PyroscopeConfig {
116121
}
117122
}
118123

124+
/// Set the Regex.
125+
pub fn regex(self, regex: Regex) -> Self {
126+
Self {
127+
regex: Some(regex),
128+
..self
129+
}
130+
}
131+
119132
/// Set the tags.
120133
///
121134
/// # Example
@@ -243,6 +256,22 @@ impl PyroscopeAgentBuilder {
243256
}
244257
}
245258

259+
/// Set Regex.
260+
/// This is optional. If not set, the agent will not apply any regex.
261+
/// #Example
262+
/// ```ignore
263+
/// let builder = PyroscopeAgentBuilder::new("http://localhost:8080", "my-app")
264+
/// .regex(Regex::new("^my-app.*").unwrap())
265+
/// .build()
266+
/// ?;
267+
/// ```
268+
pub fn regex(self, regex: Regex) -> Self {
269+
Self {
270+
config: self.config.regex(regex),
271+
..self
272+
}
273+
}
274+
246275
/// Set tags. Default is empty.
247276
///
248277
/// # Example

src/session.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,14 @@ impl Session {
151151
);
152152

153153
// Convert a report to a byte array
154-
let report_u8 = report.to_string().into_bytes();
154+
let mut report_string = report.to_string();
155+
156+
// Apply Regex to the report
157+
if let Some(regex) = self.config.regex.clone() {
158+
report_string = regex.replace_all(&report_string, "").to_string();
159+
}
160+
161+
let report_u8 = report_string.into_bytes();
155162

156163
// Check if the report is empty
157164
if report_u8.is_empty() {

0 commit comments

Comments
 (0)