forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Authority] Track size of compiled module cache (MystenLabs#6039)
New Prometheus collector outputting a gauge metric to track the number of modules in Move's `SyncModuleCache`.
- Loading branch information
Showing
3 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use std::sync::{Arc, Weak}; | ||
|
||
use move_bytecode_utils::module_cache::SyncModuleCache; | ||
use move_core_types::resolver::ModuleResolver; | ||
use prometheus::{ | ||
core::{Collector, Desc}, | ||
labels, | ||
proto::{Gauge, Metric, MetricFamily, MetricType}, | ||
}; | ||
|
||
/// Holds the module cache to collect its size and pass it to Prometheus, for monitoring in Grafana. | ||
pub struct ModuleCacheGauge<R: ModuleResolver> { | ||
desc: Desc, | ||
module_cache: Weak<SyncModuleCache<R>>, | ||
} | ||
|
||
impl<R: ModuleResolver> ModuleCacheGauge<R> { | ||
pub fn new(module_cache: &Arc<SyncModuleCache<R>>) -> Self { | ||
Self { | ||
desc: Desc::new( | ||
"module_cache_size".into(), | ||
"Number of compiled move modules in the authority's cache.".into(), | ||
/* variable_labels */ vec![], | ||
/* const_labels */ labels! {}, | ||
) | ||
.unwrap(), | ||
module_cache: Arc::downgrade(module_cache), | ||
} | ||
} | ||
|
||
fn metric(&self) -> Option<Metric> { | ||
let cache = self.module_cache.upgrade()?; | ||
let mut m = Metric::default(); | ||
let mut gauge = Gauge::default(); | ||
// NB. lossy conversion from usize to f64, to match prometheus' API. | ||
gauge.set_value(cache.len() as f64); | ||
m.set_gauge(gauge); | ||
Some(m) | ||
} | ||
} | ||
|
||
impl<R: ModuleResolver + Send + Sync> Collector for ModuleCacheGauge<R> { | ||
fn desc(&self) -> Vec<&Desc> { | ||
vec![&self.desc] | ||
} | ||
|
||
fn collect(&self) -> Vec<MetricFamily> { | ||
let mut m = MetricFamily::default(); | ||
|
||
m.set_name(self.desc.fq_name.clone()); | ||
m.set_help(self.desc.help.clone()); | ||
m.set_field_type(MetricType::GAUGE); | ||
|
||
if let Some(metric) = self.metric() { | ||
m.mut_metric().push(metric); | ||
} | ||
|
||
vec![m] | ||
} | ||
} |