use chrono::{DateTime, Utc};
use serde::Serialize;
use serde_json::{json, Value};
use super::labels::FailureReason;
const MAX_ERROR_LEN: usize = 512;
pub fn truncate_error(message: &str) -> String {
if message.len() <= MAX_ERROR_LEN {
message.to_string()
} else {
format!("{}…", &message[..MAX_ERROR_LEN.saturating_sub(1)])
}
}
#[must_use]
pub fn dlq_fields(
event_id: &str,
topic: &str,
topic_key: Option<&str>,
seq: i64,
subscription: Option<&str>,
reason: FailureReason,
error: &str,
) -> Value {
json!({
"event_id": event_id,
"topic": topic,
"topic_key": topic_key.unwrap_or(""),
"seq": seq,
"subscription": subscription.unwrap_or(""),
"reason": reason.as_str(),
"error": truncate_error(error),
})
}
#[derive(Debug, Serialize)]
pub struct PhotonDlqRow {
pub event_id: String,
pub topic: String,
pub topic_key: String,
pub seq: i64,
pub subscription: String,
pub reason: String,
pub error: String,
pub ts: DateTime<Utc>,
}
impl PhotonDlqRow {
#[must_use]
pub fn from_parts(
event_id: &str,
topic: &str,
topic_key: Option<&str>,
seq: i64,
subscription: Option<&str>,
reason: FailureReason,
error: &str,
) -> Self {
Self {
event_id: event_id.to_string(),
topic: topic.to_string(),
topic_key: topic_key.unwrap_or("").to_string(),
seq,
subscription: subscription.unwrap_or("").to_string(),
reason: reason.as_str().to_string(),
error: truncate_error(error),
ts: Utc::now(),
}
}
}
#[must_use]
pub fn ops_log_fields(
component: &str,
operation: &str,
message: &str,
topic: &str,
subscription: &str,
error: &str,
) -> Value {
json!({
"component": component,
"operation": operation,
"message": message,
"topic": topic,
"subscription": subscription,
"error": truncate_error(error),
})
}
use photon_telemetry::ops_log;
pub fn log_ops(
component: &str,
operation: &str,
message: &str,
topic: &str,
subscription: &str,
error: &str,
) {
ops_log().log_event(
"photon_ops_log",
&ops_log_fields(component, operation, message, topic, subscription, error),
);
}