Skip to content

Commit e6d7a58

Browse files
fix(api-server): Repair logging setup and enhance observability: (#1695)
1 parent eb45ee7 commit e6d7a58

File tree

4 files changed

+48
-9
lines changed

4 files changed

+48
-9
lines changed

components/api-server/src/bin/api_server.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use anyhow::Context;
22
use clap::Parser;
33
use clp_rust_utils::{clp_config::package, serde::yaml};
4-
use tracing_appender::rolling::{RollingFileAppender, Rotation};
4+
use tracing_appender::{
5+
non_blocking::WorkerGuard,
6+
rolling::{RollingFileAppender, Rotation},
7+
};
58
use tracing_subscriber::{self, fmt::writer::MakeWriterExt};
69

710
#[derive(Parser)]
@@ -39,19 +42,27 @@ fn read_config_and_credentials(
3942
Ok((config, credentials))
4043
}
4144

42-
fn set_up_logging() -> anyhow::Result<()> {
45+
fn set_up_logging() -> anyhow::Result<WorkerGuard> {
4346
let logs_directory =
4447
std::env::var("CLP_LOGS_DIR").context("Expect `CLP_LOGS_DIR` environment variable.")?;
4548
let logs_directory = std::path::Path::new(logs_directory.as_str());
4649
let file_appender =
4750
RollingFileAppender::new(Rotation::HOURLY, logs_directory, "api_server.log");
48-
let (non_blocking_writer, _guard) = tracing_appender::non_blocking(file_appender);
51+
let (non_blocking_writer, guard) = tracing_appender::non_blocking(file_appender);
4952
tracing_subscriber::fmt()
53+
.event_format(
54+
tracing_subscriber::fmt::format()
55+
.with_level(true)
56+
.with_target(false)
57+
.with_file(true)
58+
.with_line_number(true)
59+
.json(),
60+
)
5061
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
5162
.with_ansi(false)
5263
.with_writer(std::io::stdout.and(non_blocking_writer))
5364
.init();
54-
Ok(())
65+
Ok(guard)
5566
}
5667

5768
async fn shutdown_signal() {
@@ -70,7 +81,7 @@ async fn main() -> anyhow::Result<()> {
7081
let args = Args::parse();
7182

7283
let (config, credentials) = read_config_and_credentials(&args)?;
73-
set_up_logging()?;
84+
let _guard = set_up_logging()?;
7485

7586
let addr = format!(
7687
"{}:{}",

components/api-server/src/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use utoipa::ToSchema;
1818
pub use crate::error::ClientError;
1919

2020
/// Defines the request configuration for submitting a search query.
21-
#[derive(Clone, Serialize, Deserialize, ToSchema)]
21+
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
2222
#[serde(deny_unknown_fields)]
2323
pub struct QueryConfig {
2424
/// The search query as a KQL string.

components/api-server/src/routes.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,17 @@ async fn query(
9898
State(client): State<Client>,
9999
Json(query_config): Json<QueryConfig>,
100100
) -> Result<Json<QueryResultsUri>, HandlerError> {
101-
let search_job_id = client.submit_query(query_config).await?;
101+
tracing::info!("Submitting query: {:?}", query_config);
102+
let search_job_id = match client.submit_query(query_config).await {
103+
Ok(id) => {
104+
tracing::info!("Submitted query with search job ID: {}", id);
105+
id
106+
}
107+
Err(err) => {
108+
tracing::error!("Failed to submit query: {:?}", err);
109+
return Err(err.into());
110+
}
111+
};
102112
let uri = format!("/query_results/{search_job_id}");
103113
Ok(Json(QueryResultsUri {
104114
query_results_uri: uri,
@@ -133,11 +143,29 @@ async fn query_results(
133143
State(client): State<Client>,
134144
Path(search_job_id): Path<u64>,
135145
) -> Result<Sse<impl Stream<Item = Result<Event, HandlerError>>>, HandlerError> {
136-
let results_stream = client.fetch_results(search_job_id).await?;
146+
tracing::info!("Fetching results for search job ID: {}", search_job_id);
147+
let results_stream = match client.fetch_results(search_job_id).await {
148+
Ok(stream) => {
149+
tracing::info!(
150+
"Successfully initiated result stream for search job ID {}",
151+
search_job_id
152+
);
153+
stream
154+
}
155+
Err(err) => {
156+
tracing::error!(
157+
"Failed to fetch results for search job ID {}: {:?}",
158+
search_job_id,
159+
err
160+
);
161+
return Err(err.into());
162+
}
163+
};
137164
let event_stream = results_stream.map(|res| {
138165
let message = res?;
139166
let trimmed_message = message.trim();
140167
if trimmed_message.lines().count() != 1 {
168+
tracing::error!("Received malformed log line:\n{}", trimmed_message);
141169
return Err(HandlerError::InternalServer);
142170
}
143171
Ok(Event::default().data(trimmed_message))

tools/deployment/package/docker-compose-all.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ services:
510510
CLP_LOGS_DIR: "/var/log/api_server"
511511
CLP_DB_PASS: "${CLP_DB_PASS:?Please set a value.}"
512512
CLP_DB_USER: "${CLP_DB_USER:?Please set a value.}"
513-
RUST_LOG: "DEBUG"
513+
RUST_LOG: "INFO"
514514
ports:
515515
- host_ip: "${CLP_API_SERVER_HOST:-127.0.0.1}"
516516
published: "${CLP_API_SERVER_PORT:-3001}"

0 commit comments

Comments
 (0)