-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathspan.rs
633 lines (553 loc) · 23.3 KB
/
span.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
mod convert;
#[cfg(feature = "jsonschema")]
use relay_jsonschema_derive::JsonSchema;
use relay_protocol::{Annotated, Empty, FromValue, Getter, IntoValue, Object, Val, Value};
use crate::processor::ProcessValue;
use crate::protocol::{
EventId, JsonLenientString, LenientString, Measurements, MetricsSummary, OperationType,
OriginType, SpanId, SpanStatus, Timestamp, TraceId,
};
#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
#[metastructure(process_func = "process_span", value_type = "Span")]
pub struct Span {
/// Timestamp when the span was ended.
#[metastructure(required = "true")]
pub timestamp: Annotated<Timestamp>,
/// Timestamp when the span started.
#[metastructure(required = "true")]
pub start_timestamp: Annotated<Timestamp>,
/// The amount of time in milliseconds spent in this span,
/// excluding its immediate child spans.
pub exclusive_time: Annotated<f64>,
/// Human readable description of a span (e.g. method URL).
#[metastructure(pii = "maybe")]
pub description: Annotated<String>,
/// Span type (see `OperationType` docs).
#[metastructure(max_chars = 128)]
pub op: Annotated<OperationType>,
/// The Span id.
#[metastructure(required = "true")]
pub span_id: Annotated<SpanId>,
/// The ID of the span enclosing this span.
pub parent_span_id: Annotated<SpanId>,
/// The ID of the trace the span belongs to.
#[metastructure(required = "true")]
pub trace_id: Annotated<TraceId>,
/// A unique identifier for a segment within a trace (8 byte hexadecimal string).
///
/// For spans embedded in transactions, the `segment_id` is the `span_id` of the containing
/// transaction.
pub segment_id: Annotated<SpanId>,
/// Whether or not the current span is the root of the segment.
pub is_segment: Annotated<bool>,
/// The status of a span.
pub status: Annotated<SpanStatus>,
/// Arbitrary tags on a span, like on the top-level event.
#[metastructure(pii = "maybe")]
pub tags: Annotated<Object<JsonLenientString>>,
/// The origin of the span indicates what created the span (see [OriginType] docs).
#[metastructure(max_chars = 128, allow_chars = "a-zA-Z0-9_.")]
pub origin: Annotated<OriginType>,
/// ID of a profile that can be associated with the span.
pub profile_id: Annotated<EventId>,
/// Arbitrary additional data on a span.
///
/// Besides arbitrary user data, this object also contains SDK-provided fields used by the
/// product (see <https://develop.sentry.dev/sdk/performance/span-data-conventions/>).
#[metastructure(pii = "true")]
pub data: Annotated<SpanData>,
/// Tags generated by Relay. These tags are a superset of the tags set on span metrics.
pub sentry_tags: Annotated<Object<String>>,
/// Timestamp when the span has been received by Sentry.
pub received: Annotated<Timestamp>,
/// Measurements which holds observed values such as web vitals.
#[metastructure(skip_serialization = "empty")]
#[metastructure(omit_from_schema)] // we only document error events for now
pub measurements: Annotated<Measurements>,
/// Temporary protocol support for metric summaries.
///
/// This shall move to a stable location once we have stabilized the
/// interface. This is intentionally not typed today.
#[metastructure(skip_serialization = "empty")]
pub _metrics_summary: Annotated<MetricsSummary>,
/// Platform identifier.
///
/// See [`Event::platform`](`crate::protocol::Event::platform`).
#[metastructure(skip_serialization = "empty")]
pub platform: Annotated<String>,
/// Whether the span is a segment span that was converted from a transaction.
#[metastructure(skip_serialization = "empty")]
pub was_transaction: Annotated<bool>,
// TODO remove retain when the api stabilizes
/// Additional arbitrary fields for forwards compatibility.
#[metastructure(additional_properties, retain = "true", pii = "maybe")]
pub other: Object<Value>,
}
impl Getter for Span {
fn get_value(&self, path: &str) -> Option<Val<'_>> {
Some(match path.strip_prefix("span.")? {
"exclusive_time" => self.exclusive_time.value()?.into(),
"description" => self.description.as_str()?.into(),
"op" => self.op.as_str()?.into(),
// "span_id" => self.span_id.value()?.to_string().as_deref().into(),
// "parent_span_id" => self.parent_span_id.as_str()?.into(),
"trace_id" => self.trace_id.as_str()?.into(),
"status" => self.status.as_str()?.into(),
"origin" => self.origin.as_str()?.into(),
"duration" => {
let start_timestamp = *self.start_timestamp.value()?;
let timestamp = *self.timestamp.value()?;
relay_common::time::chrono_to_positive_millis(timestamp - start_timestamp).into()
}
"was_transaction" => self.was_transaction.value().unwrap_or(&false).into(),
path => {
if let Some(key) = path.strip_prefix("tags.") {
self.tags.value()?.get(key)?.as_str()?.into()
} else if let Some(key) = path.strip_prefix("data.") {
self.data.value()?.get_value(key)?
} else if let Some(key) = path.strip_prefix("sentry_tags.") {
self.sentry_tags.value()?.get(key)?.as_str()?.into()
} else if let Some(rest) = path.strip_prefix("measurements.") {
let name = rest.strip_suffix(".value")?;
self.measurements
.value()?
.get(name)?
.value()?
.value
.value()?
.into()
} else {
return None;
}
}
})
}
}
/// Arbitrary additional data on a span.
///
/// Besides arbitrary user data, this type also contains SDK-provided fields used by the
/// product (see <https://develop.sentry.dev/sdk/performance/span-data-conventions/>).
#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
pub struct SpanData {
/// Mobile app start variant.
///
/// Can be either "cold" or "warm".
#[metastructure(field = "app_start_type")] // TODO: no dot?
pub app_start_type: Annotated<Value>,
/// The client's browser name.
#[metastructure(field = "browser.name")]
pub browser_name: Annotated<String>,
/// The source code file name that identifies the code unit as uniquely as possible.
#[metastructure(field = "code.filepath", pii = "maybe")]
pub code_filepath: Annotated<Value>,
/// The line number in `code.filepath` best representing the operation.
#[metastructure(field = "code.lineno", pii = "maybe")]
pub code_lineno: Annotated<Value>,
/// The method or function name, or equivalent.
///
/// Usually rightmost part of the code unit's name.
#[metastructure(field = "code.function", pii = "maybe")]
pub code_function: Annotated<Value>,
/// The "namespace" within which `code.function` is defined.
///
/// Usually the qualified class or module name, such that
/// `code.namespace + some separator + code.function`
/// form a unique identifier for the code unit.
#[metastructure(field = "code.namespace", pii = "maybe")]
pub code_namespace: Annotated<Value>,
/// The name of the operation being executed.
///
/// E.g. the MongoDB command name such as findAndModify, or the SQL keyword.
/// Based on [OpenTelemetry's call level db attributes](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/database.md#call-level-attributes).
#[metastructure(field = "db.operation")]
pub db_operation: Annotated<Value>,
/// An identifier for the database management system (DBMS) product being used.
///
/// See [OpenTelemetry docs for a list of well-known identifiers](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/database.md#notes-and-well-known-identifiers-for-dbsystem).
#[metastructure(field = "db.system")]
pub db_system: Annotated<Value>,
/// The sentry environment.
#[metastructure(field = "sentry.environment", legacy_alias = "environment")]
pub environment: Annotated<String>,
/// The release version of the project.
#[metastructure(field = "sentry.release", legacy_alias = "release")]
pub release: Annotated<LenientString>,
/// The decoded body size of the response (in bytes).
#[metastructure(field = "http.decoded_response_content_length")]
pub http_decoded_response_content_length: Annotated<Value>,
/// The HTTP method used.
#[metastructure(
field = "http.request_method",
legacy_alias = "http.method",
legacy_alias = "method"
)]
pub http_request_method: Annotated<Value>,
/// The encoded body size of the response (in bytes).
#[metastructure(field = "http.response_content_length")]
pub http_response_content_length: Annotated<Value>,
/// The transfer size of the response (in bytes).
#[metastructure(field = "http.response_transfer_size")]
pub http_response_transfer_size: Annotated<Value>,
/// The render blocking status of the resource.
#[metastructure(field = "resource.render_blocking_status")]
pub resource_render_blocking_status: Annotated<Value>,
/// Name of the web server host.
#[metastructure(field = "server.address")]
pub server_address: Annotated<Value>,
/// Whether cache was hit or miss on a read operation.
#[metastructure(field = "cache.hit")]
pub cache_hit: Annotated<Value>,
/// The size of the cache item.
#[metastructure(field = "cache.item_size")]
pub cache_item_size: Annotated<Value>,
/// The status HTTP response.
#[metastructure(field = "http.response.status_code", legacy_alias = "status_code")]
pub http_response_status_code: Annotated<Value>,
/// The 'name' field of the ancestor span with op ai.pipeline.*
#[metastructure(field = "ai.pipeline.name")]
pub ai_pipeline_name: Annotated<Value>,
/// The input messages to an AI model call
#[metastructure(field = "ai.input_messages")]
pub ai_input_messages: Annotated<Value>,
/// The number of tokens used to generate the response to an AI call
#[metastructure(field = "ai.completion_tokens.used", pii = "false")]
pub ai_completion_tokens_used: Annotated<Value>,
/// The number of tokens used to process a request for an AI call
#[metastructure(field = "ai.prompt_tokens.used", pii = "false")]
pub ai_prompt_tokens_used: Annotated<Value>,
/// The total number of tokens used to for an AI call
#[metastructure(field = "ai.total_tokens.used", pii = "false")]
pub ai_total_tokens_used: Annotated<Value>,
/// The responses to an AI model call
#[metastructure(field = "ai.responses")]
pub ai_responses: Annotated<Value>,
/// Label identifying a thread from where the span originated.
#[metastructure(field = "thread.name")]
pub thread_name: Annotated<Value>,
/// Name of the segment that this span belongs to (see `segment_id`).
///
/// This corresponds to the transaction name in the transaction-based model.
///
/// For INP spans, this is the route name where the interaction occurred.
#[metastructure(field = "sentry.segment.name", legacy_alias = "transaction")]
pub segment_name: Annotated<String>,
/// Name of the UI component (e.g. React).
#[metastructure(field = "ui.component_name")]
pub ui_component_name: Annotated<Value>,
/// The URL scheme, e.g. `"https"`.
#[metastructure(field = "url.scheme")]
pub url_scheme: Annotated<Value>,
/// User Display
#[metastructure(field = "user")]
pub user: Annotated<Value>,
/// Replay ID
#[metastructure(field = "sentry.replay.id", legacy_alias = "replay_id")]
pub replay_id: Annotated<Value>,
/// The sentry SDK (see [`crate::protocol::ClientSdkInfo`]).
#[metastructure(field = "sentry.sdk.name")]
pub sdk_name: Annotated<String>,
/// Slow Frames
#[metastructure(field = "sentry.frames.slow", legacy_alias = "frames.slow")]
pub frames_slow: Annotated<Value>,
/// Frozen Frames
#[metastructure(field = "sentry.frames.frozen", legacy_alias = "frames.frozen")]
pub frames_frozen: Annotated<Value>,
/// Total Frames
#[metastructure(field = "sentry.frames.total", legacy_alias = "frames.total")]
pub frames_total: Annotated<Value>,
// Frames Delay (in seconds)
#[metastructure(field = "frames.delay")]
pub frames_delay: Annotated<Value>,
// Messaging Destination Name
#[metastructure(field = "messaging.destination.name")]
pub messaging_destination_name: Annotated<Value>,
/// Message Retry Count
#[metastructure(field = "messaging.message.retry.count")]
pub messaging_message_retry_count: Annotated<Value>,
/// Message Receive Latency
#[metastructure(field = "messaging.message.receive.latency")]
pub messaging_message_receive_latency: Annotated<Value>,
/// Message Body Size
#[metastructure(field = "messaging.message.body.size")]
pub messaging_message_body_size: Annotated<Value>,
/// Other fields in `span.data`.
#[metastructure(additional_properties, pii = "true", retain = "true")]
other: Object<Value>,
}
impl Getter for SpanData {
fn get_value(&self, path: &str) -> Option<Val<'_>> {
Some(match path {
"app_start_type" => self.app_start_type.value()?.into(),
"browser\\.name" => self.browser_name.as_str()?.into(),
"code\\.filepath" => self.code_filepath.value()?.into(),
"code\\.function" => self.code_function.value()?.into(),
"code\\.lineno" => self.code_lineno.value()?.into(),
"code\\.namespace" => self.code_namespace.value()?.into(),
"db.operation" => self.db_operation.value()?.into(),
"db\\.system" => self.db_system.value()?.into(),
"environment" => self.environment.as_str()?.into(),
"http\\.decoded_response_content_length" => {
self.http_decoded_response_content_length.value()?.into()
}
"http\\.request_method" | "http\\.method" | "method" => {
self.http_request_method.value()?.into()
}
"http\\.response_content_length" => self.http_response_content_length.value()?.into(),
"http\\.response_transfer_size" => self.http_response_transfer_size.value()?.into(),
"http\\.response.status_code" | "status_code" => {
self.http_response_status_code.value()?.into()
}
"resource\\.render_blocking_status" => {
self.resource_render_blocking_status.value()?.into()
}
"server\\.address" => self.server_address.value()?.into(),
"thread\\.name" => self.thread_name.value()?.into(),
"ui\\.component_name" => self.ui_component_name.value()?.into(),
"url\\.scheme" => self.url_scheme.value()?.into(),
"transaction" => self.segment_name.as_str()?.into(),
_ => {
let escaped = path.replace("\\.", "\0");
let mut path = escaped.split('.').map(|s| s.replace('\0', "."));
let root = path.next()?;
let mut val = self.other.get(&root)?.value()?;
for part in path {
// While there is path segments left, `val` has to be an Object.
let relay_protocol::Value::Object(map) = val else {
return None;
};
val = map.get(&part)?.value()?;
}
val.into()
}
})
}
}
#[cfg(test)]
mod tests {
use crate::protocol::Measurement;
use chrono::{TimeZone, Utc};
use relay_base_schema::metrics::{InformationUnit, MetricUnit};
use relay_protocol::RuleCondition;
use similar_asserts::assert_eq;
use super::*;
#[test]
fn test_span_serialization() {
let json = r#"{
"timestamp": 0.0,
"start_timestamp": -63158400.0,
"exclusive_time": 1.23,
"description": "desc",
"op": "operation",
"span_id": "fa90fdead5f74052",
"trace_id": "4c79f60c11214eb38604f4ae0781bfb2",
"status": "ok",
"origin": "auto.http",
"measurements": {
"memory": {
"value": 9001.0,
"unit": "byte"
}
}
}"#;
let mut measurements = Object::new();
measurements.insert(
"memory".into(),
Annotated::new(Measurement {
value: Annotated::new(9001.0),
unit: Annotated::new(MetricUnit::Information(InformationUnit::Byte)),
}),
);
let span = Annotated::new(Span {
timestamp: Annotated::new(Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap().into()),
start_timestamp: Annotated::new(
Utc.with_ymd_and_hms(1968, 1, 1, 0, 0, 0).unwrap().into(),
),
exclusive_time: Annotated::new(1.23),
description: Annotated::new("desc".to_owned()),
op: Annotated::new("operation".to_owned()),
trace_id: Annotated::new(TraceId("4c79f60c11214eb38604f4ae0781bfb2".into())),
span_id: Annotated::new(SpanId(0xfa90fdead5f74052)),
status: Annotated::new(SpanStatus::Ok),
origin: Annotated::new("auto.http".to_owned()),
measurements: Annotated::new(Measurements(measurements)),
..Default::default()
});
assert_eq!(json, span.to_json_pretty().unwrap());
let span_from_string = Annotated::from_json(json).unwrap();
assert_eq!(span, span_from_string);
}
#[test]
fn test_getter_span_data() {
let span = Annotated::<Span>::from_json(
r#"{
"data": {
"foo": {"bar": 1},
"foo.bar": 2
},
"measurements": {
"some": {"value": 100.0}
}
}"#,
)
.unwrap()
.into_value()
.unwrap();
assert_eq!(span.get_value("span.data.foo.bar"), Some(Val::I64(1)));
assert_eq!(span.get_value(r"span.data.foo\.bar"), Some(Val::I64(2)));
assert_eq!(span.get_value("span.data"), None);
assert_eq!(span.get_value("span.data."), None);
assert_eq!(span.get_value("span.data.x"), None);
assert_eq!(
span.get_value("span.measurements.some.value"),
Some(Val::F64(100.0))
);
}
#[test]
fn test_getter_was_transaction() {
let mut span = Span::default();
assert_eq!(
span.get_value("span.was_transaction"),
Some(Val::Bool(false))
);
assert!(RuleCondition::eq("span.was_transaction", false).matches(&span));
assert!(!RuleCondition::eq("span.was_transaction", true).matches(&span));
span.was_transaction.set_value(Some(false));
assert_eq!(
span.get_value("span.was_transaction"),
Some(Val::Bool(false))
);
assert!(RuleCondition::eq("span.was_transaction", false).matches(&span));
assert!(!RuleCondition::eq("span.was_transaction", true).matches(&span));
span.was_transaction.set_value(Some(true));
assert_eq!(
span.get_value("span.was_transaction"),
Some(Val::Bool(true))
);
assert!(RuleCondition::eq("span.was_transaction", true).matches(&span));
assert!(!RuleCondition::eq("span.was_transaction", false).matches(&span));
}
#[test]
fn test_span_duration() {
let span = Annotated::<Span>::from_json(
r#"{
"start_timestamp": 1694732407.8367,
"timestamp": 1694732408.3145
}"#,
)
.unwrap()
.into_value()
.unwrap();
assert_eq!(span.get_value("span.duration"), Some(Val::F64(477.800131)));
}
#[test]
fn test_span_data() {
let data = r#"{
"foo": 2,
"bar": "3",
"db.system": "mysql",
"code.filepath": "task.py",
"code.lineno": 123,
"code.function": "fn()",
"code.namespace": "ns",
"frames.slow": 1,
"frames.frozen": 2,
"frames.total": 9,
"frames.delay": 100,
"messaging.destination.name": "default",
"messaging.message.retry.count": 3,
"messaging.message.receive.latency": 40,
"messaging.message.body.size": 100
}"#;
let data = Annotated::<SpanData>::from_json(data)
.unwrap()
.into_value()
.unwrap();
insta::assert_debug_snapshot!(data, @r###"
SpanData {
app_start_type: ~,
browser_name: ~,
code_filepath: String(
"task.py",
),
code_lineno: I64(
123,
),
code_function: String(
"fn()",
),
code_namespace: String(
"ns",
),
db_operation: ~,
db_system: String(
"mysql",
),
environment: ~,
release: ~,
http_decoded_response_content_length: ~,
http_request_method: ~,
http_response_content_length: ~,
http_response_transfer_size: ~,
resource_render_blocking_status: ~,
server_address: ~,
cache_hit: ~,
cache_item_size: ~,
http_response_status_code: ~,
ai_pipeline_name: ~,
ai_input_messages: ~,
ai_completion_tokens_used: ~,
ai_prompt_tokens_used: ~,
ai_total_tokens_used: ~,
ai_responses: ~,
thread_name: ~,
segment_name: ~,
ui_component_name: ~,
url_scheme: ~,
user: ~,
replay_id: ~,
sdk_name: ~,
frames_slow: I64(
1,
),
frames_frozen: I64(
2,
),
frames_total: I64(
9,
),
frames_delay: I64(
100,
),
messaging_destination_name: String(
"default",
),
messaging_message_retry_count: I64(
3,
),
messaging_message_receive_latency: I64(
40,
),
messaging_message_body_size: I64(
100,
),
other: {
"bar": String(
"3",
),
"foo": I64(
2,
),
},
}
"###);
assert_eq!(data.get_value("foo"), Some(Val::U64(2)));
assert_eq!(data.get_value("bar"), Some(Val::String("3")));
assert_eq!(data.get_value("db\\.system"), Some(Val::String("mysql")));
assert_eq!(data.get_value("code\\.lineno"), Some(Val::U64(123)));
assert_eq!(data.get_value("code\\.function"), Some(Val::String("fn()")));
assert_eq!(data.get_value("code\\.namespace"), Some(Val::String("ns")));
assert_eq!(data.get_value("unknown"), None);
}
}