From 196fd4499750acfcb60d61017326c92e04d8a649 Mon Sep 17 00:00:00 2001 From: Corey Alexander Date: Mon, 18 Mar 2024 20:45:27 -0400 Subject: [PATCH] Add tracing commong to cja and re-export it for other apps to use --- Cargo.lock | 3 ++ Cargo.toml | 2 +- byte/src/main.rs | 2 +- cja/Cargo.toml | 2 + cja/src/lib.rs | 1 + cja/src/setup.rs | 1 + server/src/http_server/mod.rs | 7 +-- server/src/main.rs | 99 ++--------------------------------- tracing-common/Cargo.toml | 9 ++++ tracing-common/src/lib.rs | 46 ++++++++++++---- video-toolkit/src/main.rs | 2 +- 11 files changed, 62 insertions(+), 112 deletions(-) create mode 100644 cja/src/setup.rs diff --git a/Cargo.lock b/Cargo.lock index 1aceb912..97691ac7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1112,6 +1112,7 @@ dependencies = [ "tower-http", "tower-service", "tracing", + "tracing-common", "uuid", ] @@ -6169,6 +6170,8 @@ dependencies = [ "miette", "opentelemetry", "opentelemetry-otlp", + "sentry", + "sentry-tracing", "tracing", "tracing-opentelemetry", "tracing-subscriber", diff --git a/Cargo.toml b/Cargo.toml index c7a137d4..6274cd3e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,8 +5,8 @@ members = [ "db", "posts", "server", - "tracing-common", "video-toolkit", + "tracing-common", ] resolver = "2" diff --git a/byte/src/main.rs b/byte/src/main.rs index db6b7143..d007f106 100644 --- a/byte/src/main.rs +++ b/byte/src/main.rs @@ -21,7 +21,7 @@ pub(crate) struct Config { #[tokio::main] async fn main() -> Result<()> { - setup_tracing()?; + setup_tracing("byte")?; let (say_sender, say_reciever) = tokio::sync::mpsc::channel::(32); diff --git a/cja/Cargo.toml b/cja/Cargo.toml index 8bcfa274..888a4b9f 100644 --- a/cja/Cargo.toml +++ b/cja/Cargo.toml @@ -28,5 +28,7 @@ axum = "0.7.4" tower-service = "0.3.2" tower = "0.4.13" +tracing-common = { path = "../tracing-common" } + [lints] workspace = true diff --git a/cja/src/lib.rs b/cja/src/lib.rs index 94f969c1..313c7d8d 100644 --- a/cja/src/lib.rs +++ b/cja/src/lib.rs @@ -7,3 +7,4 @@ pub mod jobs; pub mod server; pub mod app_state; +pub mod setup; diff --git a/cja/src/setup.rs b/cja/src/setup.rs new file mode 100644 index 00000000..b3d38141 --- /dev/null +++ b/cja/src/setup.rs @@ -0,0 +1 @@ +pub use tracing_common::*; diff --git a/server/src/http_server/mod.rs b/server/src/http_server/mod.rs index 08e3a5c9..a4757772 100644 --- a/server/src/http_server/mod.rs +++ b/server/src/http_server/mod.rs @@ -7,7 +7,7 @@ use axum::{ }; use chrono::{DateTime, NaiveTime, Utc}; use include_dir::{include_dir, Dir}; -use miette::{Context, IntoDiagnostic, Result}; +use miette::{IntoDiagnostic, Result}; use posts::{ blog::{BlogPost, ToCanonicalPath}, date::PostedOn, @@ -15,10 +15,7 @@ use posts::{ title::Title, Post, }; -use std::{net::SocketAddr, sync::Arc}; -use tokio::net::TcpListener; -use tower_cookies::CookieManagerLayer; -use tower_http::trace::TraceLayer; +use std::sync::Arc; use crate::{AppConfig, AppState}; use errors::MietteError; diff --git a/server/src/main.rs b/server/src/main.rs index 7e2db7c5..2b09d8f8 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -1,20 +1,16 @@ #![allow(dead_code)] -use std::{collections::HashMap, sync::Arc, time::Duration}; +use std::sync::Arc; +use cja::setup::{setup_sentry, setup_tracing}; use clap::Parser; use commands::Command; -use miette::{Context, IntoDiagnostic}; -use opentelemetry_otlp::WithExportConfig; +use miette::IntoDiagnostic; -use sentry::ClientInitGuard; use serde::{Deserialize, Serialize}; use tracing::instrument; -use tracing_opentelemetry::OpenTelemetryLayer; -use tracing_subscriber::{prelude::*, util::SubscriberInitExt, EnvFilter, Registry}; -use tracing_tree::HierarchicalLayer; pub use miette::Result; @@ -35,93 +31,6 @@ pub(crate) use state::{AppConfig, AppState}; pub(crate) mod google; -fn setup_sentry() -> Option { - let git_commit: Option> = - option_env!("VERGEN_GIT_SHA").map(std::convert::Into::into); - let release_name = - git_commit.unwrap_or_else(|| sentry::release_name!().unwrap_or_else(|| "dev".into())); - - if let Ok(sentry_dsn) = std::env::var("SENTRY_DSN") { - println!("Sentry enabled"); - - Some(sentry::init(( - sentry_dsn, - sentry::ClientOptions { - traces_sample_rate: 0.5, - release: Some(release_name), - ..Default::default() - }, - ))) - } else { - println!("Sentry not configured in this environment"); - - None - } -} - -fn setup_tracing() -> Result<()> { - let rust_log = - std::env::var("RUST_LOG").unwrap_or_else(|_| "info,server=trace,tower_http=debug".into()); - - let env_filter = EnvFilter::builder() - .parse(&rust_log) - .into_diagnostic() - .wrap_err_with(|| miette::miette!("Couldn't create env filter from {}", rust_log))?; - - let opentelemetry_layer = if let Ok(honeycomb_key) = std::env::var("HONEYCOMB_API_KEY") { - let mut map = HashMap::::new(); - map.insert("x-honeycomb-team".to_string(), honeycomb_key); - map.insert("x-honeycomb-dataset".to_string(), "coreyja.com".to_string()); - - let tracer = opentelemetry_otlp::new_pipeline() - .tracing() - .with_exporter( - opentelemetry_otlp::new_exporter() - .http() - .with_endpoint("https://api.honeycomb.io/v1/traces") - .with_timeout(Duration::from_secs(3)) - .with_headers(map), - ) - .install_batch(opentelemetry::runtime::Tokio) - .into_diagnostic()?; - - let opentelemetry_layer = OpenTelemetryLayer::new(tracer); - println!("Honeycomb layer configured"); - - Some(opentelemetry_layer) - } else { - println!("Skipping Honeycomb layer"); - - None - }; - - let hierarchical = { - let hierarchical = HierarchicalLayer::default() - .with_writer(std::io::stdout) - .with_indent_lines(true) - .with_indent_amount(2) - .with_thread_names(true) - .with_thread_ids(true) - .with_verbose_exit(true) - .with_verbose_entry(true) - .with_targets(true); - - println!("Let's also log to stdout."); - - hierarchical - }; - - Registry::default() - .with(hierarchical) - .with(opentelemetry_layer) - .with(env_filter) - .with(sentry_tracing::layer()) - .try_init() - .into_diagnostic()?; - - Ok(()) -} - #[derive(Parser)] #[command(author, version, about)] struct CliArgs { @@ -141,7 +50,7 @@ fn main() -> Result<()> { } async fn _main() -> Result<()> { - setup_tracing()?; + setup_tracing("server")?; let cli = CliArgs::parse(); let command = cli.command.unwrap_or_default(); diff --git a/tracing-common/Cargo.toml b/tracing-common/Cargo.toml index cadff933..2605f54f 100644 --- a/tracing-common/Cargo.toml +++ b/tracing-common/Cargo.toml @@ -6,6 +6,15 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +sentry = { version = "0.31.5", default-features = false, features = [ + "rustls", + "backtrace", + "contexts", + "panic", + "tower", + "reqwest", +] } +sentry-tracing = "0.31.8" tracing = { workspace = true } tracing-opentelemetry = { workspace = true } tracing-subscriber = { workspace = true } diff --git a/tracing-common/src/lib.rs b/tracing-common/src/lib.rs index 70213b6e..76a2ca8d 100644 --- a/tracing-common/src/lib.rs +++ b/tracing-common/src/lib.rs @@ -1,14 +1,41 @@ use std::{collections::HashMap, time::Duration}; -use miette::{Context, IntoDiagnostic, Result}; +use miette::{Context as _, IntoDiagnostic as _}; use opentelemetry_otlp::WithExportConfig; +use sentry::ClientInitGuard; use tracing_opentelemetry::OpenTelemetryLayer; -use tracing_subscriber::{prelude::*, EnvFilter, Registry}; +use tracing_subscriber::{ + layer::SubscriberExt as _, util::SubscriberInitExt as _, EnvFilter, Registry, +}; use tracing_tree::HierarchicalLayer; -pub fn setup_tracing() -> Result<()> { - let rust_log = - std::env::var("RUST_LOG").unwrap_or_else(|_| "warn,server=trace,tower_http=debug".into()); +pub fn setup_sentry() -> Option { + let git_commit: Option> = + option_env!("VERGEN_GIT_SHA").map(std::convert::Into::into); + let release_name = + git_commit.unwrap_or_else(|| sentry::release_name!().unwrap_or_else(|| "dev".into())); + + if let Ok(sentry_dsn) = std::env::var("SENTRY_DSN") { + println!("Sentry enabled"); + + Some(sentry::init(( + sentry_dsn, + sentry::ClientOptions { + traces_sample_rate: 0.5, + release: Some(release_name), + ..Default::default() + }, + ))) + } else { + println!("Sentry not configured in this environment"); + + None + } +} + +pub fn setup_tracing(crate_name: &str) -> miette::Result<()> { + let rust_log = std::env::var("RUST_LOG") + .unwrap_or_else(|_| format!("info,{crate_name}=trace,tower_http=debug")); let env_filter = EnvFilter::builder() .parse(&rust_log) @@ -42,8 +69,8 @@ pub fn setup_tracing() -> Result<()> { None }; - let heirarchical = { - let heirarchical = HierarchicalLayer::default() + let hierarchical = { + let hierarchical = HierarchicalLayer::default() .with_writer(std::io::stdout) .with_indent_lines(true) .with_indent_amount(2) @@ -55,13 +82,14 @@ pub fn setup_tracing() -> Result<()> { println!("Let's also log to stdout."); - heirarchical + hierarchical }; Registry::default() - .with(heirarchical) + .with(hierarchical) .with(opentelemetry_layer) .with(env_filter) + .with(sentry_tracing::layer()) .try_init() .into_diagnostic()?; diff --git a/video-toolkit/src/main.rs b/video-toolkit/src/main.rs index 8dbc884d..2ec81616 100644 --- a/video-toolkit/src/main.rs +++ b/video-toolkit/src/main.rs @@ -36,7 +36,7 @@ enum Command { async fn main() -> Result<()> { std::env::set_var("RUST_LOG", "info"); - setup_tracing()?; + setup_tracing("video-toolkit")?; let cli = CliArgs::parse(); match cli.command {