Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add host parameter to telemetry config #1033

Merged
merged 3 commits into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ log_level = 'info'

[telemetry]
enabled = true
host = '127.0.0.1'
port = 3001

[[chains]]
Expand Down
30 changes: 22 additions & 8 deletions relayer-cli/src/commands/start.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::error::Error;

use abscissa_core::{Command, Options, Runnable};

use ibc_relayer::config::Config;
Expand All @@ -13,27 +15,39 @@ impl Runnable for StartCmd {
fn run(&self) {
let config = app_config();

let supervisor = spawn_supervisor(config.clone());
match supervisor.run() {
match spawn_supervisor(config.clone()).and_then(|s| s.run()) {
Ok(()) => Output::success_msg("done").exit(),
Err(e) => Output::error(e).exit(),
Err(e) => Output::error(format!("Hermes failed to start, last error: {}", e)).exit(),
}
}
}

#[cfg(feature = "telemetry")]
fn spawn_supervisor(config: Config) -> Supervisor {
fn spawn_supervisor(config: Config) -> Result<Supervisor, Box<dyn Error + Send + Sync>> {
let state = ibc_telemetry::new_state();

if config.telemetry.enabled {
ibc_telemetry::spawn(config.telemetry.port, state.clone());
let address = (config.telemetry.host.clone(), config.telemetry.port);

match ibc_telemetry::spawn(address, state.clone()) {
Ok((addr, _)) => {
info!(
"telemetry service running, exposing metrics at {}/metrics",
addr
);
}
Err(e) => {
error!("telemetry service failed to start: {}", e);
return Err(e);
}
}
}

Supervisor::spawn(config, state)
Ok(Supervisor::spawn(config, state))
}

#[cfg(not(feature = "telemetry"))]
fn spawn_supervisor(config: Config) -> Supervisor {
fn spawn_supervisor(config: Config) -> Result<Supervisor, Box<dyn Error + Send + Sync>> {
if config.telemetry.enabled {
warn!(
"telemetry enabled in the config but Hermes was built without telemetry support, \
Expand All @@ -42,5 +56,5 @@ fn spawn_supervisor(config: Config) -> Supervisor {
}

let telemetry = ibc_relayer::telemetry::TelemetryDisabled;
Supervisor::spawn(config, telemetry)
Ok(Supervisor::spawn(config, telemetry))
}
2 changes: 2 additions & 0 deletions relayer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,15 @@ impl Default for GlobalConfig {
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct TelemetryConfig {
pub enabled: bool,
pub host: String,
pub port: u16,
}

impl Default for TelemetryConfig {
fn default() -> Self {
Self {
enabled: false,
host: "127.0.0.1".to_string(),
port: 3001,
}
}
Expand Down
27 changes: 24 additions & 3 deletions telemetry/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
pub mod server;
pub mod state;

use std::{sync::Arc, thread::JoinHandle};
use std::{
error::Error,
net::{SocketAddr, ToSocketAddrs},
sync::Arc,
thread::JoinHandle,
};

pub use crate::state::TelemetryState;

pub fn new_state() -> Arc<TelemetryState> {
Arc::new(TelemetryState::default())
}

pub fn spawn(port: u16, state: Arc<TelemetryState>) -> JoinHandle<()> {
std::thread::spawn(move || server::run(state, port))
pub fn spawn<A>(
address: A,
state: Arc<TelemetryState>,
) -> Result<(SocketAddr, JoinHandle<()>), Box<dyn Error + Send + Sync>>
where
A: ToSocketAddrs + Send + 'static,
{
let server = server::listen(address, state);

match server {
Ok(server) => {
let address = server.server_addr();
let handle = std::thread::spawn(move || server.run());

Ok((address, handle))
}
Err(e) => Err(e),
}
}
15 changes: 10 additions & 5 deletions telemetry/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;
use std::{error::Error, net::ToSocketAddrs, sync::Arc};

use prometheus::{Encoder, TextEncoder};
use rouille::Request;
use rouille::{Request, Response, Server};

use crate::state::TelemetryState;

Expand All @@ -20,8 +20,11 @@ impl Route {
}
}

pub fn run(telemetry_state: Arc<TelemetryState>, port: u16) {
rouille::start_server(("localhost", port), move |request| {
pub fn listen(
address: impl ToSocketAddrs,
telemetry_state: Arc<TelemetryState>,
) -> Result<Server<impl Fn(&Request) -> Response>, Box<dyn Error + Send + Sync>> {
let server = Server::new(address, move |request| {
match Route::from_request(request) {
// The prometheus endpoint
Route::Metrics => {
Expand All @@ -37,5 +40,7 @@ pub fn run(telemetry_state: Arc<TelemetryState>, port: u16) {
// Return an empty response with a 404 status code.
Route::Other => rouille::Response::empty_404(),
}
})
})?;

Ok(server)
}