-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathevent.rs
2559 lines (2336 loc) · 85.5 KB
/
event.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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Event normalization.
//!
//! This module provides a function to normalize events.
use std::collections::hash_map::DefaultHasher;
use std::collections::BTreeSet;
use std::hash::{Hash, Hasher};
use std::mem;
use std::ops::Range;
use chrono::{DateTime, Duration, Utc};
use relay_base_schema::metrics::{
can_be_valid_metric_name, DurationUnit, FractionUnit, MetricUnit,
};
use relay_common::time::UnixTimestamp;
use relay_event_schema::processor::{
self, MaxChars, ProcessingAction, ProcessingResult, ProcessingState, Processor,
};
use relay_event_schema::protocol::{
AsPair, Context, ContextInner, Contexts, DeviceClass, Event, EventType, Exception, Headers,
IpAddr, Level, LogEntry, Measurement, Measurements, NelContext, Request, SpanAttribute,
SpanStatus, Tags, User,
};
use relay_protocol::{Annotated, Empty, Error, ErrorKind, Meta, Object, Value};
use smallvec::SmallVec;
use crate::normalize::request;
use crate::span::tag_extraction::{self, extract_span_tags};
use crate::timestamp::TimestampProcessor;
use crate::utils::{self, MAX_DURATION_MOBILE_MS};
use crate::{
breakdowns, mechanism, schema, span, stacktrace, transactions, trimming, user_agent,
BreakdownsConfig, ClockDriftProcessor, DynamicMeasurementsConfig, GeoIpLookup,
PerformanceScoreConfig, RawUserAgentInfo, SpanDescriptionRule, TransactionNameConfig,
};
/// Configuration for [`normalize_event`].
#[derive(Clone, Debug)]
pub struct NormalizationConfig<'a> {
/// The IP address of the SDK that sent the event.
///
/// When `{{auto}}` is specified and there is no other IP address in the payload, such as in the
/// `request` context, this IP address gets added to the `user` context.
pub client_ip: Option<&'a IpAddr>,
/// The user-agent and client hints obtained from the submission request headers.
///
/// Client hints are the preferred way to infer device, operating system, and browser
/// information should the event payload contain no such data. If no client hints are present,
/// normalization falls back to the user agent.
pub user_agent: RawUserAgentInfo<&'a str>,
/// The time at which the event was received in this Relay.
///
/// This timestamp is persisted into the event payload.
pub received_at: Option<DateTime<Utc>>,
/// The maximum amount of seconds an event can be dated in the past.
///
/// If the event's timestamp is older, the received timestamp is assumed.
pub max_secs_in_past: Option<i64>,
/// The maximum amount of seconds an event can be predated into the future.
///
/// If the event's timestamp lies further into the future, the received timestamp is assumed.
pub max_secs_in_future: Option<i64>,
/// Timestamp range in which a transaction must end.
///
/// Transactions that finish outside of this range are considered invalid.
/// This check is skipped if no range is provided.
pub transaction_range: Option<Range<UnixTimestamp>>,
/// The maximum length for names of custom measurements.
///
/// Measurements with longer names are removed from the transaction event and replaced with a
/// metadata entry.
pub max_name_and_unit_len: Option<usize>,
/// Configuration for measurement normalization in transaction events.
///
/// Has an optional [`crate::MeasurementsConfig`] from both the project and the global level.
/// If at least one is provided, then normalization will truncate custom measurements
/// and add units of known built-in measurements.
pub measurements: Option<DynamicMeasurementsConfig<'a>>,
/// Emit breakdowns based on given configuration.
pub breakdowns_config: Option<&'a BreakdownsConfig>,
/// When `Some(true)`, context information is extracted from the user agent.
pub normalize_user_agent: Option<bool>,
/// Configuration to apply to transaction names, especially around sanitizing.
pub transaction_name_config: TransactionNameConfig<'a>,
/// When `Some(true)`, it is assumed that the event has been normalized before.
///
/// This disables certain normalizations, especially all that are not idempotent. The
/// renormalize mode is intended for the use in the processing pipeline, so an event modified
/// during ingestion can be validated against the schema and large data can be trimmed. However,
/// advanced normalizations such as inferring contexts or clock drift correction are disabled.
///
/// `None` equals to `false`.
pub is_renormalize: bool,
/// When `true`, infers the device class from CPU and model.
pub device_class_synthesis_config: bool,
/// When `true`, extracts tags from event and spans and materializes them into `span.data`.
pub enrich_spans: bool,
/// When `true`, computes and materializes attributes in spans based on the given configuration.
pub normalize_spans: bool,
/// The maximum allowed size of tag values in bytes. Longer values will be cropped.
pub max_tag_value_length: usize, // TODO: move span related fields into separate config.
/// Configuration for replacing identifiers in the span description with placeholders.
///
/// This is similar to `transaction_name_config`, but applies to span descriptions.
pub span_description_rules: Option<&'a Vec<SpanDescriptionRule>>,
/// Configuration for generating performance score measurements for web vitals
pub performance_score: Option<&'a PerformanceScoreConfig>,
/// An initialized GeoIP lookup.
pub geoip_lookup: Option<&'a GeoIpLookup>,
/// When `Some(true)`, individual parts of the event payload is trimmed to a maximum size.
///
/// See the event schema for size declarations.
pub enable_trimming: bool,
}
impl<'a> Default for NormalizationConfig<'a> {
fn default() -> Self {
Self {
client_ip: Default::default(),
user_agent: Default::default(),
received_at: Default::default(),
max_secs_in_past: Default::default(),
max_secs_in_future: Default::default(),
transaction_range: Default::default(),
max_name_and_unit_len: Default::default(),
breakdowns_config: Default::default(),
normalize_user_agent: Default::default(),
transaction_name_config: Default::default(),
is_renormalize: Default::default(),
device_class_synthesis_config: Default::default(),
enrich_spans: Default::default(),
normalize_spans: Default::default(),
max_tag_value_length: usize::MAX,
span_description_rules: Default::default(),
performance_score: Default::default(),
geoip_lookup: Default::default(),
enable_trimming: false,
measurements: None,
}
}
}
/// Normalizes an event, rejecting it if necessary.
///
/// Normalization consists of applying a series of transformations on the event
/// payload based on the given configuration.
///
/// The returned [`ProcessingResult`] indicates whether the passed event should
/// be ingested or dropped.
pub fn normalize_event(
event: &mut Annotated<Event>,
config: &NormalizationConfig,
) -> ProcessingResult {
let Annotated(Some(ref mut event), ref mut meta) = event else {
return Ok(());
};
let is_renormalize = config.is_renormalize;
if !is_renormalize {
normalize(event, meta, config)?;
}
Ok(())
}
/// Normalizes the given event based on the given config.
///
/// The returned [`ProcessingResult`] indicates whether the passed event should
/// be ingested or dropped.
fn normalize(event: &mut Event, meta: &mut Meta, config: &NormalizationConfig) -> ProcessingResult {
// Validate and normalize transaction
// (internally noops for non-transaction events).
// TODO: Parts of this processor should probably be a filter so we
// can revert some changes to ProcessingAction)
let mut transactions_processor = transactions::TransactionsProcessor::new(
config.transaction_name_config.clone(),
config.transaction_range.clone(),
);
transactions_processor.process_event(event, meta, ProcessingState::root())?;
// Check for required and non-empty values
let _ = schema::SchemaProcessor.process_event(event, meta, ProcessingState::root());
normalize_timestamps(
event,
meta,
config.received_at,
config.max_secs_in_past,
config.max_secs_in_future,
); // Timestamps are core in the metrics extraction
TimestampProcessor.process_event(event, meta, ProcessingState::root())?;
// Process security reports first to ensure all props.
normalize_security_report(event, config.client_ip, &config.user_agent);
// Process NEL reports to ensure all props.
normalize_nel_report(event, config.client_ip);
// Insert IP addrs before recursing, since geo lookup depends on it.
normalize_ip_addresses(
&mut event.request,
&mut event.user,
event.platform.as_str(),
config.client_ip,
);
if let Some(geoip_lookup) = config.geoip_lookup {
if let Some(user) = event.user.value_mut() {
normalize_user_geoinfo(geoip_lookup, user)
}
}
// Validate the basic attributes we extract metrics from
let _ = processor::apply(&mut event.release, |release, meta| {
if crate::validate_release(release).is_ok() {
Ok(())
} else {
meta.add_error(ErrorKind::InvalidData);
Err(ProcessingAction::DeleteValueSoft)
}
});
let _ = processor::apply(&mut event.environment, |environment, meta| {
if crate::validate_environment(environment).is_ok() {
Ok(())
} else {
meta.add_error(ErrorKind::InvalidData);
Err(ProcessingAction::DeleteValueSoft)
}
});
// Default required attributes, even if they have errors
normalize_logentry(&mut event.logentry, meta);
normalize_release_dist(event); // dist is a tag extracted along with other metrics from transactions
normalize_event_tags(event); // Tags are added to every metric
normalize_platform_and_level(event);
// TODO: Consider moving to store normalization
if config.device_class_synthesis_config {
normalize_device_class(event);
}
normalize_stacktraces(event);
normalize_exceptions(event); // Browser extension filters look at the stacktrace
normalize_user_agent(event, config.normalize_user_agent); // Legacy browsers filter
normalize_measurements(
event,
config.measurements.clone(),
config.max_name_and_unit_len,
); // Measurements are part of the metric extraction
normalize_performance_score(event, config.performance_score);
normalize_breakdowns(event, config.breakdowns_config); // Breakdowns are part of the metric extraction too
let _ = processor::apply(&mut event.request, |request, _| {
request::normalize_request(request);
Ok(())
});
// Some contexts need to be normalized before metrics extraction takes place.
normalize_contexts(&mut event.contexts);
if config.normalize_spans && event.ty.value() == Some(&EventType::Transaction) {
// XXX(iker): span normalization runs in the store processor, but
// the exclusive time is required for span metrics. Most of
// transactions don't have many spans, but if this is no longer the
// case and we roll this flag out for most projects, we may want to
// reconsider this approach.
normalize_app_start_spans(event);
span::attributes::normalize_spans(event, &BTreeSet::from([SpanAttribute::ExclusiveTime]));
}
if config.enrich_spans {
extract_span_tags(
event,
&tag_extraction::Config {
max_tag_value_size: config.max_tag_value_length,
},
);
}
if config.enable_trimming {
// Trim large strings and databags down
let _ =
trimming::TrimmingProcessor::new().process_event(event, meta, ProcessingState::root());
}
Ok(())
}
/// Backfills the client IP address on for the NEL reports.
fn normalize_nel_report(event: &mut Event, client_ip: Option<&IpAddr>) {
if event.context::<NelContext>().is_none() {
return;
}
if let Some(client_ip) = client_ip {
let user = event.user.value_mut().get_or_insert_with(User::default);
user.ip_address = Annotated::new(client_ip.to_owned());
}
}
/// Backfills common security report attributes.
fn normalize_security_report(
event: &mut Event,
client_ip: Option<&IpAddr>,
user_agent: &RawUserAgentInfo<&str>,
) {
if !is_security_report(event) {
// This event is not a security report, exit here.
return;
}
event.logger.get_or_insert_with(|| "csp".to_string());
if let Some(client_ip) = client_ip {
let user = event.user.value_mut().get_or_insert_with(User::default);
user.ip_address = Annotated::new(client_ip.to_owned());
}
if !user_agent.is_empty() {
let headers = event
.request
.value_mut()
.get_or_insert_with(Request::default)
.headers
.value_mut()
.get_or_insert_with(Headers::default);
user_agent.populate_event_headers(headers);
}
}
fn is_security_report(event: &Event) -> bool {
event.csp.value().is_some()
|| event.expectct.value().is_some()
|| event.expectstaple.value().is_some()
|| event.hpkp.value().is_some()
}
/// Backfills IP addresses in various places.
pub fn normalize_ip_addresses(
request: &mut Annotated<Request>,
user: &mut Annotated<User>,
platform: Option<&str>,
client_ip: Option<&IpAddr>,
) {
// NOTE: This is highly order dependent, in the sense that both the statements within this
// function need to be executed in a certain order, and that other normalization code
// (geoip lookup) needs to run after this.
//
// After a series of regressions over the old Python spaghetti code we decided to put it
// back into one function. If a desire to split this code up overcomes you, put this in a
// new processor and make sure all of it runs before the rest of normalization.
// Resolve {{auto}}
if let Some(client_ip) = client_ip {
if let Some(ref mut request) = request.value_mut() {
if let Some(ref mut env) = request.env.value_mut() {
if let Some(&mut Value::String(ref mut http_ip)) = env
.get_mut("REMOTE_ADDR")
.and_then(|annotated| annotated.value_mut().as_mut())
{
if http_ip == "{{auto}}" {
*http_ip = client_ip.to_string();
}
}
}
}
if let Some(ref mut user) = user.value_mut() {
if let Some(ref mut user_ip) = user.ip_address.value_mut() {
if user_ip.is_auto() {
*user_ip = client_ip.to_owned();
}
}
}
}
// Copy IPs from request interface to user, and resolve platform-specific backfilling
let http_ip = request
.value()
.and_then(|request| request.env.value())
.and_then(|env| env.get("REMOTE_ADDR"))
.and_then(Annotated::<Value>::as_str)
.and_then(|ip| IpAddr::parse(ip).ok());
if let Some(http_ip) = http_ip {
let user = user.value_mut().get_or_insert_with(User::default);
user.ip_address.value_mut().get_or_insert(http_ip);
} else if let Some(client_ip) = client_ip {
let user = user.value_mut().get_or_insert_with(User::default);
// auto is already handled above
if user.ip_address.value().is_none() {
// In an ideal world all SDKs would set {{auto}} explicitly.
if let Some("javascript") | Some("cocoa") | Some("objc") = platform {
user.ip_address = Annotated::new(client_ip.to_owned());
}
}
}
}
/// Sets the user's GeoIp info based on user's IP address.
fn normalize_user_geoinfo(geoip_lookup: &GeoIpLookup, user: &mut User) {
// Infer user.geo from user.ip_address
if user.geo.value().is_none() {
if let Some(ip_address) = user.ip_address.value() {
if let Ok(Some(geo)) = geoip_lookup.lookup(ip_address.as_str()) {
user.geo.set_value(Some(geo));
}
}
}
}
fn normalize_logentry(logentry: &mut Annotated<LogEntry>, _meta: &mut Meta) {
let _ = processor::apply(logentry, |logentry, meta| {
crate::logentry::normalize_logentry(logentry, meta)
});
}
/// Ensures that the `release` and `dist` fields match up.
fn normalize_release_dist(event: &mut Event) {
normalize_dist(&mut event.dist);
}
fn normalize_dist(distribution: &mut Annotated<String>) {
let _ = processor::apply(distribution, |dist, meta| {
let trimmed = dist.trim();
if trimmed.is_empty() {
return Err(ProcessingAction::DeleteValueHard);
} else if bytecount::num_chars(trimmed.as_bytes()) > MaxChars::Distribution.limit() {
meta.add_error(Error::new(ErrorKind::ValueTooLong));
return Err(ProcessingAction::DeleteValueSoft);
} else if trimmed != dist {
*dist = trimmed.to_string();
}
Ok(())
});
}
/// Defaults the `platform` and `level` required attributes.
fn normalize_platform_and_level(event: &mut Event) {
// The defaulting behavior, was inherited from `StoreNormalizeProcessor` and it's put here since only light
// normalization happens before metrics extraction and we want the metrics extraction pipeline to already work
// on some normalized data.
event.platform.get_or_insert_with(|| "other".to_string());
event.level.get_or_insert_with(|| match event.ty.value() {
Some(EventType::Transaction) => Level::Info,
_ => Level::Error,
});
}
/// Validates the timestamp range and sets a default value.
fn normalize_timestamps(
event: &mut Event,
meta: &mut Meta,
received_at: Option<DateTime<Utc>>,
max_secs_in_past: Option<i64>,
max_secs_in_future: Option<i64>,
) {
let received_at = received_at.unwrap_or_else(Utc::now);
let mut sent_at = None;
let mut error_kind = ErrorKind::ClockDrift;
let _ = processor::apply(&mut event.timestamp, |timestamp, _meta| {
if let Some(secs) = max_secs_in_future {
if *timestamp > received_at + Duration::seconds(secs) {
error_kind = ErrorKind::FutureTimestamp;
sent_at = Some(*timestamp);
return Ok(());
}
}
if let Some(secs) = max_secs_in_past {
if *timestamp < received_at - Duration::seconds(secs) {
error_kind = ErrorKind::PastTimestamp;
sent_at = Some(*timestamp);
return Ok(());
}
}
Ok(())
});
let _ = ClockDriftProcessor::new(sent_at.map(|ts| ts.into_inner()), received_at)
.error_kind(error_kind)
.process_event(event, meta, ProcessingState::root());
// Apply this after clock drift correction, otherwise we will malform it.
event.received = Annotated::new(received_at.into());
if event.timestamp.value().is_none() {
event.timestamp.set_value(Some(received_at.into()));
}
let _ = processor::apply(&mut event.time_spent, |time_spent, _| {
validate_bounded_integer_field(*time_spent)
});
}
/// Validate fields that go into a `sentry.models.BoundedIntegerField`.
fn validate_bounded_integer_field(value: u64) -> ProcessingResult {
if value < 2_147_483_647 {
Ok(())
} else {
Err(ProcessingAction::DeleteValueHard)
}
}
struct DedupCache(SmallVec<[u64; 16]>);
impl DedupCache {
pub fn new() -> Self {
Self(SmallVec::default())
}
pub fn probe<H: Hash>(&mut self, element: H) -> bool {
let mut hasher = DefaultHasher::new();
element.hash(&mut hasher);
let hash = hasher.finish();
if self.0.contains(&hash) {
false
} else {
self.0.push(hash);
true
}
}
}
/// Removes internal tags and adds tags for well-known attributes.
fn normalize_event_tags(event: &mut Event) {
let tags = &mut event.tags.value_mut().get_or_insert_with(Tags::default).0;
let environment = &mut event.environment;
if environment.is_empty() {
*environment = Annotated::empty();
}
// Fix case where legacy apps pass environment as a tag instead of a top level key
if let Some(tag) = tags.remove("environment").and_then(Annotated::into_value) {
environment.get_or_insert_with(|| tag);
}
// Remove internal tags, that are generated with a `sentry:` prefix when saving the event.
// They are not allowed to be set by the client due to ambiguity. Also, deduplicate tags.
let mut tag_cache = DedupCache::new();
tags.retain(|entry| {
match entry.value() {
Some(tag) => match tag.key() {
Some("release") | Some("dist") | Some("user") | Some("filename")
| Some("function") => false,
name => tag_cache.probe(name),
},
// ToValue will decide if we should skip serializing Annotated::empty()
None => true,
}
});
for tag in tags.iter_mut() {
let _ = processor::apply(tag, |tag, _| {
if let Some(key) = tag.key() {
if key.is_empty() {
tag.0 = Annotated::from_error(Error::nonempty(), None);
} else if bytecount::num_chars(key.as_bytes()) > MaxChars::TagKey.limit() {
tag.0 = Annotated::from_error(Error::new(ErrorKind::ValueTooLong), None);
}
}
if let Some(value) = tag.value() {
if value.is_empty() {
tag.1 = Annotated::from_error(Error::nonempty(), None);
} else if bytecount::num_chars(value.as_bytes()) > MaxChars::TagValue.limit() {
tag.1 = Annotated::from_error(Error::new(ErrorKind::ValueTooLong), None);
}
}
Ok(())
});
}
let server_name = std::mem::take(&mut event.server_name);
if server_name.value().is_some() {
let tag_name = "server_name".to_string();
tags.insert(tag_name, server_name);
}
let site = std::mem::take(&mut event.site);
if site.value().is_some() {
let tag_name = "site".to_string();
tags.insert(tag_name, site);
}
}
// Reads device specs (family, memory, cpu, etc) from context and sets the device.class tag to high,
// medium, or low.
fn normalize_device_class(event: &mut Event) {
let tags = &mut event.tags.value_mut().get_or_insert_with(Tags::default).0;
let tag_name = "device.class".to_owned();
// Remove any existing device.class tag set by the client, since this should only be set by relay.
tags.remove("device.class");
if let Some(contexts) = event.contexts.value() {
if let Some(device_class) = DeviceClass::from_contexts(contexts) {
tags.insert(tag_name, Annotated::new(device_class.to_string()));
}
}
}
/// Process the last frame of the stacktrace of the first exception.
///
/// No additional frames/stacktraces are normalized as they aren't required for metric extraction.
fn normalize_stacktraces(event: &mut Event) {
match event.exceptions.value_mut() {
None => (),
Some(exception) => match exception.values.value_mut() {
None => (),
Some(exceptions) => match exceptions.first_mut() {
None => (),
Some(first) => normalize_last_stacktrace_frame(first),
},
},
};
}
fn normalize_last_stacktrace_frame(exception: &mut Annotated<Exception>) {
let _ = processor::apply(exception, |e, _| {
processor::apply(&mut e.stacktrace, |s, _| match s.frames.value_mut() {
None => Ok(()),
Some(frames) => match frames.last_mut() {
None => Ok(()),
Some(frame) => {
stacktrace::process_non_raw_frame(frame);
Ok(())
}
},
})
});
}
fn normalize_exceptions(event: &mut Event) {
let os_hint = mechanism::OsHint::from_event(event);
if let Some(exception_values) = event.exceptions.value_mut() {
if let Some(exceptions) = exception_values.values.value_mut() {
if exceptions.len() == 1 && event.stacktrace.value().is_some() {
if let Some(exception) = exceptions.get_mut(0) {
if let Some(exception) = exception.value_mut() {
mem::swap(&mut exception.stacktrace, &mut event.stacktrace);
event.stacktrace = Annotated::empty();
}
}
}
// Exception mechanism needs SDK information to resolve proper names in
// exception meta (such as signal names). "SDK Information" really means
// the operating system version the event was generated on. Some
// normalization still works without sdk_info, such as mach_exception
// names (they can only occur on macOS).
//
// We also want to validate some other aspects of it.
for exception in exceptions {
if let Some(exception) = exception.value_mut() {
if let Some(mechanism) = exception.mechanism.value_mut() {
mechanism::normalize_mechanism(mechanism, os_hint);
}
}
}
}
}
}
fn normalize_user_agent(_event: &mut Event, normalize_user_agent: Option<bool>) {
if normalize_user_agent.unwrap_or(false) {
user_agent::normalize_user_agent(_event);
}
}
/// Ensure measurements interface is only present for transaction events.
fn normalize_measurements(
event: &mut Event,
measurements_config: Option<DynamicMeasurementsConfig>,
max_mri_len: Option<usize>,
) {
if event.ty.value() != Some(&EventType::Transaction) {
// Only transaction events may have a measurements interface
event.measurements = Annotated::empty();
} else if let Annotated(Some(ref mut measurements), ref mut meta) = event.measurements {
normalize_mobile_measurements(measurements);
normalize_units(measurements);
if let Some(measurements_config) = measurements_config {
remove_invalid_measurements(measurements, meta, measurements_config, max_mri_len);
}
let duration_millis = match (event.start_timestamp.0, event.timestamp.0) {
(Some(start), Some(end)) => relay_common::time::chrono_to_positive_millis(end - start),
_ => 0.0,
};
compute_measurements(duration_millis, measurements);
}
}
/// Computes performance score measurements.
///
/// This computes score from vital measurements, using config options to define how it is
/// calculated.
fn normalize_performance_score(
event: &mut Event,
performance_score: Option<&PerformanceScoreConfig>,
) {
let Some(performance_score) = performance_score else {
return;
};
for profile in &performance_score.profiles {
if let Some(condition) = &profile.condition {
if !condition.matches(event) {
continue;
}
if let Some(measurements) = event.measurements.value_mut() {
let mut should_add_total = false;
if profile.score_components.iter().any(|c| {
!measurements.contains_key(c.measurement.as_str())
&& c.weight.abs() >= f64::EPSILON
&& !c.optional
}) {
// All non-optional measurements with a profile weight greater than 0 are
// required to exist on the event. Skip calculating performance scores if
// a measurement with weight is missing.
break;
}
let mut score_total = 0.0;
let mut weight_total = 0.0;
for component in &profile.score_components {
// Skip optional components if they are not present on the event.
if component.optional
&& !measurements.contains_key(component.measurement.as_str())
{
continue;
}
weight_total += component.weight;
}
if weight_total.abs() < f64::EPSILON {
// All components are optional or have a weight of `0`. We cannot compute
// component weights, so we bail.
break;
}
for component in &profile.score_components {
// Optional measurements that are not present are given a weight of 0.
let mut normalized_component_weight = 0.0;
if let Some(value) = measurements.get_value(component.measurement.as_str()) {
normalized_component_weight = component.weight / weight_total;
let cdf = utils::calculate_cdf_score(
value.max(0.0), // Webvitals can't be negative, but we need to clamp in case of bad data.
component.p10,
component.p50,
);
let component_score = cdf * normalized_component_weight;
score_total += component_score;
should_add_total = true;
measurements.insert(
format!("score.{}", component.measurement),
Measurement {
value: component_score.into(),
unit: (MetricUnit::Fraction(FractionUnit::Ratio)).into(),
}
.into(),
);
}
measurements.insert(
format!("score.weight.{}", component.measurement),
Measurement {
value: normalized_component_weight.into(),
unit: (MetricUnit::Fraction(FractionUnit::Ratio)).into(),
}
.into(),
);
}
if should_add_total {
measurements.insert(
"score.total".to_owned(),
Measurement {
value: score_total.into(),
unit: (MetricUnit::Fraction(FractionUnit::Ratio)).into(),
}
.into(),
);
}
break; // Measurements have successfully been added, skip any other profiles.
}
}
}
}
/// Compute additional measurements derived from existing ones.
///
/// The added measurements are:
///
/// ```text
/// frames_slow_rate := measurements.frames_slow / measurements.frames_total
/// frames_frozen_rate := measurements.frames_frozen / measurements.frames_total
/// stall_percentage := measurements.stall_total_time / transaction.duration
/// ```
fn compute_measurements(transaction_duration_ms: f64, measurements: &mut Measurements) {
if let Some(frames_total) = measurements.get_value("frames_total") {
if frames_total > 0.0 {
if let Some(frames_frozen) = measurements.get_value("frames_frozen") {
let frames_frozen_rate = Measurement {
value: (frames_frozen / frames_total).into(),
unit: (MetricUnit::Fraction(FractionUnit::Ratio)).into(),
};
measurements.insert("frames_frozen_rate".to_owned(), frames_frozen_rate.into());
}
if let Some(frames_slow) = measurements.get_value("frames_slow") {
let frames_slow_rate = Measurement {
value: (frames_slow / frames_total).into(),
unit: MetricUnit::Fraction(FractionUnit::Ratio).into(),
};
measurements.insert("frames_slow_rate".to_owned(), frames_slow_rate.into());
}
}
}
// Get stall_percentage
if transaction_duration_ms > 0.0 {
if let Some(stall_total_time) = measurements
.get("stall_total_time")
.and_then(Annotated::value)
{
if matches!(
stall_total_time.unit.value(),
// Accept milliseconds or None, but not other units
Some(&MetricUnit::Duration(DurationUnit::MilliSecond) | &MetricUnit::None) | None
) {
if let Some(stall_total_time) = stall_total_time.value.0 {
let stall_percentage = Measurement {
value: (stall_total_time / transaction_duration_ms).into(),
unit: (MetricUnit::Fraction(FractionUnit::Ratio)).into(),
};
measurements.insert("stall_percentage".to_owned(), stall_percentage.into());
}
}
}
}
}
/// Emit any breakdowns
fn normalize_breakdowns(event: &mut Event, breakdowns_config: Option<&BreakdownsConfig>) {
match breakdowns_config {
None => {}
Some(config) => breakdowns::normalize_breakdowns(event, config),
}
}
/// Replaces snake_case app start spans op with dot.case op.
///
/// This is done for the affected React Native SDK versions (from 3 to 4.4).
fn normalize_app_start_spans(event: &mut Event) {
if !event.sdk_name().eq("sentry.javascript.react-native")
|| !(event.sdk_version().starts_with("4.4")
|| event.sdk_version().starts_with("4.3")
|| event.sdk_version().starts_with("4.2")
|| event.sdk_version().starts_with("4.1")
|| event.sdk_version().starts_with("4.0")
|| event.sdk_version().starts_with('3'))
{
return;
}
if let Some(spans) = event.spans.value_mut() {
for span in spans {
if let Some(span) = span.value_mut() {
if let Some(op) = span.op.value() {
if op == "app_start_cold" {
span.op.set_value(Some("app.start.cold".to_string()));
break;
} else if op == "app_start_warm" {
span.op.set_value(Some("app.start.warm".to_string()));
break;
}
}
}
}
}
}
/// Normalizes incoming contexts for the downstream metric extraction.
fn normalize_contexts(contexts: &mut Annotated<Contexts>) {
let _ = processor::apply(contexts, |contexts, _meta| {
for annotated in &mut contexts.0.values_mut() {
if let Some(ContextInner(Context::Trace(context))) = annotated.value_mut() {
context.status.get_or_insert_with(|| SpanStatus::Unknown);
}
}
Ok(())
});
}
/// New SDKs do not send measurements when they exceed 180 seconds.
///
/// Drop those outlier measurements for older SDKs.
fn filter_mobile_outliers(measurements: &mut Measurements) {
for key in [
"app_start_cold",
"app_start_warm",
"time_to_initial_display",
"time_to_full_display",
] {
if let Some(value) = measurements.get_value(key) {
if value > MAX_DURATION_MOBILE_MS {
measurements.remove(key);
}
}
}
}
fn normalize_mobile_measurements(measurements: &mut Measurements) {
normalize_app_start_measurements(measurements);
filter_mobile_outliers(measurements);
}
fn normalize_units(measurements: &mut Measurements) {
for (name, measurement) in measurements.iter_mut() {
let measurement = match measurement.value_mut() {
Some(m) => m,
None => continue,
};
let stated_unit = measurement.unit.value().copied();
let default_unit = get_metric_measurement_unit(name);
measurement
.unit
.set_value(Some(stated_unit.or(default_unit).unwrap_or_default()))
}
}
/// Remove measurements that do not conform to the given config.
///
/// Built-in measurements are accepted if their unit is correct, dropped otherwise.
/// Custom measurements are accepted up to a limit.
///
/// Note that [`Measurements`] is a BTreeMap, which means its keys are sorted.
/// This ensures that for two events with the same measurement keys, the same set of custom
/// measurements is retained.
fn remove_invalid_measurements(
measurements: &mut Measurements,
meta: &mut Meta,
measurements_config: DynamicMeasurementsConfig,
max_name_and_unit_len: Option<usize>,
) {
let max_custom_measurements = measurements_config.max_custom_measurements().unwrap_or(0);
let mut custom_measurements_count = 0;
let mut removed_measurements = Object::new();
measurements.retain(|name, value| {
let measurement = match value.value_mut() {
Some(m) => m,
None => return false,
};
if !can_be_valid_metric_name(name) {
meta.add_error(Error::invalid(format!(
"Metric name contains invalid characters: \"{name}\""
)));
removed_measurements.insert(name.clone(), Annotated::new(std::mem::take(measurement)));
return false;
}
// TODO(jjbayer): Should we actually normalize the unit into the event?
let unit = measurement.unit.value().unwrap_or(&MetricUnit::None);
if let Some(max_name_and_unit_len) = max_name_and_unit_len {
let max_name_len = max_name_and_unit_len - unit.to_string().len();
if name.len() > max_name_len {
meta.add_error(Error::invalid(format!(
"Metric name too long {}/{max_name_len}: \"{name}\"",
name.len(),
)));