Skip to content

Commit

Permalink
Move telemetry duplicated code into new lib crate
Browse files Browse the repository at this point in the history
  • Loading branch information
SpartanPlume committed Sep 19, 2024
1 parent 4b7e933 commit 2d9a57b
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 54 deletions.
19 changes: 13 additions & 6 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
members = [
"crates/libs/core",
"crates/libs/config",
"crates/libs/telemetry",
"crates/services/api",
"crates/services/web",
]
Expand Down
15 changes: 15 additions & 0 deletions crates/libs/telemetry/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "tosurnament-telemetry"
version = "0.1.0"
authors = ["Spartan Plume <[email protected]>"]
edition = "2021"

[lib]
path = "src/lib.rs"

[dependencies]
tracing = { version = "0.1", features = ["log"] }
tracing-subscriber = { version = "0.3", features = ["registry", "env-filter"] }
tracing-bunyan-formatter = "0.3"
tracing-log = "0.2"
thiserror = "1.0"
7 changes: 7 additions & 0 deletions crates/libs/telemetry/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[derive(Debug, thiserror::Error)]
pub enum TelemetryError {
#[error(transparent)]
InitializationError(#[from] tracing_log::log_tracer::SetLoggerError),
#[error(transparent)]
SetGlobalDefaultError(#[from] tracing::subscriber::SetGlobalDefaultError),
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
mod error;

use tracing::subscriber::set_global_default;
use tracing::Subscriber;
use tracing_bunyan_formatter::{BunyanFormattingLayer, JsonStorageLayer};
use tracing_log::LogTracer;
use tracing_subscriber::{fmt::MakeWriter, layer::SubscriberExt, EnvFilter, Registry};

pub use error::TelemetryError;

pub fn get_subscriber<Sink>(
name: String,
env_filter: String,
Expand All @@ -21,7 +25,8 @@ where
.with(formatting_layer)
}

pub fn init_subscriber(subscriber: impl Subscriber + Send + Sync) {
LogTracer::init().expect("Failed to set logger");
set_global_default(subscriber).expect("Failed to set subscriber");
pub fn init_subscriber(subscriber: impl Subscriber + Send + Sync) -> Result<(), TelemetryError> {
LogTracer::init()?;
set_global_default(subscriber)?;
Ok(())
}
4 changes: 1 addition & 3 deletions crates/services/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
tower = "0.4"
tower-http = { version = "0.5", features = ["fs", "trace"] }
tracing = { version = "0.1", features = ["log"] }
tracing-subscriber = { version = "0.3", features = ["registry", "env-filter"] }
tracing-bunyan-formatter = "0.3"
tracing-log = "0.2"
tosurnament-core = { path = "../../libs/core" }
tosurnament-config = { path = "../../libs/config" }
tosurnament-telemetry = { path = "../../libs/telemetry" }

[dev-dependencies]
once_cell = "1"
Expand Down
1 change: 0 additions & 1 deletion crates/services/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ pub mod prelude;
pub mod routes;
pub mod server_error;
pub mod startup;
pub mod telemetry;
4 changes: 2 additions & 2 deletions crates/services/api/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use tosurnament_api::startup::Application;
use tosurnament_api::telemetry::{get_subscriber, init_subscriber};
use tosurnament_config::get_config;
use tosurnament_telemetry::{get_subscriber, init_subscriber};

#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
let subscriber = get_subscriber("tosurnament".into(), "info".into(), std::io::stdout);
init_subscriber(subscriber);
init_subscriber(subscriber).expect("Failed to initialize subscriber");

let config = get_config().expect("Failed to read config");
let application = Application::build(config).await?;
Expand Down
6 changes: 3 additions & 3 deletions crates/services/api/tests/api/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use sqlx::{Connection, Executor, PgConnection, PgPool};
use tosurnament_api::config::{Config, DatabaseConfig};
use tosurnament_api::context::Context;
use tosurnament_api::startup::Application;
use tosurnament_api::telemetry::{get_subscriber, init_subscriber};
use tosurnament_config::get_config;
use tosurnament_core::MIGRATOR;
use tosurnament_telemetry::{get_subscriber, init_subscriber};

pub struct TestApp {
pub address: String,
Expand Down Expand Up @@ -41,10 +41,10 @@ static TRACING: Lazy<()> = Lazy::new(|| {
let subscriber_name = "tosurnament".to_string();
if std::env::var("TEST_LOG").is_ok() {
let subscriber = get_subscriber(subscriber_name, default_filter_level, std::io::stdout);
init_subscriber(subscriber);
init_subscriber(subscriber).expect("Failed to initialize subscriber");
} else {
let subscriber = get_subscriber(subscriber_name, default_filter_level, std::io::sink);
init_subscriber(subscriber);
init_subscriber(subscriber).expect("Failed to initialize subscriber");
}
});

Expand Down
4 changes: 1 addition & 3 deletions crates/services/web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
tower = "0.4"
tower-http = { version = "0.5", features = ["fs", "trace"] }
tracing = { version = "0.1", features = ["log"] }
tracing-subscriber = { version = "0.3", features = ["registry", "env-filter"] }
tracing-bunyan-formatter = "0.3"
tracing-log = "0.2"
tosurnament-core = { path = "../../libs/core" }
tosurnament-config = { path = "../../libs/config" }
tosurnament-telemetry = { path = "../../libs/telemetry" }

[dev-dependencies]
once_cell = "1"
Expand Down
1 change: 0 additions & 1 deletion crates/services/web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pub mod prelude;
pub mod routes;
pub mod server_error;
pub mod startup;
pub mod telemetry;

use once_cell::sync::Lazy;

Expand Down
4 changes: 2 additions & 2 deletions crates/services/web/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use tosurnament_config::get_config;
use tosurnament_telemetry::{get_subscriber, init_subscriber};
use tosurnament_web::startup::Application;
use tosurnament_web::telemetry::{get_subscriber, init_subscriber};
use tosurnament_web::TEMPLATES;

#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
let subscriber = get_subscriber("tosurnament".into(), "info".into(), std::io::stdout);
init_subscriber(subscriber);
init_subscriber(subscriber).expect("Failed to initialize subscriber");

let template_names: Vec<&str> = TEMPLATES.get_template_names().collect();
tracing::debug!("Loaded templates: {:?}", template_names);
Expand Down
27 changes: 0 additions & 27 deletions crates/services/web/src/telemetry.rs

This file was deleted.

6 changes: 3 additions & 3 deletions crates/services/web/tests/web/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use once_cell::sync::Lazy;

use tosurnament_config::get_config;
use tosurnament_telemetry::{get_subscriber, init_subscriber};
use tosurnament_web::config::Config;
use tosurnament_web::startup::Application;
use tosurnament_web::telemetry::{get_subscriber, init_subscriber};

pub struct TestApp {
pub address: String,
Expand All @@ -14,10 +14,10 @@ static TRACING: Lazy<()> = Lazy::new(|| {
let subscriber_name = "tosurnament".to_string();
if std::env::var("TEST_LOG").is_ok() {
let subscriber = get_subscriber(subscriber_name, default_filter_level, std::io::stdout);
init_subscriber(subscriber);
init_subscriber(subscriber).expect("Failed to initialize subscriber");
} else {
let subscriber = get_subscriber(subscriber_name, default_filter_level, std::io::sink);
init_subscriber(subscriber);
init_subscriber(subscriber).expect("Failed to initialize subscriber");
}
});

Expand Down

0 comments on commit 2d9a57b

Please sign in to comment.