Fix telemetry span not entering properly attempt 3 (#8043)

* Fix tracing tests (#8022)

* Fix tracing tests

The tests were not working properly.

1. Some test was setting a global subscriber, this could lead to racy
conditions with other tests.

2. A logging test called `process::exit` which is completly wrong.

* Update client/tracing/src/lib.rs

Co-authored-by: David <dvdplm@gmail.com>

* Review comments

Co-authored-by: David <dvdplm@gmail.com>

* Fix tracing spans are not being forwarded to spawned task (#8009)

* 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.

* More tests

* Proper test for telemetry and prefix span

* WIP

* Fix test (need to create & enter the span at the same time)

* WIP

* Remove telemtry_span from sc_service config

* CLEANUP

* Update comment

* Incorrect indent

* More meaningful name

* Dedent

* Naming XD

* Attempt to make a more complete test

* lint

* Missing licenses

* Remove user data

* CLEANUP

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* CLEANUP

* Apply suggestion

* Update bin/node/cli/tests/telemetry.rs

Co-authored-by: David <dvdplm@gmail.com>

* Wrapping lines

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
Co-authored-by: David <dvdplm@gmail.com>
This commit is contained in:
Cecile Tonglet
2021-02-17 08:44:25 +01:00
committed by GitHub
parent f35a27cca0
commit 27274c42cf
14 changed files with 515 additions and 137 deletions
@@ -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, task::{Context, Poll},
sink::SinkExt,
};
use prometheus_endpoint::{
exponential_buckets, register,
@@ -34,43 +34,11 @@ use prometheus_endpoint::{
use sp_utils::mpsc::{TracingUnboundedSender, TracingUnboundedReceiver, tracing_unbounded};
use tracing_futures::Instrument;
use crate::{config::{TaskExecutor, TaskType, JoinFuture}, Error};
use sc_telemetry::TelemetrySpan;
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 {
@@ -78,7 +46,6 @@ pub struct SpawnTaskHandle {
executor: TaskExecutor,
metrics: Option<Metrics>,
task_notifier: TracingUnboundedSender<JoinFuture>,
telemetry_span: Option<TelemetrySpan>,
}
impl SpawnTaskHandle {
@@ -155,11 +122,7 @@ impl SpawnTaskHandle {
}
};
let future = future.in_current_span().boxed();
let join_handle = self.executor.spawn(
WithTelemetrySpan::new(self.telemetry_span.clone(), future).boxed(),
task_type,
);
let join_handle = self.executor.spawn(future.in_current_span().boxed(), task_type);
let mut task_notifier = self.task_notifier.clone();
self.executor.spawn(
@@ -266,8 +229,6 @@ pub struct TaskManager {
/// terminates and gracefully shutdown. Also ends the parent `future()` if a child's essential
/// task fails.
children: Vec<TaskManager>,
/// A `TelemetrySpan` used to enter the telemetry span when a task is spawned.
telemetry_span: Option<TelemetrySpan>,
}
impl TaskManager {
@@ -276,7 +237,6 @@ impl TaskManager {
pub(super) fn new(
executor: TaskExecutor,
prometheus_registry: Option<&Registry>,
telemetry_span: Option<TelemetrySpan>,
) -> Result<Self, PrometheusError> {
let (signal, on_exit) = exit_future::signal();
@@ -305,7 +265,6 @@ impl TaskManager {
task_notifier,
completion_future,
children: Vec::new(),
telemetry_span,
})
}
@@ -316,7 +275,6 @@ impl TaskManager {
executor: self.executor.clone(),
metrics: self.metrics.clone(),
task_notifier: self.task_notifier.clone(),
telemetry_span: self.telemetry_span.clone(),
}
}