Skip to content

Commit feaffcd

Browse files
committed
imp(lib): add report transformation function
1 parent 62b3c25 commit feaffcd

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/pyroscope.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::{
1010
};
1111

1212
use crate::{
13-
backend::{void_backend, BackendReady, BackendUninitialized, Rule, Tag, VoidConfig},
13+
backend::{void_backend, BackendReady, BackendUninitialized, Report, Rule, Tag, VoidConfig},
1414
error::Result,
1515
session::{Session, SessionManager, SessionSignal},
1616
timer::{Timer, TimerSignal},
@@ -45,6 +45,8 @@ pub struct PyroscopeConfig {
4545
pub auth_token: Option<String>,
4646
/// Regex to apply
4747
pub regex: Option<Regex>,
48+
/// Function to apply
49+
pub func: Option<fn(Report) -> Report>,
4850
}
4951

5052
impl Default for PyroscopeConfig {
@@ -60,6 +62,7 @@ impl Default for PyroscopeConfig {
6062
spy_name: "undefined".to_string(),
6163
auth_token: None,
6264
regex: None,
65+
func: None,
6366
}
6467
}
6568
}
@@ -81,6 +84,7 @@ impl PyroscopeConfig {
8184
spy_name: String::from("undefined"), // Spy Name should be set by the backend
8285
auth_token: None, // No authentication token
8386
regex: None, // No regex
87+
func: None, // No function
8488
}
8589
}
8690

@@ -129,6 +133,14 @@ impl PyroscopeConfig {
129133
}
130134
}
131135

136+
/// Set the Function.
137+
pub fn func(self, func: fn(Report) -> Report) -> Self {
138+
Self {
139+
func: Some(func),
140+
..self
141+
}
142+
}
143+
132144
/// Set the tags.
133145
///
134146
/// # Example
@@ -272,6 +284,24 @@ impl PyroscopeAgentBuilder {
272284
}
273285
}
274286

287+
/// Set the Function.
288+
/// This is optional. If not set, the agent will not apply any function.
289+
/// #Example
290+
/// ```ignore
291+
/// let builder = PyroscopeAgentBuilder::new("http://localhost:8080", "my-app")
292+
/// .func(|report| {
293+
/// report
294+
/// })
295+
/// .build()
296+
/// ?;
297+
/// ```
298+
pub fn func(self, func: fn(Report) -> Report) -> Self {
299+
Self {
300+
config: self.config.func(func),
301+
..self
302+
}
303+
}
304+
275305
/// Set tags. Default is empty.
276306
///
277307
/// # Example

src/session.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,16 @@ impl Session {
150150
self.until
151151
);
152152

153+
// own the report
154+
let mut report_owned = report.to_owned();
155+
156+
// Apply function to the report
157+
if let Some(func) = self.config.func.clone() {
158+
report_owned = func(report_owned);
159+
}
160+
153161
// Convert a report to a byte array
154-
let mut report_string = report.to_string();
162+
let mut report_string = report_owned.to_string();
155163

156164
// Apply Regex to the report
157165
if let Some(regex) = self.config.regex.clone() {

0 commit comments

Comments
 (0)