better than before???

This commit is contained in:
Louis Dureuil 2024-01-29 18:45:55 +01:00
parent bcf1c4dae5
commit 5e52107474
No known key found for this signature in database
3 changed files with 22 additions and 17 deletions

View File

@ -88,9 +88,12 @@ fn is_empty_db(db_path: impl AsRef<Path>) -> bool {
/// The handle used to update the logs at runtime. Must be accessible from the `main.rs` and the `route/logs.rs`.
pub type LogRouteHandle =
tracing_subscriber::reload::Handle<Option<LogRouteType>, tracing_subscriber::Registry>;
pub type LogRouteType =
Box<dyn tracing_subscriber::Layer<tracing_subscriber::Registry> + Sync + Send>;
tracing_subscriber::reload::Handle<LogRouteType, tracing_subscriber::Registry>;
pub type LogRouteType = tracing_subscriber::filter::Filtered<
Option<Box<dyn tracing_subscriber::Layer<tracing_subscriber::Registry> + Send + Sync>>,
tracing_subscriber::filter::LevelFilter,
tracing_subscriber::Registry,
>;
pub fn create_app(
index_scheduler: Data<IndexScheduler>,

View File

@ -16,7 +16,8 @@ use meilisearch::{analytics, create_app, prototype_name, setup_meilisearch, LogR
use meilisearch_auth::{generate_master_key, AuthController, MASTER_KEY_MIN_SIZE};
use mimalloc::MiMalloc;
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
use tracing_subscriber::layer::SubscriberExt as _;
use tracing::level_filters::LevelFilter;
use tracing_subscriber::layer::{Filter, SubscriberExt as _};
use tracing_subscriber::Layer;
#[cfg(not(feature = "stats_alloc"))]
@ -27,8 +28,9 @@ static ALLOC: MiMalloc = MiMalloc;
#[global_allocator]
static ALLOC: stats_alloc::StatsAlloc<MiMalloc> = stats_alloc::StatsAlloc::new(MiMalloc);
fn f<S>() -> Option<Box<dyn Layer<S> + Send + Sync>> {
None
fn default_layer<S: tracing::Subscriber>(
) -> tracing_subscriber::filter::Filtered<Option<Box<dyn Layer<S> + Send + Sync>>, LevelFilter, S> {
None.with_filter(tracing_subscriber::filter::LevelFilter::OFF)
}
/// does all the setup before meilisearch is launched
@ -44,11 +46,11 @@ fn setup(opt: &Opt) -> anyhow::Result<LogRouteHandle> {
#[cfg(feature = "stats_alloc")]
let (mut trace, layer) = tracing_trace::Trace::with_stats_alloc(file, &ALLOC);
let (route_layer, route_layer_handle) = tracing_subscriber::reload::Layer::new(f());
let (route_layer, route_layer_handle) = tracing_subscriber::reload::Layer::new(default_layer());
let route_layer: tracing_subscriber::reload::Layer<_, _> = route_layer;
let subscriber = tracing_subscriber::registry()
.with(route_layer.boxed())
.with(route_layer)
.with(
tracing_subscriber::fmt::layer()
.with_line_number(true)

View File

@ -99,7 +99,7 @@ impl futures_util::Stream for LogStreamer {
}
}
pub fn make_subscriber<
pub fn make_layer<
S: tracing::Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span>,
>(
opt: &GetLogs,
@ -108,10 +108,7 @@ pub fn make_subscriber<
let fmt_layer = tracing_subscriber::fmt::layer()
.with_line_number(true)
.with_writer(move || LogWriter { sender: sender.clone() })
.with_span_events(tracing_subscriber::fmt::format::FmtSpan::ACTIVE)
.with_filter(
tracing_subscriber::filter::LevelFilter::from_str(&opt.level.to_string()).unwrap(),
);
.with_span_events(tracing_subscriber::fmt::format::FmtSpan::ACTIVE);
// let subscriber = tracing_subscriber::registry().with(fmt_layer);
Box::new(fmt_layer) as Box<dyn Layer<S> + Send + Sync>
@ -144,12 +141,15 @@ pub async fn get_logs(
let mut was_available = false;
logs.modify(|layer| match layer {
logs.modify(|layer| match layer.inner_mut() {
None => {
was_available = true;
// there is already someone getting logs
let subscriber = make_subscriber(&opt, sender);
*layer = Some(subscriber)
*layer.filter_mut() =
tracing_subscriber::filter::LevelFilter::from_str(&opt.level.to_string()).unwrap();
// there is no one getting logs
let new_layer = make_layer(&opt, sender);
*layer.inner_mut() = Some(new_layer)
}
Some(_) => {
// there is already someone getting logs