Skip to content

Commit

Permalink
fix clippy warnings
Browse files Browse the repository at this point in the history
Sorry, the code was automatically formatted.
I don't know if it's an issue for you or no. Please let me know.
  • Loading branch information
yanns committed Mar 21, 2024
1 parent d118174 commit d5ec00a
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 134 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ json = "0.12.4"
tokio = { version = "1.18", features = ["full"] }
pretty_env_logger = "0.4.0"
assert_matches = "1"
claims = "0.7.1"
pyroscope_pprofrs = { path = "pyroscope_backends/pyroscope_pprofrs" }
pyroscope_pyspy = { path = "pyroscope_backends/pyroscope_pyspy" }
pyroscope_rbspy = { path = "pyroscope_backends/pyroscope_rbspy" }
Expand Down
5 changes: 2 additions & 3 deletions examples/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ pub fn transform_report(report: Report) -> Report {
})
.collect();

let new_report = Report::new(data).metadata(report.metadata.clone());

new_report
Report::new(data).metadata(report.metadata.clone())
}

fn main() -> Result<()> {
let agent = PyroscopeAgent::builder("http://localhost:4040", "example.transform")
.backend(pprof_backend(PprofConfig::new().sample_rate(100)))
Expand Down
70 changes: 36 additions & 34 deletions src/backend/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,24 @@ fn test_stack_frame_display() {

#[test]
fn test_stack_trace_display() {
let mut frames = Vec::new();
frames.push(StackFrame::new(
Some("module".to_string()),
Some("name".to_string()),
Some("filename".to_string()),
Some("relative_path".to_string()),
Some("absolute_path".to_string()),
Some(1),
));
frames.push(StackFrame::new(
Some("module".to_string()),
Some("name".to_string()),
Some("filename".to_string()),
Some("relative_path".to_string()),
Some("absolute_path".to_string()),
Some(2),
));
let frames = vec![
StackFrame::new(
Some("module".to_string()),
Some("name".to_string()),
Some("filename".to_string()),
Some("relative_path".to_string()),
Some("absolute_path".to_string()),
Some(1),
),
StackFrame::new(
Some("module".to_string()),
Some("name".to_string()),
Some("filename".to_string()),
Some("relative_path".to_string()),
Some("absolute_path".to_string()),
Some(2),
),
];

let stack_trace = StackTrace::new(&BackendConfig::default(), None, None, None, frames);

Expand Down Expand Up @@ -73,23 +74,24 @@ fn test_report_clear() {
#[test]
fn test_report_display() {
// Dummy StackTrace
let mut frames = Vec::new();
frames.push(StackFrame::new(
Some("module".to_string()),
Some("name".to_string()),
Some("filename".to_string()),
Some("absolute_path".to_string()),
Some("relative_path".to_string()),
Some(1),
));
frames.push(StackFrame::new(
Some("module".to_string()),
Some("name".to_string()),
Some("filename".to_string()),
Some("absolute_path".to_string()),
Some("relative_path".to_string()),
Some(2),
));
let frames = vec![
StackFrame::new(
Some("module".to_string()),
Some("name".to_string()),
Some("filename".to_string()),
Some("absolute_path".to_string()),
Some("relative_path".to_string()),
Some(1),
),
StackFrame::new(
Some("module".to_string()),
Some("name".to_string()),
Some("filename".to_string()),
Some("absolute_path".to_string()),
Some("relative_path".to_string()),
Some(2),
),
];

let stack_trace = StackTrace::new(&BackendConfig::default(), None, None, None, frames);

Expand Down
3 changes: 1 addition & 2 deletions src/backend/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ impl From<StackBuffer> for Vec<Report> {
},
)
.unwrap_or_default()
.into_iter()
.map(|(_, report)| report)
.into_values()
.collect()
}
}
Expand Down
27 changes: 13 additions & 14 deletions src/encode/folded.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@

use crate::backend;

use backend::types::{Report, EncodedReport};
use backend::types::{EncodedReport, Report};

pub fn encode(reports: &Vec<Report>) -> Vec<EncodedReport> {
reports.into_iter()
.map(|r| {
EncodedReport {
format: "folded".to_string(),
content_type: "binary/octet-stream".to_string(),
content_encoding: "".to_string(),
data: r.to_string().into_bytes(),
metadata: r.metadata.to_owned(),
}
}).collect()
}
pub fn encode(reports: &[Report]) -> Vec<EncodedReport> {
reports
.iter()
.map(|r| EncodedReport {
format: "folded".to_string(),
content_type: "binary/octet-stream".to_string(),
content_encoding: "".to_string(),
data: r.to_string().into_bytes(),
metadata: r.metadata.to_owned(),
})
.collect()
}
35 changes: 15 additions & 20 deletions src/encode/pprof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use prost::Message;
use crate::backend::types::{EncodedReport, Report};
use crate::encode::profiles::{Function, Label, Line, Location, Profile, Sample, ValueType};


struct PProfBuilder {
profile: Profile,
strings: HashMap<String, i64>,
Expand All @@ -28,8 +27,8 @@ pub struct FunctionMirror {
impl PProfBuilder {
fn add_string(&mut self, s: &String) -> i64 {
let v = self.strings.get(s);
if v.is_some() {
return *v.unwrap();
if let Some(v) = v {
return *v;
}
assert!(self.strings.len() != self.profile.string_table.len() + 1);
let id: i64 = self.strings.len() as i64;
Expand All @@ -40,13 +39,13 @@ impl PProfBuilder {

fn add_function(&mut self, fm: FunctionMirror) -> u64 {
let v = self.functions.get(&fm);
if v.is_some() {
return *v.unwrap();
if let Some(v) = v {
return *v;
}
assert!(self.functions.len() != self.profile.function.len() + 1);
let id: u64 = self.functions.len() as u64 + 1;
let f = Function {
id: id,
id,
name: fm.name,
system_name: 0,
filename: fm.filename,
Expand All @@ -59,8 +58,8 @@ impl PProfBuilder {

fn add_location(&mut self, lm: LocationMirror) -> u64 {
let v = self.locations.get(&lm);
if v.is_some() {
return *v.unwrap();
if let Some(v) = v {
return *v;
}
assert!(self.locations.len() != self.profile.location.len() + 1);
let id: u64 = self.locations.len() as u64 + 1;
Expand All @@ -80,7 +79,9 @@ impl PProfBuilder {
}
}

pub fn encode(reports: &Vec<Report>, sample_rate: u32, start_time_nanos: u64, duration_nanos: u64) -> Vec<EncodedReport> {
pub fn encode(
reports: &Vec<Report>, sample_rate: u32, start_time_nanos: u64, duration_nanos: u64,
) -> Vec<EncodedReport> {
let mut b = PProfBuilder {
strings: HashMap::new(),
functions: HashMap::new(),
Expand Down Expand Up @@ -125,18 +126,12 @@ pub fn encode(reports: &Vec<Report>, sample_rate: u32, start_time_nanos: u64, du
label: vec![],
};
for sf in &stacktrace.frames {
let name = b.add_string(&sf.name.as_ref().unwrap_or(&"".to_string()));
let filename = b.add_string(&sf.filename.as_ref().unwrap_or(&"".to_string()));
let name = b.add_string(sf.name.as_ref().unwrap_or(&"".to_string()));
let filename = b.add_string(sf.filename.as_ref().unwrap_or(&"".to_string()));
let line = sf.line.unwrap_or(0) as i64;
let function_id = b.add_function(FunctionMirror {
name: name,
filename: filename,
});
let location_id = b.add_location(LocationMirror {
function_id: function_id,
line: line,
});
sample.location_id.push(location_id as u64);
let function_id = b.add_function(FunctionMirror { name, filename });
let location_id = b.add_location(LocationMirror { function_id, line });
sample.location_id.push(location_id);
}
let mut labels = HashMap::new();
for l in &stacktrace.metadata.tags {
Expand Down
51 changes: 31 additions & 20 deletions src/pyroscope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use json;

use crate::backend::BackendImpl;
use crate::pyroscope::Compression::GZIP;
use crate::pyroscope::ReportEncoding::{PPROF};
use crate::pyroscope::ReportEncoding::PPROF;

const LOG_TAG: &str = "Pyroscope::Agent";

Expand Down Expand Up @@ -102,7 +102,7 @@ impl PyroscopeConfig {
spy_name: String::from("undefined"), // Spy Name should be set by the backend
auth_token: None, // No authentication token
basic_auth: None,
func: None, // No function
func: None, // No function
compression: None,
report_encoding: ReportEncoding::FOLDED,
tenant_id: None,
Expand Down Expand Up @@ -139,7 +139,6 @@ impl PyroscopeConfig {
Self { spy_name, ..self }
}


pub fn auth_token(self, auth_token: String) -> Self {
Self {
auth_token: Some(auth_token),
Expand Down Expand Up @@ -185,7 +184,6 @@ impl PyroscopeConfig {
}
}


/// Set the http request body compression.
///
/// # Example
Expand All @@ -203,7 +201,7 @@ impl PyroscopeConfig {

pub fn report_encoding(self, report_encoding: ReportEncoding) -> Self {
Self {
report_encoding: report_encoding,
report_encoding,
..self
}
}
Expand All @@ -217,7 +215,7 @@ impl PyroscopeConfig {

pub fn http_headers(self, http_headers: HashMap<String, String>) -> Self {
Self {
http_headers: http_headers,
http_headers,
..self
}
}
Expand Down Expand Up @@ -328,7 +326,9 @@ impl PyroscopeAgentBuilder {

pub fn basic_auth(self, username: impl AsRef<str>, password: impl AsRef<str>) -> Self {
Self {
config: self.config.basic_auth(username.as_ref().to_owned(), password.as_ref().to_owned()),
config: self
.config
.basic_auth(username.as_ref().to_owned(), password.as_ref().to_owned()),
..self
}
}
Expand Down Expand Up @@ -455,7 +455,7 @@ impl PyroscopeAgentBuilder {
handle: None,
running: Arc::new((
#[allow(clippy::mutex_atomic)]
Mutex::new(false),
Mutex::new(false),
Condvar::new(),
)),
_state: PhantomData,
Expand All @@ -465,7 +465,7 @@ impl PyroscopeAgentBuilder {

#[derive(Clone, Debug)]
pub enum Compression {
GZIP
GZIP,
}

impl FromStr for Compression {
Expand Down Expand Up @@ -843,31 +843,42 @@ pub fn parse_http_headers_json(http_headers_json: String) -> Result<HashMap<Stri
let mut http_headers = HashMap::new();
let parsed = json::parse(&http_headers_json)?;
if !parsed.is_object() {
return Err(PyroscopeError::AdHoc(format!("expected object, got {}", parsed)));
return Err(PyroscopeError::AdHoc(format!(
"expected object, got {}",
parsed
)));
}
for (k, v) in parsed.entries() {
if v.is_string() {
http_headers.insert(k.to_string(), v.to_string());
} else {
return Err(PyroscopeError::AdHoc(format!("invalid http header value, not a string: {}", v.to_string())));
return Err(PyroscopeError::AdHoc(format!(
"invalid http header value, not a string: {}",
v
)));
}
};
return Ok(http_headers);
}
Ok(http_headers)
}

pub fn parse_vec_string_json(s: String) -> Result<Vec<String>> {
let parsed = json::parse(&s)?;
if !parsed.is_array() {
return Err(PyroscopeError::AdHoc(format!("expected array, got {}", parsed)));
return Err(PyroscopeError::AdHoc(format!(
"expected array, got {}",
parsed
)));
}
let mut res = Vec::new();
res.reserve(parsed.len());
let mut res = Vec::with_capacity(parsed.len());
for v in parsed.members() {
if v.is_string() {
res.push(v.to_string());
} else {
return Err(PyroscopeError::AdHoc(format!("invalid element value, not a string: {}", v.to_string())));
return Err(PyroscopeError::AdHoc(format!(
"invalid element value, not a string: {}",
v
)));
}
};
return Ok(res);
}
}
Ok(res)
}
Loading

0 comments on commit d5ec00a

Please sign in to comment.