Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(capture): ignore an out-of-range offset instead of panicking (#116599)

Eli Reismand1afe36c23f9
diff --git a/rust/common/types/src/timestamp.rs b/rust/common/types/src/timestamp.rs
index 1d56926091ff..e8b8d1d2836e 100644
--- a/rust/common/types/src/timestamp.rs
+++ b/rust/common/types/src/timestamp.rs
@@ -105,8 +105,13 @@ fn handle_timestamp(
// Handle offset if present
if let Some(offset_ms) = offset {
- parsed_ts = now - Duration::milliseconds(offset_ms);
- source = TimestampSource::Offset;
+ // Chrono panics on a client offset outside its range, so such an offset is ignored.
+ let from_offset =
+ Duration::try_milliseconds(offset_ms).and_then(|age| now.checked_sub_signed(age));
+ if let Some(from_offset) = from_offset {
+ parsed_ts = from_offset;
+ source = TimestampSource::Offset;
+ }
}
ParsedTimestamp {
@@ -228,6 +233,32 @@ mod tests {
DateTime::parse_from_rfc3339(s).unwrap().with_timezone(&Utc)
}
+ #[test]
+ fn an_offset_out_of_range_is_ignored_rather_than_fatal() {
+ // `offset` is a client i64, and a panic here aborts the request, so the caller
+ // loses the whole batch.
+ let now = dt("2023-01-01T12:00:00Z");
+ for offset in [i64::MIN, i64::MIN + 1, i64::MAX, -8_300_000_000_000_000] {
+ let result = parse_event_timestamp(None, Some(offset), None, false, now);
+ assert_eq!(result.timestamp, now, "offset={offset}");
+ assert_eq!(result.source, TimestampSource::Now, "offset={offset}");
+
+ // A timestamp the client also sent still sets the event time.
+ let result =
+ parse_event_timestamp(Some("2023-01-01T11:00:00Z"), Some(offset), None, false, now);
+ assert_eq!(
+ result.timestamp,
+ dt("2023-01-01T11:00:00Z"),
+ "offset={offset}"
+ );
+ assert_eq!(
+ result.source,
+ TimestampSource::ClientTimestamp,
+ "offset={offset}"
+ );
+ }
+ }
+
#[test]
fn unparseable_timestamp_reports_the_now_fallback() {
// A caller inferring the source from argument presence reads this as a