|
| 1 | +use std::fmt::Write; |
| 2 | + |
| 3 | +use actix_web::{ |
| 4 | + body::MessageBody, |
| 5 | + dev::{ServiceRequest, ServiceResponse}, |
| 6 | + http::header, |
| 7 | + middleware::Next, |
| 8 | + web |
| 9 | +}; |
| 10 | +use documented::DocumentedFields; |
| 11 | +use prometheus_client::{ |
| 12 | + encoding::{EncodeLabelSet, EncodeLabelValue}, |
| 13 | + metrics::{counter::Counter, family::Family}, |
| 14 | + registry::Registry |
| 15 | +}; |
| 16 | +use serde::{Deserialize, Serialize}; |
| 17 | + |
| 18 | +use crate::{ |
| 19 | + api::common::{data::ApiData, metrics::MetricsGroup}, |
| 20 | + make_api_metric |
| 21 | +}; |
| 22 | + |
| 23 | +macro_rules! impl_serde_variant_as_label_value { |
| 24 | + ($enum:ty) => { |
| 25 | + impl EncodeLabelValue for $enum { |
| 26 | + fn encode( |
| 27 | + &self, |
| 28 | + encoder: &mut prometheus_client::encoding::LabelValueEncoder |
| 29 | + ) -> Result<(), std::fmt::Error> { |
| 30 | + encoder.write_str( |
| 31 | + serde_variant::to_variant_name(self).map_err(|_| std::fmt::Error)? |
| 32 | + ) |
| 33 | + } |
| 34 | + } |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +#[derive(Serialize, Deserialize, Debug, Hash, PartialEq, Eq, Clone)] |
| 39 | +#[serde(rename_all = "lowercase")] |
| 40 | +pub enum UserAgentType { |
| 41 | + Loader, |
| 42 | + Wrapper, |
| 43 | + Unknown |
| 44 | +} |
| 45 | + |
| 46 | +#[derive(Serialize, Deserialize, Debug, Hash, PartialEq, Eq, Clone, EncodeLabelSet)] |
| 47 | +pub struct OneConfigRequest { |
| 48 | + pub version: String, |
| 49 | + pub loader: String, |
| 50 | + pub user_agent_type: UserAgentType |
| 51 | +} |
| 52 | + |
| 53 | +impl_serde_variant_as_label_value!(UserAgentType); |
| 54 | + |
| 55 | +/// A struct containing all of the metrics state for the API |
| 56 | +#[derive(DocumentedFields)] |
| 57 | +pub struct ApiLegacyMetrics { |
| 58 | + /// The amount of OneConfig requests, by version, loader, and user agent |
| 59 | + /// type |
| 60 | + oneconfig_requests: Family<OneConfigRequest, Counter> |
| 61 | +} |
| 62 | + |
| 63 | +impl MetricsGroup for ApiLegacyMetrics { |
| 64 | + fn init_metrics(registry: &mut Registry) -> Self { |
| 65 | + let registry = registry.sub_registry_with_prefix("legacy"); |
| 66 | + |
| 67 | + make_api_metric!(registry, Self, oneconfig_requests); |
| 68 | + |
| 69 | + Self { oneconfig_requests } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/// A middleware to increment all metrics per-request |
| 74 | +pub async fn middleware( |
| 75 | + mut service_request: ServiceRequest, |
| 76 | + next: Next<impl MessageBody> |
| 77 | +) -> Result<ServiceResponse<impl MessageBody>, actix_web::Error> { |
| 78 | + let data = service_request.extract::<web::Data<ApiData>>().await?; |
| 79 | + |
| 80 | + #[allow(clippy::single_match)] |
| 81 | + match service_request |
| 82 | + .match_pattern() |
| 83 | + .unwrap_or("default".to_string()) |
| 84 | + .as_str() |
| 85 | + { |
| 86 | + "/oneconfig/{version}-{loader}" => 'inner: { |
| 87 | + let Some((version, loader)) = service_request.uri().path() |
| 88 | + [(const { "/oneconfig/".len() })..] |
| 89 | + .split_once('-') |
| 90 | + else { |
| 91 | + break 'inner; |
| 92 | + }; |
| 93 | + |
| 94 | + data.metrics |
| 95 | + .legacy |
| 96 | + .oneconfig_requests |
| 97 | + .get_or_create(&OneConfigRequest { |
| 98 | + version: version.to_owned(), |
| 99 | + loader: loader.to_owned(), |
| 100 | + user_agent_type: match service_request |
| 101 | + .headers() |
| 102 | + .get(header::USER_AGENT) |
| 103 | + .and_then(|v| v.to_str().ok()) |
| 104 | + { |
| 105 | + Some(s) if s.contains("OneConfigLoader") => UserAgentType::Loader, |
| 106 | + Some(s) if s.contains("OneConfigWrapper") => |
| 107 | + UserAgentType::Wrapper, |
| 108 | + Some(_) | None => UserAgentType::Unknown |
| 109 | + } |
| 110 | + }) |
| 111 | + .inc(); |
| 112 | + } |
| 113 | + _ => () |
| 114 | + }; |
| 115 | + |
| 116 | + // Let the real request handler continue |
| 117 | + next.call(service_request).await |
| 118 | +} |
0 commit comments