1use std::fmt;
2use std::str::FromStr;
3
4use relay_common::time;
5use relay_protocol::{
6 Annotated, Array, Empty, FiniteF64, FromValue, Getter, GetterIter, IntoValue, Object, Val,
7 Value,
8};
9use sentry_release_parser::Release as ParsedRelease;
10use uuid::Uuid;
11
12use crate::processor::ProcessValue;
13use crate::protocol::{
14 AppContext, Breadcrumb, Breakdowns, BrowserContext, ClientSdkInfo, Contexts, Csp, DebugMeta,
15 DefaultContext, DeviceContext, EventType, Exception, ExpectCt, ExpectStaple, Fingerprint,
16 GpuContext, Hpkp, LenientString, Level, LogEntry, Measurements, Metrics, MonitorContext,
17 OsContext, ProfileContext, RelayInfo, Request, ResponseContext, RuntimeContext, Span, SpanId,
18 Stacktrace, Tags, TemplateInfo, Thread, Timestamp, TraceContext, TransactionInfo, User, Values,
19};
20
21#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
23pub struct EventId(pub Uuid);
24
25impl EventId {
26 #[inline]
28 pub fn new() -> Self {
29 Self(Uuid::new_v4())
30 }
31
32 pub fn nil() -> Self {
34 Self(Uuid::nil())
35 }
36
37 #[inline]
39 pub fn is_nil(&self) -> bool {
40 self.0.is_nil()
41 }
42}
43
44impl Default for EventId {
45 #[inline]
46 fn default() -> Self {
47 Self::new()
48 }
49}
50
51relay_protocol::derive_string_meta_structure!(EventId, "event id");
52
53impl ProcessValue for EventId {}
54
55impl fmt::Display for EventId {
56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57 write!(f, "{}", self.0.as_simple())
58 }
59}
60
61impl FromStr for EventId {
62 type Err = <Uuid as FromStr>::Err;
63
64 fn from_str(uuid_str: &str) -> Result<Self, Self::Err> {
65 uuid_str.parse().map(EventId)
66 }
67}
68
69relay_common::impl_str_serde!(EventId, "an event identifier");
70
71impl TryFrom<&SpanId> for EventId {
72 type Error = <EventId as FromStr>::Err;
73
74 fn try_from(value: &SpanId) -> Result<Self, Self::Error> {
75 let s = format!("0000000000000000{value}");
77 s.parse()
78 }
79}
80
81#[derive(Debug, FromValue, IntoValue, ProcessValue, Empty, Clone, PartialEq)]
82pub struct ExtraValue(#[metastructure(max_depth = 7, max_bytes = 16_384)] pub Value);
83
84impl<T: Into<Value>> From<T> for ExtraValue {
85 fn from(value: T) -> ExtraValue {
86 ExtraValue(value.into())
87 }
88}
89
90#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
92pub struct EventProcessingError {
93 #[metastructure(field = "type", required = true)]
95 pub ty: Annotated<String>,
96
97 pub name: Annotated<String>,
99
100 pub value: Annotated<Value>,
102
103 #[metastructure(additional_properties, pii = "maybe")]
105 pub other: Object<Value>,
106}
107
108#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
113pub struct GroupingConfig {
114 #[metastructure(max_chars = 128)]
116 pub id: Annotated<String>,
117 pub enhancements: Annotated<String>,
119}
120
121#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
123#[metastructure(process_func = "process_event", value_type = "Event")]
124pub struct Event {
125 #[metastructure(field = "event_id")]
143 pub id: Annotated<EventId>,
144
145 pub level: Annotated<Level>,
153
154 pub version: Annotated<String>,
156
157 #[metastructure(field = "type")]
182 pub ty: Annotated<EventType>,
183
184 #[metastructure(skip_serialization = "empty")]
195 pub fingerprint: Annotated<Fingerprint>,
196
197 #[metastructure(max_chars = 200, pii = "maybe")]
201 pub culprit: Annotated<String>,
202
203 #[metastructure(max_chars = 200, trim_whitespace = true)]
208 pub transaction: Annotated<String>,
209
210 #[metastructure(skip_serialization = "null")]
212 pub transaction_info: Annotated<TransactionInfo>,
213
214 pub time_spent: Annotated<u64>,
216
217 #[metastructure(legacy_alias = "sentry.interfaces.Message", legacy_alias = "message")]
219 #[metastructure(skip_serialization = "empty")]
220 pub logentry: Annotated<LogEntry>,
221
222 #[metastructure(
224 max_chars = 64, deny_chars = "\r\n",
226 )]
227 pub logger: Annotated<String>,
228
229 #[metastructure(skip_serialization = "empty_deep", max_depth = 7, max_bytes = 8192)]
243 pub modules: Annotated<Object<String>>,
244
245 pub platform: Annotated<String>,
255
256 pub timestamp: Annotated<Timestamp>,
282
283 #[metastructure(omit_from_schema)] pub start_timestamp: Annotated<Timestamp>,
286
287 pub received: Annotated<Timestamp>,
289
290 #[metastructure(pii = "true", max_chars = 256, max_chars_allowance = 20)]
294 pub server_name: Annotated<String>,
295
296 #[metastructure(
301 max_chars = 200, required = false,
304 trim_whitespace = true,
305 nonempty = true,
306 skip_serialization = "empty"
307 )]
308 pub release: Annotated<LenientString>,
309
310 #[metastructure(
318 allow_chars = "a-zA-Z0-9_.-",
319 trim_whitespace = true,
320 required = false,
321 nonempty = true
322 )]
323 pub dist: Annotated<String>,
324
325 #[metastructure(
331 max_chars = 64,
332 nonempty = true,
334 required = false,
335 trim_whitespace = true
336 )]
337 pub environment: Annotated<String>,
338
339 #[metastructure(max_chars = 256, max_chars_allowance = 20)]
341 #[metastructure(omit_from_schema)] pub site: Annotated<String>,
343
344 #[metastructure(legacy_alias = "sentry.interfaces.User")]
346 #[metastructure(skip_serialization = "empty")]
347 pub user: Annotated<User>,
348
349 #[metastructure(legacy_alias = "sentry.interfaces.Http")]
351 #[metastructure(skip_serialization = "empty")]
352 pub request: Annotated<Request>,
353
354 #[metastructure(legacy_alias = "sentry.interfaces.Contexts")]
356 pub contexts: Annotated<Contexts>,
357
358 #[metastructure(legacy_alias = "sentry.interfaces.Breadcrumbs")]
360 #[metastructure(skip_serialization = "empty")]
361 pub breadcrumbs: Annotated<Values<Breadcrumb>>,
362
363 #[metastructure(legacy_alias = "sentry.interfaces.Exception")]
365 #[metastructure(field = "exception")]
366 #[metastructure(skip_serialization = "empty")]
367 pub exceptions: Annotated<Values<Exception>>,
368
369 #[metastructure(skip_serialization = "empty")]
373 #[metastructure(legacy_alias = "sentry.interfaces.Stacktrace")]
374 pub stacktrace: Annotated<Stacktrace>,
375
376 #[metastructure(legacy_alias = "sentry.interfaces.Template")]
380 #[metastructure(omit_from_schema)]
381 pub template: Annotated<TemplateInfo>,
382
383 #[metastructure(skip_serialization = "empty")]
385 pub threads: Annotated<Values<Thread>>,
386
387 #[metastructure(skip_serialization = "empty", pii = "maybe")]
391 pub tags: Annotated<Tags>,
392
393 #[metastructure(max_depth = 7, max_bytes = 262_144)]
403 #[metastructure(pii = "true", skip_serialization = "empty")]
404 pub extra: Annotated<Object<ExtraValue>>,
405
406 #[metastructure(skip_serialization = "empty")]
408 pub debug_meta: Annotated<DebugMeta>,
409
410 #[metastructure(field = "sdk")]
412 #[metastructure(skip_serialization = "empty")]
413 pub client_sdk: Annotated<ClientSdkInfo>,
414
415 #[metastructure(max_depth = 5, max_bytes = 2048)]
417 #[metastructure(skip_serialization = "empty", omit_from_schema)]
418 pub ingest_path: Annotated<Array<RelayInfo>>,
419
420 #[metastructure(skip_serialization = "empty_deep")]
423 pub errors: Annotated<Array<EventProcessingError>>,
424
425 #[metastructure(omit_from_schema)] pub key_id: Annotated<String>,
428
429 #[metastructure(omit_from_schema)] pub project: Annotated<u64>,
432
433 #[metastructure(omit_from_schema)] pub grouping_config: Annotated<Object<Value>>,
436
437 #[metastructure(max_chars = 128)]
439 #[metastructure(omit_from_schema)] pub checksum: Annotated<String>,
441
442 #[metastructure(legacy_alias = "sentry.interfaces.Csp")]
444 #[metastructure(omit_from_schema)] pub csp: Annotated<Csp>,
446
447 #[metastructure(pii = "true", legacy_alias = "sentry.interfaces.Hpkp")]
449 #[metastructure(omit_from_schema)] pub hpkp: Annotated<Hpkp>,
451
452 #[metastructure(pii = "true", legacy_alias = "sentry.interfaces.ExpectCT")]
454 #[metastructure(omit_from_schema)] pub expectct: Annotated<ExpectCt>,
456
457 #[metastructure(pii = "true", legacy_alias = "sentry.interfaces.ExpectStaple")]
459 #[metastructure(omit_from_schema)] pub expectstaple: Annotated<ExpectStaple>,
461
462 #[metastructure(max_bytes = 819200)]
464 #[metastructure(omit_from_schema)] pub spans: Annotated<Array<Span>>,
466
467 #[metastructure(skip_serialization = "empty")]
472 #[metastructure(omit_from_schema)] pub measurements: Annotated<Measurements>,
474
475 #[metastructure(skip_serialization = "empty")]
477 #[metastructure(omit_from_schema)] pub breakdowns: Annotated<Breakdowns>,
479
480 #[metastructure(omit_from_schema)] pub scraping_attempts: Annotated<Value>,
484
485 #[metastructure(omit_from_schema)]
489 pub _metrics: Annotated<Metrics>,
490
491 #[metastructure(omit_from_schema)]
493 pub _dsc: Annotated<Value>,
494
495 #[metastructure(
500 field = "_performance_issues_spans",
501 skip_serialization = "empty",
502 trim = false
503 )]
504 pub performance_issues_spans: Annotated<bool>,
505
506 #[metastructure(additional_properties, pii = "true")]
508 pub other: Object<Value>,
509}
510
511impl Event {
512 pub fn tag_value(&self, tag_key: &str) -> Option<&str> {
517 if let Some(tags) = self.tags.value() {
518 tags.get(tag_key)
519 } else {
520 None
521 }
522 }
523
524 pub fn has_module(&self, module_name: &str) -> bool {
526 self.modules
527 .value()
528 .map(|m| m.contains_key(module_name))
529 .unwrap_or(false)
530 }
531
532 pub fn sdk_name(&self) -> &str {
536 if let Some(client_sdk) = self.client_sdk.value()
537 && let Some(name) = client_sdk.name.as_str()
538 {
539 return name;
540 }
541
542 "unknown"
543 }
544
545 pub fn sdk_version(&self) -> &str {
549 if let Some(client_sdk) = self.client_sdk.value()
550 && let Some(version) = client_sdk.version.as_str()
551 {
552 return version;
553 }
554
555 "unknown"
556 }
557
558 pub fn user_agent(&self) -> Option<&str> {
563 let headers = self.request.value()?.headers.value()?;
564
565 for item in headers.iter() {
566 if let Some((o_k, v)) = item.value()
567 && let Some(k) = o_k.as_str()
568 && k.eq_ignore_ascii_case("user-agent")
569 {
570 return v.as_str();
571 }
572 }
573
574 None
575 }
576
577 pub fn extra_at(&self, path: &str) -> Option<&Value> {
582 let mut path = path.split('.');
583
584 let mut value = &self.extra.value()?.get(path.next()?)?.value()?.0;
586
587 for key in path {
589 if let Value::Object(object) = value {
590 value = object.get(key)?.value()?;
591 } else {
592 return None;
593 }
594 }
595
596 Some(value)
597 }
598
599 pub fn parse_release(&self) -> Option<ParsedRelease<'_>> {
601 sentry_release_parser::Release::parse(self.release.as_str()?).ok()
602 }
603
604 pub fn measurement(&self, name: &str) -> Option<FiniteF64> {
608 let annotated = self.measurements.value()?.get(name)?;
609 Some(*annotated.value()?.value.value()?)
610 }
611
612 pub fn breakdown(&self, breakdown: &str, measurement: &str) -> Option<FiniteF64> {
614 let breakdown = self.breakdowns.value()?.get(breakdown)?.value()?;
615 Some(*breakdown.get(measurement)?.value()?.value.value()?)
616 }
617
618 pub fn context<C: DefaultContext>(&self) -> Option<&C> {
620 self.contexts.value()?.get()
621 }
622
623 pub fn context_mut<C: DefaultContext>(&mut self) -> Option<&mut C> {
625 self.contexts.value_mut().as_mut()?.get_mut()
626 }
627}
628
629fn or_none(string: &Annotated<impl AsRef<str>>) -> Option<&str> {
630 match string.as_str() {
631 None | Some("") => None,
632 Some(other) => Some(other),
633 }
634}
635
636impl Getter for Event {
637 fn get_value(&self, path: &str) -> Option<Val<'_>> {
638 Some(match path.strip_prefix("event.")? {
639 "level" => self.level.value()?.name().into(),
641 "release" => self.release.as_str()?.into(),
642 "dist" => self.dist.as_str()?.into(),
643 "environment" => self.environment.as_str()?.into(),
644 "transaction" => self.transaction.as_str()?.into(),
645 "logger" => self.logger.as_str()?.into(),
646 "platform" => self.platform.as_str().unwrap_or("other").into(),
647
648 "logentry.formatted" => self.logentry.value()?.formatted.value()?.as_ref().into(),
650 "logentry.message" => self.logentry.value()?.message.value()?.as_ref().into(),
651 "user.email" => or_none(&self.user.value()?.email)?.into(),
652 "user.id" => or_none(&self.user.value()?.id)?.into(),
653 "user.ip_address" => self.user.value()?.ip_address.as_str()?.into(),
654 "user.name" => self.user.value()?.name.as_str()?.into(),
655 "user.segment" => or_none(&self.user.value()?.segment)?.into(),
656 "user.geo.city" => self.user.value()?.geo.value()?.city.as_str()?.into(),
657 "user.geo.country_code" => self
658 .user
659 .value()?
660 .geo
661 .value()?
662 .country_code
663 .as_str()?
664 .into(),
665 "user.geo.region" => self.user.value()?.geo.value()?.region.as_str()?.into(),
666 "user.geo.subdivision" => self.user.value()?.geo.value()?.subdivision.as_str()?.into(),
667 "request.method" => self.request.value()?.method.as_str()?.into(),
668 "request.url" => self.request.value()?.url.as_str()?.into(),
669 "transaction.source" => self
670 .transaction_info
671 .value()?
672 .source
673 .value()?
674 .as_str()
675 .into(),
676 "sdk.name" => self.client_sdk.value()?.name.as_str()?.into(),
677 "sdk.version" => self.client_sdk.value()?.version.as_str()?.into(),
678
679 "sentry_user" => self.user.value()?.sentry_user.as_str()?.into(),
681
682 "contexts.app.in_foreground" => {
684 self.context::<AppContext>()?.in_foreground.value()?.into()
685 }
686 "contexts.app.device_app_hash" => self
687 .context::<AppContext>()?
688 .device_app_hash
689 .as_str()?
690 .into(),
691 "contexts.device.arch" => self.context::<DeviceContext>()?.arch.as_str()?.into(),
692 "contexts.device.battery_level" => self
693 .context::<DeviceContext>()?
694 .battery_level
695 .value()?
696 .into(),
697 "contexts.device.brand" => self.context::<DeviceContext>()?.brand.as_str()?.into(),
698 "contexts.device.charging" => self.context::<DeviceContext>()?.charging.value()?.into(),
699 "contexts.device.family" => self.context::<DeviceContext>()?.family.as_str()?.into(),
700 "contexts.device.model" => self.context::<DeviceContext>()?.model.as_str()?.into(),
701 "contexts.device.locale" => self.context::<DeviceContext>()?.locale.as_str()?.into(),
702 "contexts.device.online" => self.context::<DeviceContext>()?.online.value()?.into(),
703 "contexts.device.orientation" => self
704 .context::<DeviceContext>()?
705 .orientation
706 .as_str()?
707 .into(),
708 "contexts.device.name" => self.context::<DeviceContext>()?.name.as_str()?.into(),
709 "contexts.device.screen_density" => self
710 .context::<DeviceContext>()?
711 .screen_density
712 .value()?
713 .into(),
714 "contexts.device.screen_dpi" => {
715 self.context::<DeviceContext>()?.screen_dpi.value()?.into()
716 }
717 "contexts.device.screen_width_pixels" => self
718 .context::<DeviceContext>()?
719 .screen_width_pixels
720 .value()?
721 .into(),
722 "contexts.device.screen_height_pixels" => self
723 .context::<DeviceContext>()?
724 .screen_height_pixels
725 .value()?
726 .into(),
727 "contexts.device.simulator" => {
728 self.context::<DeviceContext>()?.simulator.value()?.into()
729 }
730 "contexts.gpu.vendor_name" => {
731 self.context::<GpuContext>()?.vendor_name.as_str()?.into()
732 }
733 "contexts.gpu.name" => self.context::<GpuContext>()?.name.as_str()?.into(),
734 "contexts.monitor.id" => self.context::<MonitorContext>()?.get("id")?.value()?.into(),
735 "contexts.monitor.slug" => self
736 .context::<MonitorContext>()?
737 .get("slug")?
738 .value()?
739 .into(),
740 "contexts.os" => self.context::<OsContext>()?.os.as_str()?.into(),
741 "contexts.os.build" => self.context::<OsContext>()?.build.as_str()?.into(),
742 "contexts.os.kernel_version" => {
743 self.context::<OsContext>()?.kernel_version.as_str()?.into()
744 }
745 "contexts.os.name" => self.context::<OsContext>()?.name.as_str()?.into(),
746 "contexts.os.version" => self.context::<OsContext>()?.version.as_str()?.into(),
747 "contexts.os.rooted" => self.context::<OsContext>()?.rooted.value()?.into(),
748 "contexts.browser" => self.context::<BrowserContext>()?.browser.as_str()?.into(),
749 "contexts.browser.name" => self.context::<BrowserContext>()?.name.as_str()?.into(),
750 "contexts.browser.version" => {
751 self.context::<BrowserContext>()?.version.as_str()?.into()
752 }
753 "contexts.profile.profile_id" => {
754 (&self.context::<ProfileContext>()?.profile_id.value()?.0).into()
755 }
756 "contexts.device.uuid" => self.context::<DeviceContext>()?.uuid.value()?.into(),
757 "contexts.trace.status" => self
758 .context::<TraceContext>()?
759 .status
760 .value()?
761 .as_str()
762 .into(),
763 "contexts.trace.op" => self.context::<TraceContext>()?.op.as_str()?.into(),
764 "contexts.response.status_code" => self
765 .context::<ResponseContext>()?
766 .status_code
767 .value()?
768 .into(),
769 "contexts.unreal.crash_type" => match self.contexts.value()?.get_key("unreal")? {
770 super::Context::Other(context) => context.get("crash_type")?.value()?.into(),
771 _ => return None,
772 },
773 "contexts.runtime" => self.context::<RuntimeContext>()?.runtime.as_str()?.into(),
774 "contexts.runtime.name" => self.context::<RuntimeContext>()?.name.as_str()?.into(),
775
776 "duration" => {
778 let start = self.start_timestamp.value()?;
779 let end = self.timestamp.value()?;
780 if start <= end && self.ty.value() == Some(&EventType::Transaction) {
781 time::chrono_to_positive_millis(*end - *start).into()
782 } else {
783 return None;
784 }
785 }
786
787 path => {
789 if let Some(rest) = path.strip_prefix("release.") {
790 let release = self.parse_release()?;
791 match rest {
792 "build" => release.build_hash()?.into(),
793 "package" => release.package()?.into(),
794 "version.short" => release.version()?.raw_short().into(),
795 _ => return None,
796 }
797 } else if let Some(rest) = path.strip_prefix("measurements.") {
798 let name = rest.strip_suffix(".value")?;
799 self.measurement(name)?.into()
800 } else if let Some(rest) = path.strip_prefix("breakdowns.") {
801 let (breakdown, measurement) = rest.split_once('.')?;
802 self.breakdown(breakdown, measurement)?.into()
803 } else if let Some(rest) = path.strip_prefix("extra.") {
804 self.extra_at(rest)?.into()
805 } else if let Some(rest) = path.strip_prefix("tags.") {
806 self.tags.value()?.get(rest)?.into()
807 } else if let Some(rest) = path.strip_prefix("request.headers.") {
808 self.request
809 .value()?
810 .headers
811 .value()?
812 .get_header(rest)?
813 .into()
814 } else {
815 return None;
816 }
817 }
818 })
819 }
820
821 fn get_iter(&self, path: &str) -> Option<GetterIter<'_>> {
822 Some(match path.strip_prefix("event.")? {
823 "exception.values" => {
824 GetterIter::new_annotated(self.exceptions.value()?.values.value()?)
825 }
826 _ => return None,
827 })
828 }
829}
830
831#[cfg(test)]
832mod tests {
833 use chrono::{TimeZone, Utc};
834 use relay_protocol::{ErrorKind, HexId, Map, Meta};
835 use similar_asserts::assert_eq;
836 use std::collections::BTreeMap;
837 use uuid::uuid;
838
839 use super::*;
840 use crate::protocol::{
841 Headers, IpAddr, JsonLenientString, PairList, TagEntry, TransactionSource,
842 };
843
844 #[test]
845 fn test_event_roundtrip() {
846 let json = r#"{
848 "event_id": "52df9022835246eeb317dbd739ccd059",
849 "level": "debug",
850 "fingerprint": [
851 "myprint"
852 ],
853 "culprit": "myculprit",
854 "transaction": "mytransaction",
855 "logentry": {
856 "formatted": "mymessage"
857 },
858 "logger": "mylogger",
859 "modules": {
860 "mymodule": "1.0.0"
861 },
862 "platform": "myplatform",
863 "timestamp": 946684800.0,
864 "server_name": "myhost",
865 "release": "myrelease",
866 "dist": "mydist",
867 "environment": "myenv",
868 "tags": [
869 [
870 "tag",
871 "value"
872 ]
873 ],
874 "extra": {
875 "extra": "value"
876 },
877 "other": "value",
878 "_meta": {
879 "event_id": {
880 "": {
881 "err": [
882 "invalid_data"
883 ]
884 }
885 }
886 }
887}"#;
888
889 let event = Annotated::new(Event {
890 id: Annotated(
891 Some("52df9022-8352-46ee-b317-dbd739ccd059".parse().unwrap()),
892 Meta::from_error(ErrorKind::InvalidData),
893 ),
894 level: Annotated::new(Level::Debug),
895 fingerprint: Annotated::new(vec!["myprint".to_owned()].into()),
896 culprit: Annotated::new("myculprit".to_owned()),
897 transaction: Annotated::new("mytransaction".to_owned()),
898 logentry: Annotated::new(LogEntry {
899 formatted: Annotated::new("mymessage".to_owned().into()),
900 ..Default::default()
901 }),
902 logger: Annotated::new("mylogger".to_owned()),
903 modules: {
904 let mut map = Map::new();
905 map.insert("mymodule".to_owned(), Annotated::new("1.0.0".to_owned()));
906 Annotated::new(map)
907 },
908 platform: Annotated::new("myplatform".to_owned()),
909 timestamp: Annotated::new(Utc.with_ymd_and_hms(2000, 1, 1, 0, 0, 0).unwrap().into()),
910 server_name: Annotated::new("myhost".to_owned()),
911 release: Annotated::new("myrelease".to_owned().into()),
912 dist: Annotated::new("mydist".to_owned()),
913 environment: Annotated::new("myenv".to_owned()),
914 tags: {
915 let items = vec![Annotated::new(TagEntry(
916 Annotated::new("tag".to_owned()),
917 Annotated::new("value".to_owned()),
918 ))];
919 Annotated::new(Tags(items.into()))
920 },
921 extra: {
922 let mut map = Map::new();
923 map.insert(
924 "extra".to_owned(),
925 Annotated::new(ExtraValue(Value::String("value".to_owned()))),
926 );
927 Annotated::new(map)
928 },
929 other: {
930 let mut map = Map::new();
931 map.insert(
932 "other".to_owned(),
933 Annotated::new(Value::String("value".to_owned())),
934 );
935 map
936 },
937 ..Default::default()
938 });
939
940 assert_eq!(event, Annotated::from_json(json).unwrap());
941 assert_eq!(json, event.to_json_pretty().unwrap());
942 }
943
944 #[test]
945 fn test_event_default_values() {
946 let json = "{}";
947 let event = Annotated::new(Event::default());
948
949 assert_eq!(event, Annotated::from_json(json).unwrap());
950 assert_eq!(json, event.to_json_pretty().unwrap());
951 }
952
953 #[test]
954 fn test_event_default_values_with_meta() {
955 let json = r#"{
956 "event_id": "52df9022835246eeb317dbd739ccd059",
957 "fingerprint": [
958 "{{ default }}"
959 ],
960 "platform": "other",
961 "_meta": {
962 "event_id": {
963 "": {
964 "err": [
965 "invalid_data"
966 ]
967 }
968 },
969 "fingerprint": {
970 "": {
971 "err": [
972 "invalid_data"
973 ]
974 }
975 },
976 "platform": {
977 "": {
978 "err": [
979 "invalid_data"
980 ]
981 }
982 }
983 }
984}"#;
985
986 let event = Annotated::new(Event {
987 id: Annotated(
988 Some("52df9022-8352-46ee-b317-dbd739ccd059".parse().unwrap()),
989 Meta::from_error(ErrorKind::InvalidData),
990 ),
991 fingerprint: Annotated(
992 Some(vec!["{{ default }}".to_owned()].into()),
993 Meta::from_error(ErrorKind::InvalidData),
994 ),
995 platform: Annotated(
996 Some("other".to_owned()),
997 Meta::from_error(ErrorKind::InvalidData),
998 ),
999 ..Default::default()
1000 });
1001
1002 assert_eq!(event, Annotated::<Event>::from_json(json).unwrap());
1003 assert_eq!(json, event.to_json_pretty().unwrap());
1004 }
1005
1006 #[test]
1007 fn test_event_type() {
1008 assert_eq!(
1009 EventType::Default,
1010 *Annotated::<EventType>::from_json("\"default\"")
1011 .unwrap()
1012 .value()
1013 .unwrap()
1014 );
1015 }
1016
1017 #[test]
1018 fn test_fingerprint_empty_string() {
1019 let json = r#"{"fingerprint":[""]}"#;
1020 let event = Annotated::new(Event {
1021 fingerprint: Annotated::new(vec!["".to_owned()].into()),
1022 ..Default::default()
1023 });
1024
1025 assert_eq!(json, event.to_json().unwrap());
1026 assert_eq!(event, Annotated::from_json(json).unwrap());
1027 }
1028
1029 #[test]
1030 fn test_fingerprint_null_values() {
1031 let input = r#"{"fingerprint":[null]}"#;
1032 let output = r#"{}"#;
1033 let event = Annotated::new(Event {
1034 fingerprint: Annotated::new(vec![].into()),
1035 ..Default::default()
1036 });
1037
1038 assert_eq!(event, Annotated::from_json(input).unwrap());
1039 assert_eq!(output, event.to_json().unwrap());
1040 }
1041
1042 #[test]
1043 fn test_empty_threads() {
1044 let input = r#"{"threads": {}}"#;
1045 let output = r#"{}"#;
1046
1047 let event = Annotated::new(Event::default());
1048
1049 assert_eq!(event, Annotated::from_json(input).unwrap());
1050 assert_eq!(output, event.to_json().unwrap());
1051 }
1052
1053 #[test]
1054 fn test_lenient_release() {
1055 let input = r#"{"release":42}"#;
1056 let output = r#"{"release":"42"}"#;
1057 let event = Annotated::new(Event {
1058 release: Annotated::new("42".to_owned().into()),
1059 ..Default::default()
1060 });
1061
1062 assert_eq!(event, Annotated::from_json(input).unwrap());
1063 assert_eq!(output, event.to_json().unwrap());
1064 }
1065
1066 #[test]
1067 fn test_extra_at() {
1068 let json = serde_json::json!({
1069 "extra": {
1070 "a": "string1",
1071 "b": 42,
1072 "c": {
1073 "d": "string2",
1074 "e": null,
1075 },
1076 },
1077 });
1078
1079 let event = Event::from_value(json.into());
1080 let event = event.value().unwrap();
1081
1082 assert_eq!(
1083 Some(&Value::String("string1".to_owned())),
1084 event.extra_at("a")
1085 );
1086 assert_eq!(Some(&Value::I64(42)), event.extra_at("b"));
1087 assert!(matches!(event.extra_at("c"), Some(&Value::Object(_))));
1088 assert_eq!(None, event.extra_at("d"));
1089 assert_eq!(
1090 Some(&Value::String("string2".to_owned())),
1091 event.extra_at("c.d")
1092 );
1093 assert_eq!(None, event.extra_at("c.e"));
1094 assert_eq!(None, event.extra_at("c.f"));
1095 }
1096
1097 #[test]
1098 fn test_scrape_attempts() {
1099 let json = serde_json::json!({
1100 "scraping_attempts": [
1101 {"status": "not_attempted", "url": "http://example.com/embedded.js"},
1102 {"status": "not_attempted", "url": "http://example.com/embedded.js.map"},
1103 ]
1104 });
1105
1106 let event = Event::from_value(json.into());
1107 assert!(!event.value().unwrap().scraping_attempts.meta().has_errors());
1108 }
1109
1110 #[test]
1111 fn test_field_value_provider_event_filled() {
1112 let event = Event {
1113 level: Annotated::new(Level::Info),
1114 release: Annotated::new(LenientString("1.1.1".to_owned())),
1115 environment: Annotated::new("prod".to_owned()),
1116 user: Annotated::new(User {
1117 ip_address: Annotated::new(IpAddr("127.0.0.1".to_owned())),
1118 id: Annotated::new(LenientString("user-id".into())),
1119 segment: Annotated::new("user-seg".into()),
1120 sentry_user: Annotated::new("id:user-id".into()),
1121 ..Default::default()
1122 }),
1123 client_sdk: Annotated::new(ClientSdkInfo {
1124 name: Annotated::new("sentry-javascript".into()),
1125 version: Annotated::new("1.87.0".into()),
1126 ..Default::default()
1127 }),
1128 exceptions: Annotated::new(Values {
1129 values: Annotated::new(vec![Annotated::new(Exception {
1130 value: Annotated::new(JsonLenientString::from(
1131 "canvas.contentDocument".to_owned(),
1132 )),
1133 ..Default::default()
1134 })]),
1135 ..Default::default()
1136 }),
1137 logentry: Annotated::new(LogEntry {
1138 formatted: Annotated::new("formatted".to_owned().into()),
1139 message: Annotated::new("message".to_owned().into()),
1140 ..Default::default()
1141 }),
1142 request: Annotated::new(Request {
1143 headers: Annotated::new(Headers(PairList(vec![Annotated::new((
1144 Annotated::new("user-agent".into()),
1145 Annotated::new("Slurp".into()),
1146 ))]))),
1147 url: Annotated::new("https://sentry.io".into()),
1148 ..Default::default()
1149 }),
1150 transaction: Annotated::new("some-transaction".into()),
1151 transaction_info: Annotated::new(TransactionInfo {
1152 source: Annotated::new(TransactionSource::Route),
1153 ..Default::default()
1154 }),
1155 tags: {
1156 let items = vec![Annotated::new(TagEntry(
1157 Annotated::new("custom".to_owned()),
1158 Annotated::new("custom-value".to_owned()),
1159 ))];
1160 Annotated::new(Tags(items.into()))
1161 },
1162 contexts: Annotated::new({
1163 let mut contexts = Contexts::new();
1164 contexts.add(DeviceContext {
1165 name: Annotated::new("iphone".to_owned()),
1166 family: Annotated::new("iphone-fam".to_owned()),
1167 model: Annotated::new("iphone7,3".to_owned()),
1168 screen_dpi: Annotated::new(560),
1169 screen_width_pixels: Annotated::new(1920),
1170 screen_height_pixels: Annotated::new(1080),
1171 locale: Annotated::new("US".into()),
1172 uuid: Annotated::new(uuid!("abadcade-feed-dead-beef-baddadfeeded")),
1173 charging: Annotated::new(true),
1174 ..DeviceContext::default()
1175 });
1176 contexts.add(OsContext {
1177 name: Annotated::new("iOS".to_owned()),
1178 version: Annotated::new("11.4.2".to_owned()),
1179 kernel_version: Annotated::new("17.4.0".to_owned()),
1180 ..OsContext::default()
1181 });
1182 contexts.add(ProfileContext {
1183 profile_id: Annotated::new(EventId(uuid!(
1184 "abadcade-feed-dead-beef-8addadfeedaa"
1185 ))),
1186 ..ProfileContext::default()
1187 });
1188 let mut monitor_context_fields = BTreeMap::new();
1189 monitor_context_fields.insert(
1190 "id".to_owned(),
1191 Annotated::new(Value::String("123".to_owned())),
1192 );
1193 monitor_context_fields.insert(
1194 "slug".to_owned(),
1195 Annotated::new(Value::String("my_monitor".to_owned())),
1196 );
1197 contexts.add(MonitorContext(monitor_context_fields));
1198 contexts
1199 }),
1200 ..Default::default()
1201 };
1202
1203 assert_eq!(Some(Val::String("info")), event.get_value("event.level"));
1204
1205 assert_eq!(Some(Val::String("1.1.1")), event.get_value("event.release"));
1206 assert_eq!(
1207 Some(Val::String("prod")),
1208 event.get_value("event.environment")
1209 );
1210 assert_eq!(
1211 Some(Val::String("user-id")),
1212 event.get_value("event.user.id")
1213 );
1214 assert_eq!(
1215 Some(Val::String("id:user-id")),
1216 event.get_value("event.sentry_user")
1217 );
1218 assert_eq!(
1219 Some(Val::String("user-seg")),
1220 event.get_value("event.user.segment")
1221 );
1222 assert_eq!(
1223 Some(Val::String("some-transaction")),
1224 event.get_value("event.transaction")
1225 );
1226 assert_eq!(
1227 Some(Val::String("iphone")),
1228 event.get_value("event.contexts.device.name")
1229 );
1230 assert_eq!(
1231 Some(Val::String("iphone-fam")),
1232 event.get_value("event.contexts.device.family")
1233 );
1234 assert_eq!(
1235 Some(Val::String("iOS")),
1236 event.get_value("event.contexts.os.name")
1237 );
1238 assert_eq!(
1239 Some(Val::String("11.4.2")),
1240 event.get_value("event.contexts.os.version")
1241 );
1242 assert_eq!(
1243 Some(Val::String("custom-value")),
1244 event.get_value("event.tags.custom")
1245 );
1246 assert_eq!(None, event.get_value("event.tags.doesntexist"));
1247 assert_eq!(
1248 Some(Val::String("sentry-javascript")),
1249 event.get_value("event.sdk.name")
1250 );
1251 assert_eq!(
1252 Some(Val::String("1.87.0")),
1253 event.get_value("event.sdk.version")
1254 );
1255 assert_eq!(
1256 Some(Val::String("17.4.0")),
1257 event.get_value("event.contexts.os.kernel_version")
1258 );
1259 assert_eq!(
1260 Some(Val::I64(560)),
1261 event.get_value("event.contexts.device.screen_dpi")
1262 );
1263 assert_eq!(
1264 Some(Val::Bool(true)),
1265 event.get_value("event.contexts.device.charging")
1266 );
1267 assert_eq!(
1268 Some(Val::U64(1920)),
1269 event.get_value("event.contexts.device.screen_width_pixels")
1270 );
1271 assert_eq!(
1272 Some(Val::U64(1080)),
1273 event.get_value("event.contexts.device.screen_height_pixels")
1274 );
1275 assert_eq!(
1276 Some(Val::String("US")),
1277 event.get_value("event.contexts.device.locale")
1278 );
1279 assert_eq!(
1280 Some(Val::HexId(HexId(
1281 uuid!("abadcade-feed-dead-beef-baddadfeeded").as_bytes()
1282 ))),
1283 event.get_value("event.contexts.device.uuid")
1284 );
1285 assert_eq!(
1286 Some(Val::String("https://sentry.io")),
1287 event.get_value("event.request.url")
1288 );
1289 assert_eq!(
1290 Some(Val::HexId(HexId(
1291 uuid!("abadcade-feed-dead-beef-8addadfeedaa").as_bytes()
1292 ))),
1293 event.get_value("event.contexts.profile.profile_id")
1294 );
1295 assert_eq!(
1296 Some(Val::String("route")),
1297 event.get_value("event.transaction.source")
1298 );
1299
1300 let mut exceptions = event.get_iter("event.exception.values").unwrap();
1301 let exception = exceptions.next().unwrap();
1302 assert_eq!(
1303 Some(Val::String("canvas.contentDocument")),
1304 exception.get_value("value")
1305 );
1306 assert!(exceptions.next().is_none());
1307
1308 assert_eq!(
1309 Some(Val::String("formatted")),
1310 event.get_value("event.logentry.formatted")
1311 );
1312 assert_eq!(
1313 Some(Val::String("message")),
1314 event.get_value("event.logentry.message")
1315 );
1316 assert_eq!(
1317 Some(Val::String("123")),
1318 event.get_value("event.contexts.monitor.id")
1319 );
1320 assert_eq!(
1321 Some(Val::String("my_monitor")),
1322 event.get_value("event.contexts.monitor.slug")
1323 );
1324 }
1325
1326 #[test]
1327 fn test_field_value_provider_event_empty() {
1328 let event = Event::default();
1329
1330 assert_eq!(None, event.get_value("event.release"));
1331 assert_eq!(None, event.get_value("event.environment"));
1332 assert_eq!(None, event.get_value("event.user.id"));
1333 assert_eq!(None, event.get_value("event.user.segment"));
1334
1335 let event = Event {
1337 user: Annotated::new(User {
1338 ..Default::default()
1339 }),
1340 ..Default::default()
1341 };
1342
1343 assert_eq!(None, event.get_value("event.user.id"));
1344 assert_eq!(None, event.get_value("event.user.segment"));
1345 assert_eq!(None, event.get_value("event.transaction"));
1346 }
1347}