alternative summary specification
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
use super::log_buffer::LogBuffer;
|
use super::log_buffer::LogBuffer;
|
||||||
use super::route::{Destination, Limit, Route};
|
use super::route::{Destination, Limit, Route};
|
||||||
use super::{LimitResult, Limiter, LimiterSet, SignalAlertMessage};
|
use super::{LimitResult, Limiter, LimiterSet, SignalAlertMessage, Summary};
|
||||||
use crate::{
|
use crate::{
|
||||||
concurrent_map::ConcurrentMap,
|
concurrent_map::ConcurrentMap,
|
||||||
log_format::LogFormatConfig,
|
log_format::LogFormatConfig,
|
||||||
@@ -203,7 +203,7 @@ impl LogHandler {
|
|||||||
origin: Some(origin),
|
origin: Some(origin),
|
||||||
text,
|
text,
|
||||||
attachment_paths: Default::default(),
|
attachment_paths: Default::default(),
|
||||||
summary: None,
|
summary: Summary::Prefix(512),
|
||||||
destination_override,
|
destination_override,
|
||||||
}) {
|
}) {
|
||||||
error!("Could not send alert message, queue is closed");
|
error!("Could not send alert message, queue is closed");
|
||||||
|
|||||||
@@ -159,6 +159,21 @@ fn parse_gateway_command(s: &str) -> Result<GatewayCommand, String> {
|
|||||||
.map_err(|e| e.to_string())
|
.map_err(|e| e.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Summary for logging an alert message.
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
enum Summary {
|
||||||
|
/// Use a prefix of the message text (capped at 512 chars).
|
||||||
|
Prefix(usize),
|
||||||
|
/// Use an owned summary string.
|
||||||
|
Owned(Box<str>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Summary {
|
||||||
|
fn default() -> Self {
|
||||||
|
Summary::Prefix(512)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// A message queued to be sent to all admins.
|
/// A message queued to be sent to all admins.
|
||||||
/// This is generally an alert message, which may have attached images.
|
/// This is generally an alert message, which may have attached images.
|
||||||
#[derive(Clone, Debug, Default)]
|
#[derive(Clone, Debug, Default)]
|
||||||
@@ -168,13 +183,25 @@ struct SignalAlertMessage {
|
|||||||
text: String,
|
text: String,
|
||||||
attachment_paths: Vec<PathBuf>,
|
attachment_paths: Vec<PathBuf>,
|
||||||
/// Short summary for logging (e.g., alert names for prometheus).
|
/// Short summary for logging (e.g., alert names for prometheus).
|
||||||
/// If None, the consumer will use a truncated slice of `text` for logging.
|
summary: Summary,
|
||||||
summary: Option<String>,
|
|
||||||
/// Optional destination override from route configuration.
|
/// Optional destination override from route configuration.
|
||||||
/// If present, overrides the default alert destination.
|
/// If present, overrides the default alert destination.
|
||||||
destination_override: Option<Destination>,
|
destination_override: Option<Destination>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl SignalAlertMessage {
|
||||||
|
/// Get the summary string for logging.
|
||||||
|
fn get_summary(&self) -> &str {
|
||||||
|
match &self.summary {
|
||||||
|
Summary::Owned(s) => s,
|
||||||
|
Summary::Prefix(n) => {
|
||||||
|
let len = self.text.len().min(*n).min(512);
|
||||||
|
&self.text[..len]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// The gateway manages sending messages to signal-cli and receiving messages from signal-cli.
|
/// The gateway manages sending messages to signal-cli and receiving messages from signal-cli.
|
||||||
/// It maintains a queue of messages to be sent to all admins, generated by alerts etc.
|
/// It maintains a queue of messages to be sent to all admins, generated by alerts etc.
|
||||||
/// It also subscribes to messages received from signal and processes them one-by-one,
|
/// It also subscribes to messages received from signal and processes them one-by-one,
|
||||||
@@ -380,12 +407,7 @@ impl Gateway {
|
|||||||
},
|
},
|
||||||
outbound_admin_msg = signal_alert_mq_rx.recv() => {
|
outbound_admin_msg = signal_alert_mq_rx.recv() => {
|
||||||
if let Some(msg) = outbound_admin_msg {
|
if let Some(msg) = outbound_admin_msg {
|
||||||
// Log summary, or first 500 bytes of text if no summary provided
|
info!("Sending alert: {}", msg.get_summary());
|
||||||
let summary = msg.summary.as_deref().unwrap_or_else(|| {
|
|
||||||
let len = msg.text.len().min(500);
|
|
||||||
&msg.text[..len]
|
|
||||||
});
|
|
||||||
info!("Sending alert: {summary}");
|
|
||||||
// Prepend origin line if present
|
// Prepend origin line if present
|
||||||
let message = if let Some(origin) = &msg.origin {
|
let message = if let Some(origin) = &msg.origin {
|
||||||
format!("[{origin}]\n{}", msg.text)
|
format!("[{origin}]\n{}", msg.text)
|
||||||
@@ -767,7 +789,7 @@ impl Gateway {
|
|||||||
origin: None, // Prometheus alerts don't have a syslog origin
|
origin: None, // Prometheus alerts don't have a syslog origin
|
||||||
text,
|
text,
|
||||||
attachment_paths,
|
attachment_paths,
|
||||||
summary: Some(summary),
|
summary: Summary::Owned(summary.into()),
|
||||||
destination_override: None,
|
destination_override: None,
|
||||||
})
|
})
|
||||||
.map_err(|_err| {
|
.map_err(|_err| {
|
||||||
|
|||||||
Reference in New Issue
Block a user