Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(deps): Update metrics to 0.23. Note: 0.22 was a major change… #189

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions rust+wasm/deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = "*"},
]

Expand Down
8 changes: 4 additions & 4 deletions rust+wasm/{{project-name}}/Cargo.axum.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ 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"
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"
Expand Down
29 changes: 9 additions & 20 deletions rust+wasm/{{project-name}}/src.axum/metrics/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
}
Expand Down
21 changes: 9 additions & 12 deletions rust+wasm/{{project-name}}/src.axum/middleware/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,18 @@ use std::time::Instant;
pub async fn track(req: Request<Body>, 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
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,21 +163,23 @@ impl<T: RetryPolicy + Send + Sync> RetryTransientMiddleware<T> {
extensions: &mut Extensions,
next: Next<'a>,
) -> Result<Response> {
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;

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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions rust/Cargo.axum.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,17 @@ 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"
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"
Expand Down
4 changes: 1 addition & 3 deletions rust/deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = "*"},
]

Expand Down
29 changes: 9 additions & 20 deletions rust/src.axum/metrics/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 12 additions & 8 deletions rust/src.axum/middleware/client/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
}
Expand Down
21 changes: 9 additions & 12 deletions rust/src.axum/middleware/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,18 @@ use std::time::Instant;
pub async fn track(req: Request<Body>, 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
}
10 changes: 6 additions & 4 deletions rust/src.axum/middleware/reqwest_retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,21 +163,23 @@ impl<T: RetryPolicy + Send + Sync> RetryTransientMiddleware<T> {
extensions: &mut Extensions,
next: Next<'a>,
) -> Result<Response> {
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;

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
}
}
9 changes: 3 additions & 6 deletions rust/src.axum/tracing_layers/metrics_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading