Skip to content

Commit 638007a

Browse files
committed
Add RequestId
1 parent cb27a06 commit 638007a

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

libs/telemetry/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ cuid = { git = "https://github.com/prisma/cuid-rust", branch = "wasm32-support"
2929
crosstarget-utils = { path = "../crosstarget-utils" }
3030
lru = "0.7.7"
3131
enumflags2.workspace = true
32-
derive_more = "0.99.17"
32+
derive_more.workspace = true
3333

3434
[dev-dependencies]
3535
insta = { version = "1.41", features = ["redactions", "ron"] }

libs/telemetry/src/capturing/ng/collector.rs

+49-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
use std::{borrow::Cow, collections::HashMap, num::NonZeroU64, sync::Arc};
22

3+
use derive_more::Display;
34
use serde::{Deserialize, Serialize};
45
use tokio::time::Instant;
56
use tracing::Level;
67

78
use crate::models::{LogLevel, SpanKind, TraceSpan};
89

9-
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
10+
#[derive(Debug, Display, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
11+
#[display(fmt = "{}", _0)]
1012
struct SerializableNonZeroU64(NonZeroU64);
1113

1214
impl Serialize for SerializableNonZeroU64 {
@@ -17,7 +19,7 @@ impl Serialize for SerializableNonZeroU64 {
1719
// Serialize as string to preserve full u64 precision in JavaScript. Otherwise values
1820
// larger than 2^53 - 1 will be parsed as floats on the client side, making it possible for
1921
// IDs to collide.
20-
self.0.to_string().serialize(serializer)
22+
self.to_string().serialize(serializer)
2123
}
2224
}
2325

@@ -34,6 +36,35 @@ impl<'de> Deserialize<'de> for SerializableNonZeroU64 {
3436
}
3537
}
3638

39+
#[derive(Debug, Display, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
40+
#[display(fmt = "{}", _0)]
41+
struct SerializableU64(u64);
42+
43+
impl Serialize for SerializableU64 {
44+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
45+
where
46+
S: serde::Serializer,
47+
{
48+
// Serialize as string to preserve full u64 precision in JavaScript. Otherwise values
49+
// larger than 2^53 - 1 will be parsed as floats on the client side, making it possible for
50+
// IDs to collide.
51+
self.to_string().serialize(serializer)
52+
}
53+
}
54+
55+
impl<'de> Deserialize<'de> for SerializableU64 {
56+
fn deserialize<D>(deserializer: D) -> Result<SerializableU64, D::Error>
57+
where
58+
D: serde::Deserializer<'de>,
59+
{
60+
let value = String::deserialize(deserializer)?;
61+
let value = value.parse().map_err(serde::de::Error::custom)?;
62+
Ok(SerializableU64(value))
63+
}
64+
}
65+
66+
/// A unique identifier for a span. It maps directly to [`tracing::span::Id`] assigned by
67+
/// [`tracing_subscriber::registry::Registry`].
3768
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
3869
pub struct SpanId(SerializableNonZeroU64);
3970

@@ -49,6 +80,22 @@ impl From<tracing::span::Id> for SpanId {
4980
}
5081
}
5182

83+
/// A unique identifier for an engine trace, representing a tree of spans. These internal traces *do
84+
/// not* correspond to OpenTelemetry traces defined by [`crate::capturing::ng::traceparent::TraceParent`].
85+
/// One OpenTelemetry trace may contain multiple Prisma Client operations, each of them leading to
86+
/// one or more engine requests. Since engine traces map 1:1 to requests to the engine, we call
87+
/// these trace IDs "request IDs" to disambiguate and avoid confusion.
88+
///
89+
/// We don't use IDs of the root spans themselves for this purpose because span IDs are only
90+
/// guaranteed to be unique among the spans active at the same time. They may be reused after a
91+
/// span is closed, so they are not historically unique. We store the collected spans and events
92+
/// for some short time after the spans are closed until the client requests them, so we need
93+
/// request IDs that are guaranteed to be unique for a very long period of time (although they
94+
/// still don't necessarily have to be unique for the whole lifetime of the process).
95+
pub struct RequestId(SerializableU64);
96+
97+
impl RequestId {}
98+
5299
#[derive(Debug, Clone)]
53100
#[cfg_attr(test, derive(Serialize))]
54101
pub struct CollectedSpan {

0 commit comments

Comments
 (0)