diff --git a/Cargo.lock b/Cargo.lock index d7ad2e12..e531b44c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3128,6 +3128,7 @@ dependencies = [ "lazy_static", "log", "lru", + "prometheus", "regex", "reqwest", "secp256k1", diff --git a/common/Cargo.toml b/common/Cargo.toml index ff4e4e82..29aa886f 100644 --- a/common/Cargo.toml +++ b/common/Cargo.toml @@ -44,6 +44,7 @@ autometrics = { version = "0.6.0", features = ["prometheus-exporter"] } tracing = "0.1.40" tower = "0.4.13" tower_governor = "0.1.0" +prometheus = "0.13.3" [dev-dependencies] env_logger = "0.9.0" diff --git a/common/src/indexer_service/http/indexer_service.rs b/common/src/indexer_service/http/indexer_service.rs index b2d40c34..8c62db09 100644 --- a/common/src/indexer_service/http/indexer_service.rs +++ b/common/src/indexer_service/http/indexer_service.rs @@ -27,6 +27,7 @@ use tower_governor::{errors::display_error, governor::GovernorConfigBuilder, Gov use tracing::info; use crate::{ + indexer_service::http::metrics::IndexerServiceMetrics, prelude::{ attestation_signers, dispute_manager, escrow_accounts, indexer_allocations, AttestationSigner, DeploymentDetails, SubgraphClient, @@ -152,6 +153,7 @@ where pub config: IndexerServiceConfig, pub release: IndexerServiceRelease, pub url_namespace: &'static str, + pub metrics_prefix: &'static str, pub extra_routes: Router>, Body>, } @@ -163,6 +165,7 @@ where pub attestation_signers: Eventual>, pub tap_manager: TapManager, pub service_impl: Arc, + pub metrics: IndexerServiceMetrics, } pub struct IndexerService {} @@ -172,6 +175,8 @@ impl IndexerService { where I: IndexerServiceImpl + Sync + Send + 'static, { + let metrics = IndexerServiceMetrics::new(options.metrics_prefix); + let network_subgraph = Box::leak(Box::new(SubgraphClient::new( options .config @@ -258,6 +263,7 @@ impl IndexerService { attestation_signers, tap_manager, service_impl: Arc::new(options.service_impl), + metrics, }); // Rate limits by allowing bursts of 10 requests and requiring 100ms of diff --git a/common/src/indexer_service/http/metrics.rs b/common/src/indexer_service/http/metrics.rs new file mode 100644 index 00000000..6fe2057c --- /dev/null +++ b/common/src/indexer_service/http/metrics.rs @@ -0,0 +1,34 @@ +use prometheus::{register_int_counter_vec, IntCounterVec}; + +pub struct IndexerServiceMetrics { + pub requests: IntCounterVec, + pub successful_requests: IntCounterVec, + pub failed_requests: IntCounterVec, +} + +impl IndexerServiceMetrics { + pub fn new(prefix: &str) -> Self { + IndexerServiceMetrics { + requests: register_int_counter_vec!( + format!("{prefix}_service_requests_total"), + "Incoming requests", + &["manifest"] + ) + .unwrap(), + + successful_requests: register_int_counter_vec!( + format!("{prefix}_service_requests_ok"), + "Successfully executed requests", + &["manifest"] + ) + .unwrap(), + + failed_requests: register_int_counter_vec!( + format!("{prefix}_service_requests_failed"), + "requests that failed to execute", + &["manifest"] + ) + .unwrap(), + } + } +} diff --git a/common/src/indexer_service/http/mod.rs b/common/src/indexer_service/http/mod.rs index bf7b580f..6e7916ef 100644 --- a/common/src/indexer_service/http/mod.rs +++ b/common/src/indexer_service/http/mod.rs @@ -1,5 +1,6 @@ mod config; mod indexer_service; +mod metrics; mod request_handler; mod scalar_receipt_header; diff --git a/common/src/indexer_service/http/request_handler.rs b/common/src/indexer_service/http/request_handler.rs index 80d627e9..2cf027d1 100644 --- a/common/src/indexer_service/http/request_handler.rs +++ b/common/src/indexer_service/http/request_handler.rs @@ -19,6 +19,7 @@ use super::{ IndexerServiceImpl, }; +#[autometrics::autometrics] pub async fn request_handler( Path(manifest_id): Path, TypedHeader(receipt): TypedHeader, @@ -31,6 +32,12 @@ where { info!("Handling request for deployment `{manifest_id}`"); + state + .metrics + .requests + .with_label_values(&[&manifest_id.to_string()]) + .inc(); + let request = serde_json::from_slice(&body).map_err(|e| IndexerServiceError::InvalidRequest(e.into()))?;