Skip to content

Commit 4f84ad9

Browse files
authored
Add a metric for the number of module instances (#4674)
# Description of Changes It looks like we could be creating a lot of instances, so it would be nice to have a metric to track it. # Expected complexity level and risk 1
1 parent 7454329 commit 4f84ad9

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

crates/core/src/host/module_host.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,7 @@ impl CallProcedureParams {
752752
struct ModuleInstanceManager<M: GenericModule> {
753753
instances: Mutex<VecDeque<M::Instance>>,
754754
module: M,
755+
module_instances_metric: ModuleInstancesMetric,
755756
create_instance_time_metric: CreateInstanceTimeMetric,
756757
}
757758

@@ -763,6 +764,15 @@ struct CreateInstanceTimeMetric {
763764
database_identity: Identity,
764765
}
765766

767+
/// Handle on the `spacetime_module_instances` label for a particular database
768+
/// which calls `remove_label_values` to clean up on drop.
769+
struct ModuleInstancesMetric {
770+
metric: IntGauge,
771+
host_type: HostType,
772+
database_identity: Identity,
773+
count: std::sync::Mutex<i64>,
774+
}
775+
766776
impl Drop for CreateInstanceTimeMetric {
767777
fn drop(&mut self) {
768778
let _ = WORKER_METRICS
@@ -771,6 +781,31 @@ impl Drop for CreateInstanceTimeMetric {
771781
}
772782
}
773783

784+
impl Drop for ModuleInstancesMetric {
785+
fn drop(&mut self) {
786+
let _ = WORKER_METRICS
787+
.module_instances
788+
.remove_label_values(&self.database_identity, &self.host_type);
789+
}
790+
}
791+
792+
impl ModuleInstancesMetric {
793+
fn inc(&self) {
794+
let mut count = self.count.lock().unwrap();
795+
*count += 1;
796+
self.metric.set(*count);
797+
}
798+
799+
fn dec(&self) {
800+
let mut count = self.count.lock().unwrap();
801+
if *count == 0 {
802+
return;
803+
}
804+
*count -= 1;
805+
self.metric.set(*count);
806+
}
807+
}
808+
774809
impl CreateInstanceTimeMetric {
775810
fn observe(&self, duration: std::time::Duration) {
776811
self.metric.observe(duration.as_secs_f64());
@@ -780,6 +815,15 @@ impl CreateInstanceTimeMetric {
780815
impl<M: GenericModule> ModuleInstanceManager<M> {
781816
fn new(module: M, init_inst: M::Instance, database_identity: Identity) -> Self {
782817
let host_type = module.host_type();
818+
let module_instances_metric = ModuleInstancesMetric {
819+
metric: WORKER_METRICS
820+
.module_instances
821+
.with_label_values(&database_identity, &host_type),
822+
host_type,
823+
database_identity,
824+
count: std::sync::Mutex::new(1),
825+
};
826+
783827
let create_instance_time_metric = CreateInstanceTimeMetric {
784828
metric: WORKER_METRICS
785829
.module_create_instance_time_seconds
@@ -795,6 +839,7 @@ impl<M: GenericModule> ModuleInstanceManager<M> {
795839
Self {
796840
instances: Mutex::new(instances),
797841
module,
842+
module_instances_metric,
798843
create_instance_time_metric,
799844
}
800845
}
@@ -815,6 +860,7 @@ impl<M: GenericModule> ModuleInstanceManager<M> {
815860
let res = self.module.create_instance().await;
816861
let elapsed_time = start_time.elapsed();
817862
self.create_instance_time_metric.observe(elapsed_time);
863+
self.module_instances_metric.inc();
818864
res
819865
}
820866
}
@@ -824,6 +870,7 @@ impl<M: GenericModule> ModuleInstanceManager<M> {
824870
// Don't return trapped instances;
825871
// they may have left internal data structures in the guest `Instance`
826872
// (WASM linear memory, V8 global scope) in a bad state.
873+
self.module_instances_metric.dec();
827874
return;
828875
}
829876

crates/core/src/worker_metrics/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,11 @@ metrics_group!(
254254
#[buckets(0, 1, 2, 5, 10, 25, 50, 75, 100, 200, 300, 400, 500, 1000)]
255255
pub instance_queue_length_histogram: HistogramVec,
256256

257+
#[name = spacetime_module_instances]
258+
#[help = "Current number of live module instances (WASM or V8) for this database"]
259+
#[labels(database_identity: Identity, module_type: HostType)]
260+
pub module_instances: IntGaugeVec,
261+
257262
#[name = spacetime_reducer_wait_time_sec]
258263
#[help = "The amount of time (in seconds) a reducer spends in the queue waiting to run"]
259264
#[labels(db: Identity, reducer: str)]

0 commit comments

Comments
 (0)