switch server to the new Registry struct, this gets rid of RegistryFn pattern and simplyfies stuff, its a result of the Prometheus-tracy crate

This commit is contained in:
Marcel Märtens 2021-03-08 09:49:16 +01:00
parent 6b88545481
commit ea94ca6656
2 changed files with 75 additions and 155 deletions

View File

@ -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(&registry).unwrap();
let network_request_metrics = metrics::NetworkRequestMetrics::new(&registry).unwrap();
let player_metrics = metrics::PlayerMetrics::new(&registry).unwrap();
let ecs_system_metrics = EcsSystemMetrics::new(&registry).unwrap();
let tick_metrics = TickMetrics::new(&registry).unwrap();
let physics_metrics = PhysicsMetrics::new(&registry).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(&registry).expect("failed to register chunk gen metrics");
registry_network(&registry).expect("failed to register network request metrics");
registry_player(&registry).expect("failed to register player metrics");
registry_tick(&registry).expect("failed to register tick metrics");
registry_state(&registry).expect("failed to register state metrics");
registry_physics(&registry).expect("failed to register state metrics");
let network = Network::new_with_registry(Pid::new(), &runtime, &registry);
let metrics_shutdown = Arc::new(Notify::new());
let metrics_shutdown_clone = Arc::clone(&metrics_shutdown);

View File

@ -8,8 +8,6 @@ use std::{
time::{Duration, SystemTime, UNIX_EPOCH},
};
type RegistryFn = Box<dyn FnOnce(&Registry) -> 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<Self, prometheus::Error> {
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<Self, prometheus::Error> {
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<Self, prometheus::Error> {
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<Self, prometheus::Error> {
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<Self, prometheus::Error> {
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<dyn Error>> {
pub fn new(registry: &Registry) -> Result<Self, Box<dyn Error>> {
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,
})
}
}