-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathconsumption.rs
145 lines (121 loc) · 4.61 KB
/
consumption.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use crate::cli::{EdgeArgs, EdgeMode};
use crate::http::headers::UNLEASH_INTERVAL;
use crate::metrics::edge_metrics::EdgeInstanceData;
use actix_http::body::MessageBody;
use actix_web::HttpRequest;
use actix_web::dev::{ServiceRequest, ServiceResponse};
use actix_web::web::Data;
fn should_observe_connection_consumption(path: &str, status_code: u16) -> bool {
let is_valid_path = path.starts_with("/api/client/features")
|| path.starts_with("/api/client/delta")
|| path.starts_with("/api/client/metrics");
is_valid_path && ((200..300).contains(&status_code) || status_code == 304)
}
fn should_observe_request_consumption(path: &str, status_code: u16) -> bool {
let is_valid_path = path.starts_with("/api/frontend");
is_valid_path && ((200..300).contains(&status_code) || status_code == 304)
}
fn get_edge_args(req: &HttpRequest) -> Option<&EdgeArgs> {
req.app_data::<Data<EdgeMode>>()
.map(|mode| mode.get_ref())
.and_then(|mode| match mode {
EdgeMode::Edge(args) => Some(args),
_ => None,
})
}
pub async fn connection_consumption(
req: ServiceRequest,
srv: crate::middleware::as_async_middleware::Next<impl MessageBody + 'static>,
) -> Result<ServiceResponse<impl MessageBody>, actix_web::Error> {
let path = req.path().to_string();
let should_observe = path.starts_with("/api/client/features")
|| path.starts_with("/api/client/delta")
|| path.starts_with("/api/client/metrics");
let interval = if should_observe {
req.headers()
.get(UNLEASH_INTERVAL)
.and_then(|h| h.to_str().ok())
.and_then(|s| s.parse::<u64>().ok())
} else {
None
};
let resp = srv.call(req).await?;
let status_code = resp.status().as_u16();
if !should_observe_connection_consumption(&path, status_code) {
return Ok(resp);
}
let instance_data = resp.request().app_data::<Data<EdgeInstanceData>>();
let edge_args = get_edge_args(resp.request());
if let (Some(instance_data), Some(args)) = (instance_data, edge_args) {
if args.consumption {
instance_data
.get_ref()
.observe_connection_consumption(&path, interval);
}
}
Ok(resp)
}
pub async fn request_consumption(
req: ServiceRequest,
srv: crate::middleware::as_async_middleware::Next<impl MessageBody + 'static>,
) -> Result<ServiceResponse<impl MessageBody>, actix_web::Error> {
let path = req.path().to_string();
let resp = srv.call(req).await?;
let status_code = resp.status().as_u16();
if !should_observe_request_consumption(&path, status_code) {
return Ok(resp);
}
let instance_data = resp.request().app_data::<Data<EdgeInstanceData>>();
let edge_args = get_edge_args(resp.request());
if let (Some(instance_data), Some(args)) = (instance_data, edge_args) {
if args.consumption {
instance_data.get_ref().observe_request_consumption();
}
}
Ok(resp)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::metrics::edge_metrics::EdgeInstanceData;
use crate::middleware::as_async_middleware::as_async_middleware;
use actix_web::{App, HttpResponse, test};
#[test]
async fn test_backend_consumption() {
let instance_data = EdgeInstanceData::new("test");
let app = test::init_service(
App::new()
.app_data(Data::new(instance_data.clone()))
.wrap(as_async_middleware(connection_consumption))
.route(
"/api/client/features",
actix_web::web::get().to(|| async { HttpResponse::Ok().await }),
),
)
.await;
let req = test::TestRequest::get()
.uri("/api/client/features")
.to_request();
let resp = test::call_service(&app, req).await;
assert!(resp.status().is_success());
}
#[test]
async fn test_frontend_consumption() {
let instance_data = EdgeInstanceData::new("test");
let app = test::init_service(
App::new()
.app_data(Data::new(instance_data.clone()))
.wrap(as_async_middleware(request_consumption))
.route(
"/api/frontend/features",
actix_web::web::get().to(|| async { HttpResponse::Ok().await }),
),
)
.await;
let req = test::TestRequest::get()
.uri("/api/frontend/features")
.to_request();
let resp = test::call_service(&app, req).await;
assert!(resp.status().is_success());
}
}