Skip to content

Commit 45b78a8

Browse files
committed
Add support for tokio-vsock
1 parent 7e7b043 commit 45b78a8

File tree

5 files changed

+101
-4
lines changed

5 files changed

+101
-4
lines changed

Cargo.lock

Lines changed: 53 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

console-subscriber/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ default = ["env-filter"]
2929
parking_lot = ["dep:parking_lot", "tracing-subscriber/parking_lot"]
3030
env-filter = ["tracing-subscriber/env-filter"]
3131
grpc-web = ["dep:tonic-web"]
32+
vsock = ["dep:tokio-vsock"]
3233

3334
[dependencies]
3435
crossbeam-utils = "0.8.7"
@@ -56,6 +57,9 @@ crossbeam-channel = "0.5"
5657
# Only for the web feature:
5758
tonic-web = { version = "0.12", optional = true }
5859

60+
# Only for the vsock feature:
61+
tokio-vsock = { version = "0.7", optional = true }
62+
5963
[dev-dependencies]
6064
tokio = { version = "1.34", features = ["full", "rt-multi-thread"] }
6165
tower = { version = "0.4.12", default-features = false, features = ["util"] }

console-subscriber/src/builder.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,11 @@ impl Builder {
152152
/// before falling back on constructing a socket address from those
153153
/// defaults.
154154
///
155-
/// The socket address can be either a TCP socket address or a
156-
/// [Unix domain socket] (UDS) address. Unix domain sockets are only
157-
/// supported on Unix-compatible operating systems, such as Linux, BSDs,
158-
/// and macOS.
155+
/// The socket address can be either a TCP socket address, a
156+
/// [Unix domain socket] (UDS) address, or a [Vsock] address.
157+
/// Unix domain sockets are only supported on Unix-compatible operating systems,
158+
/// such as Linux, BSDs, and macOS. Vsock addresses are only available when the
159+
/// "vsock" feature is enabled and are supported on platforms with vsock capability.
159160
///
160161
/// Each call to this method will overwrite the previously set value.
161162
///
@@ -180,9 +181,18 @@ impl Builder {
180181
/// #[cfg(unix)]
181182
/// let builder = Builder::default().server_addr(Path::new("/tmp/tokio-console"));
182183
/// ```
184+
///
185+
/// Connect using a vsock connection (requires the "vsock" feature):
186+
///
187+
/// ```
188+
/// # use console_subscriber::Builder;
189+
/// # #[cfg(feature = "vsock")]
190+
/// let builder = Builder::default().server_addr((tokio_vsock::VMADDR_CID_ANY, 6669));
191+
/// ```
183192
///
184193
/// [environment variable]: `Builder::with_default_env`
185194
/// [Unix domain socket]: https://en.wikipedia.org/wiki/Unix_domain_socket
195+
/// [Vsock]: https://docs.rs/tokio-vsock/latest/tokio_vsock/
186196
pub fn server_addr(self, server_addr: impl Into<ServerAddr>) -> Self {
187197
Self {
188198
server_addr: server_addr.into(),
@@ -574,6 +584,9 @@ pub enum ServerAddr {
574584
/// A Unix socket address.
575585
#[cfg(unix)]
576586
Unix(PathBuf),
587+
/// A vsock address.
588+
#[cfg(feature = "vsock")]
589+
Vsock(tokio_vsock::VsockAddr),
577590
}
578591

579592
impl From<SocketAddr> for ServerAddr {
@@ -617,6 +630,13 @@ impl<'a> From<&'a Path> for ServerAddr {
617630
}
618631
}
619632

633+
#[cfg(feature = "vsock")]
634+
impl From<tokio_vsock::VsockAddr> for ServerAddr {
635+
fn from(addr: tokio_vsock::VsockAddr) -> ServerAddr {
636+
ServerAddr::Vsock(addr)
637+
}
638+
}
639+
620640
/// Initializes the console [tracing `Subscriber`][sub] and starts the console
621641
/// subscriber [`Server`] on its own background thread.
622642
///

console-subscriber/src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
use console_api as proto;
33
use proto::{instrument::instrument_server::InstrumentServer, resources::resource};
44
use serde::Serialize;
5+
#[cfg(feature = "vsock")]
6+
use tokio_vsock::VsockListener;
7+
58
use std::{
69
cell::RefCell,
710
fmt,
@@ -955,6 +958,12 @@ impl Server {
955958
let serve = router.serve_with_incoming(UnixListenerStream::new(incoming));
956959
spawn_named(serve, "console::serve").await
957960
}
961+
#[cfg(feature = "vsock")]
962+
ServerAddr::Vsock(addr) => {
963+
let incoming = VsockListener::bind(addr)?.incoming();
964+
let serve = router.serve_with_incoming(incoming);
965+
spawn_named(serve, "console::serve").await
966+
}
958967
};
959968
aggregate.abort();
960969
res?.map_err(Into::into)
@@ -1082,6 +1091,12 @@ impl Server {
10821091
let serve = router.serve_with_incoming(UnixListenerStream::new(incoming));
10831092
spawn_named(serve, "console::serve").await
10841093
}
1094+
#[cfg(feature = "vsock")]
1095+
ServerAddr::Vsock(addr) => {
1096+
let incoming = VsockListener::bind(addr)?.incoming();
1097+
let serve = router.serve_with_incoming(incoming);
1098+
spawn_named(serve, "console::serve").await
1099+
}
10851100
};
10861101
aggregate.abort();
10871102
res?.map_err(Into::into)

tokio-console/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ path-guid = "3DF6EE78-06E2-42BF-99AA-9A463E328BBD"
3232
license = false
3333
eula = false
3434

35+
[features]
36+
default = []
37+
vsock = ["dep:tokio-vsock"]
38+
3539
[dependencies]
3640
console-api = { version = "0.8.1", path = "../console-api", features = ["transport"] }
3741
clap = { version = "~4.5.4", features = ["wrap_help", "cargo", "derive", "env"] }
@@ -59,6 +63,7 @@ serde = { version = "1.0.145", features = ["derive"] }
5963
toml = "0.5"
6064
dirs = "5"
6165
hyper-util = { version = "0.1.6", features = ["tokio"] }
66+
tokio-vsock = { version = "0.7.0", optional = true }
6267

6368
[dev-dependencies]
6469
trycmd = "0.15.4"

0 commit comments

Comments
 (0)