From 53c502e75c75a4f7c555115ea667e9365da92ad2 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Fri, 3 Jul 2026 20:47:29 -0400 Subject: [PATCH] Add more fields to the pager alerting --- .../generate_deduplication_key.rs | 10 ++++++ .../src/notification/notification_details.rs | 33 +++++++++++++++++ .../notification_details_builder.rs | 36 +++++++++++++++++++ .../handlers/check_common_web_error.rs | 4 +++ .../handlers/check_status_code_fallback.rs | 4 +++ .../request_debugging_metadata.rs | 34 ++++++++++++++++++ 6 files changed, 121 insertions(+) diff --git a/crates/lib/pager/src/notification/generate_deduplication_key.rs b/crates/lib/pager/src/notification/generate_deduplication_key.rs index 4625cdb486..0ebb03c0e6 100644 --- a/crates/lib/pager/src/notification/generate_deduplication_key.rs +++ b/crates/lib/pager/src/notification/generate_deduplication_key.rs @@ -63,6 +63,11 @@ mod tests { media_file_token: None, inference_job_token: None, third_party_id: None, + http_host: None, + http_origin: None, + http_referer: None, + http_user_agent: None, + trace_id: None, request_ip_address: None, avt_cookie_token: None, session_token: None, @@ -228,6 +233,11 @@ mod tests { media_file_token: None, inference_job_token: None, third_party_id: None, + http_host: None, + http_origin: None, + http_referer: None, + http_user_agent: None, + trace_id: None, request_ip_address: None, avt_cookie_token: None, session_token: None, diff --git a/crates/lib/pager/src/notification/notification_details.rs b/crates/lib/pager/src/notification/notification_details.rs index c7f057dc89..da928959f2 100644 --- a/crates/lib/pager/src/notification/notification_details.rs +++ b/crates/lib/pager/src/notification/notification_details.rs @@ -37,6 +37,23 @@ pub struct NotificationDetails { /// HTTP status code associated with the event, if any. pub(crate) http_status_code: Option, + /// Hostname the client used to reach this server (from the `Host` or + /// forwarded-host headers), if any. Useful when a server is reachable + /// via multiple domains (eg. api.storyteller.ai, api.fakeyou.com). + pub(crate) http_host: Option, + + /// `Origin` header of the request, if any. For browser XHR/fetch this is + /// the scheme+host of the calling frontend. + pub(crate) http_origin: Option, + + /// `Referer` header of the request, if any. For browser XHR/fetch this is + /// the URL of the page that made the call (possibly truncated to the + /// origin by the browser's referrer policy). + pub(crate) http_referer: Option, + + /// `User-Agent` header of the request, if any. + pub(crate) http_user_agent: Option, + /// User token associated with the event, if any. pub(crate) user_token: Option, @@ -188,6 +205,22 @@ impl NotificationDetails { parts.push(format!("HTTP Status Code: {}", status_code)); } + if let Some(host) = &self.http_host { + parts.push(format!("HTTP Host: {}", host)); + } + + if let Some(origin) = &self.http_origin { + parts.push(format!("HTTP Origin: {}", origin)); + } + + if let Some(referer) = &self.http_referer { + parts.push(format!("HTTP Referer: {}", referer)); + } + + if let Some(user_agent) = &self.http_user_agent { + parts.push(format!("User Agent: {}", user_agent)); + } + if let Some(ip) = &self.request_ip_address { parts.push(format!("Request IP: {}", ip)); } diff --git a/crates/lib/pager/src/notification/notification_details_builder.rs b/crates/lib/pager/src/notification/notification_details_builder.rs index a45ae9d28d..34d4f8e9d4 100644 --- a/crates/lib/pager/src/notification/notification_details_builder.rs +++ b/crates/lib/pager/src/notification/notification_details_builder.rs @@ -21,6 +21,10 @@ pub struct NotificationDetailsBuilder { pub(crate) http_method: Option, pub(crate) http_path: Option, pub(crate) http_status_code: Option, + pub(crate) http_host: Option, + pub(crate) http_origin: Option, + pub(crate) http_referer: Option, + pub(crate) http_user_agent: Option, pub(crate) user_token: Option, pub(crate) media_file_token: Option, @@ -53,6 +57,10 @@ impl NotificationDetailsBuilder { http_method: None, http_path: None, http_status_code: None, + http_host: None, + http_origin: None, + http_referer: None, + http_user_agent: None, user_token: None, media_file_token: None, inference_job_token: None, @@ -84,6 +92,10 @@ impl NotificationDetailsBuilder { http_method: None, http_path: None, http_status_code: None, + http_host: None, + http_origin: None, + http_referer: None, + http_user_agent: None, user_token: None, media_file_token: None, inference_job_token: None, @@ -138,6 +150,26 @@ impl NotificationDetailsBuilder { self } + pub fn set_http_host(mut self, http_host: Option) -> Self { + self.http_host = http_host; + self + } + + pub fn set_http_origin(mut self, http_origin: Option) -> Self { + self.http_origin = http_origin; + self + } + + pub fn set_http_referer(mut self, http_referer: Option) -> Self { + self.http_referer = http_referer; + self + } + + pub fn set_http_user_agent(mut self, http_user_agent: Option) -> Self { + self.http_user_agent = http_user_agent; + self + } + pub fn set_user_token(mut self, user_token: Option) -> Self { self.user_token = user_token; self @@ -202,6 +234,10 @@ impl NotificationDetailsBuilder { http_method: self.http_method, http_path: self.http_path, http_status_code: self.http_status_code, + http_host: self.http_host, + http_origin: self.http_origin, + http_referer: self.http_referer, + http_user_agent: self.http_user_agent, user_token: self.user_token, media_file_token: self.media_file_token, inference_job_token: self.inference_job_token, diff --git a/crates/service/web/storyteller_web/src/http_server/middleware/error_alerting_middleware/handlers/check_common_web_error.rs b/crates/service/web/storyteller_web/src/http_server/middleware/error_alerting_middleware/handlers/check_common_web_error.rs index aa07a4b425..1567004390 100644 --- a/crates/service/web/storyteller_web/src/http_server/middleware/error_alerting_middleware/handlers/check_common_web_error.rs +++ b/crates/service/web/storyteller_web/src/http_server/middleware/error_alerting_middleware/handlers/check_common_web_error.rs @@ -46,6 +46,10 @@ pub(crate) fn check_common_web_error( .set_http_path(Some(path.to_string())) .set_trace_id(metadata.trace_id.clone()) .set_http_status_code(Some(500)) + .set_http_host(metadata.http_host.clone()) + .set_http_origin(metadata.http_origin.clone()) + .set_http_referer(metadata.http_referer.clone()) + .set_http_user_agent(metadata.http_user_agent.clone()) .set_request_ip_address(metadata.request_ip_address.clone()) .set_avt_cookie_token(metadata.avt_cookie_token.clone()) .set_session_token(metadata.session_token.clone()) diff --git a/crates/service/web/storyteller_web/src/http_server/middleware/error_alerting_middleware/handlers/check_status_code_fallback.rs b/crates/service/web/storyteller_web/src/http_server/middleware/error_alerting_middleware/handlers/check_status_code_fallback.rs index 780a423cbd..cdfbfd0fec 100644 --- a/crates/service/web/storyteller_web/src/http_server/middleware/error_alerting_middleware/handlers/check_status_code_fallback.rs +++ b/crates/service/web/storyteller_web/src/http_server/middleware/error_alerting_middleware/handlers/check_status_code_fallback.rs @@ -32,6 +32,10 @@ pub(crate) fn check_status_code_fallback( .set_http_path(Some(path.to_string())) .set_trace_id(metadata.trace_id.clone()) .set_http_status_code(Some(status_code)) + .set_http_host(metadata.http_host.clone()) + .set_http_origin(metadata.http_origin.clone()) + .set_http_referer(metadata.http_referer.clone()) + .set_http_user_agent(metadata.http_user_agent.clone()) .set_request_ip_address(metadata.request_ip_address.clone()) .set_avt_cookie_token(metadata.avt_cookie_token.clone()) .set_session_token(metadata.session_token.clone()) diff --git a/crates/service/web/storyteller_web/src/http_server/middleware/error_alerting_middleware/request_debugging_metadata.rs b/crates/service/web/storyteller_web/src/http_server/middleware/error_alerting_middleware/request_debugging_metadata.rs index 951c40e41b..abc1d3d39a 100644 --- a/crates/service/web/storyteller_web/src/http_server/middleware/error_alerting_middleware/request_debugging_metadata.rs +++ b/crates/service/web/storyteller_web/src/http_server/middleware/error_alerting_middleware/request_debugging_metadata.rs @@ -1,8 +1,10 @@ use std::sync::Arc; use actix_web::dev::ServiceRequest; +use actix_web::http::header::{HeaderName, ORIGIN, REFERER, USER_AGENT}; use actix_web::web; use actix_web::HttpMessage; +use actix_web::HttpRequest; use http_server_common::request::get_request_ip::get_request_ip; use log::debug; @@ -21,6 +23,16 @@ pub(crate) struct RequestDebuggingMetadata { pub session_user_token: Option, /// Set by `TraceIdMiddleware` (outermost middleware) on every request. pub trace_id: Option, + /// Hostname the client used to reach this server (Host / forwarded-host + /// headers). Distinguishes api.storyteller.ai vs api.fakeyou.com etc. + pub http_host: Option, + /// `Origin` header — scheme+host of the frontend page making the XHR call. + pub http_origin: Option, + /// `Referer` header — URL of the frontend page making the XHR call + /// (may be truncated to the origin by the browser's referrer policy). + pub http_referer: Option, + /// `User-Agent` header. + pub http_user_agent: Option, } impl RequestDebuggingMetadata { @@ -62,12 +74,34 @@ impl RequestDebuggingMetadata { .map(|payload| (Some(payload.session_token), payload.maybe_user_token)) .unwrap_or((None, None)); + // `connection_info()` prefers Forwarded / X-Forwarded-Host over the raw + // Host header, so this reflects the hostname the client actually used + // even behind the load balancer. + let http_host = Some(http_request.connection_info().host().to_string()) + .filter(|host| !host.is_empty()); + + let http_origin = get_header_value(http_request, ORIGIN); + let http_referer = get_header_value(http_request, REFERER); + let http_user_agent = get_header_value(http_request, USER_AGENT); + Self { request_ip_address, avt_cookie_token, session_token, session_user_token, trace_id, + http_host, + http_origin, + http_referer, + http_user_agent, } } } + +/// Read a header as a UTF-8 string; `None` if absent or not valid UTF-8. +fn get_header_value(request: &HttpRequest, header_name: HeaderName) -> Option { + request.headers() + .get(header_name) + .and_then(|value| value.to_str().ok()) + .map(|value| value.to_string()) +}