Skip to content

Commit

Permalink
feat: add default metric prefix and metric description (#44)
Browse files Browse the repository at this point in the history
* feat: add default metric prefix

* add metric description

* err message

---------

Co-authored-by: Revolution1 <[email protected]>
  • Loading branch information
koushiro and Revolution1 authored Jul 12, 2024
1 parent 6dbf79a commit b12b2c8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
4 changes: 2 additions & 2 deletions configs/demo_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ extensions:
stale_timeout_seconds: 180 # rotate endpoint if no new blocks for 3 minutes
finalized_interval_seconds: 10 # get finalized head every 10 seconds
telemetry:
provider: otlp
#provider: otlp
#agent_endpoint: http://127.0.0.1:4317
prometheus:
port: 9616
listen_address: "0.0.0.0"
prefix: "subway"
#prefix: "subway"
cache:
default_ttl_seconds: 60
default_size: 500
Expand Down
15 changes: 7 additions & 8 deletions src/extensions/prometheus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,15 @@ impl Drop for Prometheus {
pub struct PrometheusConfig {
pub port: u16,
pub listen_address: String,
pub prefix: Option<String>,
#[serde(default = "default_prefix")]
pub prefix: String,
pub chain_label: Option<String>,
}

fn default_prefix() -> String {
"subway".to_string()
}

#[async_trait]
impl Extension for Prometheus {
type Config = PrometheusConfig;
Expand All @@ -59,13 +64,7 @@ impl Prometheus {
.clone()
.map(|l| iter::once(("chain".to_string(), l.clone())).collect());

// make sure the prefix is not an Option of Some empty string
let prefix = match config.prefix {
Some(p) if p.is_empty() => Some("subway".to_string()),
p => p,
};
let registry = Registry::new_custom(prefix, labels)
.expect("It can't fail, we make sure the `prefix` is either `None` or `Some` of non-empty string");
let registry = Registry::new_custom(Some(config.prefix), labels).expect("prefix can not be empty string");

// add subway info metric
let info_gauge = Gauge::<U64>::with_opts(
Expand Down
35 changes: 26 additions & 9 deletions src/extensions/prometheus/rpc_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,33 @@ pub struct InnerMetrics {

impl InnerMetrics {
fn new(registry: &Registry) -> Self {
let open_counter = Counter::new("open_ws_counter", "No help").unwrap();
let closed_counter = Counter::new("closed_ws_counter", "No help").unwrap();
let cache_miss_counter = CounterVec::new(Opts::new("cache_miss_counter", "No help"), &["method"]).unwrap();
let cache_query_counter = CounterVec::new(Opts::new("cache_query_counter", "No help"), &["method"]).unwrap();
let call_times =
HistogramVec::new(HistogramOpts::new("rpc_calls_time", "No help"), &["protocol", "method"]).unwrap();
let calls_started_counter =
CounterVec::new(Opts::new("rpc_calls_started", "No help"), &["protocol", "method"]).unwrap();
let open_counter = Counter::new("open_ws_counter", "Total number of opened websocket connections").unwrap();
let closed_counter = Counter::new("closed_ws_counter", "Total number of closed websocket connections").unwrap();
let cache_miss_counter = CounterVec::new(
Opts::new("cache_miss_counter", "Total number of cache misses of RPC requests"),
&["method"],
)
.unwrap();
let cache_query_counter = CounterVec::new(
Opts::new("cache_query_counter", "Total number of cache queries of RPC requests"),
&["method"],
)
.unwrap();
let call_times = HistogramVec::new(
HistogramOpts::new(
"rpc_calls_time",
"Time interval from the initiation to the completion of an RPC request",
),
&["protocol", "method"],
)
.unwrap();
let calls_started_counter = CounterVec::new(
Opts::new("rpc_calls_started", "Total number of initiated RPC requests"),
&["protocol", "method"],
)
.unwrap();
let calls_finished_counter = CounterVec::new(
Opts::new("rpc_calls_finished", "No help"),
Opts::new("rpc_calls_finished", "Total number of completed RPC requests"),
&["protocol", "method", "is_error"],
)
.unwrap();
Expand Down

0 comments on commit b12b2c8

Please sign in to comment.