From 860f7b8c099fe981db4a733a34541c9f0395773f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Miku=C5=82a?= Date: Sun, 13 Oct 2024 22:43:27 +0200 Subject: [PATCH 1/3] fix: update time crate to fix build error with new Rust ``` error[E0282]: type annotations needed for `Box<_>` --> /home/mateusz/.cargo/registry/src/index.crates.io-6f17d22bba15001f/time-0.3.34/src/format_description/parse/mod.rs:83:9 | 83 | let items = format_items | ^^^^^ ... 86 | Ok(items.into()) | ---- type must be known at this point | = note: this is an inference error on crate `time` caused by an API change in Rust 1.80.0; update `time` to version `>=0.3.35` by calling `cargo update` ``` --- Cargo.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 837d9e1..11445f3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3371,9 +3371,9 @@ dependencies = [ [[package]] name = "time" -version = "0.3.34" +version = "0.3.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8248b6521bb14bc45b4067159b9b6ad792e2d6d754d6c41fb50e29fefe38749" +checksum = "5dfd88e563464686c916c7e46e623e520ddc6d79fa6641390f2e3fa86e83e885" dependencies = [ "deranged", "itoa", @@ -3392,9 +3392,9 @@ checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" [[package]] name = "time-macros" -version = "0.2.17" +version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ba3a3ef41e6672a2f0f001392bb5dcd3ff0a9992d618ca761a11c3121547774" +checksum = "3f252a68540fde3a3877aeea552b832b40ab9a69e318efd078774a01ddee1ccf" dependencies = [ "num-conv", "time-core", From 158839a3e8e02ffdd5c996c79e4e652e3e095265 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Miku=C5=82a?= Date: Sun, 13 Oct 2024 22:40:52 +0200 Subject: [PATCH 2/3] fix: replace json with serde_json Fixes #139 --- Cargo.lock | 10 ++-------- Cargo.toml | 2 +- pyroscope_cli/Cargo.toml | 2 +- pyroscope_cli/src/utils/app_config.rs | 6 +++--- pyroscope_cli/src/utils/error.rs | 3 +++ src/error.rs | 2 +- src/pyroscope.rs | 16 +++++++--------- 7 files changed, 18 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 11445f3..4764e59 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1558,12 +1558,6 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "json" -version = "0.12.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "078e285eafdfb6c4b434e0d31e8cfcb5115b651496faca5749b88fafd4f23bfd" - [[package]] name = "lazy_static" version = "1.4.0" @@ -2367,7 +2361,6 @@ version = "0.5.7" dependencies = [ "assert_matches", "claims", - "json", "libc", "libflate", "log", @@ -2378,6 +2371,7 @@ dependencies = [ "pyroscope_pyspy", "pyroscope_rbspy", "reqwest", + "serde_json", "thiserror", "tokio", "url", @@ -2397,7 +2391,6 @@ dependencies = [ "ctrlc", "duct", "human-panic", - "json", "lazy_static", "log", "names 0.13.0", @@ -2408,6 +2401,7 @@ dependencies = [ "pyroscope_pyspy", "pyroscope_rbspy", "serde 1.0.197", + "serde_json", "slog", "slog-async", "slog-scope", diff --git a/Cargo.toml b/Cargo.toml index d3b67bb..2148087 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -63,7 +63,7 @@ libflate = "1.2.0" libc = "^0.2.124" prost = "0.11" winapi = "0.3.9" -json = "0.12.4" +serde_json = "1.0.115" [dev-dependencies] tokio = { version = "1.18", features = ["full"] } diff --git a/pyroscope_cli/Cargo.toml b/pyroscope_cli/Cargo.toml index 3136dc6..cd33863 100644 --- a/pyroscope_cli/Cargo.toml +++ b/pyroscope_cli/Cargo.toml @@ -38,7 +38,7 @@ pyroscope = { path = "../", default-features = false } pyroscope_pprofrs = { path = "../pyroscope_backends/pyroscope_pprofrs", default-features = false } pyroscope_rbspy = { path = "../pyroscope_backends/pyroscope_rbspy", default-features = false } pyroscope_pyspy = { path = "../pyroscope_backends/pyroscope_pyspy", default-features = false } -json = "0.12.4" +serde_json = "1.0.115" [dependencies.clap] version = "3.2" diff --git a/pyroscope_cli/src/utils/app_config.rs b/pyroscope_cli/src/utils/app_config.rs index d8ed75d..d9e4d12 100644 --- a/pyroscope_cli/src/utils/app_config.rs +++ b/pyroscope_cli/src/utils/app_config.rs @@ -172,7 +172,7 @@ impl AppConfig { .map(|s| s.to_string()) .collect::>(); - AppConfig::set("command_args", &json::stringify(command_args))?; + AppConfig::set("command_args", &serde_json::to_string(&command_args)?)?; } } if sub_exec.is_present("log_level") { @@ -314,9 +314,9 @@ fn set_http_headers(cmd: &clap::ArgMatches) -> Result<()> { http_headers_map.insert(kv[0].to_string(), kv[1].to_string()); } } - let http_header = json::stringify(http_headers_map); + let http_header = serde_json::to_string(&http_headers_map)?; AppConfig::set("http_headers_json", &http_header)?; }; } return Ok(()); -} \ No newline at end of file +} diff --git a/pyroscope_cli/src/utils/error.rs b/pyroscope_cli/src/utils/error.rs index 4dd87a5..40a0bc9 100644 --- a/pyroscope_cli/src/utils/error.rs +++ b/pyroscope_cli/src/utils/error.rs @@ -29,6 +29,9 @@ pub enum Error { #[error(transparent)] PyroscopeError(#[from] pyroscope::PyroscopeError), + + #[error(transparent)] + Json(#[from] serde_json::Error), } impl Error { diff --git a/src/error.rs b/src/error.rs index 1d592f3..dd6f1d5 100644 --- a/src/error.rs +++ b/src/error.rs @@ -31,7 +31,7 @@ pub enum PyroscopeError { Io(#[from] std::io::Error), #[error(transparent)] - Json(#[from] json::JsonError), + Json(#[from] serde_json::Error), } impl PyroscopeError { diff --git a/src/pyroscope.rs b/src/pyroscope.rs index 71f3558..d395198 100644 --- a/src/pyroscope.rs +++ b/src/pyroscope.rs @@ -18,8 +18,6 @@ use crate::{ PyroscopeError, }; -use json; - use crate::backend::BackendImpl; use crate::pyroscope::Compression::GZIP; use crate::pyroscope::ReportEncoding::PPROF; @@ -841,16 +839,16 @@ impl PyroscopeAgent { pub fn parse_http_headers_json(http_headers_json: String) -> Result> { let mut http_headers = HashMap::new(); - let parsed = json::parse(&http_headers_json)?; + let parsed: serde_json::Value = serde_json::from_str(&http_headers_json)?; if !parsed.is_object() { return Err(PyroscopeError::AdHoc(format!( "expected object, got {}", parsed ))); } - for (k, v) in parsed.entries() { + for (k, v) in parsed.as_object().unwrap() { if v.is_string() { - http_headers.insert(k.to_string(), v.to_string()); + http_headers.insert(k.to_string(), v.as_str().unwrap().to_string()); } else { return Err(PyroscopeError::AdHoc(format!( "invalid http header value, not a string: {}", @@ -862,17 +860,17 @@ pub fn parse_http_headers_json(http_headers_json: String) -> Result Result> { - let parsed = json::parse(&s)?; + let parsed: serde_json::Value = serde_json::from_str(&s)?; if !parsed.is_array() { return Err(PyroscopeError::AdHoc(format!( "expected array, got {}", parsed ))); } - let mut res = Vec::with_capacity(parsed.len()); - for v in parsed.members() { + let mut res = Vec::with_capacity(parsed.as_array().unwrap().len()); + for v in parsed.as_array().unwrap() { if v.is_string() { - res.push(v.to_string()); + res.push(v.as_str().unwrap().to_string()); } else { return Err(PyroscopeError::AdHoc(format!( "invalid element value, not a string: {}", From 2fd0e79e558bc88bc68373a93d8b402bf97e341f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Miku=C5=82a?= Date: Sun, 13 Oct 2024 23:09:43 +0200 Subject: [PATCH 3/3] refactor: avoid unwrapping serde_json methods --- src/pyroscope.rs | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/src/pyroscope.rs b/src/pyroscope.rs index d395198..7dc4e11 100644 --- a/src/pyroscope.rs +++ b/src/pyroscope.rs @@ -840,15 +840,12 @@ impl PyroscopeAgent { pub fn parse_http_headers_json(http_headers_json: String) -> Result> { let mut http_headers = HashMap::new(); let parsed: serde_json::Value = serde_json::from_str(&http_headers_json)?; - if !parsed.is_object() { - return Err(PyroscopeError::AdHoc(format!( - "expected object, got {}", - parsed - ))); - } - for (k, v) in parsed.as_object().unwrap() { - if v.is_string() { - http_headers.insert(k.to_string(), v.as_str().unwrap().to_string()); + let parsed = parsed.as_object().ok_or_else(|| + PyroscopeError::AdHoc(format!("expected object, got {}", parsed)) + )?; + for (k, v) in parsed { + if let Some(value) = v.as_str() { + http_headers.insert(k.to_string(), value.to_string()); } else { return Err(PyroscopeError::AdHoc(format!( "invalid http header value, not a string: {}", @@ -861,16 +858,13 @@ pub fn parse_http_headers_json(http_headers_json: String) -> Result Result> { let parsed: serde_json::Value = serde_json::from_str(&s)?; - if !parsed.is_array() { - return Err(PyroscopeError::AdHoc(format!( - "expected array, got {}", - parsed - ))); - } - let mut res = Vec::with_capacity(parsed.as_array().unwrap().len()); - for v in parsed.as_array().unwrap() { - if v.is_string() { - res.push(v.as_str().unwrap().to_string()); + let parsed = parsed.as_array().ok_or_else(|| + PyroscopeError::AdHoc(format!("expected array, got {}", parsed)) + )?; + let mut res = Vec::with_capacity(parsed.len()); + for v in parsed { + if let Some(s) = v.as_str() { + res.push(s.to_string()); } else { return Err(PyroscopeError::AdHoc(format!( "invalid element value, not a string: {}",