Skip to content

Commit

Permalink
refactor: better main function
Browse files Browse the repository at this point in the history
  • Loading branch information
hseeberger committed Nov 16, 2023
1 parent a9e5197 commit 53c5e41
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 30 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ jobs:

- name: Install protoc
uses: arduino/setup-protoc@v2
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}

- name: Set up Rust cache
uses: Swatinem/rust-cache@v2
Expand Down
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
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[toolchain]
channel = "stable"
channel = "nightly"

0 comments on commit 53c5e41

Please sign in to comment.