Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref(relay): Use DateTime instead of Instant for start_time #4184

Merged
merged 12 commits into from
Oct 30, 2024

Conversation

iambriccardo
Copy link
Member

@iambriccardo iambriccardo commented Oct 28, 2024

This PR changes the start_time field of several components within Relay from type Instant to DateTime<Utc> and renames it to received_at.

Closes: https://github.com/getsentry/team-ingest/issues/564

@iambriccardo iambriccardo changed the title Implement ref(relay): Use DateTime instead of Instant for start_time Oct 28, 2024
@iambriccardo iambriccardo changed the title ref(relay): Use DateTime instead of Instant for start_time ref(relay): Use DateTime instead of Instant for start_time Oct 28, 2024
Comment on lines 23 to 28
/// Returns the [`ReceivedAt`] corresponding to provided timestamp.
pub fn from_timestamp_millis(timestamp: i64) -> Self {
let datetime = DateTime::<Utc>::from_timestamp_millis(timestamp).unwrap_or_else(Utc::now);

Self(datetime)
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was changed.

Comment on lines 53 to 64
fn start_time_from_timestamp() {
let elapsed = Duration::from_secs(10);
let now = Utc::now();
let past = now - chrono::Duration::from_std(elapsed).unwrap();
let start_time = ReceivedAt::from_timestamp_millis(past.timestamp_millis()).into_inner();

// Check that the difference between the now and generated start_time is about 10s
let diff = now - start_time;
let diff_duration = diff.to_std().unwrap();
assert!(diff_duration < elapsed + Duration::from_millis(50));
assert!(diff_duration > elapsed - Duration::from_millis(50));
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test was changed with the same logic.

@@ -26,7 +29,7 @@ pub async fn metrics(mut request: Request, next: Next) -> Response {
let response = next.run(request).await;

relay_statsd::metric!(
timer(RelayTimers::RequestsDuration) = start_time.into_inner().elapsed(),
timer(RelayTimers::RequestsDuration) = request_start.elapsed(),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we now use received_at as DateTime I wanted to still use an Instant for monitoring the request duration.

sent_at,
source,
} = message;

let received_timestamp = UnixTimestamp::from_instant(start_time);
let received_timestamp =
Copy link
Member Author

@iambriccardo iambriccardo Oct 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I deemed the unwrap_or as a safe operation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by this? Why wouldn't it be safe?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By safe I meant semantically, meaning if it makes sense to default to now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking, defaulting to 1970-01-01 (UnixTimestamp::from_secs(0)) would make sense semantically, because from_datetime returns None only if the datetime is less than that. Either way it's a programming error or data corruption if it happens, since we control received_at ourselves.

@iambriccardo iambriccardo marked this pull request as ready for review October 29, 2024 08:45
@iambriccardo iambriccardo requested a review from a team as a code owner October 29, 2024 08:46
relay-common/src/time.rs Show resolved Hide resolved
relay-server/src/services/buffer/envelope_store/sqlite.rs Outdated Show resolved Hide resolved
relay-server/src/utils/managed_envelope.rs Outdated Show resolved Hide resolved
relay-server/src/services/processor.rs Outdated Show resolved Hide resolved
@iambriccardo iambriccardo merged commit 0adf112 into master Oct 30, 2024
24 checks passed
@iambriccardo iambriccardo deleted the riccardo/feat/split-dates branch October 30, 2024 09:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants