From 9583f23b1ce4f4cbb11c3ed2b72339e4c2ab4a11 Mon Sep 17 00:00:00 2001 From: Heiko Seeberger Date: Thu, 16 Nov 2023 20:32:17 +0100 Subject: [PATCH] refactor: better main function (#37) --- hello-tracing-backend/src/main.rs | 23 +++++++++-------------- hello-tracing-common/src/lib.rs | 2 +- hello-tracing-common/src/tracing.rs | 1 + hello-tracing-gateway/src/main.rs | 23 +++++++++-------------- 4 files changed, 20 insertions(+), 29 deletions(-) diff --git a/hello-tracing-backend/src/main.rs b/hello-tracing-backend/src/main.rs index 89249bb..9259e2d 100644 --- a/hello-tracing-backend/src/main.rs +++ b/hello-tracing-backend/src/main.rs @@ -1,3 +1,5 @@ +#![feature(result_option_inspect)] + mod api; use anyhow::{Context, Result}; @@ -11,33 +13,26 @@ use std::panic; use tracing::{error, info}; #[tokio::main] -async fn main() { +async fn main() -> Result<()> { // Load configuration first, because needed for tracing initialization. - let config = match Config::load().context("load configuration") { - Ok(config) => config, - Err(error) => { - log_error(error); - return; - } - }; + let config = Config::load() + .context("load configuration") + .inspect_err(log_error)?; // If tracing initialization fails, nevertheless emit a structured log event. - if let Err(error) = init_tracing(config.tracing.clone()) { - log_error(error); - return; - } + init_tracing(config.tracing.clone()).inspect_err(log_error)?; // Replace the default panic hook with one that uses structured logging at ERROR level. panic::set_hook(Box::new(|panic| error!(%panic, "process panicked"))); // Run and log any error. - if let Err(error) = run(config).await { + run(config).await.inspect_err(|error| { error!( error = format!("{error:#}"), backtrace = %error.backtrace(), "process exited with ERROR" ); - }; + }) } #[derive(Debug, Deserialize)] diff --git a/hello-tracing-common/src/lib.rs b/hello-tracing-common/src/lib.rs index f539048..c3d691d 100644 --- a/hello-tracing-common/src/lib.rs +++ b/hello-tracing-common/src/lib.rs @@ -5,7 +5,7 @@ use time::{format_description::well_known::Rfc3339, OffsetDateTime}; pub mod otel; pub mod tracing; -pub fn log_error(error: impl Display) { +pub fn log_error(error: &impl Display) { let now = OffsetDateTime::now_utc().format(&Rfc3339).unwrap(); let error = serde_json::to_string(&json!({ "timestamp": now, diff --git a/hello-tracing-common/src/tracing.rs b/hello-tracing-common/src/tracing.rs index cb0d0a3..8a9958c 100644 --- a/hello-tracing-common/src/tracing.rs +++ b/hello-tracing-common/src/tracing.rs @@ -6,6 +6,7 @@ use opentelemetry::{ }; use opentelemetry_otlp::WithExportConfig; use serde::Deserialize; + use tracing::{error, Subscriber}; use tracing_subscriber::{ fmt, layer::SubscriberExt, registry::LookupSpan, util::SubscriberInitExt, EnvFilter, Layer, diff --git a/hello-tracing-gateway/src/main.rs b/hello-tracing-gateway/src/main.rs index d729550..9bd5b36 100644 --- a/hello-tracing-gateway/src/main.rs +++ b/hello-tracing-gateway/src/main.rs @@ -1,3 +1,5 @@ +#![feature(result_option_inspect)] + mod api; mod backend; @@ -13,33 +15,26 @@ use std::panic; use tracing::{error, info}; #[tokio::main] -async fn main() { +async fn main() -> Result<()> { // Load configuration first, because needed for tracing initialization. - let config = match Config::load().context("load configuration") { - Ok(config) => config, - Err(error) => { - log_error(error); - return; - } - }; + let config = Config::load() + .context("load configuration") + .inspect_err(log_error)?; // If tracing initialization fails, nevertheless emit a structured log event. - if let Err(error) = init_tracing(config.tracing.clone()) { - log_error(error); - return; - } + init_tracing(config.tracing.clone()).inspect_err(log_error)?; // Replace the default panic hook with one that uses structured logging at ERROR level. panic::set_hook(Box::new(|panic| error!(%panic, "process panicked"))); // Run and log any error. - if let Err(error) = run(config).await { + run(config).await.inspect_err(|error| { error!( error = format!("{error:#}"), backtrace = %error.backtrace(), "process exited with ERROR" ); - }; + }) } #[derive(Debug, Deserialize)]