Skip to content

Commit

Permalink
Simplify logging
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Nov 9, 2024
1 parent 454d885 commit c6f89f3
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 46 deletions.
4 changes: 2 additions & 2 deletions src/area/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ pub async fn get(
None => Area::select_all(conn),
})
.await??;
let areas_len = areas.len() as i64;
let areas_len = areas.len();
let res = Either::Left(Json(areas.into_iter().map(|it| it.into()).collect()));
req.extensions_mut()
.insert(RequestExtension::new("v2/areas", areas_len));
.insert(RequestExtension::new(areas_len));
Ok(res)
}

Expand Down
2 changes: 1 addition & 1 deletion src/area/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub async fn get(
})
.await??;
req.extensions_mut()
.insert(RequestExtension::new("v3/areas", areas.len() as i64));
.insert(RequestExtension::new(areas.len()));
Ok(Json(areas.into_iter().map(|it| it.into()).collect()))
}

Expand Down
6 changes: 2 additions & 4 deletions src/area_element/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,8 @@ pub async fn get(
AreaElement::select_updated_since(&args.updated_since, Some(args.limit), conn)
})
.await??;
req.extensions_mut().insert(RequestExtension::new(
"v3/area-elements",
area_elements.len() as i64,
));
req.extensions_mut()
.insert(RequestExtension::new(area_elements.len()));
Ok(Json(
area_elements.into_iter().map(|it| it.into()).collect(),
))
Expand Down
4 changes: 2 additions & 2 deletions src/element/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ pub async fn get(
None => Element::select_all(args.limit, conn),
})
.await??;
let elements_len = elements.len() as i64;
let elements_len = elements.len();
let res = Either::Left(Json(elements.into_iter().map(|it| it.into()).collect()));
req.extensions_mut()
.insert(RequestExtension::new("v2/elements", elements_len));
.insert(RequestExtension::new(elements_len));
Ok(res)
}

Expand Down
2 changes: 1 addition & 1 deletion src/element/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub async fn get(
})
.await??;
req.extensions_mut()
.insert(RequestExtension::new("v3/elements", elements.len() as i64));
.insert(RequestExtension::new(elements.len()));
Ok(Json(elements.into_iter().map(|it| it.into()).collect()))
}

Expand Down
6 changes: 2 additions & 4 deletions src/element_comment/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,8 @@ pub async fn get(
ElementComment::select_updated_since(&args.updated_since, Some(args.limit), conn)
})
.await??;
req.extensions_mut().insert(RequestExtension::new(
"v3/element-comments",
element_comments.len() as i64,
));
req.extensions_mut()
.insert(RequestExtension::new(element_comments.len()));
Ok(Json(
element_comments.into_iter().map(|it| it.into()).collect(),
))
Expand Down
4 changes: 2 additions & 2 deletions src/event/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ pub async fn get(
),
})
.await??;
let events_len = events.len() as i64;
let events_len = events.len();
let res = Either::Left(Json(events.into_iter().map(|it| it.into()).collect()));
req.extensions_mut()
.insert(RequestExtension::new("v2/events", events_len));
.insert(RequestExtension::new(events_len));
Ok(res)
}

Expand Down
2 changes: 1 addition & 1 deletion src/event/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub async fn get(
})
.await??;
req.extensions_mut()
.insert(RequestExtension::new("v3/events", events.len() as i64));
.insert(RequestExtension::new(events.len()));
Ok(Json(events.into_iter().map(|it| it.into()).collect()))
}

Expand Down
36 changes: 23 additions & 13 deletions src/log/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{data_dir_file, Result};
use actix_web::{
body::MessageBody,
dev::{ServiceRequest, ServiceResponse},
http::StatusCode,
middleware::Next,
Error, HttpMessage, HttpRequest,
};
Expand All @@ -27,15 +28,13 @@ fn open_conn() -> Result<Connection> {
}

pub struct RequestExtension {
pub endpoint: String,
pub entities: i64,
}

