From ea94ca6656767dd0d3b4f05de150ce400dde1a95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20M=C3=A4rtens?= Date: Mon, 8 Mar 2021 09:49:16 +0100 Subject: [PATCH] switch server to the new Registry struct, this gets rid of RegistryFn pattern and simplyfies stuff, its a result of the Prometheus-tracy crate --- server/src/lib.rs | 24 ++--- server/src/metrics.rs | 206 ++++++++++++++---------------------------- 2 files changed, 75 insertions(+), 155 deletions(-) diff --git a/server/src/lib.rs b/server/src/lib.rs index acf9d5dfea..8b83cf7e88 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -160,14 +160,13 @@ impl Server { panic!("Migration error: {:?}", e); } - let (chunk_gen_metrics, registry_chunk) = metrics::ChunkGenMetrics::new().unwrap(); - let (network_request_metrics, registry_network) = - metrics::NetworkRequestMetrics::new().unwrap(); - let (player_metrics, registry_player) = metrics::PlayerMetrics::new().unwrap(); - let (ecs_system_metrics, registry_state) = EcsSystemMetrics::new().unwrap(); - let (tick_metrics, registry_tick) = - TickMetrics::new().expect("Failed to initialize server tick metrics submodule."); - let (physics_metrics, registry_physics) = PhysicsMetrics::new().unwrap(); + let registry = Arc::new(Registry::new()); + let chunk_gen_metrics = metrics::ChunkGenMetrics::new(®istry).unwrap(); + let network_request_metrics = metrics::NetworkRequestMetrics::new(®istry).unwrap(); + let player_metrics = metrics::PlayerMetrics::new(®istry).unwrap(); + let ecs_system_metrics = EcsSystemMetrics::new(®istry).unwrap(); + let tick_metrics = TickMetrics::new(®istry).unwrap(); + let physics_metrics = PhysicsMetrics::new(®istry).unwrap(); let mut state = State::server(); state.ecs_mut().insert(settings.clone()); @@ -349,15 +348,6 @@ impl Server { state.ecs_mut().insert(DeletedEntities::default()); - // register all metrics submodules here - let registry = Arc::new(Registry::new()); - registry_chunk(®istry).expect("failed to register chunk gen metrics"); - registry_network(®istry).expect("failed to register network request metrics"); - registry_player(®istry).expect("failed to register player metrics"); - registry_tick(®istry).expect("failed to register tick metrics"); - registry_state(®istry).expect("failed to register state metrics"); - registry_physics(®istry).expect("failed to register state metrics"); - let network = Network::new_with_registry(Pid::new(), &runtime, ®istry); let metrics_shutdown = Arc::new(Notify::new()); let metrics_shutdown_clone = Arc::clone(&metrics_shutdown); diff --git a/server/src/metrics.rs b/server/src/metrics.rs index 4f7a7e60a6..596611cef8 100644 --- a/server/src/metrics.rs +++ b/server/src/metrics.rs @@ -8,8 +8,6 @@ use std::{ time::{Duration, SystemTime, UNIX_EPOCH}, }; -type RegistryFn = Box Result<(), prometheus::Error>>; - pub struct PhysicsMetrics { pub entity_entity_collision_checks_count: IntCounter, pub entity_entity_collisions_count: IntCounter, @@ -57,7 +55,7 @@ pub struct TickMetrics { } impl PhysicsMetrics { - pub fn new() -> Result<(Self, RegistryFn), prometheus::Error> { + pub fn new(registry: &Registry) -> Result { let entity_entity_collision_checks_count = IntCounter::with_opts(Opts::new( "entity_entity_collision_checks_count", "shows the number of collision checks", @@ -67,28 +65,18 @@ impl PhysicsMetrics { "shows the number of actual collisions detected", ))?; - let entity_entity_collision_checks_count_clone = - entity_entity_collision_checks_count.clone(); - let entity_entity_collisions_count_clone = entity_entity_collisions_count.clone(); + registry.register(Box::new(entity_entity_collision_checks_count.clone()))?; + registry.register(Box::new(entity_entity_collisions_count.clone()))?; - let f = |registry: &Registry| { - registry.register(Box::new(entity_entity_collision_checks_count_clone))?; - registry.register(Box::new(entity_entity_collisions_count_clone))?; - Ok(()) - }; - - Ok(( - Self { - entity_entity_collision_checks_count, - entity_entity_collisions_count, - }, - Box::new(f), - )) + Ok(Self { + entity_entity_collision_checks_count, + entity_entity_collisions_count, + }) } } impl EcsSystemMetrics { - pub fn new() -> Result<(Self, RegistryFn), prometheus::Error> { + pub fn new(registry: &Registry) -> Result { let bucket = vec![ Duration::from_micros(1).as_secs_f64(), Duration::from_micros(10).as_secs_f64(), @@ -137,36 +125,24 @@ impl EcsSystemMetrics { &["system"], )?; - let system_length_hist_clone = system_length_hist.clone(); - let system_length_count_clone = system_length_count.clone(); - let system_start_time_clone = system_start_time.clone(); - let system_length_time_clone = system_length_time.clone(); - let system_thread_avg_clone = system_thread_avg.clone(); + registry.register(Box::new(system_length_hist.clone()))?; + registry.register(Box::new(system_length_count.clone()))?; + registry.register(Box::new(system_start_time.clone()))?; + registry.register(Box::new(system_length_time.clone()))?; + registry.register(Box::new(system_thread_avg.clone()))?; - let f = |registry: &Registry| { - registry.register(Box::new(system_length_hist_clone))?; - registry.register(Box::new(system_length_count_clone))?; - registry.register(Box::new(system_start_time_clone))?; - registry.register(Box::new(system_length_time_clone))?; - registry.register(Box::new(system_thread_avg_clone))?; - Ok(()) - }; - - Ok(( - Self { - system_length_hist, - system_length_count, - system_start_time, - system_length_time, - system_thread_avg, - }, - Box::new(f), - )) + Ok(Self { + system_length_hist, + system_length_count, + system_start_time, + system_length_time, + system_thread_avg, + }) } } impl PlayerMetrics { - pub fn new() -> Result<(Self, RegistryFn), prometheus::Error> { + pub fn new(registry: &Registry) -> Result { let clients_connected = IntCounter::with_opts(Opts::new( "clients_connected", "shows the number of clients joined to the server", @@ -184,30 +160,20 @@ impl PlayerMetrics { &["reason"], )?; - let clients_connected_clone = clients_connected.clone(); - let players_connected_clone = players_connected.clone(); - let clients_disconnected_clone = clients_disconnected.clone(); + registry.register(Box::new(clients_connected.clone()))?; + registry.register(Box::new(players_connected.clone()))?; + registry.register(Box::new(clients_disconnected.clone()))?; - let f = |registry: &Registry| { - registry.register(Box::new(clients_connected_clone))?; - registry.register(Box::new(players_connected_clone))?; - registry.register(Box::new(clients_disconnected_clone))?; - Ok(()) - }; - - Ok(( - Self { - clients_connected, - players_connected, - clients_disconnected, - }, - Box::new(f), - )) + Ok(Self { + clients_connected, + players_connected, + clients_disconnected, + }) } } impl NetworkRequestMetrics { - pub fn new() -> Result<(Self, RegistryFn), prometheus::Error> { + pub fn new(registry: &Registry) -> Result { let chunks_request_dropped = IntCounter::with_opts(Opts::new( "chunks_request_dropped", "number of all chunk request dropped, e.g because the player was to far away", @@ -221,30 +187,20 @@ impl NetworkRequestMetrics { "number of all chunks that were requested and needs to be generated", ))?; - let chunks_request_dropped_clone = chunks_request_dropped.clone(); - let chunks_served_from_memory_clone = chunks_served_from_memory.clone(); - let chunks_generation_triggered_clone = chunks_generation_triggered.clone(); + registry.register(Box::new(chunks_request_dropped.clone()))?; + registry.register(Box::new(chunks_served_from_memory.clone()))?; + registry.register(Box::new(chunks_generation_triggered.clone()))?; - let f = |registry: &Registry| { - registry.register(Box::new(chunks_request_dropped_clone))?; - registry.register(Box::new(chunks_served_from_memory_clone))?; - registry.register(Box::new(chunks_generation_triggered_clone))?; - Ok(()) - }; - - Ok(( - Self { - chunks_request_dropped, - chunks_served_from_memory, - chunks_generation_triggered, - }, - Box::new(f), - )) + Ok(Self { + chunks_request_dropped, + chunks_served_from_memory, + chunks_generation_triggered, + }) } } impl ChunkGenMetrics { - pub fn new() -> Result<(Self, RegistryFn), prometheus::Error> { + pub fn new(registry: &Registry) -> Result { let chunks_requested = IntCounter::with_opts(Opts::new( "chunks_requested", "number of all chunks requested on the server", @@ -258,30 +214,20 @@ impl ChunkGenMetrics { "number of all canceled chunks on the server", ))?; - let chunks_requested_clone = chunks_requested.clone(); - let chunks_served_clone = chunks_served.clone(); - let chunks_canceled_clone = chunks_canceled.clone(); + registry.register(Box::new(chunks_requested.clone()))?; + registry.register(Box::new(chunks_served.clone()))?; + registry.register(Box::new(chunks_canceled.clone()))?; - let f = |registry: &Registry| { - registry.register(Box::new(chunks_requested_clone))?; - registry.register(Box::new(chunks_served_clone))?; - registry.register(Box::new(chunks_canceled_clone))?; - Ok(()) - }; - - Ok(( - Self { - chunks_requested, - chunks_served, - chunks_canceled, - }, - Box::new(f), - )) + Ok(Self { + chunks_requested, + chunks_served, + chunks_canceled, + }) } } impl TickMetrics { - pub fn new() -> Result<(Self, RegistryFn), Box> { + pub fn new(registry: &Registry) -> Result> { let chonks_count = IntGauge::with_opts(Opts::new( "chonks_count", "number of all chonks currently active on the server", @@ -322,42 +268,26 @@ impl TickMetrics { .expect("Time went backwards"); start_time.set(since_the_epoch.as_secs().try_into()?); - let chonks_count_clone = chonks_count.clone(); - let chunks_count_clone = chunks_count.clone(); - let chunk_groups_count_clone = chunk_groups_count.clone(); - let entity_count_clone = entity_count.clone(); - let build_info_clone = build_info.clone(); - let start_time_clone = start_time.clone(); - let time_of_day_clone = time_of_day.clone(); - let light_count_clone = light_count.clone(); - let tick_time_clone = tick_time.clone(); + registry.register(Box::new(chonks_count.clone()))?; + registry.register(Box::new(chunks_count.clone()))?; + registry.register(Box::new(chunk_groups_count.clone()))?; + registry.register(Box::new(entity_count.clone()))?; + registry.register(Box::new(build_info.clone()))?; + registry.register(Box::new(start_time.clone()))?; + registry.register(Box::new(time_of_day.clone()))?; + registry.register(Box::new(light_count.clone()))?; + registry.register(Box::new(tick_time.clone()))?; - let f = |registry: &Registry| { - registry.register(Box::new(chonks_count_clone))?; - registry.register(Box::new(chunks_count_clone))?; - registry.register(Box::new(chunk_groups_count_clone))?; - registry.register(Box::new(entity_count_clone))?; - registry.register(Box::new(build_info_clone))?; - registry.register(Box::new(start_time_clone))?; - registry.register(Box::new(time_of_day_clone))?; - registry.register(Box::new(light_count_clone))?; - registry.register(Box::new(tick_time_clone))?; - Ok(()) - }; - - Ok(( - Self { - chonks_count, - chunks_count, - chunk_groups_count, - entity_count, - tick_time, - build_info, - start_time, - time_of_day, - light_count, - }, - Box::new(f), - )) + Ok(Self { + chonks_count, + chunks_count, + chunk_groups_count, + entity_count, + tick_time, + build_info, + start_time, + time_of_day, + light_count, + }) } }