Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix tracing spans are not being forwarded to spawned task
There is a bug that tracing spans are not forwarded to spawned task. The
problem was that only the telemetry span was forwarded. The solution to
this is to use the tracing provided `in_current_span` to capture the
current active span and pass the telemetry span explictely. We will now
always enter the span when the future is polled. This is essentially the
same strategy as tracing is doing with its `Instrumented`, but now
extended for our use case with having multiple spans active.
  • Loading branch information
bkchr committed Jan 29, 2021
commit 0f2dfdde4d71b6df8bd9b6c3992f033cbf49fcb2
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions client/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,4 @@ grandpa = { version = "0.8.0", package = "sc-finality-grandpa", path = "../final
grandpa-primitives = { version = "2.0.0", package = "sp-finality-grandpa", path = "../../primitives/finality-grandpa" }
tokio = { version = "0.2", default-features = false }
async-std = { version = "1.6.5", default-features = false }
tracing-subscriber = "0.2.15"
42 changes: 37 additions & 5 deletions client/service/src/task_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use log::{debug, error};
use futures::{
Future, FutureExt, StreamExt,
future::{select, Either, BoxFuture, join_all, try_join_all, pending},
sink::SinkExt,
sink::SinkExt, task::{Context, Poll},
};
use prometheus_endpoint::{
exponential_buckets, register,
Expand All @@ -40,6 +40,37 @@ mod prometheus_future;
#[cfg(test)]
mod tests;

/// A wrapper around a `[Option<TelemetrySpan>]` and a [`Future`].
///
/// The telemetry in Substrate uses a span to identify the telemetry context. The span "infrastructure"
/// is provided by the tracing-crate. Now it is possible to have your own spans as well. To support
/// this with the [`TaskManager`] we have this wrapper. This wrapper enters the telemetry span every
/// time the future is polled and polls the inner future. So, the inner future can still have its
/// own span attached and we get our telemetry span ;)
struct WithTelemetrySpan<T> {
span: Option<TelemetrySpan>,
inner: T,
}

impl<T> WithTelemetrySpan<T> {
fn new(span: Option<TelemetrySpan>, inner: T) -> Self {
Self {
span,
inner,
}
}
}

impl<T: Future<Output = ()> + Unpin> Future for WithTelemetrySpan<T> {
type Output = ();

fn poll(mut self: Pin<&mut Self>, ctx: &mut Context) -> Poll<Self::Output> {
let span = self.span.clone();
let _enter = span.as_ref().map(|s| s.enter());
Pin::new(&mut self.inner).poll(ctx)
}
}

/// An handle for spawning tasks in the service.
#[derive(Clone)]
pub struct SpawnTaskHandle {
Expand Down Expand Up @@ -124,10 +155,11 @@ impl SpawnTaskHandle {
}
};

let join_handle = {
let _span = self.telemetry_span.as_ref().map(|s| s.enter());
self.executor.spawn(Box::pin(future.in_current_span()), task_type)
};
let future = future.in_current_span().boxed();
let join_handle = self.executor.spawn(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you explore instrumenting the executor itself? Not sure if that would work, but if it did maybe it could perhaps save some busywork?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the end that is what we are doing here. We instrument the executor :D

The TaskExecutor itself is just a Fn(Future, Type)

WithTelemetrySpan::new(self.telemetry_span.clone(), future).boxed(),
task_type,
);

let mut task_notifier = self.task_notifier.clone();
self.executor.spawn(
Expand Down
87 changes: 84 additions & 3 deletions client/service/src/task_manager/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ use crate::config::TaskExecutor;
use crate::task_manager::TaskManager;
use futures::{future::FutureExt, pin_mut, select};
use parking_lot::Mutex;
use std::any::Any;
use std::sync::Arc;
use std::time::Duration;
use std::{any::Any, sync::Arc, time::Duration};
use tracing_subscriber::{layer::{SubscriberExt, Context}, Layer};
use tracing::{subscriber::Subscriber, span::{Attributes, Id, Record}, event::Event};
use sc_telemetry::TelemetrySpan;

#[derive(Clone, Debug)]
struct DropTester(Arc<Mutex<usize>>);
Expand Down Expand Up @@ -312,3 +313,83 @@ fn ensure_task_manager_future_continues_when_childs_not_essential_task_fails() {
runtime.block_on(task_manager.clean_shutdown());
assert_eq!(drop_tester, 0);
}

struct TestLayer {
spans_entered: Arc<Mutex<Vec<String>>>,
spans: Arc<Mutex<std::collections::HashMap<Id, String>>>,
}

impl<S: Subscriber> Layer<S> for TestLayer {
fn new_span(&self, attrs: &Attributes<'_>, id: &Id, _ctx: Context<S>) {
self.spans.lock().insert(id.clone(), attrs.metadata().name().to_string());
}

fn on_record(&self, _: &Id, _: &Record<'_>, _: Context<S>) {}

fn on_event(&self, _: &Event<'_>, _: Context<S>) {}

fn on_enter(&self, span: &Id, _: Context<S>) {
let name = self.spans.lock().get(span).unwrap().clone();
self.spans_entered.lock().push(name);
}

fn on_exit(&self, _: &Id, _: Context<S>) {}

fn on_close(&self, _: Id, _: Context<S>) {}
}

type TestSubscriber = tracing_subscriber::layer::Layered<
TestLayer,
tracing_subscriber::fmt::Subscriber
>;

fn setup_subscriber() -> (
TestSubscriber,
Arc<Mutex<Vec<String>>>,
) {
let spans_entered = Arc::new(Mutex::new(Default::default()));
let layer = TestLayer {
spans: Arc::new(Mutex::new(Default::default())),
spans_entered: spans_entered.clone(),
};
let subscriber = tracing_subscriber::fmt().finish().with(layer);
(subscriber, spans_entered)
}

#[test]
fn telemetry_span_is_forwarded_to_task() {
let (subscriber, spans_entered) = setup_subscriber();
let _sub_guard = tracing::subscriber::set_default(subscriber);

let telemetry_span = TelemetrySpan::new();

let span = tracing::info_span!("test");
let _enter = span.enter();

let mut runtime = tokio::runtime::Runtime::new().unwrap();
let handle = runtime.handle().clone();
let task_executor = TaskExecutor::from(move |fut, _| handle.spawn(fut).map(|_| ()));
let task_manager = TaskManager::new(task_executor, None, Some(telemetry_span.clone())).unwrap();

let (sender, receiver) = futures::channel::oneshot::channel();

task_manager.spawn_handle().spawn(
"test",
async move {
sender.send(()).unwrap();
}.boxed(),
);

runtime.block_on(receiver).unwrap();
runtime.block_on(task_manager.clean_shutdown());
drop(runtime);

let spans = spans_entered.lock();
// We entered the telemetry span and the "test" in the future and the "test" span outside
// of the future. So, we should have recorded 3 spans.
assert_eq!(3, spans.len());

assert_eq!(spans[0], "test");
assert_eq!(spans[1], telemetry_span.span().metadata().unwrap().name());
assert_eq!(spans[2], "test");
}