Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Leshiy committed May 25, 2024
1 parent 56c5b22 commit 7391100
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 52 deletions.
4 changes: 2 additions & 2 deletions catalyst-gateway/bin/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub(crate) fn init(log_level: LogLevel) -> Handle<LevelFilter, Registry> {
.with_span_list(true);
// Create a reloadable layer with the specified log_level
let filter = LevelFilter::from_level(log_level.into());
let (filter, reload_handle) = reload::Layer::new(filter);
let (filter, logger_handle) = reload::Layer::new(filter);
tracing_subscriber::registry()
.with(filter)
.with(layer)
Expand All @@ -75,5 +75,5 @@ pub(crate) fn init(log_level: LogLevel) -> Handle<LevelFilter, Registry> {
// Logging is globally disabled by default, so globally enable it to the required level.
tracing::log::set_max_level(log_level.into());

reload_handle
logger_handle
}
10 changes: 3 additions & 7 deletions catalyst-gateway/bin/src/service/api/health/inspection_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::Arc;

use poem::web::Data;
use poem_openapi::ApiResponse;
use tracing::{debug, error};
use tracing::debug;

use crate::{
event_db::DeepQueryInspectionFlag, logger::LogLevel,
Expand All @@ -30,13 +30,9 @@ pub(crate) async fn endpoint(
query_inspection: Option<DeepQueryInspectionFlag>,
) -> AllResponses {
if let Some(level) = log_level {
let inspection_settings = state.inspection_settings();
let settings = inspection_settings.read().await;
match settings.modify_logger_level(level) {
match state.modify_logger_level(level) {
Ok(()) => debug!("successfully set log level to: {:?}", level),
Err(_) => {
error!("failed to set log level: {:?}", level);
},
Err(err) => return AllResponses::handle_error(&err),
}
}

Expand Down
2 changes: 1 addition & 1 deletion catalyst-gateway/bin/src/service/common/responses/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl<T> WithErrorResponses<T> {
err.to_string(),
))))
},
err if err.is::<NetworkValidationError>() => {
err if err.is::<bb8::RunError<tokio_postgres::Error>>() => {
WithErrorResponses::Error(ErrorResponses::ServiceUnavailable)
},
err => {
Expand Down
56 changes: 14 additions & 42 deletions catalyst-gateway/bin/src/state/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Shared state used by all endpoints.
use std::sync::Arc;

use tokio::sync::RwLock;
use tracing::level_filters::LevelFilter;
use tracing_subscriber::{reload::Handle, Registry};

Expand All @@ -12,39 +11,8 @@ use crate::{

/// Settings for logger level
pub(crate) struct LoggerSettings {
/// Reload handle for formatting layer.
layer_handle: Handle<LevelFilter, Registry>,
}

/// Settings for service inspection during runtime
pub(crate) struct InspectionSettings {
/// Settings for inspecting the logs.
log: LoggerSettings,
}

impl InspectionSettings {
/// Create a new inspection settings
///
/// # Arguments
/// * `deep_query_inspection_enabled` - enable deep query inspection
/// * `layer_handle` - reload handle for formatting layer
///
/// # Returns
/// A new inspection settings
pub(crate) fn new(layer_handle: Handle<LevelFilter, Registry>) -> Self {
Self {
log: LoggerSettings { layer_handle },
}
}

/// Modify the logger level setting.
/// This will reload the logger.
pub(crate) fn modify_logger_level(&self, level: LogLevel) -> anyhow::Result<()> {
self.log
.layer_handle
.modify(|f| *f = LevelFilter::from_level(level.into()))?;
Ok(())
}
/// Logger handle for formatting layer.
logger_handle: Handle<LevelFilter, Registry>,
}

/// Global State of the service
Expand All @@ -57,22 +25,22 @@ pub(crate) struct State {
// Private need to get it with a function.
event_db: Arc<EventDB>, /* This needs to be obsoleted, we want the DB
* to be able to be down. */
/// Settings for service inspection during runtime.
inspection: Arc<RwLock<InspectionSettings>>,
/// Logger settings
logger_settings: Arc<LoggerSettings>,
}

impl State {
/// Create a new global [`State`]
pub(crate) async fn new(
database_url: Option<String>, inspection_settings: InspectionSettings,
database_url: Option<String>, logger_handle: Handle<LevelFilter, Registry>,
) -> anyhow::Result<Self> {
// Get a configured pool to the Database, runs schema version check internally.
let event_db = Arc::new(establish_connection(database_url).await?);
let inspection = Arc::new(RwLock::new(inspection_settings));
let logger_settings = Arc::new(LoggerSettings { logger_handle });

let state = Self {
event_db,
inspection,
logger_settings,
};

// We don't care if this succeeds or not.
Expand All @@ -87,8 +55,12 @@ impl State {
self.event_db.clone()
}

/// Get the reference to the inspection settings.
pub(crate) fn inspection_settings(&self) -> Arc<RwLock<InspectionSettings>> {
self.inspection.clone()
/// Modify the logger level setting.
/// This will reload the logger.
pub(crate) fn modify_logger_level(&self, level: LogLevel) -> anyhow::Result<()> {
self.logger_settings
.logger_handle
.modify(|f| *f = LevelFilter::from_level(level.into()))?;
Ok(())
}
}

0 comments on commit 7391100

Please sign in to comment.