forked from linkerd/linkerd2-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_stream.rs
More file actions
272 lines (234 loc) · 7.84 KB
/
log_stream.rs
File metadata and controls
272 lines (234 loc) · 7.84 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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
use super::*;
use futures::future::{self, FutureExt};
use tokio::{sync::oneshot, task::JoinHandle};
#[tokio::test]
async fn is_valid_json() {
let Fixture {
client,
metrics,
proxy: _proxy,
_profile,
dst_tx: _dst_tx,
..
} = Fixture::outbound().await;
let (logs, done) = get_log_stream(metrics, "info,linkerd=debug").await;
info!("client.get(/)");
assert_eq!(client.get("/").await, "hello");
// finish streaming logs so we don't loop forever
let _ = done.send(());
let json = logs.await.unwrap();
assert!(!json.is_empty());
for obj in json {
println!("{obj}\n");
}
}
#[tokio::test]
async fn query_is_valid_json() {
let Fixture {
client,
metrics,
proxy: _proxy,
_profile,
dst_tx: _dst_tx,
..
} = Fixture::outbound().await;
let (logs, done) = query_log_stream(metrics, "info,linkerd=debug").await;
info!("client.get(/)");
assert_eq!(client.get("/").await, "hello");
// finish streaming logs so we don't loop forever
let _ = done.send(());
let json = logs.await.unwrap();
assert!(!json.is_empty());
for obj in json {
println!("{obj}\n");
}
}
#[tokio::test]
async fn valid_get_does_not_error() {
let Fixture {
metrics,
proxy: _proxy,
_profile,
dst_tx: _dst_tx,
..
} = Fixture::outbound().await;
let (logs, done) = get_log_stream(metrics, "info,linkerd=debug").await;
// finish streaming logs so we don't loop forever
let _ = done.send(());
let json = logs.await.unwrap();
for obj in json {
println!("{obj}\n");
if obj.get("error").is_some() {
panic!("expected the log stream to contain no error responses!\njson = {obj}");
}
}
}
#[tokio::test]
async fn valid_query_does_not_error() {
let Fixture {
metrics,
proxy: _proxy,
_profile,
dst_tx: _dst_tx,
..
} = Fixture::outbound().await;
let (logs, done) = query_log_stream(metrics, "info,linkerd=debug").await;
// finish streaming logs so we don't loop forever
let _ = done.send(());
let json = logs.await.unwrap();
for obj in json {
println!("{obj}\n");
if obj.get("error").is_some() {
panic!("expected the log stream to contain no error responses!\njson = {obj}");
}
}
}
#[tokio::test]
async fn multi_filter() {
let Fixture {
client,
metrics,
proxy,
_profile,
dst_tx: _dst_tx,
..
} = Fixture::outbound().await;
// start streaming the logs
let (debug_logs, debug_done) = get_log_stream(metrics, "debug").await;
let (hyper_logs, hyper_done) =
get_log_stream(client::http1(proxy.admin, "localhost"), "hyper=trace").await;
info!("client.get(/)");
assert_eq!(client.get("/").await, "hello");
// finish streaming logs so we don't loop forever
let _ = debug_done.send(());
let _ = hyper_done.send(());
let json = debug_logs.await.unwrap();
for obj in json {
let level = obj.get("level");
assert!(
matches!(
level.and_then(|value| value.as_str()),
Some("DEBUG") | Some("INFO") | Some("WARN") | Some("ERROR")
),
"level must be DEBUG, INFO, WARN, or ERROR\n level: {level:?}\n json: {obj:#?}"
);
}
let json = hyper_logs.await.unwrap();
for obj in json {
let target = obj.get("target").and_then(|value| value.as_str());
match target {
Some(s) if s.starts_with("hyper") => {}
_ => panic!(
"target must be from a module in `hyper`!\n target: {:?}\n json: {:#?}",
obj.get("target"),
obj
),
}
}
}
const PATH: &str = "/logs.json";
/// Start a log stream with a GET request
async fn get_log_stream(
client: client::Client,
filter: impl ToString,
) -> (JoinHandle<Vec<serde_json::Value>>, oneshot::Sender<()>) {
let filter = filter.to_string();
// start the request
let req = client
.request_body(
client
.request_builder(&format!("{PATH}?{filter}"))
.method(http::Method::GET)
.body(http_body_util::Full::new(Bytes::from(filter)))
.unwrap(),
)
.await;
assert_eq!(req.status(), http::StatusCode::OK);
// spawn a task to collect and parse all the logs
collect_logs(req.into_body())
}
/// Start a log stream with a QUERY request
async fn query_log_stream(
client: client::Client,
filter: impl ToString,
) -> (JoinHandle<Vec<serde_json::Value>>, oneshot::Sender<()>) {
let filter = filter.to_string();
// start the request
let req = client
.request_body(
client
.request_builder(PATH)
.method("QUERY")
.body(http_body_util::Full::new(Bytes::from(filter)))
.unwrap(),
)
.await;
assert_eq!(req.status(), http::StatusCode::OK);
// spawn a task to collect and parse all the logs
collect_logs(req.into_body())
}
/// Spawns a task to collect all the logs in a streaming body and parse them as
/// JSON.
fn collect_logs<B>(mut body: B) -> (JoinHandle<Vec<serde_json::Value>>, oneshot::Sender<()>)
where
B: Body<Data = Bytes> + Send + Unpin + 'static,
B::Error: std::error::Error,
{
use http_body_util::BodyExt;
let (done_tx, done_rx) = oneshot::channel();
let result = tokio::spawn(async move {
let mut result = Vec::new();
let logs = &mut result;
let fut = async move {
let mut buffer = Vec::new();
while let Some(res) = body.frame().await {
let chunk = match res {
Ok(frame) => {
if let Ok(data) = frame.into_data() {
data
} else {
break;
}
}
Err(e) => {
println!("body failed: {e}");
break;
}
};
buffer.extend_from_slice(&chunk[..]);
// Process complete lines since the format is newline-delimited JSON (NDJSON)
while let Some(newline_pos) = buffer.iter().position(|&b| b == b'\n') {
let line = buffer.drain(..=newline_pos).collect::<Vec<_>>();
// Skip empty lines
if line.iter().all(|&b| b.is_ascii_whitespace()) {
continue;
}
let deserialized = serde_json::from_slice(&line[..]);
tracing::info!(?deserialized);
match deserialized {
Ok(json) => logs.push(json),
Err(error) => panic!(
"parsing logs as JSON failed\n error: {error}\n line: {:?}",
String::from_utf8_lossy(&line[..])
),
}
}
}
// Handle remaining data in buffer
if !buffer.is_empty() && !buffer.iter().all(|&b| b.is_ascii_whitespace()) {
let deserialized = serde_json::from_slice(&buffer[..]);
tracing::info!(?deserialized);
match deserialized {
Ok(json) => logs.push(json),
Err(error) => panic!(
"parsing logs as JSON failed (incomplete final line)\nerror: {error}\nbuffer: {:?}",
String::from_utf8_lossy(&buffer[..])
),
}
}
};
future::select(Box::pin(fut), done_rx.map(|_| ())).await;
result
});
(result, done_tx)
}