Skip to content

Commit 048b838

Browse files
authored
Use rand instead of getrandom for IDs (#614)
The `getrandom` crate uses syscalls to get *true* randomness from the OS. We do not need that high quality of randomness, we do not even need cryptographically secure randomness. This switches to the `rand` crate which uses a reseeding PRNG which is still cryptographically secure though. This is much faster, and we can switch to an even simpler PRNG if it is not fast enough still.
1 parent 64949dc commit 048b838

File tree

9 files changed

+19
-23
lines changed

9 files changed

+19
-23
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sentry-core/src/api.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ use crate::{Hub, Integration, IntoBreadcrumbs, Scope};
1717
///
1818
/// ```
1919
/// use sentry::protocol::{Event, Level};
20-
/// use sentry::types::Uuid;
20+
/// use sentry::types::{Uuid, random_uuid};
2121
///
22-
/// let uuid = Uuid::new_v4();
22+
/// let uuid = random_uuid();
2323
/// let event = Event {
2424
/// event_id: uuid,
2525
/// message: Some("Hello World!".into()),

sentry-core/src/client.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::time::Duration;
88

99
use rand::random;
1010
use sentry_types::protocol::v7::SessionUpdate;
11+
use sentry_types::random_uuid;
1112

1213
use crate::constants::SDK_INFO;
1314
use crate::protocol::{ClientSdkInfo, Event};
@@ -161,7 +162,7 @@ impl Client {
161162
// event_id and sdk_info are set before the processors run so that the
162163
// processors can poke around in that data.
163164
if event.event_id.is_nil() {
164-
event.event_id = Uuid::new_v4();
165+
event.event_id = random_uuid();
165166
}
166167

167168
if event.sdk.is_none() {

sentry-core/src/performance.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ impl Transaction {
406406
Some(client) => (
407407
client.is_transaction_sampled(&ctx),
408408
Some(protocol::Transaction {
409-
name: Some(ctx.name.clone()),
409+
name: Some(ctx.name),
410410
..Default::default()
411411
}),
412412
),

sentry-core/src/session.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::protocol::{
1414
SessionStatus, SessionUpdate,
1515
};
1616
use crate::scope::StackLayer;
17-
use crate::types::Uuid;
17+
use crate::types::random_uuid;
1818
use crate::{Client, Envelope};
1919

2020
#[derive(Clone, Debug)]
@@ -50,7 +50,7 @@ impl Session {
5050
Some(Self {
5151
client: client.clone(),
5252
session_update: SessionUpdate {
53-
session_id: Uuid::new_v4(),
53+
session_id: random_uuid(),
5454
distinct_id,
5555
sequence: None,
5656
timestamp: None,

sentry-types/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ protocol = []
2222

2323
[dependencies]
2424
debugid = { version = "0.8.0", features = ["serde"] }
25-
getrandom = "0.2.3"
2625
hex = "0.4.3"
26+
rand = "0.8.5"
2727
serde = { version = "1.0.104", features = ["derive"] }
2828
serde_json = "1.0.46"
2929
thiserror = "1.0.15"
3030
time = { version = "0.3.5", features = ["formatting", "parsing"] }
3131
url = { version = "2.1.1", features = ["serde"] }
32-
uuid = { version = "1.0.0", features = ["v4", "serde"] }
32+
uuid = { version = "1.0.0", features = ["serde"] }

sentry-types/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,8 @@ pub use crate::project_id::*;
5252
// Re-export external types and traits for convenience
5353
pub use debugid::*;
5454
pub use uuid::{Uuid, Variant as UuidVariant, Version as UuidVersion};
55+
56+
/// Generates a random [Uuid] using a thread local RNG.
57+
pub fn random_uuid() -> Uuid {
58+
uuid::Builder::from_random_bytes(rand::random()).into_uuid()
59+
}

sentry-types/src/protocol/session.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ fn is_false(val: &bool) -> bool {
9292
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
9393
pub struct SessionUpdate<'a> {
9494
/// The session identifier.
95-
#[serde(rename = "sid", default = "Uuid::new_v4")]
95+
#[serde(rename = "sid", default = "crate::random_uuid")]
9696
pub session_id: Uuid,
9797

9898
/// The distinct identifier. Should be device or user ID.

sentry-types/src/protocol/v7.rs

+3-13
Original file line numberDiff line numberDiff line change
@@ -1338,12 +1338,7 @@ pub struct SpanId([u8; 8]);
13381338

13391339
impl Default for SpanId {
13401340
fn default() -> Self {
1341-
let mut buf = [0; 8];
1342-
1343-
getrandom::getrandom(&mut buf)
1344-
.unwrap_or_else(|err| panic!("could not retrieve random bytes for SpanId: {err}"));
1345-
1346-
Self(buf)
1341+
Self(rand::random())
13471342
}
13481343
}
13491344

@@ -1384,12 +1379,7 @@ pub struct TraceId([u8; 16]);
13841379

13851380
impl Default for TraceId {
13861381
fn default() -> Self {
1387-
let mut buf = [0; 16];
1388-
1389-
getrandom::getrandom(&mut buf)
1390-
.unwrap_or_else(|err| panic!("could not retrieve random bytes for TraceId: {err}"));
1391-
1392-
Self(buf)
1382+
Self(rand::random())
13931383
}
13941384
}
13951385

@@ -1520,7 +1510,7 @@ mod event {
15201510
use super::*;
15211511

15221512
pub fn default_id() -> Uuid {
1523-
Uuid::new_v4()
1513+
crate::random_uuid()
15241514
}
15251515

15261516
pub fn serialize_id<S: Serializer>(uuid: &Uuid, serializer: S) -> Result<S::Ok, S::Error> {

0 commit comments

Comments
 (0)