Skip to content

Commit 955bf1c

Browse files
committed
docs: add documentation to middlewares
Signed-off-by: Gustavo Inacio <[email protected]>
1 parent 750b761 commit 955bf1c

File tree

8 files changed

+46
-13
lines changed

8 files changed

+46
-13
lines changed

crates/monitor/src/deployment_to_allocation.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use tokio::sync::watch::Receiver;
88
use indexer_allocation::Allocation;
99
use indexer_watcher::map_watcher;
1010

11-
/// An always up-to-date list of attestation signers, one for each of the indexer's allocations.
11+
/// Watcher of indexer allocation
12+
/// returning a map of subgraph deployment to allocation id
1213
pub fn deployment_to_allocation(
1314
indexer_allocations_rx: Receiver<HashMap<Address, Allocation>>,
1415
) -> Receiver<HashMap<DeploymentId, Address>> {

crates/service/src/metrics.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,30 @@ use lazy_static::lazy_static;
55
use prometheus::{register_counter_vec, register_histogram_vec, CounterVec, HistogramVec};
66

77
lazy_static! {
8-
/// Register indexer error metrics in Prometheus registry
8+
/// Metric registered in global registry for
9+
/// indexer query handler
10+
///
11+
/// Labels: "deployment", "allocation", "sender"
912
pub static ref HANDLER_HISTOGRAM: HistogramVec = register_histogram_vec!(
1013
"indexer_query_handler_seconds",
1114
"Histogram for default indexer query handler",
1215
&["deployment", "allocation", "sender"]
1316
).unwrap();
1417

18+
/// Metric registered in global registry for
19+
/// Failed queries to handler
20+
///
21+
/// Labels: "deployment"
1522
pub static ref HANDLER_FAILURE: CounterVec = register_counter_vec!(
1623
"indexer_query_handler_failed_total",
1724
"Failed queries to handler",
1825
&["deployment"]
1926
).unwrap();
2027

28+
/// Metric registered in global registry for
29+
/// Failed receipt checks
30+
///
31+
/// Labels: "deployment", "allocation", "sender"
2132
pub static ref FAILED_RECEIPT: CounterVec = register_counter_vec!(
2233
"indexer_receipt_failed_total",
2334
"Failed receipt checks",

crates/service/src/middleware/inject_allocation.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
//! injects allocation id in extensions
5-
//! - check if allocation id already exists
6-
//! - else, try to fetch allocation id from deployment_id and allocations watcher
7-
//! - execute query
8-
//!
9-
//! Needs signed receipt Extension to be added OR deployment id
10-
114
use std::collections::HashMap;
125

136
use alloy::primitives::Address;
@@ -20,6 +13,7 @@ use tap_core::receipt::SignedReceipt;
2013
use thegraph_core::DeploymentId;
2114
use tokio::sync::watch;
2215

16+
/// The current query Allocation Id address
2317
#[derive(Clone)]
2418
pub struct Allocation(pub Address);
2519

@@ -29,11 +23,18 @@ impl From<Allocation> for String {
2923
}
3024
}
3125

26+
/// State to be used by allocation middleware
3227
#[derive(Clone)]
3328
pub struct AllocationState {
29+
/// watcher that maps deployment ids to allocation ids
3430
pub deployment_to_allocation: watch::Receiver<HashMap<DeploymentId, Address>>,
3531
}
3632

33+
/// Injects allocation id in extensions
34+
/// - check if allocation id already exists
35+
/// - else, try to fetch allocation id from deployment_id to allocations map
36+
///
37+
/// Requires signed receipt Extension to be added OR deployment id
3738
pub async fn allocation_middleware(
3839
State(my_state): State<AllocationState>,
3940
mut request: Request,

crates/service/src/middleware/inject_deployment.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use axum::{
99
};
1010
use thegraph_core::DeploymentId;
1111

12+
/// Injects deployment id in the extensions from the path
1213
pub async fn deployment_middleware(mut request: Request, next: Next) -> Response {
1314
let deployment_id = request.extract_parts::<Path<DeploymentId>>().await.ok();
1415
if let Some(Path(deployment_id)) = deployment_id {

crates/service/src/middleware/inject_labels.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
//! Injects Metric Labels
5-
//!
6-
//! Require Sender, Allocation and Deployment extensions
7-
84
use std::sync::Arc;
95

106
use axum::{extract::Request, middleware::Next, response::Response};
@@ -20,6 +16,10 @@ const NO_DEPLOYMENT_ID: &str = "no-deployment";
2016
const NO_ALLOCATION: &str = "no-allocation";
2117
const NO_SENDER: &str = "no-sender";
2218

19+
/// Labels used by metrics which implements MetricLabelProvider
20+
///
21+
/// Might contain sender, allocation and deployment id and fills
22+
/// the gaps with constant values when they are not present
2323
#[derive(Clone, Default)]
2424
pub struct SenderAllocationDeploymentLabels {
2525
sender: Option<String>,
@@ -49,6 +49,9 @@ impl MetricLabelProvider for SenderAllocationDeploymentLabels {
4949
}
5050
}
5151

52+
/// Injects Metric Labels to be used by MetricMiddleware
53+
///
54+
/// Soft requirement for Sender, Allocation and Deployment extensions
5255
pub async fn labels_middleware(mut request: Request, next: Next) -> Response {
5356
let sender: Option<String> = request
5457
.extensions()

crates/service/src/middleware/inject_receipt.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ use axum_extra::TypedHeader;
66

77
use crate::service::TapReceipt;
88

9+
/// Injects tap receipts in the extensions
10+
///
11+
/// This is useful to not deserialize multiple times the same receipt
912
pub async fn receipt_middleware(mut request: Request, next: Next) -> Response {
1013
if let Ok(TypedHeader(receipt)) = request.extract_parts::<TypedHeader<TapReceipt>>().await {
1114
if let Some(receipt) = receipt.into_signed_receipt() {

crates/service/src/middleware/inject_sender.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ use tokio::sync::watch;
1313

1414
use crate::error::IndexerServiceError;
1515

16+
/// Stated used by sender middleware
1617
#[derive(Clone)]
1718
pub struct SenderState {
19+
/// Used to recover the signer address
1820
pub domain_separator: Eip712Domain,
21+
/// Used to get the sender address given the signer address
1922
pub escrow_accounts: watch::Receiver<EscrowAccounts>,
2023
}
2124

25+
/// The current query Sender address
2226
#[derive(Clone)]
2327
pub struct Sender(pub Address);
2428

@@ -28,6 +32,9 @@ impl From<Sender> for String {
2832
}
2933
}
3034

35+
/// Injects the sender found from the signer in the receipt
36+
///
37+
/// Requires Receipt extension
3138
pub async fn sender_middleware(
3239
State(state): State<SenderState>,
3340
mut request: Request,

crates/service/src/middleware/prometheus_metrics.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,22 @@ pub trait MetricLabelProvider {
2020
fn get_labels(&self) -> Vec<&str>;
2121
}
2222

23+
/// Middleware for metrics
2324
#[derive(Clone)]
2425
pub struct PrometheusMetricsMiddleware<S> {
2526
inner: S,
2627
histogram: prometheus::HistogramVec,
2728
failure: prometheus::CounterVec,
2829
}
2930

31+
/// MetricsMiddleware used in tower components
32+
///
33+
/// Register prometheus metrics in case of success or failure
3034
#[derive(Clone)]
3135
pub struct PrometheusMetricsMiddlewareLayer {
36+
/// Histogram used to register the processing timer
3237
histogram: prometheus::HistogramVec,
38+
/// Counter metric in case of failure
3339
failure: prometheus::CounterVec,
3440
}
3541

0 commit comments

Comments
 (0)