|
|
@@ -5,7 +5,6 @@ use {
|
|
|
BlockStatus,
|
|
|
EntropyReader,
|
|
|
},
|
|
|
- metrics::Metrics,
|
|
|
state::HashChainState,
|
|
|
},
|
|
|
anyhow::Result,
|
|
|
@@ -20,10 +19,19 @@ use {
|
|
|
Router,
|
|
|
},
|
|
|
ethers::core::types::Address,
|
|
|
+ prometheus_client::{
|
|
|
+ encoding::EncodeLabelSet,
|
|
|
+ metrics::{
|
|
|
+ counter::Counter,
|
|
|
+ family::Family,
|
|
|
+ },
|
|
|
+ registry::Registry,
|
|
|
+ },
|
|
|
std::{
|
|
|
collections::HashMap,
|
|
|
sync::Arc,
|
|
|
},
|
|
|
+ tokio::sync::RwLock,
|
|
|
url::Url,
|
|
|
};
|
|
|
pub use {
|
|
|
@@ -44,20 +52,44 @@ mod revelation;
|
|
|
|
|
|
pub type ChainId = String;
|
|
|
|
|
|
+#[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet)]
|
|
|
+pub struct RequestLabel {
|
|
|
+ pub value: String,
|
|
|
+}
|
|
|
+
|
|
|
+pub struct ApiMetrics {
|
|
|
+ pub metrics_registry: Arc<RwLock<Registry>>,
|
|
|
+ pub http_requests: Family<RequestLabel, Counter>,
|
|
|
+}
|
|
|
+
|
|
|
#[derive(Clone)]
|
|
|
pub struct ApiState {
|
|
|
pub chains: Arc<HashMap<ChainId, BlockchainState>>,
|
|
|
|
|
|
/// Prometheus metrics
|
|
|
- pub metrics: Arc<Metrics>,
|
|
|
+ pub metrics: Arc<ApiMetrics>,
|
|
|
}
|
|
|
|
|
|
impl ApiState {
|
|
|
- pub fn new(chains: &[(ChainId, BlockchainState)]) -> ApiState {
|
|
|
- let map: HashMap<ChainId, BlockchainState> = chains.into_iter().cloned().collect();
|
|
|
+ pub async fn new(
|
|
|
+ chains: HashMap<ChainId, BlockchainState>,
|
|
|
+ metrics_registry: Arc<RwLock<Registry>>,
|
|
|
+ ) -> ApiState {
|
|
|
+ let metrics = ApiMetrics {
|
|
|
+ http_requests: Family::default(),
|
|
|
+ metrics_registry,
|
|
|
+ };
|
|
|
+
|
|
|
+ let http_requests = metrics.http_requests.clone();
|
|
|
+ metrics.metrics_registry.write().await.register(
|
|
|
+ "http_requests",
|
|
|
+ "Number of HTTP requests received",
|
|
|
+ http_requests,
|
|
|
+ );
|
|
|
+
|
|
|
ApiState {
|
|
|
- chains: Arc::new(map),
|
|
|
- metrics: Arc::new(Metrics::new()),
|
|
|
+ chains: Arc::new(chains),
|
|
|
+ metrics: Arc::new(metrics),
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -185,7 +217,12 @@ mod test {
|
|
|
},
|
|
|
ethers::prelude::Address,
|
|
|
lazy_static::lazy_static,
|
|
|
- std::sync::Arc,
|
|
|
+ prometheus_client::registry::Registry,
|
|
|
+ std::{
|
|
|
+ collections::HashMap,
|
|
|
+ sync::Arc,
|
|
|
+ },
|
|
|
+ tokio::sync::RwLock,
|
|
|
};
|
|
|
|
|
|
const PROVIDER: Address = Address::zero();
|
|
|
@@ -203,7 +240,7 @@ mod test {
|
|
|
));
|
|
|
}
|
|
|
|
|
|
- fn test_server() -> (TestServer, Arc<MockEntropyReader>, Arc<MockEntropyReader>) {
|
|
|
+ async fn test_server() -> (TestServer, Arc<MockEntropyReader>, Arc<MockEntropyReader>) {
|
|
|
let eth_read = Arc::new(MockEntropyReader::with_requests(10, &[]));
|
|
|
|
|
|
let eth_state = BlockchainState {
|
|
|
@@ -215,6 +252,8 @@ mod test {
|
|
|
confirmed_block_status: BlockStatus::Latest,
|
|
|
};
|
|
|
|
|
|
+ let metrics_registry = Arc::new(RwLock::new(Registry::default()));
|
|
|
+
|
|
|
let avax_read = Arc::new(MockEntropyReader::with_requests(10, &[]));
|
|
|
|
|
|
let avax_state = BlockchainState {
|
|
|
@@ -226,10 +265,11 @@ mod test {
|
|
|
confirmed_block_status: BlockStatus::Latest,
|
|
|
};
|
|
|
|
|
|
- let api_state = ApiState::new(&[
|
|
|
- ("ethereum".into(), eth_state),
|
|
|
- ("avalanche".into(), avax_state),
|
|
|
- ]);
|
|
|
+ let mut chains = HashMap::new();
|
|
|
+ chains.insert("ethereum".into(), eth_state);
|
|
|
+ chains.insert("avalanche".into(), avax_state);
|
|
|
+
|
|
|
+ let api_state = ApiState::new(chains, metrics_registry).await;
|
|
|
|
|
|
let app = api::routes(api_state);
|
|
|
(TestServer::new(app).unwrap(), eth_read, avax_read)
|
|
|
@@ -247,7 +287,7 @@ mod test {
|
|
|
|
|
|
#[tokio::test]
|
|
|
async fn test_revelation() {
|
|
|
- let (server, eth_contract, avax_contract) = test_server();
|
|
|
+ let (server, eth_contract, avax_contract) = test_server().await;
|
|
|
|
|
|
// Can't access a revelation if it hasn't been requested
|
|
|
get_and_assert_status(
|
|
|
@@ -376,7 +416,7 @@ mod test {
|
|
|
|
|
|
#[tokio::test]
|
|
|
async fn test_revelation_confirmation_delay() {
|
|
|
- let (server, eth_contract, avax_contract) = test_server();
|
|
|
+ let (server, eth_contract, avax_contract) = test_server().await;
|
|
|
|
|
|
eth_contract.insert(PROVIDER, 0, 10, false);
|
|
|
eth_contract.insert(PROVIDER, 1, 11, false);
|