|
1 | 1 | pub mod rejectua; |
2 | 2 | pub mod weave; |
3 | | - |
4 | | -// # Web Middleware |
5 | | -// |
6 | | -// Matches the [Sync Storage middleware](https://github.com/mozilla-services/server-syncstorage/blob/master/syncstorage/tweens.py) (tweens). |
7 | | - |
8 | | -use std::collections::HashMap; |
9 | | -use std::future::Future; |
10 | | - |
11 | | -use actix_web::{ |
12 | | - HttpMessage, |
13 | | - dev::{Service, ServiceRequest, ServiceResponse}, |
14 | | - web::Data, |
15 | | -}; |
16 | | -use syncserver_common::Metrics; |
17 | | -use tokenserver_auth::TokenserverOrigin; |
18 | | - |
19 | | -use crate::error::{ApiError, ApiErrorKind}; |
20 | | -use crate::server::ServerState; |
21 | | - |
22 | | -pub fn emit_http_status_with_tokenserver_origin<B, S>( |
23 | | - req: ServiceRequest, |
24 | | - srv: &S, |
25 | | -) -> impl Future<Output = Result<ServiceResponse<B>, actix_web::Error>> + use<B, S> |
26 | | -where |
27 | | - S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = actix_web::Error>, |
28 | | -{ |
29 | | - let fut = srv.call(req); |
30 | | - |
31 | | - async move { |
32 | | - let res = fut.await?; |
33 | | - let req = res.request(); |
34 | | - let metrics = { |
35 | | - let statsd_client = req |
36 | | - .app_data::<Data<ServerState>>() |
37 | | - .map(|state| state.metrics.clone()) |
38 | | - .ok_or_else(|| ApiError::from(ApiErrorKind::NoServerState))?; |
39 | | - |
40 | | - Metrics::from(&statsd_client) |
41 | | - }; |
42 | | - |
43 | | - let mut tags = HashMap::default(); |
44 | | - if let Some(origin) = req.extensions().get::<TokenserverOrigin>().copied() { |
45 | | - tags.insert("tokenserver_origin".to_string(), origin.to_string()); |
46 | | - }; |
47 | | - |
48 | | - if res.status().is_informational() { |
49 | | - metrics.incr_with_tags("http_1XX", tags); |
50 | | - } else if res.status().is_success() { |
51 | | - metrics.incr_with_tags("http_2XX", tags); |
52 | | - } else if res.status().is_redirection() { |
53 | | - metrics.incr_with_tags("http_3XX", tags); |
54 | | - } else if res.status().is_client_error() { |
55 | | - metrics.incr_with_tags("http_4XX", tags); |
56 | | - } else if res.status().is_server_error() { |
57 | | - metrics.incr_with_tags("http_5XX", tags); |
58 | | - } |
59 | | - |
60 | | - Ok(res) |
61 | | - } |
62 | | -} |
0 commit comments