Skip to content

Commit

Permalink
feat: add 'GET health/inspection' endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
saibatizoku committed May 8, 2024
1 parent ed73c1d commit 536ee0f
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 2 deletions.
76 changes: 76 additions & 0 deletions catalyst-gateway/bin/src/service/api/health/inspection_get.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//! Implementation of the GET /health/inspection endpoint

use std::sync::Arc;

use poem::web::Data;
use poem_extensions::{response, UniResponse::T204};
use tracing::{debug, error};

use crate::{
logger::LogLevel,
service::common::responses::{
resp_2xx::NoContent,
resp_4xx::ApiValidationError,
resp_5xx::{ServerError, ServiceUnavailable},
},
state::{DeepQueryInspection, State},
};

/// All responses
pub(crate) type AllResponses = response! {
204: NoContent,
400: ApiValidationError,
500: ServerError,
503: ServiceUnavailable,
};

/// # GET /health/inspection
///
/// Inspection settings endpoint.
///
///
/// ## Responses
///
/// * 204 No Content - Service is Started and can serve requests.
/// * 400 API Validation Error
/// * 500 Server Error - If anything within this function fails unexpectedly. (Possible
/// but unlikely)
/// * 503 Service Unavailable - Service has not started, do not send other requests.
#[allow(clippy::unused_async)]
pub(crate) async fn endpoint(
state: Data<&Arc<State>>, log_level: Option<LogLevel>,
query_inspection: Option<DeepQueryInspection>,
) -> AllResponses {
if let Some(level) = log_level {
match state.inspection_settings().modify_logger_level(level) {
Ok(()) => {
debug!("successfully set log level to: {:?}", level);
},
Err(_) => {
error!("failed to set log level: {:?}", level);
},
}
}

if let Some(inspection_mode) = query_inspection {
match state
.inspection_settings()
.modify_deep_query(inspection_mode)
{
Ok(()) => {
debug!(
"successfully set deep query inspection mode to: {:?}",
inspection_mode
);
},
Err(_) => {
error!(
"failed to set deep query inspection mode: {:?}",
inspection_mode
);
},
}
}
// otherwise everything seems to be A-OK
T204(NoContent)
}
22 changes: 20 additions & 2 deletions catalyst-gateway/bin/src/service/api/health/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
//! Health Endpoints
use std::sync::Arc;

use poem::web::Data;
use poem::web::{Data, Query};
use poem_openapi::OpenApi;

use crate::{service::common::tags::ApiTags, state::State};
use crate::{
logger::LogLevel,
service::common::tags::ApiTags,
state::{DeepQueryInspection, State},
};

mod inspection_get;
mod live_get;
mod ready_get;
mod started_get;
Expand Down Expand Up @@ -76,4 +81,17 @@ impl HealthApi {
async fn live_get(&self) -> live_get::AllResponses {
live_get::endpoint().await
}

#[oai(
path = "/inspection",
method = "get",
operation_id = "healthInspection"
)]
/// Options for service inspection.
async fn inspection(
&self, state: Data<&Arc<State>>, log_level: Query<Option<LogLevel>>,
query_inspection: Query<Option<DeepQueryInspection>>,
) -> inspection_get::AllResponses {
inspection_get::endpoint(state, log_level.0, query_inspection.0).await
}
}
26 changes: 26 additions & 0 deletions catalyst-gateway/bin/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ pub(crate) struct InspectionSettings {
pub(crate) logger_handle: Handle<LevelFilter, Registry>,
}

impl InspectionSettings {
/// Modify the deep query inspection setting.
pub(crate) fn modify_deep_query(&self, deep_query: DeepQueryInspection) -> anyhow::Result<()> {
*self
.deep_query
.lock()
.map_err(|_| Error::QueryInspectionUpdate)? = deep_query;
Ok(())
}

/// Modify the logger level setting.
/// This will reload the logger.
pub(crate) fn modify_logger_level(&self, level: LogLevel) -> anyhow::Result<()> {
self.logger_handle
.modify(|f| *f = LevelFilter::from_level(level.into()))?;
Ok(())
}
}
/// Global State of the service
pub(crate) struct State {
/// This can be None, or a handle to the DB.
Expand Down Expand Up @@ -72,3 +90,11 @@ impl State {
self.inspection.clone()
}
}

#[derive(thiserror::Error, Debug)]
/// `State` error.
pub(crate) enum Error {
#[error("failed to update deep query inspection mode")]
/// Failed to update deep query inspection mode.
QueryInspectionUpdate,
}

0 comments on commit 536ee0f

Please sign in to comment.