@@ -38,6 +38,16 @@ pub use builder::{init, spawn};
38
38
use crate :: aggregator:: Id ;
39
39
use crate :: visitors:: { PollOpVisitor , StateUpdateVisitor } ;
40
40
41
+ /// A [`ConsoleLayer`] is a [`tracing_subscriber::Layer`] that records [`tracing`]
42
+ /// spans and events emitted by the async runtime.
43
+ ///
44
+ /// Runtimes emit [`tracing`] spans and events that represent specific operations
45
+ /// that occur in asynchronous Rust programs, such as spawning tasks and waker
46
+ /// operations. The `ConsoleLayer` collects and aggregates these events, and the
47
+ /// resulting diagnostic data is exported to clients by the corresponding gRPC
48
+ /// [`Server`] instance.
49
+ ///
50
+ /// [`tracing`]: https://docs.rs/tracing
41
51
pub struct ConsoleLayer {
42
52
current_spans : ThreadLocal < RefCell < SpanStack > > ,
43
53
tx : mpsc:: Sender < Event > ,
@@ -60,7 +70,7 @@ pub struct ConsoleLayer {
60
70
/// they might all have their own sets of waker ops.
61
71
waker_callsites : Callsites < 16 > ,
62
72
63
- /// Set of callsites for spans reprenting resources
73
+ /// Set of callsites for spans representing resources
64
74
///
65
75
/// TODO: Take some time to determine more reasonable numbers
66
76
resource_callsites : Callsites < 32 > ,
@@ -94,6 +104,17 @@ pub struct ConsoleLayer {
94
104
no_dispatch : Dispatch ,
95
105
}
96
106
107
+ /// A gRPC [`Server`] that implements the [`tokio-console` wire format][wire].
108
+ ///
109
+ /// Client applications, such as the [`tokio-console CLI][cli] connect to the gRPC
110
+ /// server, and stream data about the runtime's history (such as a list of the
111
+ /// currently active tasks, or statistics summarizing polling times). A [`Server`] also
112
+ /// interprets commands from a client application, such a request to focus in on
113
+ /// a specific task, and translates that into a stream of details specific to
114
+ /// that task.
115
+ ///
116
+ /// [wire]: https://docs.rs/console-api
117
+ /// [cli]: https://crates.io/crates/tokio-console
97
118
pub struct Server {
98
119
subscribe : mpsc:: Sender < Command > ,
99
120
addr : SocketAddr ,
@@ -209,11 +230,21 @@ enum WakeOp {
209
230
}
210
231
211
232
impl ConsoleLayer {
233
+ /// Returns a `ConsoleLayer` built with the default settings.
234
+ ///
235
+ /// Note: these defaults do *not* include values provided via the
236
+ /// environment variables specified in [`Builder::with_default_env`].
237
+ ///
238
+ /// See also [`Builder::build`].
212
239
pub fn new ( ) -> ( Self , Server ) {
213
240
Self :: builder ( ) . build ( )
214
241
}
215
242
216
243
/// Returns a [`Builder`] for configuring a `ConsoleLayer`.
244
+ ///
245
+ /// Note that the returned builder does *not* include values provided via
246
+ /// the environment variables specified in [`Builder::with_default_env`].
247
+ /// To extract those, you can call that method on the returned builder.
217
248
pub fn builder ( ) -> Builder {
218
249
Builder :: default ( )
219
250
}
@@ -272,11 +303,45 @@ impl ConsoleLayer {
272
303
}
273
304
274
305
impl ConsoleLayer {
306
+ /// Default maximum capacity for the channel of events sent from a
307
+ /// [`ConsoleLayer`] to a [`Server`].
308
+ ///
309
+ /// When this capacity is exhausted, additional events will be dropped.
310
+ /// Decreasing this value will reduce memory usage, but may result in
311
+ /// events being dropped more frequently.
312
+ ///
313
+ /// See also [`Builder::event_buffer_capacity`].
275
314
pub const DEFAULT_EVENT_BUFFER_CAPACITY : usize = 1024 * 10 ;
315
+ /// Default maximum capacity for th echannel of events sent from a
316
+ /// [`Server`] to each subscribed client.
317
+ ///
318
+ /// When this capacity is exhausted, the client is assumed to be inactive,
319
+ /// and may be disconnected.
320
+ ///
321
+ /// See also [`Builder::client_buffer_capacity`].
276
322
pub const DEFAULT_CLIENT_BUFFER_CAPACITY : usize = 1024 * 4 ;
323
+
324
+ /// Default frequency for publishing events to clients.
325
+ ///
326
+ /// Note that methods like [`init`][`crate::init`] and [`spawn`][`crate::spawn`] will take the value
327
+ /// from the `TOKIO_CONSOLE_PUBLISH_INTERVAL` [environment variable] before falling
328
+ /// back on this default.
329
+ ///
330
+ /// See also [`Builder::publish_interval`].
331
+ ///
332
+ /// [environment variable]: `Builder::with_default_env`
277
333
pub const DEFAULT_PUBLISH_INTERVAL : Duration = Duration :: from_secs ( 1 ) ;
278
334
279
335
/// By default, completed spans are retained for one hour.
336
+ ///
337
+ /// Note that methods like [`init`][`crate::init`] and
338
+ /// [`spawn`][`crate::spawn`] will take the value from the
339
+ /// `TOKIO_CONSOLE_RETENTION` [environment variable] before falling back on
340
+ /// this default.
341
+ ///
342
+ /// See also [`Builder::retention`].
343
+ ///
344
+ /// [environment variable]: `Builder::with_default_env`
280
345
pub const DEFAULT_RETENTION : Duration = Duration :: from_secs ( 60 * 60 ) ;
281
346
282
347
fn is_spawn ( & self , meta : & ' static Metadata < ' static > ) -> bool {
@@ -641,13 +706,59 @@ impl fmt::Debug for ConsoleLayer {
641
706
642
707
impl Server {
643
708
// XXX(eliza): why is `SocketAddr::new` not `const`???
709
+ /// A [`Server`] by default binds socket address 127.0.0.1 to service remote
710
+ /// procedure calls.
711
+ ///
712
+ /// Note that methods like [`init`][`crate::init`] and
713
+ /// [`spawn`][`crate::spawn`] will parse the socket address from the
714
+ /// `TOKIO_CONSOLE_BIND` [environment variable] before falling back on
715
+ /// constructing a socket address from this default.
716
+ ///
717
+ /// See also [`Builder::server_addr`].
718
+ ///
719
+ /// [environment variable]: `Builder::with_default_env`
644
720
pub const DEFAULT_IP : IpAddr = IpAddr :: V4 ( Ipv4Addr :: new ( 127 , 0 , 0 , 1 ) ) ;
721
+
722
+ /// A [`Server`] by default binds port 6669 to service remote procedure
723
+ /// calls.
724
+ ///
725
+ /// Note that methods like [`init`][`crate::init`] and
726
+ /// [`spawn`][`crate::spawn`] will parse the socket address from the
727
+ /// `TOKIO_CONSOLE_BIND` [environment variable] before falling back on
728
+ /// constructing a socket address from this default.
729
+ ///
730
+ /// See also [`Builder::server_addr`].
731
+ ///
732
+ /// [environment variable]: `Builder::with_default_env`
645
733
pub const DEFAULT_PORT : u16 = 6669 ;
646
734
735
+ /// Starts the gRPC service with the default gRPC settings.
736
+ ///
737
+ /// To configure gRPC server settings before starting the server, use
738
+ /// [`serve_with`] instead. This method is equivalent to calling [`serve_with`]
739
+ /// and providing the default gRPC server settings:
740
+ ///
741
+ /// ```rust
742
+ /// # async fn docs() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
743
+ /// # let (_, server) = console_subscriber::ConsoleLayer::new();
744
+ /// server.serve_with(tonic::transport::Server::default()).await
745
+ /// # }
746
+ /// ```
747
+ /// [`serve_with`]: Server::serve_with
647
748
pub async fn serve ( self ) -> Result < ( ) , Box < dyn std:: error:: Error + Send + Sync + ' static > > {
648
749
self . serve_with ( tonic:: transport:: Server :: default ( ) ) . await
649
750
}
650
751
752
+ /// Starts the gRPC service with the given [`tonic`] gRPC transport server
753
+ /// `builder`.
754
+ ///
755
+ /// The `builder` parameter may be used to configure gRPC-specific settings
756
+ /// prior to starting the server.
757
+ ///
758
+ /// This spawns both the server task and the event aggregation worker
759
+ /// task on the current async runtime.
760
+ ///
761
+ /// [`tonic`]: https://docs.rs/tonic/
651
762
pub async fn serve_with (
652
763
mut self ,
653
764
mut builder : tonic:: transport:: Server ,
0 commit comments