-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #791 from helium/macpie/custom-tracing
Macpie/custom tracing
- Loading branch information
Showing
10 changed files
with
533 additions
and
82 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
[package] | ||
name = "custom-tracing" | ||
version = "0.1.0" | ||
authors.workspace = true | ||
license.workspace = true | ||
edition.workspace = true | ||
|
||
[dependencies] | ||
notify = { version = "6", default-features = false } | ||
anyhow = "1" | ||
tokio = { version = "1", features = ["rt-multi-thread", "sync", "signal"] } | ||
tracing = "0" | ||
tracing-subscriber = { version = "0", default-features = true, features = [ | ||
"env-filter", | ||
"registry", | ||
"fmt", | ||
] } | ||
tower-http = { version = "0", features = ["trace"] } | ||
tower-layer = { version = "0" } | ||
axum = { version = "0.7", features = ["tracing"], optional = true } | ||
helium-proto = { workspace = true, optional = true } | ||
http = { workspace = true, optional = true } | ||
|
||
|
||
[target.'cfg(target_os = "macos")'.dependencies] | ||
notify = { version = "6", default-features = false, features = [ | ||
"macos_fsevent", | ||
] } | ||
|
||
|
||
[features] | ||
default = [] | ||
http-1 = ["axum"] | ||
grpc = ["helium-proto", "http"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use helium_proto::services::Body; | ||
use http::request::Request; | ||
use tower_http::{ | ||
classify::{GrpcErrorsAsFailures, SharedClassifier}, | ||
trace::{DefaultOnFailure, DefaultOnResponse, TraceLayer}, | ||
LatencyUnit, | ||
}; | ||
use tracing::{Level, Span}; | ||
|
||
type GrpcLayer = | ||
TraceLayer<SharedClassifier<GrpcErrorsAsFailures>, for<'a> fn(&'a http::Request<Body>) -> Span>; | ||
|
||
pub fn new_with_span(make_span: fn(&Request<Body>) -> Span) -> GrpcLayer { | ||
TraceLayer::new_for_grpc() | ||
.make_span_with(make_span) | ||
.on_response( | ||
DefaultOnResponse::new() | ||
.level(Level::DEBUG) | ||
.latency_unit(LatencyUnit::Millis), | ||
) | ||
.on_failure( | ||
DefaultOnFailure::new() | ||
.level(Level::WARN) | ||
.latency_unit(LatencyUnit::Millis), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use axum::{body::Body, http::Request}; | ||
use tower_http::{ | ||
classify::{ServerErrorsAsFailures, SharedClassifier}, | ||
trace::{DefaultOnFailure, DefaultOnResponse, TraceLayer}, | ||
LatencyUnit, | ||
}; | ||
use tracing::{Level, Span}; | ||
|
||
pub fn new_with_span( | ||
make_span: fn(&Request<Body>) -> Span, | ||
) -> TraceLayer< | ||
SharedClassifier<ServerErrorsAsFailures>, | ||
for<'a> fn(&'a axum::http::Request<axum::body::Body>) -> Span, | ||
> { | ||
TraceLayer::new_for_http() | ||
.make_span_with(make_span) | ||
.on_response( | ||
DefaultOnResponse::new() | ||
.level(Level::DEBUG) | ||
.latency_unit(LatencyUnit::Millis), | ||
) | ||
.on_failure( | ||
DefaultOnFailure::new() | ||
.level(Level::WARN) | ||
.latency_unit(LatencyUnit::Millis), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
use anyhow::Result; | ||
use notify::{event::DataChange, Config, RecommendedWatcher, RecursiveMode, Watcher}; | ||
use std::{fs, path::Path}; | ||
use tracing::Span; | ||
use tracing_subscriber::{ | ||
layer::SubscriberExt, | ||
reload::{self, Handle}, | ||
util::SubscriberInitExt, | ||
EnvFilter, Registry, | ||
}; | ||
|
||
#[cfg(feature = "grpc")] | ||
pub mod grpc_layer; | ||
#[cfg(feature = "http-1")] | ||
pub mod http_layer; | ||
|
||
pub async fn init(og_filter: String, tracing_cfg_file: String) -> Result<()> { | ||
let (filtered_layer, reload_handle) = | ||
reload::Layer::new(tracing_subscriber::EnvFilter::new(og_filter.clone())); | ||
|
||
tracing_subscriber::registry() | ||
.with(filtered_layer) | ||
.with(tracing_subscriber::fmt::layer()) | ||
.init(); | ||
|
||
tokio::spawn(async move { | ||
let state = State { | ||
og_filter: og_filter.clone(), | ||
tracing_cfg_file, | ||
reload_handle, | ||
}; | ||
if let Err(err) = state.watch().await { | ||
tracing::warn!(?err, "tracing error watching configuration for update") | ||
} | ||
}); | ||
|
||
tracing::info!("custom tracing installed"); | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn record<T>(field: &str, value: T) | ||
where | ||
T: std::fmt::Display, | ||
{ | ||
Span::current().record(field, &tracing::field::display(value)); | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct State { | ||
pub og_filter: String, | ||
pub tracing_cfg_file: String, | ||
pub reload_handle: Handle<EnvFilter, Registry>, | ||
} | ||
|
||
impl State { | ||
async fn watch(&self) -> Result<()> { | ||
let (tx, mut rx) = tokio::sync::mpsc::channel(10); | ||
|
||
let mut watcher = RecommendedWatcher::new( | ||
move |res| { | ||
tx.blocking_send(res).expect("Failed to send event"); | ||
}, | ||
Config::default(), | ||
)?; | ||
|
||
watcher.watch(".".as_ref(), RecursiveMode::NonRecursive)?; | ||
|
||
while let Some(res) = rx.recv().await { | ||
match res { | ||
Err(err) => tracing::warn!(?err, "tracing config watcher configuration file error"), | ||
Ok(event) => match event.kind { | ||
notify::EventKind::Modify(notify::event::ModifyKind::Data( | ||
DataChange::Content, | ||
)) => { | ||
if let Some(event_path) = event.paths.first() { | ||
if Path::new(event_path).exists() { | ||
match fs::read_to_string(event_path) { | ||
Err(_err) => tracing::warn!( | ||
?_err, | ||
"tracing config watcher failed to read file" | ||
), | ||
Ok(content) => { | ||
if file_match(event_path, self.tracing_cfg_file.clone()) { | ||
self.handle_change(content)?; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
notify::EventKind::Remove(notify::event::RemoveKind::File) => { | ||
if let Some(event_path) = event.paths.first() { | ||
if file_match(event_path, self.tracing_cfg_file.clone()) { | ||
self.handle_delete()?; | ||
} | ||
} | ||
} | ||
_event => { | ||
tracing::debug!(?_event, "tracing config watcher ignored unhandled message") | ||
} | ||
}, | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn handle_change(&self, content: String) -> Result<()> { | ||
if content.is_empty() { | ||
self.handle_delete() | ||
} else { | ||
match tracing_subscriber::EnvFilter::try_new(content.clone()) { | ||
Err(_err) => tracing::warn!( | ||
filter = content, | ||
?_err, | ||
"tracing config watcher failed to parse filter" | ||
), | ||
Ok(new_filter) => { | ||
self.reload_handle.modify(|filter| *filter = new_filter)?; | ||
tracing::info!(filter = content, "custom tracing config updated"); | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
fn handle_delete(&self) -> Result<()> { | ||
let new_filter = self.og_filter.clone(); | ||
|
||
self.reload_handle | ||
.modify(|filter| *filter = tracing_subscriber::EnvFilter::new(new_filter.clone()))?; | ||
|
||
tracing::info!( | ||
filter = new_filter, | ||
"tracing config watcher file deleted, reverting to rustlog filter" | ||
); | ||
Ok(()) | ||
} | ||
} | ||
|
||
fn file_match(event_path: &Path, file: String) -> bool { | ||
event_path | ||
.to_str() | ||
.map(|path| path.split('/')) | ||
.map(|path| path.last().is_some_and(|file_name| file_name == file)) | ||
.unwrap_or_default() | ||
} |
Oops, something went wrong.