-
Notifications
You must be signed in to change notification settings - Fork 349
/
mod.rs
241 lines (216 loc) 路 6.26 KB
/
mod.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright 2024 Zinc Labs Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
use ::config::{meta::cluster::Role, utils::rand::get_rand_element, CONFIG};
use actix_web::{http::Error, route, web, HttpRequest, HttpResponse};
use crate::common::infra::cluster;
const QUERIER_ROUTES: [&str; 16] = [
"/summary",
"/schema",
"/streams",
"/_search",
"/_around",
"/_values",
"/prometheus/api/v1/series",
"/prometheus/api/v1/query_range",
"/prometheus/api/v1/query",
"/prometheus/api/v1/metadata",
"/prometheus/api/v1/labels",
"/prometheus/api/v1/label/",
"/config/dex_login",
"/config/logout",
"/config/redirect",
"/config/dex_refresh",
];
const FIXED_QUERIER_ROUTES: [&str; 3] = ["/summary", "/schema", "/streams"];
#[inline]
fn check_querier_route(path: &str) -> bool {
QUERIER_ROUTES.iter().any(|x| path.contains(x))
}
#[inline]
fn is_fixed_querier_route(path: &str) -> bool {
FIXED_QUERIER_ROUTES.iter().any(|x| path.contains(x))
}
#[route(
"/config",
method = "GET",
method = "POST",
method = "PUT",
method = "DELETE"
)]
pub async fn config(
req: HttpRequest,
payload: web::Payload,
client: web::Data<awc::Client>,
) -> actix_web::Result<HttpResponse, Error> {
dispatch(req, payload, client).await
}
#[route(
"/config/{path:.*}",
method = "GET",
method = "POST",
method = "PUT",
method = "DELETE"
)]
pub async fn config_paths(
req: HttpRequest,
payload: web::Payload,
client: web::Data<awc::Client>,
) -> actix_web::Result<HttpResponse, Error> {
dispatch(req, payload, client).await
}
#[route(
"/api/{path:.*}",
method = "GET",
method = "POST",
method = "PUT",
method = "DELETE"
)]
pub async fn api(
req: HttpRequest,
payload: web::Payload,
client: web::Data<awc::Client>,
) -> actix_web::Result<HttpResponse, Error> {
dispatch(req, payload, client).await
}
#[route(
"/aws/{path:.*}",
method = "GET",
method = "POST",
method = "PUT",
method = "DELETE"
)]
pub async fn aws(
req: HttpRequest,
payload: web::Payload,
client: web::Data<awc::Client>,
) -> actix_web::Result<HttpResponse, Error> {
dispatch(req, payload, client).await
}
#[route(
"/gcp/{path:.*}",
method = "GET",
method = "POST",
method = "PUT",
method = "DELETE"
)]
pub async fn gcp(
req: HttpRequest,
payload: web::Payload,
client: web::Data<awc::Client>,
) -> actix_web::Result<HttpResponse, Error> {
dispatch(req, payload, client).await
}
#[route(
"/rum/{path:.*}",
// method = "GET",
method = "POST",
)]
pub async fn rum(
req: HttpRequest,
payload: web::Payload,
client: web::Data<awc::Client>,
) -> actix_web::Result<HttpResponse, Error> {
dispatch(req, payload, client).await
}
async fn dispatch(
req: HttpRequest,
payload: web::Payload,
client: web::Data<awc::Client>,
) -> actix_web::Result<HttpResponse, Error> {
// get online nodes
let path = req.uri().path_and_query().map(|x| x.as_str()).unwrap_or("");
// send query
let new_url = get_url(path).await;
if new_url.is_error {
return Ok(HttpResponse::ServiceUnavailable().body(new_url.value));
}
let resp = client
.request_from(new_url.value.clone(), req.head())
.send_stream(payload)
.await;
if let Err(e) = resp {
log::error!("dispatch: {}, error: {}", new_url.value, e);
return Ok(HttpResponse::ServiceUnavailable().body(e.to_string()));
}
// handle response
let mut resp = resp.unwrap();
let mut new_resp = HttpResponse::build(resp.status());
// copy headers
for (key, value) in resp.headers() {
if !key.eq("content-encoding") {
new_resp.insert_header((key.clone(), value.clone()));
}
}
// set body
let body = match resp.body().limit(CONFIG.limit.req_payload_limit).await {
Ok(b) => b,
Err(e) => {
log::error!("{}: {}", new_url.value, e);
return Ok(HttpResponse::ServiceUnavailable().body(e.to_string()));
}
};
Ok(new_resp.body(body))
}
async fn get_url(path: &str) -> URLDetails {
let node_type;
let is_querier_path = check_querier_route(path);
let nodes = if is_querier_path {
node_type = Role::Querier;
let nodes = cluster::get_cached_online_querier_nodes().await;
if is_fixed_querier_route(path) && nodes.is_some() && !nodes.as_ref().unwrap().is_empty() {
nodes.map(|v| v.into_iter().take(1).collect())
} else {
nodes
}
} else {
node_type = Role::Ingester;
cluster::get_cached_online_ingester_nodes().await
};
if nodes.is_none() || nodes.as_ref().unwrap().is_empty() {
if node_type == Role::Ingester && !CONFIG.route.ingester_srv_url.is_empty() {
return URLDetails {
is_error: false,
value: format!(
"http://{}:{}{}",
CONFIG.route.ingester_srv_url, CONFIG.http.port, path
),
};
}
return URLDetails {
is_error: true,
value: format!("No online {node_type} nodes"),
};
}
let nodes = nodes.unwrap();
let node = get_rand_element(&nodes);
URLDetails {
is_error: false,
value: format!("{}{}", node.http_addr, path),
}
}
struct URLDetails {
is_error: bool,
value: String,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_check_querier_route() {
assert!(check_querier_route("/api/_search"));
assert!(check_querier_route("/api/_around"));
assert!(!check_querier_route("/api/_bulk"));
}
}