From 50075f6debc226eb7ccae368b24ae0fe791246b6 Mon Sep 17 00:00:00 2001 From: John Eckhart Date: Mon, 22 Jul 2024 18:26:36 -0400 Subject: [PATCH] refactor(deps): Update metrics to 0.23. Note: 0.22 was a major change and required some refactor of the metrics --- rust+wasm/deny.toml | 4 +-- rust+wasm/{{project-name}}/Cargo.axum.toml | 8 ++--- .../src.axum/metrics/process.rs | 29 ++++++------------- .../src.axum/middleware/client/metrics.rs | 20 ++++++++----- .../src.axum/middleware/metrics.rs | 21 ++++++-------- .../src.axum/middleware/reqwest_retry.rs | 10 ++++--- .../src.axum/tracing_layers/metrics_layer.rs | 9 ++---- rust/Cargo.axum.toml | 8 ++--- rust/deny.toml | 4 +-- rust/src.axum/metrics/process.rs | 29 ++++++------------- rust/src.axum/middleware/client/metrics.rs | 20 ++++++++----- rust/src.axum/middleware/metrics.rs | 21 ++++++-------- rust/src.axum/middleware/reqwest_retry.rs | 10 ++++--- rust/src.axum/tracing_layers/metrics_layer.rs | 9 ++---- 14 files changed, 88 insertions(+), 114 deletions(-) diff --git a/rust+wasm/deny.toml b/rust+wasm/deny.toml index 8200940..c9aa6ec 100644 --- a/rust+wasm/deny.toml +++ b/rust+wasm/deny.toml @@ -51,9 +51,6 @@ ignore = [ "RUSTSEC-2021-0145", # atty on windows only "RUSTSEC-2023-0071", # Impacts rsa crate, which is only used in dev, see # https://github.com/RustCrypto/RSA/pull/394 for remediation - "RUSTSEC-2024-0336", # Ignore a DOS issue w/ rustls-0.20.9. This will go - # away when we update opentelemetry-otlp soon. - { id = "RUSTSEC-2020-0168", reason = "Not planning to force upgrade to mach2 yet" }, { id = "RUSTSEC-2024-0320", reason = "Not planning to force upgrade to rust-yaml2 yet" }, ] # Threshold for security vulnerabilities, any vulnerability with a CVSS score @@ -118,6 +115,7 @@ exceptions = [ # this is not a problem for us. See https://github.com/dtolnay/unicode-ident/pull/9/files { allow = ["Unicode-DFS-2016"], name = "unicode-ident", version = "*"}, { allow = ["OpenSSL"], name = "ring", version = "*" }, + { allow = ["OpenSSL"], name = "aws-lc-sys", version = "*" }, { allow = ["MPL-2.0"], name = "webpki-roots", version = "*"}, ] diff --git a/rust+wasm/{{project-name}}/Cargo.axum.toml b/rust+wasm/{{project-name}}/Cargo.axum.toml index af4300b..cef5144 100644 --- a/rust+wasm/{{project-name}}/Cargo.axum.toml +++ b/rust+wasm/{{project-name}}/Cargo.axum.toml @@ -45,7 +45,7 @@ axum-extra = { version = "0.9", features = ["typed-header"] } axum-tracing-opentelemetry = { version = "0.19" } base64 = "0.21" chrono = { version = "0.4", default-features = false, features = ["clock"] } -config = "0.13" +config = "0.14" console-subscriber = { version = "0.1", default-features = false, features = [ "parking_lot" ], optional = true } const_format = "0.2" futures = "0.3" @@ -53,9 +53,9 @@ headers = "0.4" http = "1.1" http-serde = "2.1" hyper = "1.0.1" -metrics = "0.20" -metrics-exporter-prometheus = "0.11" -metrics-util = { version = "0.14", default-features = true } +metrics = "0.23" +metrics-exporter-prometheus = "0.15" +metrics-util = { version = "0.17", default-features = true } mime = "0.3" num_cpus = "1.0" once_cell = "1.14" diff --git a/rust+wasm/{{project-name}}/src.axum/metrics/process.rs b/rust+wasm/{{project-name}}/src.axum/metrics/process.rs index 9ae4aa4..67fc3a3 100644 --- a/rust+wasm/{{project-name}}/src.axum/metrics/process.rs +++ b/rust+wasm/{{project-name}}/src.axum/metrics/process.rs @@ -80,31 +80,20 @@ async fn get_proc_stats(mut sys: System) -> Result<()> { let disk = proc.disk_usage(); // cpu-usage divided by # of cores. - metrics::gauge!( - "process_cpu_usage_percentage", - f64::from(proc.cpu_usage() / (cpus as f32)) - ); + metrics::gauge!("process_cpu_usage_percentage") + .set(f64::from(proc.cpu_usage() / (cpus as f32))); // The docs for sysinfo indicate that `virtual_memory` // returns in KB, but that is incorrect. // See this issue: https://github.com/GuillaumeGomez/sysinfo/issues/428#issuecomment-774098021 // And this PR: https://github.com/GuillaumeGomez/sysinfo/pull/430/files - metrics::gauge!( - "process_virtual_memory_bytes", - (proc.virtual_memory()) as f64 - ); - metrics::gauge!("process_memory_bytes", (proc.memory() * 1_000) as f64); - metrics::gauge!("process_uptime_seconds", proc.run_time() as f64); - metrics::gauge!( - "process_disk_total_written_bytes", - disk.total_written_bytes as f64, - ); - metrics::gauge!("process_disk_written_bytes", disk.written_bytes as f64); - metrics::gauge!( - "process_disk_total_read_bytes", - disk.total_read_bytes as f64, - ); - metrics::gauge!("process_disk_read_bytes", disk.read_bytes as f64); + metrics::gauge!("process_virtual_memory_bytes").set(proc.virtual_memory() as f64); + metrics::gauge!("process_memory_bytes").set((proc.memory()) as f64); + metrics::gauge!("process_uptime_seconds").set(proc.run_time() as f64); + metrics::gauge!("process_disk_total_written_bytes").set(disk.total_written_bytes as f64); + metrics::gauge!("process_disk_written_bytes").set(disk.written_bytes as f64); + metrics::gauge!("process_disk_total_read_bytes").set(disk.total_read_bytes as f64); + metrics::gauge!("process_disk_read_bytes").set(disk.read_bytes as f64); } else { info!( subject = "metrics.process_collection", diff --git a/rust+wasm/{{project-name}}/src.axum/middleware/client/metrics.rs b/rust+wasm/{{project-name}}/src.axum/middleware/client/metrics.rs index 215c3e6..5112b8f 100644 --- a/rust+wasm/{{project-name}}/src.axum/middleware/client/metrics.rs +++ b/rust+wasm/{{project-name}}/src.axum/middleware/client/metrics.rs @@ -29,7 +29,7 @@ impl ReqwestMiddleware for Metrics { let now = Instant::now(); let url = request.url().clone(); - let request_path: String = url.path().to_string(); + let request_path = url.path().to_string(); let method = request.method().clone(); let result = next.run(request, extensions).await; @@ -38,17 +38,21 @@ impl ReqwestMiddleware for Metrics { let labels = vec![ ("client", self.name.to_string()), ("method", method.to_string()), - ("request_path", request_path), + ("request_path", request_path.clone()), ]; let extended_labels = extend_labels_for_response(labels, &result); - metrics::increment_counter!("client_http_requests_total", &extended_labels); - metrics::histogram!( - "client_http_request_duration_seconds", - latency, - &extended_labels - ); + metrics::counter!("client_http_requests_total").increment(1u64); + metrics::counter!("client_http_requests_total", &extended_labels).increment(1u64); + metrics::counter!("client_http_requests_total", "client" => self.name.to_string()) + .increment(1u64); + metrics::counter!("client_http_requests_total", "client" => self.name.to_string(), "request_path" => request_path.clone()).increment(1u64); + metrics::histogram!("client_http_request_duration_seconds").record(latency); + metrics::histogram!("client_http_request_duration_seconds", &extended_labels) + .record(latency); + metrics::histogram!("client_http_request_duration_seconds", "client" => self.name.to_string()).record(latency); + metrics::histogram!("client_http_request_duration_seconds", "client" => self.name.to_string(), "request_path" => request_path.clone()).record(latency); result } diff --git a/rust+wasm/{{project-name}}/src.axum/middleware/metrics.rs b/rust+wasm/{{project-name}}/src.axum/middleware/metrics.rs index 2b3719a..767c10a 100644 --- a/rust+wasm/{{project-name}}/src.axum/middleware/metrics.rs +++ b/rust+wasm/{{project-name}}/src.axum/middleware/metrics.rs @@ -9,21 +9,18 @@ use std::time::Instant; pub async fn track(req: Request, next: Next) -> impl IntoResponse { let start = Instant::now(); - let method = req.method().clone(); - let path = req.path(); + let path = req.path().to_string(); let res = next.run(req).await; let latency = start.elapsed().as_secs_f64(); - let status = res.status().as_u16().to_string(); - - let labels = [ - ("method", method.to_string()), - ("request_path", path), - ("status", status), - ]; - - metrics::increment_counter!("http_requests_total", &labels); - metrics::histogram!("http_request_duration_seconds", latency, &labels); + let status = res.status().as_u16(); + metrics::counter!("http_requests_total").increment(1); + metrics::counter!("http_requests_total", "request_path" => path.clone() ).increment(1); + metrics::counter!("http_requests_total", "request_path" => path.clone(), "status" => status.to_string() ).increment(1); + metrics::histogram!("http_request_duration_seconds").record(latency); + metrics::histogram!("http_request_duration_seconds", "request_path" => path.clone()) + .record(latency); + metrics::histogram!("http_request_duration_seconds", "request_path" => path.clone(), "status" => status.to_string()).record(latency); res } diff --git a/rust+wasm/{{project-name}}/src.axum/middleware/reqwest_retry.rs b/rust+wasm/{{project-name}}/src.axum/middleware/reqwest_retry.rs index d118161..543204d 100644 --- a/rust+wasm/{{project-name}}/src.axum/middleware/reqwest_retry.rs +++ b/rust+wasm/{{project-name}}/src.axum/middleware/reqwest_retry.rs @@ -163,8 +163,7 @@ impl RetryTransientMiddleware { extensions: &mut Extensions, next: Next<'a>, ) -> Result { - let url = request.url().clone(); - let request_path: String = url.path().to_string(); + let request_path = request.url().path().to_string(); let method = request.method().clone(); let result = next.run(request, extensions).await; @@ -172,12 +171,15 @@ impl RetryTransientMiddleware { let labels = vec![ ("client", self.client_name.to_string()), ("method", method.to_string()), - ("request_path", request_path), + ("request_path", request_path.clone()), ]; let extended_labels = client::metrics::extend_labels_for_response(labels, &result); - metrics::increment_counter!("client_http_requests_retry_total", &extended_labels); + metrics::counter!("client_http_requests_retry_total").increment(1); + metrics::counter!("client_http_requests_retry_total", "request_path" => request_path) + .increment(1); + metrics::counter!("client_http_requests_retry_total", &extended_labels).increment(1); result } } diff --git a/rust+wasm/{{project-name}}/src.axum/tracing_layers/metrics_layer.rs b/rust+wasm/{{project-name}}/src.axum/tracing_layers/metrics_layer.rs index cae1451..0423873 100644 --- a/rust+wasm/{{project-name}}/src.axum/tracing_layers/metrics_layer.rs +++ b/rust+wasm/{{project-name}}/src.axum/tracing_layers/metrics_layer.rs @@ -68,12 +68,9 @@ where // Need to sort labels to remain the same across all metrics. labels.sort_unstable(); - metrics::increment_counter!(format!("{name}_total"), &labels); - metrics::histogram!( - format!("{name}_duration_seconds"), - elapsed_secs_f64, - &labels - ); + metrics::counter!(format!("{name}_total"), &labels).increment(1); + metrics::histogram!(format!("{name}_duration_seconds"), &labels) + .record(elapsed_secs_f64); // Remove storage as this is the last layer. extensions diff --git a/rust/Cargo.axum.toml b/rust/Cargo.axum.toml index 34d1941..e05b707 100644 --- a/rust/Cargo.axum.toml +++ b/rust/Cargo.axum.toml @@ -52,7 +52,7 @@ axum-extra = { version = "0.9", features = ["typed-header"] } axum-tracing-opentelemetry = { version = "0.19" } base64 = "0.21" chrono = { version = "0.4", default-features = false, features = ["clock"] } -config = "0.13" +config = "0.14" console-subscriber = { version = "0.1", default-features = false, features = [ "parking_lot" ], optional = true } const_format = "0.2" futures = "0.3" @@ -60,9 +60,9 @@ headers = "0.4" http = "1.1" http-serde = "2.1" hyper = "1.0.1" -metrics = "0.20" -metrics-exporter-prometheus = "0.11" -metrics-util = { version = "0.14", default-features = true } +metrics = "0.23" +metrics-exporter-prometheus = "0.15" +metrics-util = { version = "0.17", default-features = true } mime = "0.3" num_cpus = "1.0" once_cell = "1.14" diff --git a/rust/deny.toml b/rust/deny.toml index 8200940..c9aa6ec 100644 --- a/rust/deny.toml +++ b/rust/deny.toml @@ -51,9 +51,6 @@ ignore = [ "RUSTSEC-2021-0145", # atty on windows only "RUSTSEC-2023-0071", # Impacts rsa crate, which is only used in dev, see # https://github.com/RustCrypto/RSA/pull/394 for remediation - "RUSTSEC-2024-0336", # Ignore a DOS issue w/ rustls-0.20.9. This will go - # away when we update opentelemetry-otlp soon. - { id = "RUSTSEC-2020-0168", reason = "Not planning to force upgrade to mach2 yet" }, { id = "RUSTSEC-2024-0320", reason = "Not planning to force upgrade to rust-yaml2 yet" }, ] # Threshold for security vulnerabilities, any vulnerability with a CVSS score @@ -118,6 +115,7 @@ exceptions = [ # this is not a problem for us. See https://github.com/dtolnay/unicode-ident/pull/9/files { allow = ["Unicode-DFS-2016"], name = "unicode-ident", version = "*"}, { allow = ["OpenSSL"], name = "ring", version = "*" }, + { allow = ["OpenSSL"], name = "aws-lc-sys", version = "*" }, { allow = ["MPL-2.0"], name = "webpki-roots", version = "*"}, ] diff --git a/rust/src.axum/metrics/process.rs b/rust/src.axum/metrics/process.rs index 9ae4aa4..67fc3a3 100644 --- a/rust/src.axum/metrics/process.rs +++ b/rust/src.axum/metrics/process.rs @@ -80,31 +80,20 @@ async fn get_proc_stats(mut sys: System) -> Result<()> { let disk = proc.disk_usage(); // cpu-usage divided by # of cores. - metrics::gauge!( - "process_cpu_usage_percentage", - f64::from(proc.cpu_usage() / (cpus as f32)) - ); + metrics::gauge!("process_cpu_usage_percentage") + .set(f64::from(proc.cpu_usage() / (cpus as f32))); // The docs for sysinfo indicate that `virtual_memory` // returns in KB, but that is incorrect. // See this issue: https://github.com/GuillaumeGomez/sysinfo/issues/428#issuecomment-774098021 // And this PR: https://github.com/GuillaumeGomez/sysinfo/pull/430/files - metrics::gauge!( - "process_virtual_memory_bytes", - (proc.virtual_memory()) as f64 - ); - metrics::gauge!("process_memory_bytes", (proc.memory() * 1_000) as f64); - metrics::gauge!("process_uptime_seconds", proc.run_time() as f64); - metrics::gauge!( - "process_disk_total_written_bytes", - disk.total_written_bytes as f64, - ); - metrics::gauge!("process_disk_written_bytes", disk.written_bytes as f64); - metrics::gauge!( - "process_disk_total_read_bytes", - disk.total_read_bytes as f64, - ); - metrics::gauge!("process_disk_read_bytes", disk.read_bytes as f64); + metrics::gauge!("process_virtual_memory_bytes").set(proc.virtual_memory() as f64); + metrics::gauge!("process_memory_bytes").set((proc.memory()) as f64); + metrics::gauge!("process_uptime_seconds").set(proc.run_time() as f64); + metrics::gauge!("process_disk_total_written_bytes").set(disk.total_written_bytes as f64); + metrics::gauge!("process_disk_written_bytes").set(disk.written_bytes as f64); + metrics::gauge!("process_disk_total_read_bytes").set(disk.total_read_bytes as f64); + metrics::gauge!("process_disk_read_bytes").set(disk.read_bytes as f64); } else { info!( subject = "metrics.process_collection", diff --git a/rust/src.axum/middleware/client/metrics.rs b/rust/src.axum/middleware/client/metrics.rs index 215c3e6..5112b8f 100644 --- a/rust/src.axum/middleware/client/metrics.rs +++ b/rust/src.axum/middleware/client/metrics.rs @@ -29,7 +29,7 @@ impl ReqwestMiddleware for Metrics { let now = Instant::now(); let url = request.url().clone(); - let request_path: String = url.path().to_string(); + let request_path = url.path().to_string(); let method = request.method().clone(); let result = next.run(request, extensions).await; @@ -38,17 +38,21 @@ impl ReqwestMiddleware for Metrics { let labels = vec![ ("client", self.name.to_string()), ("method", method.to_string()), - ("request_path", request_path), + ("request_path", request_path.clone()), ]; let extended_labels = extend_labels_for_response(labels, &result); - metrics::increment_counter!("client_http_requests_total", &extended_labels); - metrics::histogram!( - "client_http_request_duration_seconds", - latency, - &extended_labels - ); + metrics::counter!("client_http_requests_total").increment(1u64); + metrics::counter!("client_http_requests_total", &extended_labels).increment(1u64); + metrics::counter!("client_http_requests_total", "client" => self.name.to_string()) + .increment(1u64); + metrics::counter!("client_http_requests_total", "client" => self.name.to_string(), "request_path" => request_path.clone()).increment(1u64); + metrics::histogram!("client_http_request_duration_seconds").record(latency); + metrics::histogram!("client_http_request_duration_seconds", &extended_labels) + .record(latency); + metrics::histogram!("client_http_request_duration_seconds", "client" => self.name.to_string()).record(latency); + metrics::histogram!("client_http_request_duration_seconds", "client" => self.name.to_string(), "request_path" => request_path.clone()).record(latency); result } diff --git a/rust/src.axum/middleware/metrics.rs b/rust/src.axum/middleware/metrics.rs index 2b3719a..767c10a 100644 --- a/rust/src.axum/middleware/metrics.rs +++ b/rust/src.axum/middleware/metrics.rs @@ -9,21 +9,18 @@ use std::time::Instant; pub async fn track(req: Request, next: Next) -> impl IntoResponse { let start = Instant::now(); - let method = req.method().clone(); - let path = req.path(); + let path = req.path().to_string(); let res = next.run(req).await; let latency = start.elapsed().as_secs_f64(); - let status = res.status().as_u16().to_string(); - - let labels = [ - ("method", method.to_string()), - ("request_path", path), - ("status", status), - ]; - - metrics::increment_counter!("http_requests_total", &labels); - metrics::histogram!("http_request_duration_seconds", latency, &labels); + let status = res.status().as_u16(); + metrics::counter!("http_requests_total").increment(1); + metrics::counter!("http_requests_total", "request_path" => path.clone() ).increment(1); + metrics::counter!("http_requests_total", "request_path" => path.clone(), "status" => status.to_string() ).increment(1); + metrics::histogram!("http_request_duration_seconds").record(latency); + metrics::histogram!("http_request_duration_seconds", "request_path" => path.clone()) + .record(latency); + metrics::histogram!("http_request_duration_seconds", "request_path" => path.clone(), "status" => status.to_string()).record(latency); res } diff --git a/rust/src.axum/middleware/reqwest_retry.rs b/rust/src.axum/middleware/reqwest_retry.rs index d118161..543204d 100644 --- a/rust/src.axum/middleware/reqwest_retry.rs +++ b/rust/src.axum/middleware/reqwest_retry.rs @@ -163,8 +163,7 @@ impl RetryTransientMiddleware { extensions: &mut Extensions, next: Next<'a>, ) -> Result { - let url = request.url().clone(); - let request_path: String = url.path().to_string(); + let request_path = request.url().path().to_string(); let method = request.method().clone(); let result = next.run(request, extensions).await; @@ -172,12 +171,15 @@ impl RetryTransientMiddleware { let labels = vec![ ("client", self.client_name.to_string()), ("method", method.to_string()), - ("request_path", request_path), + ("request_path", request_path.clone()), ]; let extended_labels = client::metrics::extend_labels_for_response(labels, &result); - metrics::increment_counter!("client_http_requests_retry_total", &extended_labels); + metrics::counter!("client_http_requests_retry_total").increment(1); + metrics::counter!("client_http_requests_retry_total", "request_path" => request_path) + .increment(1); + metrics::counter!("client_http_requests_retry_total", &extended_labels).increment(1); result } } diff --git a/rust/src.axum/tracing_layers/metrics_layer.rs b/rust/src.axum/tracing_layers/metrics_layer.rs index cae1451..0423873 100644 --- a/rust/src.axum/tracing_layers/metrics_layer.rs +++ b/rust/src.axum/tracing_layers/metrics_layer.rs @@ -68,12 +68,9 @@ where // Need to sort labels to remain the same across all metrics. labels.sort_unstable(); - metrics::increment_counter!(format!("{name}_total"), &labels); - metrics::histogram!( - format!("{name}_duration_seconds"), - elapsed_secs_f64, - &labels - ); + metrics::counter!(format!("{name}_total"), &labels).increment(1); + metrics::histogram!(format!("{name}_duration_seconds"), &labels) + .record(elapsed_secs_f64); // Remove storage as this is the last layer. extensions