Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 46 additions & 19 deletions crates/trapfall-ingest/src/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,27 +143,27 @@ fn parse_envelope_binary(data: &[u8]) -> Result<ParsedEnvelope> {
};

match hdr.item_type.as_str() {
"event" => {
if let Ok(event) = serde_json::from_str::<Event>(body_text) {
result.events.push(event);
"event" => match serde_json::from_str::<Event>(body_text) {
Ok(event) => result.events.push(event),
Err(e) => tracing::warn!(item = "event", error = %e, "dropping event: deserialize failed"),
},
"transaction" => match serde_json::from_str::<Transaction>(body_text) {
Ok(txn) => result.transactions.push(txn),
Err(e) => {
tracing::warn!(item = "transaction", error = %e, "dropping transaction: deserialize failed")
}
}
"transaction" => {
if let Ok(txn) = serde_json::from_str::<Transaction>(body_text) {
result.transactions.push(txn);
}
}
"session" => {
if let Ok(session) = serde_json::from_str::<SessionUpdate>(body_text) {
result.session_updates.push(session);
}
}
"sessions" => {
if let Ok(aggregates) = serde_json::from_str::<SessionAggregates>(body_text) {
result.session_aggregates.push(aggregates);
},
"session" => match serde_json::from_str::<SessionUpdate>(body_text) {
Ok(session) => result.session_updates.push(session),
Err(e) => tracing::warn!(item = "session", error = %e, "dropping session: deserialize failed"),
},
"sessions" => match serde_json::from_str::<SessionAggregates>(body_text) {
Ok(aggregates) => result.session_aggregates.push(aggregates),
Err(e) => {
tracing::warn!(item = "sessions", error = %e, "dropping session aggregates: deserialize failed")
}
}
_ => {} // Ignore unknown item types.
},
_ => tracing::debug!(item_type = %hdr.item_type, "ignoring unknown envelope item type"),
}
}
}
Expand Down Expand Up @@ -298,6 +298,33 @@ mod tests {
assert_eq!(result.events[0].message.as_deref(), Some("hello"));
}

#[test]
fn parse_event_message_as_object_sentry_dart_format() {
// Modern SDKs (Dart/Flutter, JS, etc.) send `message` as the
// Sentry Message interface object, not a plain string. Without the
// custom deserializer this whole event is silently dropped.
let envelope = r#"{"event_id":"abc123","sent_at":"2026-07-01T00:00:00Z"}
{"type":"event"}
{"event_id":"abc123","message":{"formatted":"Null check operator used on a null value","message":"%s"},"level":"error","platform":"dart"}"#;

let result = parse_envelope_text(envelope).unwrap();
assert_eq!(result.events.len(), 1, "message object must not drop the event");
assert_eq!(result.events[0].event_id, "abc123");
assert_eq!(result.events[0].message.as_deref(), Some("Null check operator used on a null value"));
}

#[test]
fn parse_event_message_object_falls_back_to_message_key() {
// When `formatted` is absent, fall back to the `message` template.
let envelope = r#"{"event_id":"abc123","sent_at":"2026-07-01T00:00:00Z"}
{"type":"event"}
{"event_id":"abc123","message":{"message":"template only"},"level":"error"}"#;

let result = parse_envelope_text(envelope).unwrap();
assert_eq!(result.events.len(), 1);
assert_eq!(result.events[0].message.as_deref(), Some("template only"));
}

#[test]
fn parse_envelope_session_and_event_together() {
let envelope = r#"{"event_id":"abc123","sent_at":"2026-01-01T00:00:00Z"}
Expand Down
25 changes: 25 additions & 0 deletions crates/trapfall-proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ pub struct Event {
#[serde(default)]
pub breadcrumbs: Breadcrumbs,
pub exception: Option<ExceptionValues>,
#[serde(default, deserialize_with = "deserialize_message", alias = "Message")]
pub message: Option<String>,
#[serde(default)]
pub tags: serde_json::Value,
Expand All @@ -135,6 +136,30 @@ pub struct Event {
pub timestamp: Option<String>,
}

/// Deserialize a Sentry event `message` field that may arrive either as a
/// plain string (minimal SDKs) or as the `Message` interface object
/// `{ "formatted": "...", "message": "..." }` (modern SDKs incl. Dart/Flutter).
///
/// We collapse both forms into the formatted display string, preferring
/// `formatted`, then `message`, then falling back to `None`.
/// Without this, a `message` object silently fails whole-event
/// deserialization (serde "invalid type: map, expected a string") and the
/// event is dropped.
pub fn deserialize_message<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
where
D: serde::Deserializer<'de>,
{
let value = Option::<serde_json::Value>::deserialize(deserializer)?;
Ok(value.and_then(|v| match v {
serde_json::Value::String(s) => Some(s),
serde_json::Value::Object(map) => map
.get("formatted")
.and_then(|f| f.as_str().map(str::to_string))
.or_else(|| map.get("message").and_then(|m| m.as_str().map(str::to_string))),
_ => None,
}))
}

/// Wrapper for exception values (Sentry format).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExceptionValues {
Expand Down
Loading