From bc69576ce026c33649c7eecbe6229a4ed6f92922 Mon Sep 17 00:00:00 2001 From: Sebastian Rollen Date: Mon, 21 Jun 2021 10:32:15 -0400 Subject: [PATCH] add anyhow context for errors --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/main.rs | 25 +++++++++++++++++-------- src/types.rs | 12 +++++++++--- 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3051f2f..1ab4a70 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -976,7 +976,7 @@ dependencies = [ [[package]] name = "reporting" -version = "0.1.2" +version = "0.1.3" dependencies = [ "alpaca", "anyhow", diff --git a/Cargo.toml b/Cargo.toml index 62e7c42..35f29b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "reporting" -version = "0.1.2" +version = "0.1.3" authors = ["Sebastian Rollen "] edition = "2018" diff --git a/src/main.rs b/src/main.rs index 3c31163..bee1a04 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use alpaca::AlpacaMessage; -use anyhow::Result; +use anyhow::{Context, Result}; use dogstatsd::Client; use futures::StreamExt; use kafka_settings::consumer; @@ -20,20 +20,29 @@ async fn main() -> Result<()> { .finish(); set_global_default(subscriber).expect("Failed to set subscriber"); info!("Starting reporting"); - let settings = settings::Settings::new()?; - let consumer = consumer(&settings.kafka)?; + let settings = settings::Settings::new().context("Failed to load settings")?; + let consumer = consumer(&settings.kafka).context("Failed to create kafka consumer")?; debug!("Creating dogstatsd client"); - let client = Client::new("0.0.0.0:0", &settings.app.target_address).await?; + let client = Client::new("127.0.0.1:0", &settings.app.target_address) + .await + .context("Failed to create dogstatsd client")?; while let Some(message) = consumer.stream().next().await { - let message = message?; + let message = message.context("Error from kafka")?; if let Some(payload) = message.payload() { trace!("Received payload"); - let alpaca_message: AlpacaMessage = serde_json::from_slice(payload)?; + let alpaca_message: AlpacaMessage = + serde_json::from_slice(payload).context("Failed to deserialize alpaca message")?; if let AlpacaMessage::TradeUpdates(order_update) = alpaca_message { debug!("Received trade update, generating metrics"); - let metrics = order_update.order.to_metrics()?; + let metrics = order_update + .order + .to_metrics() + .context("Failed to convert order to metrics")?; for metric in metrics { - client.send(metric).await?; + client + .send(metric) + .await + .context("Failed to send metrics")?; } } } diff --git a/src/types.rs b/src/types.rs index a65105d..018c070 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,5 +1,5 @@ use alpaca::Order; -use anyhow::Result; +use anyhow::{Context, Result}; use dogstatsd::Metric; pub trait ToMetrics { @@ -9,10 +9,16 @@ pub trait ToMetrics { impl ToMetrics for Order { fn to_metrics(&self) -> Result> { let metrics = vec![Metric::increase("order") - .add_key_value("status", serde_plain::to_string(&self.status)?) + .add_key_value( + "status", + serde_plain::to_string(&self.status).context("Failed to serialize order status")?, + ) .add_key_value("id", &self.client_order_id) .add_key_value("ticker", &self.symbol) - .add_key_value("side", serde_plain::to_string(&self.side)?)]; + .add_key_value( + "side", + serde_plain::to_string(&self.side).context("Failed to serialize order side")?, + )]; Ok(metrics) } }