mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-11-02 21:07:58 +08:00
feat(events): add events for incoming API requests (#2621)
Co-authored-by: Nishant Joshi <nishant.joshi@juspay.in>
This commit is contained in:
41
crates/router/src/events/api_logs.rs
Normal file
41
crates/router/src/events/api_logs.rs
Normal file
@ -0,0 +1,41 @@
|
||||
use router_env::{tracing_actix_web::RequestId, types::FlowMetric};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use time::OffsetDateTime;
|
||||
|
||||
use super::Event;
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
||||
pub struct ApiEvent {
|
||||
api_flow: String,
|
||||
created_at_timestamp: i128,
|
||||
request_id: String,
|
||||
latency: u128,
|
||||
status_code: i64,
|
||||
}
|
||||
|
||||
impl ApiEvent {
|
||||
pub fn new(
|
||||
api_flow: &impl FlowMetric,
|
||||
request_id: &RequestId,
|
||||
latency: u128,
|
||||
status_code: i64,
|
||||
) -> Self {
|
||||
Self {
|
||||
api_flow: api_flow.to_string(),
|
||||
created_at_timestamp: OffsetDateTime::now_utc().unix_timestamp_nanos(),
|
||||
request_id: request_id.as_hyphenated().to_string(),
|
||||
latency,
|
||||
status_code,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Event for ApiEvent {
|
||||
fn event_type() -> super::EventType {
|
||||
super::EventType::ApiLogs
|
||||
}
|
||||
|
||||
fn key(&self) -> String {
|
||||
self.request_id.to_string()
|
||||
}
|
||||
}
|
||||
@ -5,11 +5,7 @@ use crate::services::logger;
|
||||
pub struct EventLogger {}
|
||||
|
||||
impl EventHandler for EventLogger {
|
||||
fn log_event<T: Event>(&self, event: T, previous: Option<T>) {
|
||||
if let Some(prev) = previous {
|
||||
logger::info!(previous = ?serde_json::to_string(&prev).unwrap_or(r#"{ "error": "Serialization failed" }"#.to_string()), current = ?serde_json::to_string(&event).unwrap_or(r#"{ "error": "Serialization failed" }"#.to_string()), event_type =? T::event_type(), event_id =? event.key(), log_type = "event");
|
||||
} else {
|
||||
logger::info!(current = ?serde_json::to_string(&event).unwrap_or(r#"{ "error": "Serialization failed" }"#.to_string()), event_type =? T::event_type(), event_id =? event.key(), log_type = "event");
|
||||
}
|
||||
fn log_event<T: Event>(&self, event: T) {
|
||||
logger::info!(current = ?serde_json::to_string(&event).unwrap_or(r#"{ "error": "Serialization failed" }"#.to_string()), event_type =? T::event_type(), event_id =? event.key(), log_type = "event");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user