-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathtrace.rs
379 lines (337 loc) · 12.4 KB
/
trace.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
use std::fmt;
use std::str::FromStr;
#[cfg(feature = "jsonschema")]
use relay_jsonschema_derive::JsonSchema;
use relay_protocol::{Annotated, Empty, Error, FromValue, IntoValue, Object, Value};
use crate::processor::ProcessValue;
use crate::protocol::{OperationType, OriginType, SpanStatus};
/// A 32-character hex string as described in the W3C trace context spec.
#[derive(Clone, Debug, Default, PartialEq, Empty, IntoValue, ProcessValue)]
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
pub struct TraceId(pub String);
impl FromValue for TraceId {
fn from_value(value: Annotated<Value>) -> Annotated<Self> {
match value {
Annotated(Some(Value::String(value)), mut meta) => {
if !is_hex_string(&value, 32) || value.bytes().all(|x| x == b'0') {
meta.add_error(Error::invalid("not a valid trace id"));
meta.set_original_value(Some(value));
Annotated(None, meta)
} else {
Annotated(Some(TraceId(value.to_ascii_lowercase())), meta)
}
}
Annotated(None, meta) => Annotated(None, meta),
Annotated(Some(value), mut meta) => {
meta.add_error(Error::expected("trace id"));
meta.set_original_value(Some(value));
Annotated(None, meta)
}
}
}
}
impl AsRef<str> for TraceId {
fn as_ref(&self) -> &str {
&self.0
}
}
/// A 16-character hex string as described in the W3C trace context spec.
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq, Empty, IntoValue, ProcessValue)]
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
pub struct SpanId(pub u64);
relay_common::impl_str_serde!(SpanId, "a span identifier");
impl FromStr for SpanId {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(SpanId(
u64::from_str_radix(s, 16).map_err(|_| Error::invalid("invalid span id"))?,
))
}
}
impl fmt::Display for SpanId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:x}", self.0)
}
}
impl FromValue for SpanId {
fn from_value(value: Annotated<Value>) -> Annotated<Self> {
match value {
Annotated(Some(Value::String(value)), mut meta) => match SpanId::from_str(&value) {
Ok(value) => Annotated(Some(value), meta),
Err(e) => {
meta.add_error(e);
meta.set_original_value(Some(value));
Annotated(None, meta)
}
},
Annotated(None, meta) => Annotated(None, meta),
Annotated(Some(value), mut meta) => {
meta.add_error(Error::expected("span id"));
meta.set_original_value(Some(value));
Annotated(None, meta)
}
}
}
}
fn is_hex_string(string: &str, len: usize) -> bool {
string.len() == len && string.bytes().all(|b| b.is_ascii_hexdigit())
}
/// Trace context
#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
#[metastructure(process_func = "process_trace_context")]
pub struct TraceContext {
/// The trace ID.
#[metastructure(required = "true")]
pub trace_id: Annotated<TraceId>,
/// The ID of the span.
#[metastructure(required = "true")]
pub span_id: Annotated<SpanId>,
/// The ID of the span enclosing this span.
pub parent_span_id: Annotated<SpanId>,
/// Span type (see `OperationType` docs).
#[metastructure(max_chars = 128)]
pub op: Annotated<OperationType>,
/// Whether the trace failed or succeeded. Currently only used to indicate status of individual
/// transactions.
pub status: Annotated<SpanStatus>,
/// The amount of time in milliseconds spent in this transaction span,
/// excluding its immediate child spans.
pub exclusive_time: Annotated<f64>,
/// The client-side sample rate as reported in the envelope's `trace.sample_rate` header.
///
/// The server takes this field from envelope headers and writes it back into the event. Clients
/// should not ever send this value.
pub client_sample_rate: Annotated<f64>,
/// The origin of the trace indicates what created the trace (see [OriginType] docs).
#[metastructure(max_chars = 128, allow_chars = "a-zA-Z0-9_.")]
pub origin: Annotated<OriginType>,
/// Track whether the trace connected to this event has been sampled entirely.
///
/// This flag only applies to events with [`Error`] type that have an associated dynamic sampling context.
pub sampled: Annotated<bool>,
/// Arbitrary additional data on a trace.
#[metastructure(pii = "maybe", skip_serialization = "empty")]
pub data: Annotated<Data>,
/// Additional arbitrary fields for forwards compatibility.
#[metastructure(additional_properties, retain = "true", pii = "maybe")]
pub other: Object<Value>,
}
/// The arbitrary data on the trace.
#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
pub struct Data {
/// The current route in the application.
///
/// Set by React Native SDK.
#[metastructure(pii = "maybe", skip_serialization = "empty")]
pub route: Annotated<Route>,
/// The previous route in the application
///
/// Set by React Native SDK.
#[metastructure(field = "previousRoute", pii = "maybe", skip_serialization = "empty")]
pub previous_route: Annotated<Route>,
/// Additional arbitrary fields for forwards compatibility.
#[metastructure(
additional_properties,
retain = "true",
pii = "maybe",
skip_serialization = "empty"
)]
pub other: Object<Value>,
}
/// The route in the application, set by React Native SDK.
#[derive(Clone, Debug, Default, PartialEq, Empty, IntoValue, ProcessValue)]
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
pub struct Route {
/// The name of the route.
#[metastructure(pii = "maybe", skip_serialization = "empty")]
name: Annotated<String>,
/// Parameters assigned to this route.
#[metastructure(
pii = "true",
skip_serialization = "empty",
max_depth = 5,
max_bytes = 2048
)]
params: Annotated<Object<Value>>,
/// Additional arbitrary fields for forwards compatibility.
#[metastructure(
additional_properties,
retain = "true",
pii = "maybe",
skip_serialization = "empty"
)]
pub other: Object<Value>,
}
impl FromValue for Route {
fn from_value(value: Annotated<Value>) -> Annotated<Self>
where
Self: Sized,
{
match value {
Annotated(Some(Value::String(name)), meta) => Annotated(
Some(Route {
name: Annotated::new(name),
..Default::default()
}),
meta,
),
Annotated(Some(Value::Object(mut values)), meta) => {
let mut route: Route = Default::default();
if let Some(Annotated(Some(Value::String(name)), _)) = values.remove("name") {
route.name = Annotated::new(name);
}
if let Some(Annotated(Some(Value::Object(params)), _)) = values.remove("params") {
route.params = Annotated::new(params);
}
if !values.is_empty() {
route.other = values;
}
Annotated(Some(route), meta)
}
Annotated(None, meta) => Annotated(None, meta),
Annotated(Some(value), mut meta) => {
meta.add_error(Error::expected("route expected to be an object"));
meta.set_original_value(Some(value));
Annotated(None, meta)
}
}
}
}
impl super::DefaultContext for TraceContext {
fn default_key() -> &'static str {
"trace"
}
fn from_context(context: super::Context) -> Option<Self> {
match context {
super::Context::Trace(c) => Some(*c),
_ => None,
}
}
fn cast(context: &super::Context) -> Option<&Self> {
match context {
super::Context::Trace(c) => Some(c),
_ => None,
}
}
fn cast_mut(context: &mut super::Context) -> Option<&mut Self> {
match context {
super::Context::Trace(c) => Some(c),
_ => None,
}
}
fn into_context(self) -> super::Context {
super::Context::Trace(Box::new(self))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::protocol::Context;
#[test]
fn test_trace_context_roundtrip() {
let json = r#"{
"trace_id": "4c79f60c11214eb38604f4ae0781bfb2",
"span_id": "fa90fdead5f74052",
"parent_span_id": "fa90fdead5f74053",
"op": "http",
"status": "ok",
"exclusive_time": 0.0,
"client_sample_rate": 0.5,
"origin": "auto.http",
"data": {
"route": {
"name": "/users",
"params": {
"tok": "test"
},
"custom_field": "something"
}
},
"other": "value",
"type": "trace"
}"#;
let context = Annotated::new(Context::Trace(Box::new(TraceContext {
trace_id: Annotated::new(TraceId("4c79f60c11214eb38604f4ae0781bfb2".into())),
span_id: Annotated::new(SpanId(0xfa90fdead5f74052)),
parent_span_id: Annotated::new(SpanId(0xfa90fdead5f74053)),
op: Annotated::new("http".into()),
status: Annotated::new(SpanStatus::Ok),
exclusive_time: Annotated::new(0.0),
client_sample_rate: Annotated::new(0.5),
origin: Annotated::new("auto.http".to_owned()),
data: Annotated::new(Data {
route: Annotated::new(Route {
name: Annotated::new("/users".into()),
params: Annotated::new({
let mut map = Object::new();
map.insert(
"tok".to_string(),
Annotated::new(Value::String("test".into())),
);
map
}),
other: {
let mut map = Object::new();
map.insert(
"custom_field".into(),
Annotated::new(Value::String("something".into())),
);
map
},
}),
..Default::default()
}),
other: {
let mut map = Object::new();
map.insert(
"other".to_string(),
Annotated::new(Value::String("value".to_string())),
);
map
},
sampled: Annotated::empty(),
})));
assert_eq!(context, Annotated::from_json(json).unwrap());
assert_eq!(json, context.to_json_pretty().unwrap());
}
#[test]
fn test_trace_context_normalization() {
let json = r#"{
"trace_id": "4C79F60C11214EB38604F4AE0781BFB2",
"span_id": "FA90FDEAD5F74052",
"type": "trace"
}"#;
let context = Annotated::new(Context::Trace(Box::new(TraceContext {
trace_id: Annotated::new(TraceId("4c79f60c11214eb38604f4ae0781bfb2".into())),
span_id: Annotated::new(SpanId(0xfa90fdead5f74052)),
..Default::default()
})));
assert_eq!(context, Annotated::from_json(json).unwrap());
}
#[test]
fn test_trace_context_with_routes() {
let json = r#"{
"trace_id": "4C79F60C11214EB38604F4AE0781BFB2",
"span_id": "FA90FDEAD5F74052",
"type": "trace",
"data": {
"route": "HomeRoute"
}
}"#;
let context = Annotated::new(Context::Trace(Box::new(TraceContext {
trace_id: Annotated::new(TraceId("4c79f60c11214eb38604f4ae0781bfb2".into())),
span_id: Annotated::new(SpanId(0xfa90fdead5f74052)),
data: Annotated::new(Data {
route: Annotated::new(Route {
name: Annotated::new("HomeRoute".into()),
..Default::default()
}),
..Default::default()
}),
..Default::default()
})));
assert_eq!(context, Annotated::from_json(json).unwrap());
}
}