Skip to content

Commit ecdb24a

Browse files
authored
ref: Replace chrono usage with time/SystemTime (#409)
1 parent 16a5e5a commit ecdb24a

File tree

15 files changed

+275
-123
lines changed

15 files changed

+275
-123
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ jobs:
9999
fail-fast: false
100100
matrix:
101101
os: [ubuntu-latest, macos-latest, windows-latest]
102-
rust: [1.51.0]
102+
rust: [1.53.0]
103103

104104
name: Check / Test MSRV ${{ matrix.rust }} on ${{ matrix.os }}
105105
runs-on: ${{ matrix.os }}

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
**Breaking Changes**:
66

7-
- The minium supported Rust version was bumped to **1.51.0** due to requirements from dependencies.
7+
- The minium supported Rust version was bumped to **1.53.0** due to requirements from dependencies.
88
- The `backtrace` feature of `sentry-anyhow` is enabled by default. ([#362](https://github.com/getsentry/sentry-rust/pull/362))
99
- The `tracing-subscriber` dependency of `sentry-tracing` has been bumped to version `0.3.x`. ([#377](https://github.com/getsentry/sentry-rust/pull/377))
1010
- `Scope::add_event_processor` now takes a generic parameter instead of a boxed function.([#380](https://github.com/getsentry/sentry-rust/pull/380))

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ best API and adding new features.
9494
We currently only verify this crate against a recent version of Sentry hosted on [sentry.io](https://sentry.io/) but it
9595
should work with on-prem Sentry versions 20.6 and later.
9696

97-
The **Minimum Supported Rust Version** is currently at _1.51.0_.
97+
The **Minimum Supported Rust Version** is currently at _1.53.0_.
9898
The Sentry crates will support a _6 month_ old Rust version at time of release,
9999
and the MSRV will be increased in accordance with its dependencies.
100100

clippy.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
msrv = "1.51"
1+
msrv = "1.53"

sentry-core/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ test = ["client"]
2929
[dependencies]
3030
sentry-types = { version = "0.23.0", path = "../sentry-types" }
3131
serde = { version = "1.0.104", features = ["derive"] }
32-
chrono = { version = "0.4.13", default-features = false }
3332
lazy_static = "1.4.0"
3433
rand = { version = "0.8.1", optional = true }
3534
serde_json = "1.0.46"

sentry-core/src/session.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
use std::collections::HashMap;
66
use std::sync::{Arc, Condvar, Mutex, MutexGuard};
77
use std::thread::JoinHandle;
8-
use std::time::{Duration, Instant};
9-
10-
use chrono::{Duration as ChronoDuration, DurationRound};
8+
use std::time::{Duration, Instant, SystemTime};
119

1210
use crate::client::TransportArc;
1311
use crate::clientoptions::SessionMode;
@@ -16,7 +14,7 @@ use crate::protocol::{
1614
SessionStatus, SessionUpdate,
1715
};
1816
use crate::scope::StackLayer;
19-
use crate::types::{DateTime, Utc, Uuid};
17+
use crate::types::Uuid;
2018
use crate::{Client, Envelope};
2119

2220
#[derive(Clone, Debug)]
@@ -56,7 +54,7 @@ impl Session {
5654
distinct_id,
5755
sequence: None,
5856
timestamp: None,
59-
started: Utc::now(),
57+
started: SystemTime::now(),
6058
init: true,
6159
duration: None,
6260
status: SessionStatus::Ok,
@@ -164,7 +162,7 @@ impl From<AggregatedSessions> for EnvelopeItem {
164162

165163
#[derive(Debug, PartialEq, Eq, Hash)]
166164
struct AggregationKey {
167-
started: DateTime<Utc>,
165+
started: SystemTime,
168166
distinct_id: Option<String>,
169167
}
170168

@@ -257,9 +255,13 @@ impl SessionFlusher {
257255
attributes: session_update.attributes.clone(),
258256
});
259257

260-
let started = session_update
258+
let duration = session_update
261259
.started
262-
.duration_trunc(ChronoDuration::minutes(1))
260+
.duration_since(SystemTime::UNIX_EPOCH)
261+
.unwrap();
262+
let duration = (duration.as_secs() / 60) * 60;
263+
let started = SystemTime::UNIX_EPOCH
264+
.checked_add(Duration::from_secs(duration))
263265
.unwrap();
264266

265267
let key = AggregationKey {

sentry-types/Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ default = ["protocol"]
2020
protocol = []
2121

2222
[dependencies]
23-
thiserror = "1.0.15"
23+
debugid = { version = "0.7.2", features = ["serde"] }
24+
getrandom = "0.2.3"
25+
hex = "0.4.3"
2426
serde = { version = "1.0.104", features = ["derive"] }
2527
serde_json = "1.0.46"
28+
thiserror = "1.0.15"
29+
time = { version = "0.3.5", features = ["formatting", "parsing"] }
2630
url = { version = "2.1.1", features = ["serde"] }
27-
chrono = { version = "0.4.13", default-features = false, features = ["clock", "std", "serde"] }
2831
uuid = { version = "0.8.1", features = ["v4", "serde"] }
29-
debugid = { version = "0.7.2", features = ["serde"] }
30-
getrandom = "0.2.3"
31-
hex = "0.4.3"

sentry-types/src/auth.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::borrow::Cow;
22
use std::fmt;
33
use std::str::FromStr;
4+
use std::time::SystemTime;
45

5-
use chrono::{DateTime, Utc};
66
use serde::{Deserialize, Serialize};
77
use thiserror::Error;
88
use url::form_urlencoded;
@@ -29,7 +29,7 @@ pub enum ParseAuthError {
2929
#[derive(Debug, Serialize, Deserialize, Clone)]
3030
pub struct Auth {
3131
#[serde(skip)]
32-
timestamp: Option<DateTime<Utc>>,
32+
timestamp: Option<SystemTime>,
3333
#[serde(rename = "sentry_client")]
3434
client: Option<String>,
3535
#[serde(rename = "sentry_version")]
@@ -60,11 +60,7 @@ impl Auth {
6060
let value = value.into();
6161
match key.as_ref() {
6262
"sentry_timestamp" => {
63-
let timestamp = value
64-
.parse()
65-
.ok()
66-
.and_then(|ts| timestamp_to_datetime(ts).single())
67-
.or_else(|| value.parse().ok());
63+
let timestamp = value.parse().ok().and_then(timestamp_to_datetime);
6864

6965
rv.timestamp = timestamp;
7066
}
@@ -100,8 +96,8 @@ impl Auth {
10096
Auth::from_pairs(form_urlencoded::parse(qs))
10197
}
10298

103-
/// Returns the unix timestamp the client defined
104-
pub fn timestamp(&self) -> Option<DateTime<Utc>> {
99+
/// Returns the timestamp the client defined
100+
pub fn timestamp(&self) -> Option<SystemTime> {
105101
self.timestamp
106102
}
107103

@@ -179,7 +175,7 @@ impl FromStr for Auth {
179175

180176
pub(crate) fn auth_from_dsn_and_client(dsn: &Dsn, client: Option<&str>) -> Auth {
181177
Auth {
182-
timestamp: Some(Utc::now()),
178+
timestamp: Some(SystemTime::now()),
183179
client: client.map(|x| x.to_string()),
184180
version: protocol::LATEST,
185181
key: dsn.public_key().to_string(),

sentry-types/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,5 @@ pub use crate::dsn::*;
5050
pub use crate::project_id::*;
5151

5252
// Re-export external types and traits for convenience
53-
pub use chrono::{DateTime, ParseError as ChronoParseError, TimeZone, Utc};
5453
pub use debugid::*;
5554
pub use uuid::{Uuid, Variant as UuidVariant, Version as UuidVersion};

sentry-types/src/protocol/envelope.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,10 @@ impl From<Transaction<'static>> for Envelope {
250250

251251
#[cfg(test)]
252252
mod test {
253-
use chrono::{DateTime, Utc};
253+
use std::time::{Duration, SystemTime};
254+
255+
use time::format_description::well_known::Rfc3339;
256+
use time::OffsetDateTime;
254257

255258
use super::*;
256259
use crate::protocol::v7::{SessionAttributes, SessionStatus, Span};
@@ -261,6 +264,14 @@ mod test {
261264
String::from_utf8_lossy(&vec).to_string()
262265
}
263266

267+
fn timestamp(s: &str) -> SystemTime {
268+
let dt = OffsetDateTime::parse(s, &Rfc3339).unwrap();
269+
let secs = dt.unix_timestamp() as u64;
270+
let nanos = dt.nanosecond();
271+
let duration = Duration::new(secs, nanos);
272+
SystemTime::UNIX_EPOCH.checked_add(duration).unwrap()
273+
}
274+
264275
#[test]
265276
fn test_empty() {
266277
assert_eq!(to_str(Envelope::new()), "{}\n");
@@ -269,7 +280,7 @@ mod test {
269280
#[test]
270281
fn test_event() {
271282
let event_id = Uuid::parse_str("22d00b3f-d1b1-4b5d-8d20-49d138cd8a9c").unwrap();
272-
let timestamp = "2020-07-20T14:51:14.296Z".parse::<DateTime<Utc>>().unwrap();
283+
let timestamp = timestamp("2020-07-20T14:51:14.296Z");
273284
let event = Event {
274285
event_id,
275286
timestamp,
@@ -288,7 +299,7 @@ mod test {
288299
#[test]
289300
fn test_session() {
290301
let session_id = Uuid::parse_str("22d00b3f-d1b1-4b5d-8d20-49d138cd8a9c").unwrap();
291-
let started = "2020-07-20T14:51:14.296Z".parse::<DateTime<Utc>>().unwrap();
302+
let started = timestamp("2020-07-20T14:51:14.296Z");
292303
let session = SessionUpdate {
293304
session_id,
294305
distinct_id: Some("[email protected]".to_owned()),
@@ -322,7 +333,7 @@ mod test {
322333
let event_id = Uuid::parse_str("22d00b3f-d1b1-4b5d-8d20-49d138cd8a9c").unwrap();
323334
let span_id = "d42cee9fc3e74f5c".parse().unwrap();
324335
let trace_id = "335e53d614474acc9f89e632b776cc28".parse().unwrap();
325-
let start_timestamp = "2020-07-20T14:51:14.296Z".parse::<DateTime<Utc>>().unwrap();
336+
let start_timestamp = timestamp("2020-07-20T14:51:14.296Z");
326337
let spans = vec![Span {
327338
span_id,
328339
trace_id,

sentry-types/src/protocol/session.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ use std::borrow::Cow;
22
use std::fmt;
33
use std::net::IpAddr;
44
use std::str;
5+
use std::time::SystemTime;
56

6-
use chrono::{DateTime, Utc};
77
use serde::{Deserialize, Serialize};
88
use thiserror::Error;
99
use uuid::Uuid;
1010

11+
use crate::utils::{ts_rfc3339, ts_rfc3339_opt};
12+
1113
/// The Status of a Release Health Session.
1214
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
1315
#[serde(rename_all = "snake_case")]
@@ -102,12 +104,16 @@ pub struct SessionUpdate<'a> {
102104
pub sequence: Option<u64>,
103105

104106
/// The timestamp of when the session change event was created.
105-
#[serde(default, skip_serializing_if = "Option::is_none")]
106-
pub timestamp: Option<DateTime<Utc>>,
107+
#[serde(
108+
default,
109+
skip_serializing_if = "Option::is_none",
110+
with = "ts_rfc3339_opt"
111+
)]
112+
pub timestamp: Option<SystemTime>,
107113

108114
/// The timestamp of when the session itself started.
109-
#[serde(default = "Utc::now")]
110-
pub started: DateTime<Utc>,
115+
#[serde(default = "SystemTime::now", with = "ts_rfc3339")]
116+
pub started: SystemTime,
111117

112118
/// A flag that indicates that this is the initial transmission of the session.
113119
#[serde(default, skip_serializing_if = "is_false")]
@@ -139,7 +145,8 @@ fn is_zero(val: &u32) -> bool {
139145
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
140146
pub struct SessionAggregateItem {
141147
/// The timestamp of when the session itself started.
142-
pub started: DateTime<Utc>,
148+
#[serde(with = "ts_rfc3339")]
149+
pub started: SystemTime,
143150
/// The distinct identifier.
144151
#[serde(rename = "did", default, skip_serializing_if = "Option::is_none")]
145152
pub distinct_id: Option<String>,

0 commit comments

Comments
 (0)