partner_chains_db_sync_data_sources/
metrics.rs

1//! Substrate Prometheus metrics client for Db-Sync-based Partner Chain data sources
2use log::warn;
3use substrate_prometheus_endpoint::{
4	CounterVec, HistogramOpts, HistogramVec, Opts, PrometheusError, Registry, U64, register,
5};
6
7/// Substrate Prometheus metrics client used by Partner Chain data sources
8#[derive(Clone)]
9pub struct McFollowerMetrics {
10	time_elapsed: HistogramVec,
11	call_count: CounterVec<U64>,
12}
13
14impl McFollowerMetrics {
15	pub(crate) fn time_elapsed(&self) -> &HistogramVec {
16		&self.time_elapsed
17	}
18	pub(crate) fn call_count(&self) -> &CounterVec<U64> {
19		&self.call_count
20	}
21	pub(crate) fn register(registry: &Registry) -> Result<Self, PrometheusError> {
22		Ok(Self {
23			time_elapsed: register(
24				HistogramVec::new(
25					HistogramOpts::new(
26						"partner_chains_data_source_method_time_elapsed",
27						"Time spent in a method call",
28					),
29					&["method_name"],
30				)?,
31				registry,
32			)?,
33			call_count: register(
34				CounterVec::new(
35					Opts::new(
36						"partner_chains_data_source_method_call_count",
37						"Total number of data source method calls",
38					),
39					&["method_name"],
40				)?,
41				registry,
42			)?,
43		})
44	}
45}
46
47/// Registers new metrics with Substrate Prometheus metrics service and returns a client instance
48pub fn register_metrics_warn_errors(
49	metrics_registry_opt: Option<&Registry>,
50) -> Option<McFollowerMetrics> {
51	metrics_registry_opt.and_then(|registry| match McFollowerMetrics::register(registry) {
52		Ok(metrics) => Some(metrics),
53		Err(err) => {
54			warn!("Failed registering data source metrics with err: {}", err);
55			None
56		},
57	})
58}
59
60/// Logs each method invocation and each returned result.
61/// Has to be made at the level of trait, because otherwise #[async_trait] is expanded first.
62/// '&self' matching yields "__self" identifier not found error, so "&$self:tt" is required.
63/// Works only if return type is Result.
64#[macro_export]
65macro_rules! observed_async_trait {
66	(impl $trait_name:ident for $target_type:ty {
67		$(type $type_name:ident = $type:ty;)*
68		$(async fn $method:ident(&$self:tt $(,$param_name:ident: $param_type:ty)* $(,)?) -> $res:ty $body:block)*
69	})=> {
70		#[async_trait::async_trait]
71		impl $trait_name for $target_type {
72		$(type $type_name = $type;)*
73		$(
74			async fn $method(&$self $(,$param_name: $param_type)*,) -> $res {
75				let method_name = stringify!($method);
76				let _timer = if let Some(metrics) = &$self.metrics_opt {
77					metrics.call_count().with_label_values(&[method_name]).inc();
78					Some(metrics.time_elapsed().with_label_values(&[method_name]).start_timer())
79				} else { None };
80				let params: Vec<String> = vec![$(format!("{:?}", $param_name.clone()),)*];
81				log::debug!("{} called with parameters: {:?}", method_name, params);
82				let result = $body;
83				match &result {
84					Ok(value) => {
85						log::debug!("{} returns {:?}", method_name, value);
86					},
87					Err(error) => {
88						log::error!("{} failed with {:?}", method_name, error);
89					},
90				};
91				result
92			}
93		)*
94		}
95	};
96}
97
98#[cfg(test)]
99pub(crate) mod mock {
100	use crate::metrics::McFollowerMetrics;
101	use substrate_prometheus_endpoint::{CounterVec, HistogramOpts, HistogramVec, Opts};
102
103	pub(crate) fn test_metrics() -> McFollowerMetrics {
104		McFollowerMetrics {
105			time_elapsed: HistogramVec::new(HistogramOpts::new("test", "test"), &["method_name"])
106				.unwrap(),
107			call_count: CounterVec::new(Opts::new("test", "test"), &["method_name"]).unwrap(),
108		}
109	}
110}
111
112#[cfg(test)]
113mod tests {
114	use crate::metrics::{McFollowerMetrics, mock::test_metrics};
115	use async_trait::async_trait;
116	use std::convert::Infallible;
117	use substrate_prometheus_endpoint::prometheus::core::Metric;
118
119	struct MetricsMacroTestStruct {
120		metrics_opt: Option<McFollowerMetrics>,
121	}
122
123	#[async_trait]
124	trait MetricMacroTestTrait {
125		async fn test_method_one(&self) -> Result<(), Infallible>;
126		async fn test_method_two(&self) -> Result<(), Infallible>;
127	}
128
129	observed_async_trait!(
130	impl MetricMacroTestTrait for MetricsMacroTestStruct {
131		async fn test_method_one(&self) -> Result<(), Infallible> {
132			tokio::time::sleep(core::time::Duration::from_millis(10)).await;
133			Ok(())
134		}
135
136		async fn test_method_two(&self) -> Result<(), Infallible> {
137			Ok(())
138		}
139	});
140
141	#[tokio::test]
142	async fn calculate_metrics_correctly() {
143		let metrics = test_metrics();
144		let histogram_method_one = metrics.time_elapsed().with_label_values(&["test_method_one"]);
145		let histogram_method_two = metrics.time_elapsed().with_label_values(&["test_method_two"]);
146		let histogram_method_random = metrics.time_elapsed().with_label_values(&["random"]);
147		let counter_method_one = metrics.call_count().with_label_values(&["test_method_one"]);
148		let counter_method_two = metrics.call_count().with_label_values(&["test_method_two"]);
149		let counter_method_random = metrics.call_count().with_label_values(&["random"]);
150
151		let metrics_struct = MetricsMacroTestStruct { metrics_opt: Some(metrics.clone()) };
152		metrics_struct.test_method_one().await.unwrap();
153		metrics_struct.test_method_two().await.unwrap();
154
155		for bucket in histogram_method_one.metric().get_histogram().get_bucket().iter().take(2) {
156			assert_eq!(bucket.get_cumulative_count(), 0);
157		}
158
159		// Assert below has a tiny potential to be flaky - if it is, please increase sleep time in MetricMacroTestTrait implementation or
160		// remove the Assert completely
161		assert!(histogram_method_one.get_sample_sum() > histogram_method_two.get_sample_sum());
162		assert_eq!(histogram_method_one.get_sample_count(), 1);
163		assert_eq!(histogram_method_two.get_sample_count(), 1);
164		assert_eq!(histogram_method_random.get_sample_count(), 0);
165
166		assert_eq!(counter_method_one.get(), 1);
167		assert_eq!(counter_method_two.get(), 1);
168		assert_eq!(counter_method_random.get(), 0);
169	}
170}