feat(events): add request details to api events (#2769)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
Sampras Lopes
2023-11-08 15:59:13 +05:30
committed by GitHub
parent a429b23c7f
commit 164d1c66fb
2 changed files with 16 additions and 0 deletions

View File

@ -1,3 +1,4 @@
use actix_web::HttpRequest;
use router_env::{tracing_actix_web::RequestId, types::FlowMetric}; use router_env::{tracing_actix_web::RequestId, types::FlowMetric};
use serde::Serialize; use serde::Serialize;
use time::OffsetDateTime; use time::OffsetDateTime;
@ -15,10 +16,14 @@ pub struct ApiEvent {
#[serde(flatten)] #[serde(flatten)]
auth_type: AuthenticationType, auth_type: AuthenticationType,
request: serde_json::Value, request: serde_json::Value,
user_agent: Option<String>,
ip_addr: Option<String>,
url_path: String,
response: Option<serde_json::Value>, response: Option<serde_json::Value>,
} }
impl ApiEvent { impl ApiEvent {
#[allow(clippy::too_many_arguments)]
pub fn new( pub fn new(
api_flow: &impl FlowMetric, api_flow: &impl FlowMetric,
request_id: &RequestId, request_id: &RequestId,
@ -27,6 +32,7 @@ impl ApiEvent {
request: serde_json::Value, request: serde_json::Value,
response: Option<serde_json::Value>, response: Option<serde_json::Value>,
auth_type: AuthenticationType, auth_type: AuthenticationType,
http_req: &HttpRequest,
) -> Self { ) -> Self {
Self { Self {
api_flow: api_flow.to_string(), api_flow: api_flow.to_string(),
@ -37,6 +43,15 @@ impl ApiEvent {
request, request,
response, response,
auth_type, auth_type,
ip_addr: http_req
.connection_info()
.realip_remote_addr()
.map(ToOwned::to_owned),
user_agent: http_req
.headers()
.get("user-agent")
.and_then(|user_agent_value| user_agent_value.to_str().ok().map(ToOwned::to_owned)),
url_path: http_req.path().to_string(),
} }
} }
} }

View File

@ -852,6 +852,7 @@ where
serialized_request, serialized_request,
serialized_response, serialized_response,
auth_type, auth_type,
request,
); );
match api_event.clone().try_into() { match api_event.clone().try_into() {
Ok(event) => { Ok(event) => {