Skip to content

Commit

Permalink
Add support for connecting tokio-console to binaries (#1915)
Browse files Browse the repository at this point in the history
# Description

If compiled with `RUSTFLAGS="--cfg tokio_unstable"` we can now attact
[tokio-console](https://github.com/tokio-rs/console) to our binaries
which can help us to debug issues with debug issues with our async code

# Changes
- Adds https://github.com/tokio-rs/console/tree/main/console-subscriber
as a dependency to our observe crate
- Conditionally registers the console subscriber as an extra layer in
our tracing subscriber registry (this required some rewriting of the
existing layering)

Docs:
https://docs.rs/console-subscriber/latest/console_subscriber/#adding-the-console-subscriber

## How to test
```
RUSTFLAGS="--cfg tokio_unstable" cargo run --bin autopilot
```

In a separate console

```
cargo install --locked tokio-console
tokio-console
```

and observe tokio runtime information

<img width="1401" alt="image"
src="https://github.com/cowprotocol/services/assets/1200333/df47ff54-24df-49bf-ae29-f7df26045339">
  • Loading branch information
fleupold authored Oct 6, 2023
1 parent f9f5efd commit 8be3d0d
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 14 deletions.
153 changes: 153 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,17 @@ ANVIL_IP_ADDR=0.0.0.0 anvil \
--timestamp 1577836800
```

### Profiling

In order to attach [tokio-console](https://github.com/tokio-rs/console) to the running process, compile the binary using `RUSTFLAGS="--cfg tokio_unstable"`.

Then in another shell, simply run

```
cargo install --locked tokio-console
tokio-console
```

## Running the Services Locally

### Prerequisites
Expand Down
1 change: 1 addition & 0 deletions crates/observe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ license = "MIT OR Apache-2.0"

[dependencies]
atty = "0.2"
console-subscriber = "0.2.0"
once_cell = { workspace = true }
prometheus = { workspace = true }
prometheus-metric-storage = { workspace = true }
Expand Down
41 changes: 27 additions & 14 deletions crates/observe/src/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ use {
std::{panic::PanicInfo, sync::Once},
time::macros::format_description,
tracing::level_filters::LevelFilter,
tracing_subscriber::fmt::{time::UtcTime, writer::MakeWriterExt as _},
tracing_subscriber::{
fmt::{time::UtcTime, writer::MakeWriterExt as _},
prelude::*,
util::SubscriberInitExt,
EnvFilter,
Layer,
},
};

/// Initializes tracing setup that is shared between the binaries.
Expand All @@ -25,22 +31,29 @@ pub fn initialize_reentrant(env_filter: &str) {
}

fn set_tracing_subscriber(env_filter: &str, stderr_threshold: LevelFilter) {
// This is what kibana uses to separate multi line log messages.
let subscriber_builder = tracing_subscriber::fmt::fmt()
let fmt_layer = tracing_subscriber::fmt::layer()
.with_writer(
std::io::stdout
.with_min_level(
stderr_threshold
.into_level()
.unwrap_or(tracing::Level::ERROR),
)
.or_else(std::io::stderr),
)
.with_timer(UtcTime::new(format_description!(
// This is what kibana uses to separate multi line log messages.
"[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond digits:3]Z"
)))
.with_env_filter(env_filter)
.with_ansi(atty::is(atty::Stream::Stdout));
match stderr_threshold.into_level() {
Some(threshold) => subscriber_builder
.with_writer(
std::io::stderr
.with_max_level(threshold)
.or_else(std::io::stdout),
)
.init(),
None => subscriber_builder.init(),
.with_ansi(atty::is(atty::Stream::Stdout))
.with_filter::<EnvFilter>(env_filter.into());

let registry = tracing_subscriber::registry().with(fmt_layer);
if cfg!(tokio_unstable) {
// Start with tokio_console subscriber
registry.with(console_subscriber::spawn()).init();
} else {
registry.init()
}
}

Expand Down

0 comments on commit 8be3d0d

Please sign in to comment.