forked from console-rs/console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
351 lines (316 loc) · 10.8 KB
/
lib.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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
use console_api as proto;
use tokio::sync::mpsc;
use std::{
net::{IpAddr, Ipv4Addr, SocketAddr},
ptr,
sync::{
atomic::{AtomicPtr, Ordering::*},
Arc,
},
time::{Duration, SystemTime},
};
use tracing_core::{
field::{self, Visit},
span,
subscriber::{self, Subscriber},
Metadata,
};
use tracing_subscriber::{layer::Context, registry::LookupSpan, Layer};
mod aggregator;
use aggregator::Aggregator;
mod builder;
pub use builder::Builder;
pub struct TasksLayer {
task_meta: AtomicPtr<Metadata<'static>>,
blocking_meta: AtomicPtr<Metadata<'static>>,
tx: mpsc::Sender<Event>,
flush: Arc<aggregator::Flush>,
}
pub struct Server {
subscribe: mpsc::Sender<Watch>,
addr: SocketAddr,
aggregator: Option<Aggregator>,
client_buffer: usize,
}
struct FieldVisitor {
fields: Vec<proto::Field>,
meta_id: proto::MetaId,
}
struct Watch(mpsc::Sender<Result<proto::tasks::TaskUpdate, tonic::Status>>);
enum Event {
Metadata(&'static Metadata<'static>),
Spawn {
id: span::Id,
metadata: &'static Metadata<'static>,
at: SystemTime,
fields: Vec<proto::Field>,
},
Enter {
id: span::Id,
at: SystemTime,
},
Exit {
id: span::Id,
at: SystemTime,
},
Close {
id: span::Id,
at: SystemTime,
},
}
impl TasksLayer {
pub fn new() -> (Self, Server) {
Self::builder().build()
}
/// Returns a [`Builder`] for configuring a `TasksLayer`.
pub fn builder() -> Builder {
Builder::default()
}
fn build(config: Builder) -> (Self, Server) {
// The `cfg` value *appears* to be a constant to clippy, but it changes
// depending on the build-time configuration...
#![allow(clippy::assertions_on_constants)]
assert!(
cfg!(tokio_unstable),
"task tracing requires Tokio to be built with RUSTFLAGS=\"--cfg tokio_unstable\"!"
);
tracing::debug!(
config.event_buffer_capacity,
config.client_buffer_capacity,
?config.publish_interval,
?config.retention,
?config.server_addr,
"configured console subscriber"
);
let (tx, events) = mpsc::channel(config.event_buffer_capacity);
let (subscribe, rpcs) = mpsc::channel(256);
let aggregator = Aggregator::new(events, rpcs, &config);
let flush = aggregator.flush().clone();
let server = Server {
aggregator: Some(aggregator),
addr: config.server_addr,
subscribe,
client_buffer: config.client_buffer_capacity,
};
let layer = Self {
tx,
flush,
task_meta: AtomicPtr::new(ptr::null_mut()),
blocking_meta: AtomicPtr::new(ptr::null_mut()),
};
(layer, server)
}
}
impl TasksLayer {
pub const DEFAULT_EVENT_BUFFER_CAPACITY: usize = 1024 * 10;
pub const DEFAULT_CLIENT_BUFFER_CAPACITY: usize = 1024 * 4;
pub const DEFAULT_PUBLISH_INTERVAL: Duration = Duration::from_secs(1);
/// By default, completed spans are retained for one hour.
pub const DEFAULT_RETENTION: Duration = Duration::from_secs(60 * 60);
// how much capacity should remain in the buffer before triggering a
// flush on capacity?
//
// chosen by fair die roll, guaranteed to be random :)
const FLUSH_AT_CAPACITY: usize = 100;
#[inline(always)]
fn is_spawn(&self, meta: &'static Metadata<'static>) -> bool {
ptr::eq(self.task_meta.load(Relaxed), meta as *const _ as *mut _)
// || ptr::eq(self.blocking_meta.load(Relaxed), meta as *const _ as *mut _)
}
fn is_id_spawned<S>(&self, id: &span::Id, cx: &Context<'_, S>) -> bool
where
S: Subscriber + for<'a> LookupSpan<'a>,
{
cx.span(id)
.map(|span| self.is_spawn(span.metadata()))
.unwrap_or(false)
}
fn send(&self, event: Event) {
use mpsc::error::TrySendError;
match self.tx.try_reserve() {
Ok(permit) => permit.send(event),
Err(TrySendError::Closed(_)) => tracing::warn!(
"console server task has terminated; task stats will no longer be updated"
),
Err(TrySendError::Full(_)) => {
// this shouldn't happen, since we trigger a flush when
// approaching the high water line...but if the executor wait
// time is very high, maybe the aggregator task hasn't been
// polled yet. so, try to handle it gracefully...
tracing::warn!(
"console buffer is full; some task stats may be delayed. \
preemptive flush interval should be adjusted..."
);
let tx = self.tx.clone();
tokio::spawn(async move {
if tx.send(event).await.is_err() {
tracing::debug!("task event channel closed after lag");
}
});
}
}
let capacity = self.tx.capacity();
if capacity <= Self::FLUSH_AT_CAPACITY {
tracing::trace!(
flush_at = Self::FLUSH_AT_CAPACITY,
capacity,
"at flush capacity..."
);
self.flush.trigger();
}
}
}
impl<S> Layer<S> for TasksLayer
where
S: Subscriber + for<'a> LookupSpan<'a>,
{
fn register_callsite(&self, meta: &'static Metadata<'static>) -> subscriber::Interest {
if meta.target() == "tokio::task" && meta.name() == "task" {
if meta.fields().iter().any(|f| f.name() == "function") {
let _ = self.blocking_meta.compare_exchange(
ptr::null_mut(),
meta as *const _ as *mut _,
AcqRel,
Acquire,
);
} else {
let _ = self.task_meta.compare_exchange(
ptr::null_mut(),
meta as *const _ as *mut _,
AcqRel,
Acquire,
);
}
}
self.send(Event::Metadata(meta));
subscriber::Interest::always()
}
fn new_span(&self, attrs: &span::Attributes<'_>, id: &span::Id, _: Context<'_, S>) {
let metadata = attrs.metadata();
if self.is_spawn(metadata) {
let at = SystemTime::now();
let mut fields_collector = FieldVisitor {
fields: Vec::default(),
meta_id: metadata.into(),
};
attrs.record(&mut fields_collector);
self.send(Event::Spawn {
id: id.clone(),
at,
metadata,
fields: fields_collector.fields,
});
}
}
fn on_enter(&self, id: &span::Id, cx: Context<'_, S>) {
if !self.is_id_spawned(id, &cx) {
return;
}
self.send(Event::Enter {
at: SystemTime::now(),
id: id.clone(),
});
}
fn on_exit(&self, id: &span::Id, cx: Context<'_, S>) {
if !self.is_id_spawned(id, &cx) {
return;
}
self.send(Event::Exit {
at: SystemTime::now(),
id: id.clone(),
});
}
fn on_close(&self, id: span::Id, cx: Context<'_, S>) {
if !self.is_id_spawned(&id, &cx) {
return;
}
self.send(Event::Close {
at: SystemTime::now(),
id,
});
}
}
impl Server {
// XXX(eliza): why is `SocketAddr::new` not `const`???
pub const DEFAULT_IP: IpAddr = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
pub const DEFAULT_PORT: u16 = 6669;
pub async fn serve(self) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
self.serve_with(tonic::transport::Server::default()).await
}
pub async fn serve_with(
mut self,
mut builder: tonic::transport::Server,
) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
let aggregate = self
.aggregator
.take()
.expect("cannot start server multiple times");
let aggregate = tokio::spawn(aggregate.run());
let addr = self.addr;
let res = builder
.add_service(proto::tasks::tasks_server::TasksServer::new(self))
.serve(addr)
.await;
aggregate.abort();
res.map_err(Into::into)
}
}
#[tonic::async_trait]
impl proto::tasks::tasks_server::Tasks for Server {
type WatchTasksStream =
tokio_stream::wrappers::ReceiverStream<Result<proto::tasks::TaskUpdate, tonic::Status>>;
async fn watch_tasks(
&self,
req: tonic::Request<proto::tasks::TasksRequest>,
) -> Result<tonic::Response<Self::WatchTasksStream>, tonic::Status> {
match req.remote_addr() {
Some(addr) => tracing::debug!(client.addr = %addr, "starting a new watch"),
None => tracing::debug!(client.addr = %"<unknown>", "starting a new watch"),
}
let permit = self.subscribe.reserve().await.map_err(|_| {
tonic::Status::internal("cannot start new watch, aggregation task is not running")
})?;
let (tx, rx) = mpsc::channel(self.client_buffer);
permit.send(Watch(tx));
tracing::debug!("watch started");
let stream = tokio_stream::wrappers::ReceiverStream::new(rx);
Ok(tonic::Response::new(stream))
}
}
impl Visit for FieldVisitor {
fn record_debug(&mut self, field: &field::Field, value: &dyn std::fmt::Debug) {
self.fields.push(proto::Field {
name: Some(field.name().into()),
value: Some(value.into()),
metadata_id: Some(self.meta_id.clone()),
});
}
fn record_i64(&mut self, field: &tracing_core::Field, value: i64) {
self.fields.push(proto::Field {
name: Some(field.name().into()),
value: Some(value.into()),
metadata_id: Some(self.meta_id.clone()),
});
}
fn record_u64(&mut self, field: &tracing_core::Field, value: u64) {
self.fields.push(proto::Field {
name: Some(field.name().into()),
value: Some(value.into()),
metadata_id: Some(self.meta_id.clone()),
});
}
fn record_bool(&mut self, field: &tracing_core::Field, value: bool) {
self.fields.push(proto::Field {
name: Some(field.name().into()),
value: Some(value.into()),
metadata_id: Some(self.meta_id.clone()),
});
}
fn record_str(&mut self, field: &tracing_core::Field, value: &str) {
self.fields.push(proto::Field {
name: Some(field.name().into()),
value: Some(value.into()),
metadata_id: Some(self.meta_id.clone()),
});
}
}