From 034bef5368f72fce199d13443a5cd914a2dd70eb Mon Sep 17 00:00:00 2001 From: Ossi Herrala Date: Tue, 4 Feb 2025 11:22:42 +0200 Subject: [PATCH] Update to rand 0.9 I also dropped rand_distr dependency since it was unnecessary. --- Cargo.toml | 4 ++-- metrics-exporter-dogstatsd/Cargo.toml | 2 +- .../examples/dogstatsd_synchronous.rs | 8 ++++---- metrics-exporter-dogstatsd/src/builder.rs | 1 + .../examples/prometheus_push_gateway.rs | 3 +-- .../examples/prometheus_server.rs | 3 +-- .../examples/prometheus_uds_server.rs | 3 +-- metrics-exporter-tcp/examples/tcp_server.rs | 5 ++--- metrics-util/Cargo.toml | 5 ++--- metrics-util/examples/bucket-crusher.rs | 6 +++--- metrics-util/examples/segqueue-torture.rs | 6 +++--- metrics-util/src/storage/reservoir.rs | 6 +++--- metrics-util/src/storage/summary.rs | 11 +++++------ metrics/benches/macros.rs | 5 ++--- tooling/metrics-histogram-fidelity/Cargo.toml | 3 +-- tooling/metrics-histogram-fidelity/src/main.rs | 9 ++++----- 16 files changed, 36 insertions(+), 44 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 123dd428..fb290b3e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -63,8 +63,8 @@ quanta = { version = "0.12", default-features = false } quickcheck = { version = "1", default-features = false } quickcheck_macros = { version = "1", default-features = false } radix_trie = { version = "0.2", default-features = false } -rand = { version = "0.8", default-features = false, features = ["std", "std_rng"] } -rand_distr = { version = "0.4", default-features = false } +rand = { version = "0.9", default-features = false, features = [ "thread_rng" ] } +rand_xoshiro = { version = "0.7", default-features = false } ratatui = { version = "0.28", default-features = false } sketches-ddsketch = { version = "0.3", default-features = false } thiserror = { version = "1", default-features = false } diff --git a/metrics-exporter-dogstatsd/Cargo.toml b/metrics-exporter-dogstatsd/Cargo.toml index 51d3b213..feed29ea 100644 --- a/metrics-exporter-dogstatsd/Cargo.toml +++ b/metrics-exporter-dogstatsd/Cargo.toml @@ -27,6 +27,6 @@ tracing = { workspace = true } [dev-dependencies] proptest = { workspace = true } rand = { workspace = true } -rand_xoshiro = { version = "0.6", default-features = false } +rand_xoshiro = { workspace = true } tracing = { workspace = true } tracing-subscriber = { workspace = true, features = ["fmt"] } diff --git a/metrics-exporter-dogstatsd/examples/dogstatsd_synchronous.rs b/metrics-exporter-dogstatsd/examples/dogstatsd_synchronous.rs index dc95fd63..54f651f9 100644 --- a/metrics-exporter-dogstatsd/examples/dogstatsd_synchronous.rs +++ b/metrics-exporter-dogstatsd/examples/dogstatsd_synchronous.rs @@ -1,6 +1,6 @@ use metrics::{counter, gauge, histogram}; use metrics_exporter_dogstatsd::DogStatsDBuilder; -use rand::{thread_rng, Rng, SeedableRng as _}; +use rand::{Rng, SeedableRng}; use rand_xoshiro::Xoshiro256StarStar; fn main() { @@ -18,14 +18,14 @@ fn main() { let server_loops = counter!("tcp_server_loops", "system" => "foo"); let server_loops_delta_secs = histogram!("tcp_server_loop_delta_secs", "system" => "foo"); - let mut rng = Xoshiro256StarStar::from_rng(thread_rng()).unwrap(); + let mut rng = Xoshiro256StarStar::try_from_rng(&mut rand::rng()).unwrap(); // Loop over and over, pretending to do some work. loop { server_loops.increment(1); - server_loops_delta_secs.record(rng.gen_range(0.0..1.0)); + server_loops_delta_secs.record(rng.random_range(0.0..1.0)); - let increment_gauge = thread_rng().gen_bool(0.75); + let increment_gauge = rand::random_bool(0.75); let gauge = gauge!("lucky_iterations"); if increment_gauge { gauge.increment(1.0); diff --git a/metrics-exporter-dogstatsd/src/builder.rs b/metrics-exporter-dogstatsd/src/builder.rs index b1f0a254..b0833954 100644 --- a/metrics-exporter-dogstatsd/src/builder.rs +++ b/metrics-exporter-dogstatsd/src/builder.rs @@ -438,6 +438,7 @@ mod tests { ); } + #[cfg(target_os = "linux")] mod linux { use super::*; diff --git a/metrics-exporter-prometheus/examples/prometheus_push_gateway.rs b/metrics-exporter-prometheus/examples/prometheus_push_gateway.rs index 229a0f38..17f26593 100644 --- a/metrics-exporter-prometheus/examples/prometheus_push_gateway.rs +++ b/metrics-exporter-prometheus/examples/prometheus_push_gateway.rs @@ -12,7 +12,6 @@ use metrics_exporter_prometheus::PrometheusBuilder; use metrics_util::MetricKindMask; use quanta::Clock; -use rand::{thread_rng, Rng}; fn main() { tracing_subscriber::fmt::init(); @@ -59,7 +58,7 @@ fn main() { histogram!("tcp_server_loop_delta_secs", "system" => "foo").record(delta); } - let increment_gauge = thread_rng().gen_bool(0.75); + let increment_gauge = rand::random_bool(0.75); let gauge = gauge!("lucky_iterations"); if increment_gauge { gauge.increment(1.0); diff --git a/metrics-exporter-prometheus/examples/prometheus_server.rs b/metrics-exporter-prometheus/examples/prometheus_server.rs index 13a68a56..372e4102 100644 --- a/metrics-exporter-prometheus/examples/prometheus_server.rs +++ b/metrics-exporter-prometheus/examples/prometheus_server.rs @@ -6,7 +6,6 @@ use metrics_exporter_prometheus::PrometheusBuilder; use metrics_util::MetricKindMask; use quanta::Clock; -use rand::{thread_rng, Rng}; fn main() { tracing_subscriber::fmt::init(); @@ -47,7 +46,7 @@ fn main() { histogram!("tcp_server_loop_delta_secs", "system" => "foo").record(delta); } - let increment_gauge = thread_rng().gen_bool(0.75); + let increment_gauge = rand::random_bool(0.75); let gauge = gauge!("lucky_iterations"); if increment_gauge { gauge.increment(1.0); diff --git a/metrics-exporter-prometheus/examples/prometheus_uds_server.rs b/metrics-exporter-prometheus/examples/prometheus_uds_server.rs index 770c07ff..d71f43f0 100644 --- a/metrics-exporter-prometheus/examples/prometheus_uds_server.rs +++ b/metrics-exporter-prometheus/examples/prometheus_uds_server.rs @@ -6,7 +6,6 @@ use metrics_exporter_prometheus::PrometheusBuilder; use metrics_util::MetricKindMask; use quanta::Clock; -use rand::{thread_rng, Rng}; fn main() { tracing_subscriber::fmt::init(); @@ -47,7 +46,7 @@ fn main() { histogram!("tcp_server_loop_delta_secs", "system" => "foo").record(delta); } - let increment_gauge = thread_rng().gen_bool(0.75); + let increment_gauge = rand::random_bool(0.75); let gauge = gauge!("lucky_iterations"); if increment_gauge { gauge.increment(1.0); diff --git a/metrics-exporter-tcp/examples/tcp_server.rs b/metrics-exporter-tcp/examples/tcp_server.rs index 1953fa2b..b763cb70 100644 --- a/metrics-exporter-tcp/examples/tcp_server.rs +++ b/metrics-exporter-tcp/examples/tcp_server.rs @@ -5,7 +5,6 @@ use metrics::{counter, describe_histogram, gauge, histogram, Unit}; use metrics_exporter_tcp::TcpBuilder; use quanta::Clock; -use rand::{thread_rng, Rng}; fn main() { tracing_subscriber::fmt::init(); @@ -30,7 +29,7 @@ fn main() { histogram!("tcp_server_loop_delta_secs", "system" => "foo").record(delta); } - let increment_gauge = thread_rng().gen_bool(0.75); + let increment_gauge = rand::random_bool(0.75); let gauge = gauge!("lucky_iterations"); if increment_gauge { gauge.increment(1.0); @@ -40,7 +39,7 @@ fn main() { last = Some(clock.now()); - let sleep_time = thread_rng().gen_range(250..750); + let sleep_time = rand::random_range(250..750); thread::sleep(Duration::from_millis(sleep_time)); } diff --git a/metrics-util/Cargo.toml b/metrics-util/Cargo.toml index 3cbe7c83..c0931c0e 100644 --- a/metrics-util/Cargo.toml +++ b/metrics-util/Cargo.toml @@ -56,7 +56,7 @@ metrics = { version = "^0.24", path = "../metrics" } ordered-float = { workspace = true, optional = true } quanta = { workspace = true, optional = true } rand = { workspace = true, optional = true } -rand_xoshiro = { version = "0.6", default-features = false, optional = true } +rand_xoshiro = { workspace = true, default-features = false, optional = true } radix_trie = { workspace = true, optional = true } sketches-ddsketch = { workspace = true, optional = true } @@ -76,8 +76,7 @@ predicates-core = { workspace = true } predicates-tree = { workspace = true } quickcheck = { workspace = true } quickcheck_macros = { workspace = true } -rand = { workspace = true, features = ["small_rng"] } -rand_distr = { workspace = true } +rand = { workspace = true } sketches-ddsketch = { workspace = true } tracing = { workspace = true } tracing-subscriber = { workspace = true, features = ["fmt", "ansi"] } diff --git a/metrics-util/examples/bucket-crusher.rs b/metrics-util/examples/bucket-crusher.rs index 01ef44a8..0da6fe70 100644 --- a/metrics-util/examples/bucket-crusher.rs +++ b/metrics-util/examples/bucket-crusher.rs @@ -8,7 +8,7 @@ use std::time::{Duration, Instant}; use getopts::Options; use metrics_util::storage::AtomicBucket; -use rand::{thread_rng, Rng}; +use rand::Rng; use tracing::{debug, error, info}; const COUNTER_LOOP: usize = 1024; @@ -102,7 +102,7 @@ fn run_producer( ) { let mut counter_local = 0; let mut total_local = 0; - let mut rand = thread_rng(); + let mut rand = rand::rng(); loop { // Every COUNTER_LOOP iterations, do housekeeping. @@ -120,7 +120,7 @@ fn run_producer( // Significantly speeds things up if we just push a bunch of values in a tight loop, // which should really exercise the core bucket push logic more efficiently. - let value = rand.gen_range(0..1024); + let value = rand.random_range(0..1024); let n = 32; for _ in 0..n { bucket.push(value); diff --git a/metrics-util/examples/segqueue-torture.rs b/metrics-util/examples/segqueue-torture.rs index 88883a01..8fd41f2c 100644 --- a/metrics-util/examples/segqueue-torture.rs +++ b/metrics-util/examples/segqueue-torture.rs @@ -8,7 +8,7 @@ use std::time::Duration; use crossbeam_queue::SegQueue; use getopts::Options; -use rand::{thread_rng, Rng}; +use rand::Rng; use tracing::{error, info}; const COUNTER_LOOP: usize = 1024; @@ -102,7 +102,7 @@ fn run_producer( ) { let mut counter_local = 0; let mut total_local = 0; - let mut rand = thread_rng(); + let mut rand = rand::rng(); loop { // Every COUNTER_LOOP iterations, do housekeeping. @@ -118,7 +118,7 @@ fn run_producer( } } - let value = rand.gen_range(0..1024); + let value = rand.random_range(0..1024); queue.push(value); total_local += value; diff --git a/metrics-util/src/storage/reservoir.rs b/metrics-util/src/storage/reservoir.rs index d1fc68d8..1b8e8205 100644 --- a/metrics-util/src/storage/reservoir.rs +++ b/metrics-util/src/storage/reservoir.rs @@ -11,12 +11,12 @@ use std::{ }, }; -use rand::{rngs::OsRng, Rng as _, SeedableRng as _}; +use rand::{rngs::OsRng, Rng, SeedableRng}; use rand_xoshiro::Xoshiro256StarStar; thread_local! { static FAST_RNG: UnsafeCell = { - UnsafeCell::new(Xoshiro256StarStar::from_rng(OsRng).unwrap()) + UnsafeCell::new(Xoshiro256StarStar::try_from_rng(&mut OsRng).unwrap()) }; } @@ -25,7 +25,7 @@ fn fastrand(upper: usize) -> usize { // SAFETY: We know it's safe to take a mutable reference since we're getting a pointer to a thread-local value, // and the reference never outlives the closure executing on this thread. let rng = unsafe { &mut *rng.get() }; - rng.gen_range(0..upper) + rng.random_range(0..upper) }) } diff --git a/metrics-util/src/storage/summary.rs b/metrics-util/src/storage/summary.rs index 3056741b..7b4518a7 100644 --- a/metrics-util/src/storage/summary.rs +++ b/metrics-util/src/storage/summary.rs @@ -179,8 +179,7 @@ mod tests { use ndarray_stats::{interpolate::Linear, QuantileExt}; use noisy_float::types::n64; use ordered_float::NotNan; - use rand::{distributions::Distribution, thread_rng}; - use rand_distr::Uniform; + use rand::distr::{Distribution, Uniform}; #[test] fn test_basics() { @@ -248,8 +247,8 @@ mod tests { let max_bins = 32_768; let min_value = 1.0e-9; - let mut rng = thread_rng(); - let dist = Uniform::new(0.0, 100.0); + let mut rng = rand::rng(); + let dist = Uniform::new(0.0, 100.0).unwrap(); let mut summary = Summary::new(alpha, max_bins, min_value); let mut uniform = Vec::new(); @@ -283,8 +282,8 @@ mod tests { let max_bins = 65_536; let min_value = 1.0e-9; - let mut rng = thread_rng(); - let dist = Uniform::new(-100.0, 100.0); + let mut rng = rand::rng(); + let dist = Uniform::new(-100.0, 100.0).unwrap(); let mut summary = Summary::new(alpha, max_bins, min_value); let mut uniform = Vec::new(); diff --git a/metrics/benches/macros.rs b/metrics/benches/macros.rs index 7394d2c8..cca2f1bb 100644 --- a/metrics/benches/macros.rs +++ b/metrics/benches/macros.rs @@ -6,7 +6,6 @@ use criterion::Criterion; use metrics::{ counter, Counter, Gauge, Histogram, Key, KeyName, Metadata, Recorder, SharedString, Unit, }; -use rand::{thread_rng, Rng}; #[derive(Debug)] struct TestRecorder; @@ -53,7 +52,7 @@ fn macro_benchmark(c: &mut Criterion) { group.bench_function("global_initialized/with_dynamic_labels", |b| { let _ = metrics::set_global_recorder(TestRecorder); - let label_val = thread_rng().gen::().to_string(); + let label_val = rand::random::().to_string(); b.iter(move || { counter!("counter_bench", "request" => "http", "uid" => label_val.clone()) .increment(42); @@ -75,7 +74,7 @@ fn macro_benchmark(c: &mut Criterion) { }); group.bench_function("local_initialized/with_dynamic_labels", |b| { metrics::with_local_recorder(&TestRecorder, || { - let label_val = thread_rng().gen::().to_string(); + let label_val = rand::random::().to_string(); b.iter(move || { counter!("counter_bench", "request" => "http", "uid" => label_val.clone()) .increment(42); diff --git a/tooling/metrics-histogram-fidelity/Cargo.toml b/tooling/metrics-histogram-fidelity/Cargo.toml index fbd0967f..0232ecd1 100644 --- a/tooling/metrics-histogram-fidelity/Cargo.toml +++ b/tooling/metrics-histogram-fidelity/Cargo.toml @@ -9,8 +9,7 @@ rust-version = "1.56.1" [dependencies] metrics-util = { path = "../../metrics-util" } -rand = { version = "0.7", features = ["small_rng"] } -rand_distr = "0.3" +rand = "0.9" ndarray = "0.13" ndarray-stats = "0.3" noisy_float = "0.1" diff --git a/tooling/metrics-histogram-fidelity/src/main.rs b/tooling/metrics-histogram-fidelity/src/main.rs index a16f66f1..08eab66f 100644 --- a/tooling/metrics-histogram-fidelity/src/main.rs +++ b/tooling/metrics-histogram-fidelity/src/main.rs @@ -1,10 +1,9 @@ -use metrics_util::Summary; +use metrics_util::storage::Summary; use ndarray::{Array1, Axis}; use ndarray_stats::{interpolate::Linear, QuantileExt}; use noisy_float::types::n64; use ordered_float::NotNan; -use rand::{distributions::Distribution, thread_rng}; -use rand_distr::Uniform; +use rand::distr::{Distribution, Uniform}; use std::fs::File; use std::io; use std::io::prelude::*; @@ -15,8 +14,8 @@ fn main() -> io::Result<()> { // Generates a consistent set of inputs, and uses them to feed to a reference DDSketch // implementation so we can get the quantiles produced for our comparison. println!("generating uniform distribution..."); - let mut rng = thread_rng(); - let dist = Uniform::new(-25.0, 75.0); + let mut rng = rand::rng(); + let dist = Uniform::new(-25.0, 75.0).unwrap(); let mut summary = Summary::new(0.0001, 32768, 1.0e-9); let mut uniform = Vec::new();