Lesson 7 of 9 · 23m
Observing Async Systems: Tracing, Metrics, and tokio-console
Instrument tasks, spans, queue depth, and scheduler behavior so stalls are diagnosed from evidence rather than restarted away.
Most async incidents are invisible in ordinary logs because the interesting event is the absence of progress. A task is parked, a queue is full, a mutex is held, or workers are busy doing something that was supposed to be rare. You need instrumentation that shows waiting as clearly as it shows work.
Spans should describe units of work
tracing integrates naturally with async Rust, but only if spans are attached deliberately:
use tracing::Instrument;
async fn process(job: Job) {
let span = tracing::info_span!("process_job", job_id = job.id);
process_inner(job).instrument(span).await;
}Without .instrument, the future may execute outside the span context you expected. Include stable identifiers such as job ID, tenant ID, or shard ID, but avoid high-cardinality free-form payloads.
Measure queue depth and age, not only throughput
Throughput can look healthy while the oldest job is minutes old. The minimum useful queue metrics are:
queue_depth current items waiting
oldest_item_age_seconds time since oldest item entered
enqueue_total attempted admissions
dropped_total admissions rejected or shed
process_duration_seconds consumer execution timeWhen a downstream dependency slows, queue_depth and oldest_item_age_seconds move before users complain. They also tell you whether backpressure is propagating as designed.
tokio-console is for task-level truth
Tokio's unstable console instrumentation can show tasks, poll counts, wakeups, and parking behavior. A typical setup uses the console-subscriber crate during development or controlled production diagnostics:
console_subscriber::init();Then run the console CLI against the configured port. Treat this as a diagnostic capability, not a free always-on feature. It can expose internal task names and metadata, so bind it carefully and consider feature-gating it.
Correlate runtime evidence with OS evidence
A stalled async service often requires three views:
top -H -p "$PID"
pidstat -t -p "$PID" 1
RUST_LOG=info,target_crate=debug cargo runThread CPU tells you whether workers are busy or idle. Runtime metrics tell you whether tasks are waiting. Application spans tell you where they are waiting. If all worker threads are idle and queues are full, suspect lock or channel blockage. If all worker threads are hot, suspect synchronous CPU or lock spinning. If both look calm, suspect a downstream dependency.
Field notes
- High-cardinality tracing fields can become a metrics outage of their own.
- Instrument the rejection path. Silent load shedding is indistinguishable from healthy low traffic.
- Do not expose diagnostic ports broadly; task metadata can leak tenant or shard information.
- Capture evidence before restarting. A reboot is often the destruction of the only useful reproduction.
Was this lesson helpful?