alternative summary specification

This commit is contained in:
Chris Beck
2025-12-05 22:16:31 -07:00
parent 7a163f3962
commit 04ad4035f9
2 changed files with 33 additions and 11 deletions
+2 -2
View File
@@ -1,6 +1,6 @@
use super::log_buffer::LogBuffer;
use super::route::{Destination, Limit, Route};
use super::{LimitResult, Limiter, LimiterSet, SignalAlertMessage};
use super::{LimitResult, Limiter, LimiterSet, SignalAlertMessage, Summary};
use crate::{
concurrent_map::ConcurrentMap,
log_format::LogFormatConfig,
@@ -203,7 +203,7 @@ impl LogHandler {
origin: Some(origin),
text,
attachment_paths: Default::default(),
summary: None,
summary: Summary::Prefix(512),
destination_override,
}) {
error!("Could not send alert message, queue is closed");
+31 -9
View File
@@ -159,6 +159,21 @@ fn parse_gateway_command(s: &str) -> Result<GatewayCommand, 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.
/// This is generally an alert message, which may have attached images.
#[derive(Clone, Debug, Default)]
@@ -168,13 +183,25 @@ struct SignalAlertMessage {
text: String,
attachment_paths: Vec<PathBuf>,
/// Short summary for logging (e.g., alert names for prometheus).
/// If None, the consumer will use a truncated slice of `text` for logging.
summary: Option<String>,
summary: Summary,
/// Optional destination override from route configuration.
/// If present, overrides the default alert 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.
/// 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,
@@ -380,12 +407,7 @@ impl Gateway {
},
outbound_admin_msg = signal_alert_mq_rx.recv() => {
if let Some(msg) = outbound_admin_msg {
// Log summary, or first 500 bytes of text if no summary provided
let summary = msg.summary.as_deref().unwrap_or_else(|| {
let len = msg.text.len().min(500);
&msg.text[..len]
});
info!("Sending alert: {summary}");
info!("Sending alert: {}", msg.get_summary());
// Prepend origin line if present
let message = if let Some(origin) = &msg.origin {
format!("[{origin}]\n{}", msg.text)
@@ -767,7 +789,7 @@ impl Gateway {
origin: None, // Prometheus alerts don't have a syslog origin
text,
attachment_paths,
summary: Some(summary),
summary: Summary::Owned(summary.into()),
destination_override: None,
})
.map_err(|_err| {