Rework telemetry to replace the use of tracing with an object we pass around (#8143)

polkadot companion: paritytech/polkadot#2535
This commit is contained in:
Cecile Tonglet
2021-03-11 11:05:45 +01:00
committed by GitHub
parent 7aaba0c154
commit 8031b6eacb
55 changed files with 1028 additions and 838 deletions
+28 -8
View File
@@ -16,6 +16,8 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::TelemetryPayload;
use futures::channel::mpsc;
use futures::prelude::*;
use libp2p::core::transport::Transport;
use libp2p::Multiaddr;
@@ -23,7 +25,13 @@ use rand::Rng as _;
use std::{fmt, mem, pin::Pin, task::Context, task::Poll, time::Duration};
use wasm_timer::Delay;
pub(crate) type ConnectionNotifierSender = sp_utils::mpsc::TracingUnboundedSender<()>;
pub(crate) type ConnectionNotifierSender = mpsc::Sender<()>;
pub(crate) type ConnectionNotifierReceiver = mpsc::Receiver<()>;
pub(crate) fn connection_notifier_channel() -> (ConnectionNotifierSender, ConnectionNotifierReceiver)
{
mpsc::channel(0)
}
/// Handler for a single telemetry node.
///
@@ -45,7 +53,7 @@ pub(crate) struct Node<TTrans: Transport> {
/// Transport used to establish new connections.
transport: TTrans,
/// Messages that are sent when the connection (re-)establishes.
pub(crate) connection_messages: Vec<serde_json::Map<String, serde_json::Value>>,
pub(crate) connection_messages: Vec<TelemetryPayload>,
/// Notifier for when the connection (re-)establishes.
pub(crate) telemetry_connection_notifier: Vec<ConnectionNotifierSender>,
}
@@ -123,7 +131,7 @@ where
pub(crate) enum Infallible {}
impl<TTrans: Transport, TSinkErr> Sink<String> for Node<TTrans>
impl<TTrans: Transport, TSinkErr> Sink<TelemetryPayload> for Node<TTrans>
where
TTrans: Clone + Unpin,
TTrans::Dial: Unpin,
@@ -234,16 +242,28 @@ where
Poll::Ready(Ok(()))
}
fn start_send(mut self: Pin<&mut Self>, item: String) -> Result<(), Self::Error> {
fn start_send(mut self: Pin<&mut Self>, item: TelemetryPayload) -> Result<(), Self::Error> {
match &mut self.socket {
NodeSocket::Connected(conn) => {
let _ = conn.sink.start_send_unpin(item.into()).expect("boo");
}
NodeSocket::Connected(conn) => match serde_json::to_vec(&item) {
Ok(data) => {
let _ = conn.sink.start_send_unpin(data);
}
Err(err) => log::debug!(
target: "telemetry",
"Could not serialize payload: {}",
err,
),
},
_socket => {
log::trace!(
target: "telemetry",
"Message has been discarded: {}",
item,
serde_json::to_string(&item)
.unwrap_or_else(|err| format!(
"could not be serialized ({}): {:?}",
err,
item,
)),
);
}
}