Skip to content

Commit 7d16ead

Browse files
authored
docs(subscriber): console-subscriber API docs (#198)
1 parent fbadf2f commit 7d16ead

File tree

2 files changed

+137
-3
lines changed

2 files changed

+137
-3
lines changed

console-subscriber/src/builder.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ impl Builder {
8484
/// result in the program spending more time preparing task data updates.
8585
///
8686
/// By default, this is [`ConsoleLayer::DEFAULT_PUBLISH_INTERVAL`].
87+
/// Methods like [`init`][`crate::init`] and [`spawn`][`crate::spawn`] will
88+
/// take the value from the `TOKIO_CONSOLE_PUBLISH_INTERVAL` [environment
89+
/// variable] before falling back on that default.
90+
///
91+
/// [environment variable]: `Builder::with_default_env`
8792
pub fn publish_interval(self, publish_interval: Duration) -> Self {
8893
Self {
8994
publish_interval,
@@ -98,15 +103,26 @@ impl Builder {
98103
/// will reduce memory usage, but less historical data from completed tasks
99104
/// will be retained.
100105
///
101-
/// By default, this is [`ConsoleLayer::DEFAULT_RETENTION`].
106+
/// By default, this is [`ConsoleLayer::DEFAULT_RETENTION`]. Methods
107+
/// like [`init`][`crate::init`] and [`spawn`][`crate::spawn`] will take the
108+
/// value from the `TOKIO_CONSOLE_RETENTION` [environment variable] before
109+
/// falling back on that default.
110+
///
111+
/// [environment variable]: `Builder::with_default_env`
102112
pub fn retention(self, retention: Duration) -> Self {
103113
Self { retention, ..self }
104114
}
105115

106116
/// Sets the socket address on which to serve the RPC server.
107117
///
108118
/// By default, the server is bound on the IP address [`Server::DEFAULT_IP`]
109-
/// on port [`Server::DEFAULT_PORT`].
119+
/// on port [`Server::DEFAULT_PORT`]. Methods like
120+
/// [`init`][`crate::init`] and [`spawn`][`crate::spawn`] will parse the
121+
/// socket address from the `TOKIO_CONSOLE_BIND` [environment variable]
122+
/// before falling back on constructing a socket address from those
123+
/// defaults.
124+
///
125+
/// [environment variable]: `Builder::with_default_env`
110126
pub fn server_addr(self, server_addr: impl Into<SocketAddr>) -> Self {
111127
Self {
112128
server_addr: server_addr.into(),
@@ -115,6 +131,13 @@ impl Builder {
115131
}
116132

117133
/// Sets the path to record the events to the file system.
134+
///
135+
/// By default, this is initially `None`. Methods like
136+
/// [`init`][`crate::init`] and [`spawn`][`crate::spawn`] will take the
137+
/// value from the `TOKIO_CONSOLE_RECORD_PATH` [environment variable] before
138+
/// falling back on that default.
139+
///
140+
/// [environment variable]: `Builder::with_default_env`
118141
pub fn recording_path(self, path: impl Into<PathBuf>) -> Self {
119142
Self {
120143
recording_path: Some(path.into()),

console-subscriber/src/lib.rs

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ pub use builder::{init, spawn};
3838
use crate::aggregator::Id;
3939
use crate::visitors::{PollOpVisitor, StateUpdateVisitor};
4040

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
4151
pub struct ConsoleLayer {
4252
current_spans: ThreadLocal<RefCell<SpanStack>>,
4353
tx: mpsc::Sender<Event>,
@@ -60,7 +70,7 @@ pub struct ConsoleLayer {
6070
/// they might all have their own sets of waker ops.
6171
waker_callsites: Callsites<16>,
6272

63-
/// Set of callsites for spans reprenting resources
73+
/// Set of callsites for spans representing resources
6474
///
6575
/// TODO: Take some time to determine more reasonable numbers
6676
resource_callsites: Callsites<32>,
@@ -94,6 +104,17 @@ pub struct ConsoleLayer {
94104
no_dispatch: Dispatch,
95105
}
96106

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
97118
pub struct Server {
98119
subscribe: mpsc::Sender<Command>,
99120
addr: SocketAddr,
@@ -209,11 +230,21 @@ enum WakeOp {
209230
}
210231

211232
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`].
212239
pub fn new() -> (Self, Server) {
213240
Self::builder().build()
214241
}
215242

216243
/// 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.
217248
pub fn builder() -> Builder {
218249
Builder::default()
219250
}
@@ -272,11 +303,45 @@ impl ConsoleLayer {
272303
}
273304

274305
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`].
275314
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`].
276322
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`
277333
pub const DEFAULT_PUBLISH_INTERVAL: Duration = Duration::from_secs(1);
278334

279335
/// 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`
280345
pub const DEFAULT_RETENTION: Duration = Duration::from_secs(60 * 60);
281346

282347
fn is_spawn(&self, meta: &'static Metadata<'static>) -> bool {
@@ -641,13 +706,59 @@ impl fmt::Debug for ConsoleLayer {
641706

642707
impl Server {
643708
// 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`
644720
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`
645733
pub const DEFAULT_PORT: u16 = 6669;
646734

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
647748
pub async fn serve(self) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
648749
self.serve_with(tonic::transport::Server::default()).await
649750
}
650751

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/
651762
pub async fn serve_with(
652763
mut self,
653764
mut builder: tonic::transport::Server,

0 commit comments

Comments
 (0)