Skip to content

Commit

Permalink
Merge pull request #2 from Overmuse/SR/error_context
Browse files Browse the repository at this point in the history
add anyhow context for errors
  • Loading branch information
SebRollen authored Jun 21, 2021
2 parents 28f0b51 + bc69576 commit bcf3580
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "reporting"
version = "0.1.2"
version = "0.1.3"
authors = ["Sebastian Rollen <[email protected]>"]
edition = "2018"

Expand Down
25 changes: 17 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use alpaca::AlpacaMessage;
use anyhow::Result;
use anyhow::{Context, Result};
use dogstatsd::Client;
use futures::StreamExt;
use kafka_settings::consumer;
Expand All @@ -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")?;
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use alpaca::Order;
use anyhow::Result;
use anyhow::{Context, Result};
use dogstatsd::Metric;

pub trait ToMetrics {
Expand All @@ -9,10 +9,16 @@ pub trait ToMetrics {
impl ToMetrics for Order {
fn to_metrics(&self) -> Result<Vec<Metric>> {
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)
}
}

0 comments on commit bcf3580

Please sign in to comment.