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 Oct 30, 2023
1 parent 515c9e8 commit 9540163
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
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 9540163

Please sign in to comment.