Skip to content

Commit

Permalink
trickle errors on metrics setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Keksoj committed Sep 21, 2022
1 parent e3e4e23 commit 05a62f4
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ fn start(args: &cli::Args) -> Result<(), anyhow::Error> {

util::setup_logging(&config);
info!("Starting up");
util::setup_metrics(&config);
util::setup_metrics(&config).with_context(|| "Could not setup metrics")?;
util::write_pid_file(&config).with_context(|| "PID file is not writeable")?;

update_process_limits(&config)?;
Expand Down
2 changes: 1 addition & 1 deletion bin/src/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ pub fn begin_new_main_process(
let config = upgrade_data.config.clone();

util::setup_logging(&config);
util::setup_metrics(&config);
util::setup_metrics(&config).with_context(|| "Could not setup metrics")?;
//info!("new main got upgrade data: {:?}", upgrade_data);

let mut server = CommandServer::from_upgrade_data(upgrade_data)?;
Expand Down
5 changes: 3 additions & 2 deletions bin/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,16 @@ pub fn setup_logging(config: &Config) {
);
}

pub fn setup_metrics(config: &Config) {
pub fn setup_metrics(config: &Config) -> anyhow::Result<()> {
if let Some(metrics) = config.metrics.as_ref() {
metrics::setup(
return metrics::setup(
&metrics.address,
"MAIN",
metrics.tagged_metrics,
metrics.prefix.clone(),
);
}
Ok(())
}

pub fn write_pid_file(config: &Config) -> Result<(), anyhow::Error> {
Expand Down
3 changes: 2 additions & 1 deletion bin/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ pub fn begin_worker_process(
worker_id,
metrics.tagged_metrics,
metrics.prefix.clone(),
);
)
.with_context(|| "Could not setup metrics")?;
}

let mut server = Server::new_from_config(
Expand Down
14 changes: 9 additions & 5 deletions lib/src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::{
time::Instant,
};

use anyhow::Context;
use mio::net::UdpSocket;

use crate::sozu_command::proxy::{
Expand Down Expand Up @@ -116,8 +117,8 @@ pub fn setup<O: Into<String>>(
origin: O,
use_tagged_metrics: bool,
prefix: Option<String>,
) {
let metrics_socket = udp_bind();
) -> anyhow::Result<()> {
let metrics_socket = udp_bind()?;

debug!(
"setting up metrics: local address = {:#?}",
Expand All @@ -132,6 +133,7 @@ pub fn setup<O: Into<String>>(
(*metrics.borrow_mut()).set_up_origin(origin.into());
(*metrics.borrow_mut()).set_up_tagged_metrics(use_tagged_metrics);
});
Ok(())
}

pub trait Subscriber {
Expand Down Expand Up @@ -273,9 +275,11 @@ impl Write for MetricSocket {
}
}

// this should return a Result and trickle up the errors
pub fn udp_bind() -> UdpSocket {
UdpSocket::bind("0.0.0.0:0".parse().unwrap()).expect("could not parse address")
pub fn udp_bind() -> anyhow::Result<UdpSocket> {
let address = "0.0.0.0:0"
.parse()
.with_context(|| "could not parse 0.0.0.0:0")?;
UdpSocket::bind(address).with_context(|| "Could not bind to 0.0.0.0:0 udp socket")
}

#[macro_export]
Expand Down

0 comments on commit 05a62f4

Please sign in to comment.