Skip to content

Commit 4575c39

Browse files
committed
chain/ethereum, core: Better fix for deployment_eth_rpc_* metrics
1 parent c043b7a commit 4575c39

File tree

5 files changed

+58
-24
lines changed

5 files changed

+58
-24
lines changed

chain/ethereum/src/adapter.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -769,42 +769,44 @@ impl ProviderEthRpcMetrics {
769769

770770
#[derive(Clone)]
771771
pub struct SubgraphEthRpcMetrics {
772-
request_duration: Box<GaugeVec>,
773-
errors: Box<CounterVec>,
772+
request_duration: GaugeVec,
773+
errors: CounterVec,
774+
deployment: String,
774775
}
775776

776777
impl SubgraphEthRpcMetrics {
777778
pub fn new(registry: Arc<dyn MetricsRegistry>, subgraph_hash: &str) -> Self {
778779
let request_duration = registry
779-
.new_deployment_gauge_vec(
780+
.global_gauge_vec(
780781
"deployment_eth_rpc_request_duration",
781782
"Measures eth rpc request duration for a subgraph deployment",
782-
&subgraph_hash,
783-
vec![String::from("method"), String::from("provider")],
783+
vec!["deployment", "method", "provider"].as_slice(),
784784
)
785785
.unwrap();
786786
let errors = registry
787-
.new_deployment_counter_vec(
787+
.global_counter_vec(
788788
"deployment_eth_rpc_errors",
789789
"Counts eth rpc request errors for a subgraph deployment",
790-
&subgraph_hash,
791-
vec![String::from("method"), String::from("provider")],
790+
vec!["deployment", "method", "provider"].as_slice(),
792791
)
793792
.unwrap();
794793
Self {
795794
request_duration,
796795
errors,
796+
deployment: subgraph_hash.into(),
797797
}
798798
}
799799

800800
pub fn observe_request(&self, duration: f64, method: &str, provider: &str) {
801801
self.request_duration
802-
.with_label_values(&[method, provider])
802+
.with_label_values(&[&self.deployment, method, provider])
803803
.set(duration);
804804
}
805805

806806
pub fn add_error(&self, method: &str, provider: &str) {
807-
self.errors.with_label_values(&[method, provider]).inc();
807+
self.errors
808+
.with_label_values(&[&self.deployment, method, provider])
809+
.inc();
808810
}
809811
}
810812

chain/ethereum/src/chain.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ use graph::{
2323
},
2424
};
2525
use prost::Message;
26-
use std::collections::{HashMap, HashSet};
26+
use std::collections::HashSet;
2727
use std::iter::FromIterator;
28-
use std::sync::{Arc, Mutex};
28+
use std::sync::Arc;
2929

3030
use crate::data_source::DataSourceTemplate;
3131
use crate::data_source::UnresolvedDataSourceTemplate;
@@ -58,7 +58,6 @@ pub struct Chain {
5858
chain_head_update_listener: Arc<dyn ChainHeadUpdateListener>,
5959
reorg_threshold: BlockNumber,
6060
pub is_ingestible: bool,
61-
deployment_rpc_metrics: Arc<Mutex<HashMap<String, Arc<SubgraphEthRpcMetrics>>>>,
6261
}
6362

6463
impl std::fmt::Debug for Chain {
@@ -94,7 +93,6 @@ impl Chain {
9493
chain_head_update_listener,
9594
reorg_threshold,
9695
is_ingestible,
97-
deployment_rpc_metrics: Default::default(),
9896
}
9997
}
10098

@@ -159,15 +157,7 @@ impl Blockchain for Chain {
159157
self.eth_adapters.cheapest_with(capabilities)?.clone()
160158
};
161159

162-
let ethrpc_metrics = self
163-
.deployment_rpc_metrics
164-
.lock()
165-
.unwrap()
166-
.entry(String::from(loc.hash.as_str()))
167-
.or_insert_with(|| {
168-
Arc::new(SubgraphEthRpcMetrics::new(self.registry.clone(), &loc.hash))
169-
})
170-
.cheap_clone();
160+
let ethrpc_metrics = Arc::new(SubgraphEthRpcMetrics::new(self.registry.clone(), &loc.hash));
171161

172162
let adapter = TriggersAdapter {
173163
logger,

core/src/metrics/registry.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub struct MetricsRegistry {
1717
global_counters: Arc<RwLock<HashMap<u64, Counter>>>,
1818
global_counter_vecs: Arc<RwLock<HashMap<u64, CounterVec>>>,
1919
global_gauges: Arc<RwLock<HashMap<u64, Gauge>>>,
20+
global_gauge_vecs: Arc<RwLock<HashMap<u64, GaugeVec>>>,
2021
}
2122

2223
impl MetricsRegistry {
@@ -35,6 +36,7 @@ impl MetricsRegistry {
3536
global_counters: Arc::new(RwLock::new(HashMap::new())),
3637
global_counter_vecs: Arc::new(RwLock::new(HashMap::new())),
3738
global_gauges: Arc::new(RwLock::new(HashMap::new())),
39+
global_gauge_vecs: Arc::new(RwLock::new(HashMap::new())),
3840
}
3941
}
4042

@@ -193,6 +195,28 @@ impl MetricsRegistryTrait for MetricsRegistry {
193195
}
194196
}
195197

198+
fn global_gauge_vec(
199+
&self,
200+
name: &str,
201+
help: &str,
202+
variable_labels: &[&str],
203+
) -> Result<GaugeVec, PrometheusError> {
204+
let opts = Opts::new(name, help);
205+
let gauges = GaugeVec::new(opts, variable_labels)?;
206+
let id = gauges.desc().first().unwrap().id;
207+
let maybe_gauge = self.global_gauge_vecs.read().unwrap().get(&id).cloned();
208+
if let Some(gauges) = maybe_gauge {
209+
Ok(gauges)
210+
} else {
211+
self.register(name, Box::new(gauges.clone()));
212+
self.global_gauge_vecs
213+
.write()
214+
.unwrap()
215+
.insert(id, gauges.clone());
216+
Ok(gauges)
217+
}
218+
}
219+
196220
fn unregister(&self, metric: Box<dyn Collector>) {
197221
match self.registry.unregister(metric) {
198222
Ok(_) => {

graph/src/components/metrics/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ pub trait MetricsRegistry: Send + Sync + 'static {
7070
const_labels: HashMap<String, String>,
7171
) -> Result<Gauge, PrometheusError>;
7272

73+
fn global_gauge_vec(
74+
&self,
75+
name: &str,
76+
help: &str,
77+
variable_labels: &[&str],
78+
) -> Result<GaugeVec, PrometheusError>;
79+
7380
fn new_gauge(
7481
&self,
7582
name: &str,

mock/src/metrics_registry.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use graph::components::metrics::{Collector, Counter, Gauge, Opts, PrometheusError};
22
use graph::prelude::MetricsRegistry as MetricsRegistryTrait;
3-
use graph::prometheus::CounterVec;
3+
use graph::prometheus::{CounterVec, GaugeVec};
44

55
use std::collections::HashMap;
66

@@ -55,4 +55,15 @@ impl MetricsRegistryTrait for MockMetricsRegistry {
5555
let counters = CounterVec::new(opts, variable_labels)?;
5656
Ok(counters)
5757
}
58+
59+
fn global_gauge_vec(
60+
&self,
61+
name: &str,
62+
help: &str,
63+
variable_labels: &[&str],
64+
) -> Result<GaugeVec, PrometheusError> {
65+
let opts = Opts::new(name, help);
66+
let gauges = GaugeVec::new(opts, variable_labels)?;
67+
Ok(gauges)
68+
}
5869
}

0 commit comments

Comments
 (0)