feat(events): add events for incoming API requests (#2621)

Co-authored-by: Nishant Joshi <nishant.joshi@juspay.in>
This commit is contained in:
Sampras Lopes
2023-10-18 14:40:56 +05:30
committed by GitHub
parent da77d1393b
commit 7a76d6c01a
6 changed files with 77 additions and 23 deletions

View 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()
}
}

View File

@ -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");
}
}