Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: better main function #37

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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