impl RequestExtension {
pub fn new(endpoint: &str, entities: i64) -> Self {
pub fn new(entities: usize) -> Self {
RequestExtension {
endpoint: endpoint.into(),
entities,
entities: entities as i64,
}
}
}
Expand All @@ -48,20 +47,30 @@ pub async fn handle_request(
let res = next.call(req).await;
let Ok(res) = res else { return res };
let extensions = res.request().extensions();
let Some(extension) = extensions.get::<RequestExtension>() else {
drop(extensions);
return Ok(res);
};
let endpoint = extension.endpoint.clone();
let entities = extension.entities;
let entities = extensions.get::<RequestExtension>().map(|it| it.entities);
drop(extensions);
let time_ns = Instant::now().duration_since(started_at).as_nanos();
log_request(res.request(), entities, time_ns as i64)?;
log_summary(res.request(), &endpoint, entities, time_ns as i64)?;
log_request(
res.request(),
res.response().status(),
entities,
time_ns as i64,
)?;
log_summary(
res.request(),
res.request().path(),
entities.unwrap_or_default(),
time_ns as i64,
)?;
Ok(res)
}

fn log_request(req: &HttpRequest, entities: i64, time_ns: i64) -> Result<()> {
fn log_request(
req: &HttpRequest,
code: StatusCode,
entities: Option<i64>,
time_ns: i64,
) -> Result<()> {
let conn_info = req.connection_info();
let Some(addr) = conn_info.realip_remote_addr() else {
return Ok(());
Expand All @@ -71,6 +80,7 @@ fn log_request(req: &HttpRequest, entities: i64, time_ns: i64) -> Result<()> {
addr,
req.path(),
req.query_string(),
code.as_u16() as i64,
entities,
time_ns,
conn,
Expand Down
26 changes: 16 additions & 10 deletions src/log/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const COL_ID: &str = "id";
const COL_DATE: &str = "date";
const COL_IP: &str = "ip";
const COL_PATH: &str = "path";
const COL_ARGS: &str = "args";
const COL_QUERY: &str = "query";
const COL_CODE: &str = "code";
const COL_ENTITIES: &str = "entities";
const COL_TIME_NS: &str = "time_ns";

Expand All @@ -18,8 +19,9 @@ pub fn init(conn: &Connection) -> Result<()> {
{COL_DATE} TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ')),
{COL_IP} TEXT NOT NULL,
{COL_PATH} TEXT NOT NULL,
{COL_ARGS} TEXT NOT NULL,
{COL_ENTITIES} INTEGER NOT NULL,
{COL_QUERY} TEXT NOT NULL,
{COL_CODE} INTEGER NOT NULL,
{COL_ENTITIES} INTEGER,
{COL_TIME_NS} INTEGER NOT NULL
) STRICT;
"#
Expand All @@ -31,34 +33,38 @@ pub fn init(conn: &Connection) -> Result<()> {
pub fn insert(
ip: &str,
path: &str,
args: &str,
entities: i64,
query: &str,
code: i64,
entities: Option<i64>,
time_ns: i64,
conn: &Connection,
) -> Result<()> {
let query = format!(
let sql = format!(
r#"
INSERT INTO {TABLE_NAME} (
{COL_IP},
{COL_PATH},
{COL_ARGS},
{COL_QUERY},
{COL_CODE},
{COL_ENTITIES},
{COL_TIME_NS}
) VALUES (
:{COL_IP},
:{COL_PATH},
:{COL_ARGS},
:{COL_QUERY},
:{COL_CODE},
:{COL_ENTITIES},
:{COL_TIME_NS}
);
"#
);
conn.execute(
&query,
&sql,
named_params! {
":ip": ip,
":path": path,
":args": args,
":query": query,
":code": code,
":entities": entities,
":time_ns": time_ns,
},
Expand Down
4 changes: 2 additions & 2 deletions src/report/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ pub async fn get(
),
})
.await??;
let reports_len = reports.len() as i64;
let reports_len = reports.len();
let res = Either::Left(Json(reports.into_iter().map(|it| it.into()).collect()));
req.extensions_mut()
.insert(RequestExtension::new("v2/reports", reports_len));
.insert(RequestExtension::new(reports_len));
Ok(res)
}

Expand Down
2 changes: 1 addition & 1 deletion src/report/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub async fn get(
})
.await??;
req.extensions_mut()
.insert(RequestExtension::new("v3/reports", reports.len() as i64));
.insert(RequestExtension::new(reports.len()));
Ok(Json(reports.into_iter().map(|it| it.into()).collect()))
}

Expand Down
4 changes: 2 additions & 2 deletions src/user/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ pub async fn get(
None => User::select_all(args.limit, conn),
})
.await??;
let users_len = users.len() as i64;
let users_len = users.len();
let res = Either::Left(Json(users.into_iter().map(|it| it.into()).collect()));
req.extensions_mut()
.insert(RequestExtension::new("v2/users", users_len));
.insert(RequestExtension::new(users_len));
Ok(res)
}

Expand Down
2 changes: 1 addition & 1 deletion src/user/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub async fn get(
})
.await??;
req.extensions_mut()
.insert(RequestExtension::new("v3/users", users.len() as i64));
.insert(RequestExtension::new(users.len()));
Ok(Json(users.into_iter().map(|it| it.into()).collect()))
}

Expand Down

0 comments on commit c6f89f3

Please sign in to comment.