Repository navigation
Expand file tree
/
Copy pathdiagnostics.rs
More file actions
128 lines (122 loc) · 5.24 KB
/
Copy pathdiagnostics.rs
File metadata and controls
128 lines (122 loc) · 5.24 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//! Deliberately small diagnostic events: no request-controlled text is logged.
use axum::{
extract::{MatchedPath, Request},
http::{HeaderValue, Method},
middleware::Next,
response::Response,
};
use std::{
sync::atomic::{AtomicU64, Ordering},
time::Instant,
};
static REQUEST_SEQUENCE: AtomicU64 = AtomicU64::new(1);
pub(crate) async fn request(request: Request, next: Next) -> Response {
let request_id = REQUEST_SEQUENCE.fetch_add(1, Ordering::Relaxed).to_string();
let method = match *request.method() {
Method::GET => "GET",
Method::POST => "POST",
Method::PUT => "PUT",
Method::PATCH => "PATCH",
Method::DELETE => "DELETE",
Method::HEAD => "HEAD",
Method::OPTIONS => "OPTIONS",
_ => "OTHER",
};
let route = match request
.extensions()
.get::<MatchedPath>()
.map(MatchedPath::as_str)
{
Some("/api/v1/health") => "/api/v1/health",
Some("/api/v1/images") => "/api/v1/images",
Some("/api/v1/provisioning/create") => "/api/v1/provisioning/create",
Some("/api/v1/provisioning/plan") => "/api/v1/provisioning/plan",
Some("/api/v1/provisioning/preflight") => "/api/v1/provisioning/preflight",
Some("/api/v1/provider/inventory") => "/api/v1/provider/inventory",
Some("/api/v1/provider/nodes/{node}/vms/{vmid}/status") => {
"/api/v1/provider/nodes/{node}/vms/{vmid}/status"
}
Some("/api/v1/provider/inventory/refresh") => "/api/v1/provider/inventory/refresh",
Some("/api/v1/operations/{id}") => "/api/v1/operations/{id}",
Some("/api/v1/operations") => "/api/v1/operations",
Some("/api/v1/environments") => "/api/v1/environments",
Some("/api/v1/environments/{id}") => "/api/v1/environments/{id}",
Some("/api/v1/environments/{id}/lifecycle-preflight") => {
"/api/v1/environments/{id}/lifecycle-preflight"
}
Some("/api/v1/environments/{id}/deletion-operations") => {
"/api/v1/environments/{id}/deletion-operations"
}
Some("/api/v1/environments/{id}/deletion-operations/{operation_id}") => {
"/api/v1/environments/{id}/deletion-operations/{operation_id}"
}
Some("/api/v1/environments/{id}/delete") => "/api/v1/environments/{id}/delete",
Some("/api/v1/environments/{id}/delete-preflight") => {
"/api/v1/environments/{id}/delete-preflight"
}
Some("/api/v1/environments/{id}/start") => "/api/v1/environments/{id}/start",
Some("/api/v1/environments/{id}/shutdown") => "/api/v1/environments/{id}/shutdown",
Some("/api/v1/environments/{id}/refresh") => "/api/v1/environments/{id}/refresh",
Some("/api/v1/environments/{id}/restore-operations") => {
"/api/v1/environments/{id}/restore-operations"
}
Some("/api/v1/environments/{id}/restore-operations/{operation_id}") => {
"/api/v1/environments/{id}/restore-operations/{operation_id}"
}
Some("/api/v1/environments/{id}/snapshot-operations") => {
"/api/v1/environments/{id}/snapshot-operations"
}
Some("/api/v1/environments/{id}/snapshot-operations/{operation_id}") => {
"/api/v1/environments/{id}/snapshot-operations/{operation_id}"
}
Some("/api/v1/environments/{id}/snapshots/{snapshot_name}/restore") => {
"/api/v1/environments/{id}/snapshots/{snapshot_name}/restore"
}
Some("/api/v1/environments/{id}/snapshot-restore-preflight") => {
"/api/v1/environments/{id}/snapshot-restore-preflight"
}
Some("/api/v1/environments/{id}/snapshot-preflight") => {
"/api/v1/environments/{id}/snapshot-preflight"
}
Some("/api/v1/environments/{id}/snapshots") => "/api/v1/environments/{id}/snapshots",
Some("/api/v1/environments/{id}/status") => "/api/v1/environments/{id}/status",
Some("/api/v1/definitions") => "/api/v1/definitions",
Some("/api/v1/definitions/validate") => "/api/v1/definitions/validate",
Some("/api/v1/definitions/{name}") => "/api/v1/definitions/{name}",
_ => "unmatched",
};
let started = Instant::now();
let mut response = next.run(request).await;
response.headers_mut().insert(
"x-request-id",
HeaderValue::from_str(&request_id).expect("numeric request ID"),
);
eprintln!(
"{}",
serde_json::json!({
"event": "request_complete", "requestId": request_id,
"method": method, "route": route, "status": response.status().as_u16(),
"durationMs": started.elapsed().as_millis().min(u64::MAX as u128) as u64,
})
);
response
}
pub fn listening(port: u16) {
eprintln!(
"{}",
serde_json::json!({"event": "server_listening", "port": port})
);
}
pub fn stopped() {
eprintln!("{}", serde_json::json!({"event": "server_stopped"}));
}
pub fn drain_timed_out() {
eprintln!("{}", serde_json::json!({"event": "server_drain_timed_out"}));
}
/// Reasons are static strings chosen by the service, never external errors.
pub fn failed(reason: &'static str) {
eprintln!(
"{}",
serde_json::json!({"event": "server_failed", "reason": reason})
);
}