Skip to content

Commit

Permalink
refactor: better main function (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
hseeberger authored Nov 16, 2023
1 parent 019b0ed commit 9583f23
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 29 deletions.
23 changes: 9 additions & 14 deletions hello-tracing-backend/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![feature(result_option_inspect)]

mod api;

use anyhow::{Context, Result};
Expand All @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion hello-tracing-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions hello-tracing-common/src/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 9 additions & 14 deletions hello-tracing-gateway/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![feature(result_option_inspect)]

mod api;
mod backend;

Expand All @@ -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)]
Expand Down

0 comments on commit 9583f23

Please sign in to comment.