|
| 1 | +// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! Injects Metric Labels |
| 5 | +//! |
| 6 | +//! Require Sender, Allocation and Deployment extensions |
| 7 | +
|
| 8 | +use std::sync::Arc; |
| 9 | + |
| 10 | +use axum::{extract::Request, middleware::Next, response::Response}; |
| 11 | +use thegraph_core::DeploymentId; |
| 12 | + |
| 13 | +use super::{ |
| 14 | + inject_allocation::Allocation, |
| 15 | + inject_sender::Sender, |
| 16 | + metrics::{MetricLabelProvider, MetricLabels}, |
| 17 | +}; |
| 18 | + |
| 19 | +const NO_DEPLOYMENT_ID: &str = "no-deployment"; |
| 20 | +const NO_ALLOCATION: &str = "no-allocation"; |
| 21 | +const NO_SENDER: &str = "no-sender"; |
| 22 | + |
| 23 | +#[derive(Clone, Default)] |
| 24 | +pub struct SenderAllocationDeploymentLabels { |
| 25 | + sender: Option<String>, |
| 26 | + allocation: Option<String>, |
| 27 | + deployment_id: Option<String>, |
| 28 | +} |
| 29 | + |
| 30 | +impl MetricLabelProvider for SenderAllocationDeploymentLabels { |
| 31 | + fn get_labels(&self) -> Vec<&str> { |
| 32 | + let mut list = vec![]; |
| 33 | + if let Some(deployment_id) = &self.deployment_id { |
| 34 | + list.push(deployment_id.as_str()); |
| 35 | + } else { |
| 36 | + list.push(NO_DEPLOYMENT_ID); |
| 37 | + } |
| 38 | + if let Some(allocation) = &self.allocation { |
| 39 | + list.push(allocation.as_str()); |
| 40 | + } else { |
| 41 | + list.push(NO_ALLOCATION); |
| 42 | + } |
| 43 | + if let Some(sender) = &self.sender { |
| 44 | + list.push(sender.as_str()); |
| 45 | + } else { |
| 46 | + list.push(NO_SENDER); |
| 47 | + } |
| 48 | + list |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +pub async fn labels_middleware(mut request: Request, next: Next) -> Response { |
| 53 | + let sender: Option<String> = request |
| 54 | + .extensions() |
| 55 | + .get::<Sender>() |
| 56 | + .map(|s| s.clone().into()); |
| 57 | + |
| 58 | + let allocation: Option<String> = request |
| 59 | + .extensions() |
| 60 | + .get::<Allocation>() |
| 61 | + .map(|s| s.clone().into()); |
| 62 | + |
| 63 | + let deployment_id: Option<String> = request |
| 64 | + .extensions() |
| 65 | + .get::<DeploymentId>() |
| 66 | + .map(|s| s.clone().to_string()); |
| 67 | + |
| 68 | + let labels: MetricLabels = Arc::new(SenderAllocationDeploymentLabels { |
| 69 | + sender, |
| 70 | + allocation, |
| 71 | + deployment_id, |
| 72 | + }); |
| 73 | + request.extensions_mut().insert(labels); |
| 74 | + |
| 75 | + next.run(request).await |
| 76 | +} |
| 77 | + |
| 78 | +#[cfg(test)] |
| 79 | +mod tests { |
| 80 | + use crate::middleware::{ |
| 81 | + inject_allocation::Allocation, inject_sender::Sender, metrics::MetricLabels, |
| 82 | + }; |
| 83 | + |
| 84 | + use super::labels_middleware; |
| 85 | + |
| 86 | + use alloy::primitives::Address; |
| 87 | + use axum::{ |
| 88 | + body::Body, |
| 89 | + http::{Extensions, Request}, |
| 90 | + middleware::from_fn, |
| 91 | + routing::get, |
| 92 | + Router, |
| 93 | + }; |
| 94 | + use reqwest::StatusCode; |
| 95 | + use test_assets::ESCROW_SUBGRAPH_DEPLOYMENT; |
| 96 | + use tower::ServiceExt; |
| 97 | + |
| 98 | + #[tokio::test] |
| 99 | + async fn test_receipt_middleware() { |
| 100 | + let middleware = from_fn(labels_middleware); |
| 101 | + |
| 102 | + let deployment = *ESCROW_SUBGRAPH_DEPLOYMENT; |
| 103 | + let sender = Address::ZERO; |
| 104 | + let allocation = Address::ZERO; |
| 105 | + |
| 106 | + let handle = move |extensions: Extensions| async move { |
| 107 | + let metrics = extensions |
| 108 | + .get::<MetricLabels>() |
| 109 | + .expect("Should decode tap receipt"); |
| 110 | + assert_eq!( |
| 111 | + metrics.get_labels(), |
| 112 | + vec![ |
| 113 | + &deployment.to_string(), |
| 114 | + &allocation.to_string(), |
| 115 | + &sender.to_string(), |
| 116 | + ] |
| 117 | + ); |
| 118 | + Body::empty() |
| 119 | + }; |
| 120 | + |
| 121 | + let app = Router::new().route("/", get(handle)).layer(middleware); |
| 122 | + |
| 123 | + let res = app |
| 124 | + .oneshot( |
| 125 | + Request::builder() |
| 126 | + .uri("/") |
| 127 | + .extension(Sender(sender)) |
| 128 | + .extension(deployment) |
| 129 | + .extension(Allocation(allocation)) |
| 130 | + .body(Body::empty()) |
| 131 | + .unwrap(), |
| 132 | + ) |
| 133 | + .await |
| 134 | + .unwrap(); |
| 135 | + assert_eq!(res.status(), StatusCode::OK); |
| 136 | + } |
| 137 | +} |
0 commit comments