Skip to content

Commit

Permalink
feat: add request handling metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Jannis committed Nov 13, 2023
1 parent f0cf841 commit 69fbb17
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
1 change: 0 additions & 1 deletion common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ tokio = { version = "1.32.0", features = ["full", "macros", "rt"] }
toolshed = { git = "https://github.com/edgeandnode/toolshed", branch = "main", features = [
"graphql",
] }
tracing = "0.1.34"
graphql = { git = "https://github.com/edgeandnode/toolshed", branch = "main" }
tap_core = "0.6.0"
axum = { version = "0.6.20", default_features = true, features = ["headers"] }
Expand Down
6 changes: 6 additions & 0 deletions common/src/indexer_service/http/indexer_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<Arc<IndexerServiceState<I>>, Body>,
}

Expand All @@ -163,6 +165,7 @@ where
pub attestation_signers: Eventual<HashMap<Address, AttestationSigner>>,
pub tap_manager: TapManager,
pub service_impl: Arc<I>,
pub metrics: IndexerServiceMetrics,
}

pub struct IndexerService {}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions common/src/indexer_service/http/metrics.rs
Original file line number Diff line number Diff line change
@@ -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(),
}
}
}
1 change: 1 addition & 0 deletions common/src/indexer_service/http/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod config;
mod indexer_service;
mod metrics;
mod request_handler;
mod scalar_receipt_header;

Expand Down
7 changes: 7 additions & 0 deletions common/src/indexer_service/http/request_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use super::{
IndexerServiceImpl,
};

#[autometrics::autometrics]
pub async fn request_handler<I>(
Path(manifest_id): Path<DeploymentId>,
TypedHeader(receipt): TypedHeader<ScalarReceipt>,
Expand All @@ -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()))?;

Expand Down

0 comments on commit 69fbb17

Please sign in to comment.