Skip to content

Commit

Permalink
Add tracing commong to cja and re-export it for other apps to use
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyja committed Mar 19, 2024
1 parent c8f40b1 commit 196fd44
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 112 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ members = [
"db",
"posts",
"server",
"tracing-common",
"video-toolkit",
"tracing-common",
]
resolver = "2"

Expand Down
2 changes: 1 addition & 1 deletion byte/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>(32);

Expand Down
2 changes: 2 additions & 0 deletions cja/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions cja/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ pub mod jobs;
pub mod server;

pub mod app_state;
pub mod setup;
1 change: 1 addition & 0 deletions cja/src/setup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub use tracing_common::*;
7 changes: 2 additions & 5 deletions server/src/http_server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@ 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,
til::TilPost,
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;
Expand Down
99 changes: 4 additions & 95 deletions server/src/main.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -35,93 +31,6 @@ pub(crate) use state::{AppConfig, AppState};

pub(crate) mod google;

fn setup_sentry() -> Option<ClientInitGuard> {
let git_commit: Option<std::borrow::Cow<_>> =
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::<String, String>::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 {
Expand All @@ -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();
Expand Down
9 changes: 9 additions & 0 deletions tracing-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
46 changes: 37 additions & 9 deletions tracing-common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<ClientInitGuard> {
let git_commit: Option<std::borrow::Cow<_>> =
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)
Expand Down Expand Up @@ -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)
Expand All @@ -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()?;

Expand Down
2 changes: 1 addition & 1 deletion video-toolkit/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 196fd44

Please sign in to comment.