| 12 | use crate::metric_names::*; |
| 13 | |
| 14 | pub(crate) async fn request_telemetry( |
| 15 | req: ServiceRequest, |
| 16 | next: Next<impl MessageBody>, |
| 17 | ) -> actix_web::Result<ServiceResponse<impl MessageBody>> { |
| 18 | let now = Instant::now(); |
| 19 | |
| 20 | metrics::gauge!(GAUGE_HTTP_CONCURRENT_REQUESTS).increment(1); |
| 21 | |
| 22 | let mut res = next.call(req).await?; |
| 23 | |
| 24 | let req_id = res.request().extensions().get::<RequestId>().copied(); |
| 25 | |
| 26 | if let Some(req_id) = req_id { |
| 27 | res.headers_mut().insert( |
| 28 | HeaderName::from_static("request-id"), |
| 29 | // this unwrap never fails, since UUIDs are valid ASCII strings |
| 30 | HeaderValue::from_str(&req_id.to_string()).unwrap(), |
| 31 | ); |
| 32 | }; |
| 33 | |
| 34 | let outcome = if res.status().is_success() || res.status().is_redirection() { |
| 35 | "success" |
| 36 | } else { |
| 37 | "failure" |
| 38 | }; |
| 39 | |
| 40 | let diff = now.elapsed(); |
| 41 | metrics::histogram!(HISTOGRAM_HTTP_REQUEST_DURATION, "outcome" => outcome).record(diff); |
| 42 | |
| 43 | metrics::gauge!(GAUGE_HTTP_CONCURRENT_REQUESTS).decrement(1); |
| 44 | |
| 45 | Ok(res) |
| 46 | } |