feat: harden community production foundation through story 1.5
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
collections::BTreeMap,
|
||||
env, fmt,
|
||||
fmt,
|
||||
future::Future,
|
||||
time::{Duration, SystemTime},
|
||||
};
|
||||
@@ -13,11 +13,8 @@ use sentry::{
|
||||
};
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::{
|
||||
RedactionLimits, ServiceIdentity, propagation::current_trace_id, redaction::truncate_string,
|
||||
};
|
||||
use crate::{RedactionLimits, ServiceIdentity, propagation::current_trace_id};
|
||||
|
||||
const SENTRY_DSN_ENV: &str = "CRANK_SENTRY_DSN";
|
||||
const CRITICAL_ERROR_MESSAGE: &str = "critical error";
|
||||
const SENTRY_SHUTDOWN_TIMEOUT: Duration = Duration::from_secs(2);
|
||||
// Sentry serializes SystemTime as a finite f64; this keeps conservative fixed headroom.
|
||||
@@ -25,6 +22,7 @@ const MAX_SERIALIZED_TIMESTAMP_BYTES: usize = 32;
|
||||
|
||||
tokio::task_local! {
|
||||
static REQUEST_ID: String;
|
||||
static TRACE_ID: String;
|
||||
}
|
||||
|
||||
pub struct SentryConfig {
|
||||
@@ -43,14 +41,6 @@ impl SentryConfig {
|
||||
Ok(Self { dsn: Some(dsn) })
|
||||
}
|
||||
|
||||
pub fn from_env() -> Result<Self, SentryConfigError> {
|
||||
match env::var(SENTRY_DSN_ENV) {
|
||||
Ok(value) => Self::parse(Some(&value)),
|
||||
Err(env::VarError::NotPresent) => Self::parse(None),
|
||||
Err(env::VarError::NotUnicode(_)) => Err(SentryConfigError::InvalidEnvironmentEncoding),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn enabled(&self) -> bool {
|
||||
self.dsn.is_some()
|
||||
}
|
||||
@@ -69,8 +59,6 @@ impl fmt::Debug for SentryConfig {
|
||||
pub enum SentryConfigError {
|
||||
#[error("CRANK_SENTRY_DSN is not a valid Sentry DSN")]
|
||||
InvalidDsn,
|
||||
#[error("CRANK_SENTRY_DSN is not valid UTF-8")]
|
||||
InvalidEnvironmentEncoding,
|
||||
#[error("critical error event budget cannot hold the required fields")]
|
||||
EventBudgetTooSmall,
|
||||
}
|
||||
@@ -123,11 +111,43 @@ pub fn capture_critical_error(category: CriticalErrorCategory) {
|
||||
});
|
||||
}
|
||||
|
||||
pub async fn with_request_correlation<F>(request_id: String, future: F) -> F::Output
|
||||
pub async fn with_request_correlation<F>(
|
||||
request_id: String,
|
||||
trace_id: String,
|
||||
future: F,
|
||||
) -> F::Output
|
||||
where
|
||||
F: Future,
|
||||
{
|
||||
REQUEST_ID.scope(request_id, future).await
|
||||
if !valid_request_id(&request_id) || !valid_trace_id(&trace_id) {
|
||||
return future.await;
|
||||
}
|
||||
REQUEST_ID
|
||||
.scope(request_id, TRACE_ID.scope(trace_id, future))
|
||||
.await
|
||||
}
|
||||
|
||||
fn valid_request_id(value: &str) -> bool {
|
||||
!value.is_empty()
|
||||
&& value.len() <= 128
|
||||
&& value
|
||||
.bytes()
|
||||
.all(|byte| (0x21..=0x7e).contains(&byte) && byte != b',' && byte != b';')
|
||||
}
|
||||
|
||||
fn valid_trace_id(value: &str) -> bool {
|
||||
value.len() == 32
|
||||
&& value != "00000000000000000000000000000000"
|
||||
&& value
|
||||
.bytes()
|
||||
.all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
|
||||
}
|
||||
|
||||
pub fn current_request_correlation() -> (Option<String>, Option<String>) {
|
||||
(
|
||||
REQUEST_ID.try_with(Clone::clone).ok(),
|
||||
TRACE_ID.try_with(Clone::clone).ok(),
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn init_sentry(
|
||||
@@ -184,16 +204,7 @@ fn sanitize_event(
|
||||
CriticalErrorCategory::Panic
|
||||
}
|
||||
});
|
||||
let mut tags = correlation_tags()
|
||||
.into_iter()
|
||||
.map(|(key, value)| (key, truncate_string(&value, limits.max_string_bytes)))
|
||||
.collect::<BTreeMap<_, _>>();
|
||||
for key in ["request_id", "trace_id"] {
|
||||
if let Some(value) = event.tags.get(key) {
|
||||
tags.entry(key.to_owned())
|
||||
.or_insert_with(|| truncate_string(value, limits.max_string_bytes));
|
||||
}
|
||||
}
|
||||
let mut tags = correlation_tags().into_iter().collect::<BTreeMap<_, _>>();
|
||||
tags.insert("service".to_owned(), identity.service().to_owned());
|
||||
tags.insert("category".to_owned(), category.as_str().to_owned());
|
||||
|
||||
@@ -222,19 +233,21 @@ fn correlation_tags() -> BTreeMap<String, String> {
|
||||
if let Ok(request_id) = REQUEST_ID.try_with(Clone::clone) {
|
||||
tags.insert("request_id".to_owned(), request_id);
|
||||
}
|
||||
if let Some(trace_id) = current_trace_id() {
|
||||
if let Ok(trace_id) = TRACE_ID.try_with(Clone::clone) {
|
||||
tags.insert("trace_id".to_owned(), trace_id);
|
||||
} else if let Some(trace_id) = current_trace_id() {
|
||||
tags.insert("trace_id".to_owned(), trace_id);
|
||||
}
|
||||
tags
|
||||
}
|
||||
|
||||
fn enforce_event_budget(mut event: Event<'static>, max_event_bytes: usize) -> Event<'static> {
|
||||
fn enforce_event_budget(event: Event<'static>, max_event_bytes: usize) -> Event<'static> {
|
||||
if serialized_event_len(&event) <= max_event_bytes {
|
||||
return event;
|
||||
}
|
||||
|
||||
event.tags.remove("request_id");
|
||||
event.tags.remove("trace_id");
|
||||
// Startup validation reserves enough room for maximum canonical IDs. They
|
||||
// are never evicted from a support event to satisfy a byte budget.
|
||||
debug_assert!(serialized_event_len(&event) <= max_event_bytes);
|
||||
event
|
||||
}
|
||||
@@ -257,7 +270,7 @@ fn required_critical_event_budget(identity: &ServiceIdentity, limits: RedactionL
|
||||
CriticalErrorCategory::ALL
|
||||
.into_iter()
|
||||
.map(|category| {
|
||||
let event = sanitize_event(
|
||||
let mut event = sanitize_event(
|
||||
Event {
|
||||
tags: BTreeMap::from([("category".to_owned(), category.as_str().to_owned())]),
|
||||
timestamp: SystemTime::UNIX_EPOCH,
|
||||
@@ -266,6 +279,8 @@ fn required_critical_event_budget(identity: &ServiceIdentity, limits: RedactionL
|
||||
identity,
|
||||
unbounded_limits,
|
||||
);
|
||||
event.tags.insert("request_id".to_owned(), "r".repeat(128));
|
||||
event.tags.insert("trace_id".to_owned(), "a".repeat(32));
|
||||
serialized_event_len(&event)
|
||||
.saturating_add(MAX_SERIALIZED_TIMESTAMP_BYTES.saturating_sub(1))
|
||||
})
|
||||
@@ -439,11 +454,15 @@ mod tests {
|
||||
let events = sentry::test::with_captured_events_options(
|
||||
|| {
|
||||
tracing::dispatcher::with_default(&dispatch, || {
|
||||
runtime.block_on(with_request_correlation("request-123".to_owned(), async {
|
||||
let span = tracing::info_span!(target: "crank::trace", "http.request");
|
||||
let _span_guard = span.enter();
|
||||
capture_critical_error(CriticalErrorCategory::DataIntegrity);
|
||||
}));
|
||||
runtime.block_on(with_request_correlation(
|
||||
"request-123".to_owned(),
|
||||
"0af7651916cd43dd8448eb211c80319c".to_owned(),
|
||||
async {
|
||||
let span = tracing::info_span!(target: "crank::trace", "http.request");
|
||||
let _span_guard = span.enter();
|
||||
capture_critical_error(CriticalErrorCategory::DataIntegrity);
|
||||
},
|
||||
));
|
||||
});
|
||||
},
|
||||
options,
|
||||
@@ -496,6 +515,7 @@ mod tests {
|
||||
tracing::dispatcher::with_default(&dispatch, || {
|
||||
runtime.block_on(with_request_correlation(
|
||||
"panic-request-123".to_owned(),
|
||||
"0af7651916cd43dd8448eb211c80319c".to_owned(),
|
||||
async {
|
||||
let span =
|
||||
tracing::info_span!(target: "crank::trace", "http.request");
|
||||
|
||||
Reference in New Issue
Block a user