objectstore_server/extractors/
downstream_service.rs

1//! Downstream service extractor from the `x-downstream-service` request header.
2
3use axum::extract::FromRequestParts;
4use axum::http::request::Parts;
5
6/// Header used to identify the downstream service making the request, for use in killswitches and
7/// logging.
8const HEADER_SERVICE: &str = "x-downstream-service";
9
10/// Extractor for the downstream service identifier from the request header.
11///
12/// This extracts the `x-downstream-service` header value, which is used to identify which
13/// Kubernetes service or downstream system is making the request. This can be used for
14/// killswitches, rate limiting, and logging.
15#[derive(Debug, Clone)]
16pub struct DownstreamService(pub Option<String>);
17
18impl DownstreamService {
19    /// Returns the downstream service identifier, if present.
20    pub fn as_str(&self) -> Option<&str> {
21        self.0.as_deref()
22    }
23}
24
25impl<S: Send + Sync> FromRequestParts<S> for DownstreamService {
26    type Rejection = std::convert::Infallible;
27
28    async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
29        let service = parts
30            .headers
31            .get(HEADER_SERVICE)
32            .and_then(|v| v.to_str().ok())
33            .map(|s| s.to_string());
34
35        if let Some(ref service) = service {
36            sentry::configure_scope(|s| {
37                s.set_tag("downstream_service", service);
38            });
39        }
40
41        Ok(DownstreamService(service))
42    }
43}