-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathv1.rs
More file actions
111 lines (103 loc) · 3.3 KB
/
v1.rs
File metadata and controls
111 lines (103 loc) · 3.3 KB
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
//! CloudMon metrics processor API v1
//!
//! API v1 of the metrics convertor
//!
use axum::{
extract::Query,
extract::State,
http::StatusCode,
response::{IntoResponse, Json, Response},
routing::get,
Router,
};
use serde::{Deserialize, Serialize};
use serde_json::json;
use crate::common::get_service_health;
use crate::types::{AppState, CloudMonError, ServiceHealthData};
/// Query parameters supported by the /health API call
#[derive(Debug, Deserialize)]
pub struct HealthQuery {
/// Start point to query metrics
pub from: String,
pub to: String,
#[serde(default = "default_max_data_points")]
pub max_data_points: u32,
pub service: String,
pub environment: String,
}
fn default_max_data_points() -> u32 {
100
}
/// Response of the /health API call
#[derive(Debug, Serialize, Deserialize)]
pub struct ServiceHealthResponse {
pub name: String,
pub service_category: String,
pub environment: String,
pub metrics: ServiceHealthData,
}
/// Construct supported api v1 routes
pub fn get_v1_routes() -> Router<AppState> {
Router::new()
.route("/", get(root))
.route("/info", get(info))
.route("/health", get(handler_health))
}
/// Return API v1 root info
async fn root() -> impl IntoResponse {
(StatusCode::OK, Json(json!({"name": "v1"})))
}
/// Return v1 API infos
async fn info() -> impl IntoResponse {
(StatusCode::OK, "V1 API of the CloudMon\n")
}
/// Handler method invoked for /health request
pub async fn handler_health(query: Query<HealthQuery>, State(state): State<AppState>) -> Response {
tracing::debug!("Processing query {:?}", query);
match state.health_metrics.get(&query.service) {
Some(hm_config) => {
// We have health metric configuration
match get_service_health(
&state,
query.service.as_str(),
query.environment.as_str(),
query.from.as_str(),
query.to.as_str(),
query.max_data_points as u16,
)
.await
{
Ok(health_data) => (
StatusCode::OK,
Json(ServiceHealthResponse {
name: query.service.clone(),
service_category: hm_config.category.clone(),
environment: query.environment.clone(),
metrics: health_data,
}),
)
.into_response(),
Err(error) => match error {
CloudMonError::EnvNotSupported | CloudMonError::ServiceNotSupported => (
StatusCode::CONFLICT,
Json(json!({ "message": format!("{}", error) })),
)
.into_response(),
_ => (
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({ "message": format!("{}", error) })),
)
.into_response(),
},
}
}
_ => {
// Requested service is not known
(
StatusCode::CONFLICT,
Json(json!({"message": "Service not supported"})),
)
.into_response()
}
}
}