From 154e20484dba1f5531123049c5291516428ea9c6 Mon Sep 17 00:00:00 2001 From: Sathish <44555499+sambley@users.noreply.github.com> Date: Mon, 10 Dec 2018 22:59:38 -0800 Subject: [PATCH] Use hostname in database if env is set (#2101) --- Cargo.lock | 2 +- metrics/Cargo.toml | 2 +- metrics/src/lib.rs | 2 +- metrics/src/metrics.rs | 19 +++++++++++++++---- net/remote/remote-node.sh | 1 + 5 files changed, 19 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9a145d586..52564ac48 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2010,9 +2010,9 @@ dependencies = [ "influx_db_client 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.9.5 (registry+https://github.com/rust-lang/crates.io-index)", "solana-sdk 0.11.0", + "sys-info 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] diff --git a/metrics/Cargo.toml b/metrics/Cargo.toml index ac33c106f..05bb92f11 100644 --- a/metrics/Cargo.toml +++ b/metrics/Cargo.toml @@ -9,9 +9,9 @@ license = "Apache-2.0" [dependencies] influx_db_client = "0.3.6" log = "0.4.2" -rand = "0.6.1" reqwest = "0.9.0" lazy_static = "1.2.0" +sys-info = "0.5.6" solana-sdk = { path = "../sdk", version = "0.11.0" } [lib] diff --git a/metrics/src/lib.rs b/metrics/src/lib.rs index 8825c2899..c70c85880 100644 --- a/metrics/src/lib.rs +++ b/metrics/src/lib.rs @@ -1,11 +1,11 @@ pub extern crate influx_db_client; #[macro_use] extern crate lazy_static; -extern crate rand; extern crate reqwest; #[macro_use] extern crate log; extern crate solana_sdk; +extern crate sys_info; mod metrics; pub use metrics::flush; diff --git a/metrics/src/metrics.rs b/metrics/src/metrics.rs index 6425da17a..fa37edc8f 100644 --- a/metrics/src/metrics.rs +++ b/metrics/src/metrics.rs @@ -3,16 +3,27 @@ extern crate reqwest; use influx_db_client as influxdb; -use rand; +use solana_sdk::hash::hash; use solana_sdk::timing; use std::env; use std::sync::mpsc::{channel, Receiver, RecvTimeoutError, Sender}; use std::sync::{Arc, Barrier, Mutex, Once, ONCE_INIT}; use std::thread; use std::time::{Duration, Instant}; +use sys_info::hostname; lazy_static! { - static ref HOST_ID: i64 = rand::random::(); + static ref HOST_INFO: String = { + let v = env::var("SOLANA_METRICS_DISPLAY_HOSTNAME") + .map(|x| x.parse().unwrap_or(0)) + .unwrap_or(0); + let name: String = hostname().unwrap_or_else(|_| "".to_string()); + if v == 0 { + hash(name.as_bytes()).to_string() + } else { + name + } + }; } #[derive(Debug)] @@ -135,7 +146,7 @@ impl MetricsAgent { } pub fn submit(&self, mut point: influxdb::Point) { - point.add_field("host_id", influxdb::Value::Integer(*HOST_ID)); + point.add_field("host_id", influxdb::Value::String(HOST_INFO.to_string())); if point.timestamp.is_none() { point.timestamp = Some(timing::timestamp() as i64); } @@ -244,7 +255,7 @@ pub fn set_panic_hook(program: &'static str) { None => "?".to_string(), }), ) - .add_field("host_id", influxdb::Value::Integer(*HOST_ID)) + .add_field("host_id", influxdb::Value::String(HOST_INFO.to_string())) .to_owned(), ); // Flush metrics immediately in case the process exits immediately diff --git a/net/remote/remote-node.sh b/net/remote/remote-node.sh index 6ec43089a..371e2c8e7 100755 --- a/net/remote/remote-node.sh +++ b/net/remote/remote-node.sh @@ -124,6 +124,7 @@ local|tar) PATH="$HOME"/.cargo/bin:"$PATH" export USE_INSTALL=1 export RUST_LOG + export SOLANA_METRICS_DISPLAY_HOSTNAME=1 # Setup `/var/snap/solana/current` symlink so rsyncing the genesis # ledger works (reference: `net/scripts/install-rsync.sh`)