|
| 1 | +// Copyright 2017 Parity Technologies (UK) Ltd. |
| 2 | +// This file is part of Polkadot. |
| 3 | + |
| 4 | +// Polkadot is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | + |
| 9 | +// Polkadot is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | + |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with Polkadot. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +//! Telemtetry utils. |
| 18 | +//! |
| 19 | +//! `telemetry` macro be used from whereever in the Polkadot codebase |
| 20 | +//! in order to send real-time logging information to the telemetry |
| 21 | +//! server (if there is one). We use the async drain adapter of `slog` |
| 22 | +//! so that the logging thread doesn't get held up at all. |
| 23 | +
|
| 24 | +extern crate parking_lot; |
| 25 | +extern crate websocket as ws; |
| 26 | +extern crate slog_async; |
| 27 | +extern crate slog_json; |
| 28 | +#[macro_use(o, kv)] |
| 29 | +extern crate slog; |
| 30 | +extern crate slog_scope; |
| 31 | + |
| 32 | +use std::io; |
| 33 | +use parking_lot::Mutex; |
| 34 | +use slog::Drain; |
| 35 | +pub use slog_scope::with_logger; |
| 36 | + |
| 37 | +/// Configuration for telemetry. |
| 38 | +pub struct TelemetryConfig { |
| 39 | + /// URL of the telemetry WS server. |
| 40 | + pub url: String, |
| 41 | + /// What do do when we connect to the server. |
| 42 | + pub on_connect: Box<Fn() + Send + 'static>, |
| 43 | +} |
| 44 | + |
| 45 | +/// Initialise telemetry. |
| 46 | +pub fn init_telemetry(config: TelemetryConfig) -> slog_scope::GlobalLoggerGuard { |
| 47 | + let log = slog::Logger::root( |
| 48 | + slog_async::Async::new( |
| 49 | + slog_json::Json::default( |
| 50 | + TelemetryWriter { |
| 51 | + buffer: vec![], |
| 52 | + out: Mutex::new( |
| 53 | + ws::ClientBuilder::new(&config.url).ok().and_then(|mut x| x.connect(None).ok()) |
| 54 | + ), |
| 55 | + config, |
| 56 | + first_time: true, // ensures that on_connect will be called. |
| 57 | + } |
| 58 | + ).fuse() |
| 59 | + ).build().fuse(), o!() |
| 60 | + ); |
| 61 | + slog_scope::set_global_logger(log) |
| 62 | +} |
| 63 | + |
| 64 | +/// Exactly equivalent to `slog_scope::info`, provided as a convenience. |
| 65 | +#[macro_export] |
| 66 | +macro_rules! telemetry { |
| 67 | + ( $($t:tt)* ) => { $crate::with_logger(|l| slog_info!(l, $($t)* )) } |
| 68 | +} |
| 69 | + |
| 70 | +struct TelemetryWriter { |
| 71 | + buffer: Vec<u8>, |
| 72 | + out: Mutex<Option<ws::sync::Client<Box<ws::stream::sync::NetworkStream + Send>>>>, |
| 73 | + config: TelemetryConfig, |
| 74 | + first_time: bool, |
| 75 | +} |
| 76 | + |
| 77 | +impl TelemetryWriter { |
| 78 | + fn ensure_connected(&mut self) { |
| 79 | + if self.first_time { |
| 80 | + (self.config.on_connect)(); |
| 81 | + self.first_time = false; |
| 82 | + } |
| 83 | + let mut client = self.out.lock(); |
| 84 | + if client.is_none() { |
| 85 | + *client = ws::ClientBuilder::new(&self.config.url).ok().and_then(|mut x| x.connect(None).ok()); |
| 86 | + drop(client); |
| 87 | + (self.config.on_connect)(); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +impl io::Write for TelemetryWriter { |
| 93 | + fn write(&mut self, msg: &[u8]) -> io::Result<usize> { |
| 94 | + if msg == b"\n" { |
| 95 | + let _ = self.flush(); |
| 96 | + } else { |
| 97 | + self.buffer.extend_from_slice(msg); |
| 98 | + } |
| 99 | + Ok(msg.len()) |
| 100 | + } |
| 101 | + |
| 102 | + fn flush(&mut self) -> io::Result<()> { |
| 103 | + self.ensure_connected(); |
| 104 | + if if let Some(ref mut socket) = *self.out.lock() { |
| 105 | + if let Ok(s) = ::std::str::from_utf8(&self.buffer[..]) { |
| 106 | + socket.send_message(&ws::Message::text(s)).is_err() |
| 107 | + } else { false } |
| 108 | + } else { false } { |
| 109 | + *self.out.lock() = None; |
| 110 | + } |
| 111 | + self.buffer.clear(); |
| 112 | + Ok(()) |
| 113 | + } |
| 114 | +} |
0 commit comments