1use std::borrow::Cow;
9use std::cmp;
10use std::convert::TryFrom;
11use std::fmt;
12use std::iter::FromIterator;
13use std::net::{AddrParseError, IpAddr};
14use std::ops;
15use std::str;
16use std::time::SystemTime;
17
18use self::debugid::{CodeId, DebugId};
19use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
20use thiserror::Error;
21
22pub use url::Url;
23pub use uuid::Uuid;
24
25use crate::utils::{ts_rfc3339_opt, ts_seconds_float};
26
27pub use super::attachment::*;
28pub use super::envelope::*;
29pub use super::monitor::*;
30pub use super::session::*;
31
32pub mod value {
34 pub use serde_json::value::{from_value, to_value, Index, Map, Number, Value};
35}
36
37pub mod map {
39 pub use std::collections::btree_map::{BTreeMap as Map, *};
40}
41
42pub mod debugid {
44 pub use debugid::{BreakpadFormat, CodeId, DebugId, ParseDebugIdError};
45}
46
47pub use self::value::Value;
49
50pub use self::map::Map;
52
53#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
60pub struct Values<T> {
61 pub values: Vec<T>,
63}
64
65impl<T> Values<T> {
66 pub fn new() -> Values<T> {
68 Values { values: Vec::new() }
69 }
70
71 pub fn is_empty(&self) -> bool {
73 self.values.is_empty()
74 }
75}
76
77impl<T> Default for Values<T> {
78 fn default() -> Self {
79 Values::new()
81 }
82}
83
84impl<T> From<Vec<T>> for Values<T> {
85 fn from(values: Vec<T>) -> Self {
86 Values { values }
87 }
88}
89
90impl<T> AsRef<[T]> for Values<T> {
91 fn as_ref(&self) -> &[T] {
92 &self.values
93 }
94}
95
96impl<T> AsMut<Vec<T>> for Values<T> {
97 fn as_mut(&mut self) -> &mut Vec<T> {
98 &mut self.values
99 }
100}
101
102impl<T> ops::Deref for Values<T> {
103 type Target = [T];
104
105 fn deref(&self) -> &Self::Target {
106 &self.values
107 }
108}
109
110impl<T> ops::DerefMut for Values<T> {
111 fn deref_mut(&mut self) -> &mut Self::Target {
112 &mut self.values
113 }
114}
115
116impl<T> FromIterator<T> for Values<T> {
117 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
118 Vec::<T>::from_iter(iter).into()
119 }
120}
121
122impl<T> Extend<T> for Values<T> {
123 fn extend<I>(&mut self, iter: I)
124 where
125 I: IntoIterator<Item = T>,
126 {
127 self.values.extend(iter)
128 }
129}
130
131impl<'a, T> IntoIterator for &'a mut Values<T> {
132 type Item = <&'a mut Vec<T> as IntoIterator>::Item;
133 type IntoIter = <&'a mut Vec<T> as IntoIterator>::IntoIter;
134
135 fn into_iter(self) -> Self::IntoIter {
136 self.values.iter_mut()
137 }
138}
139
140impl<'a, T> IntoIterator for &'a Values<T> {
141 type Item = <&'a Vec<T> as IntoIterator>::Item;
142 type IntoIter = <&'a Vec<T> as IntoIterator>::IntoIter;
143
144 fn into_iter(self) -> Self::IntoIter {
145 self.values.iter()
146 }
147}
148
149impl<T> IntoIterator for Values<T> {
150 type Item = <Vec<T> as IntoIterator>::Item;
151 type IntoIter = <Vec<T> as IntoIterator>::IntoIter;
152
153 fn into_iter(self) -> Self::IntoIter {
154 self.values.into_iter()
155 }
156}
157
158#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq)]
163pub struct LogEntry {
164 pub message: String,
166 #[serde(default, skip_serializing_if = "Vec::is_empty")]
168 pub params: Vec<Value>,
169}
170
171#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq)]
173pub struct Frame {
174 #[serde(default, skip_serializing_if = "Option::is_none")]
179 pub function: Option<String>,
180 #[serde(default, skip_serializing_if = "Option::is_none")]
186 pub symbol: Option<String>,
187 #[serde(default, skip_serializing_if = "Option::is_none")]
192 pub module: Option<String>,
193 #[serde(default, skip_serializing_if = "Option::is_none")]
198 pub package: Option<String>,
199 #[serde(default, skip_serializing_if = "Option::is_none")]
201 pub filename: Option<String>,
202 #[serde(default, skip_serializing_if = "Option::is_none")]
204 pub abs_path: Option<String>,
205 #[serde(default, skip_serializing_if = "Option::is_none")]
207 pub lineno: Option<u64>,
208 #[serde(default, skip_serializing_if = "Option::is_none")]
210 pub colno: Option<u64>,
211 #[serde(default, skip_serializing_if = "Vec::is_empty")]
213 pub pre_context: Vec<String>,
214 #[serde(default, skip_serializing_if = "Option::is_none")]
216 pub context_line: Option<String>,
217 #[serde(default, skip_serializing_if = "Vec::is_empty")]
219 pub post_context: Vec<String>,
220 #[serde(default, skip_serializing_if = "Option::is_none")]
222 pub in_app: Option<bool>,
223 #[serde(default, skip_serializing_if = "Map::is_empty")]
225 pub vars: Map<String, Value>,
226 #[serde(default, skip_serializing_if = "Option::is_none")]
228 pub image_addr: Option<Addr>,
229 #[serde(default, skip_serializing_if = "Option::is_none")]
231 pub instruction_addr: Option<Addr>,
232 #[serde(default, skip_serializing_if = "Option::is_none")]
234 pub symbol_addr: Option<Addr>,
235 #[serde(default, skip_serializing_if = "Option::is_none")]
241 pub addr_mode: Option<String>,
242}
243
244#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq)]
246pub struct TemplateInfo {
247 #[serde(default, skip_serializing_if = "Option::is_none")]
249 pub filename: Option<String>,
250 #[serde(default, skip_serializing_if = "Option::is_none")]
252 pub abs_path: Option<String>,
253 #[serde(default, skip_serializing_if = "Option::is_none")]
255 pub lineno: Option<u64>,
256 #[serde(default, skip_serializing_if = "Option::is_none")]
258 pub colno: Option<u64>,
259 #[serde(default, skip_serializing_if = "Vec::is_empty")]
261 pub pre_context: Vec<String>,
262 #[serde(default, skip_serializing_if = "Option::is_none")]
264 pub context_line: Option<String>,
265 #[serde(default, skip_serializing_if = "Vec::is_empty")]
267 pub post_context: Vec<String>,
268}
269
270#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq)]
272pub struct Stacktrace {
273 #[serde(default)]
275 pub frames: Vec<Frame>,
276 #[serde(default, skip_serializing_if = "Option::is_none")]
278 pub frames_omitted: Option<(u64, u64)>,
279 #[serde(default, skip_serializing_if = "Map::is_empty")]
281 pub registers: Map<String, RegVal>,
282}
283
284impl Stacktrace {
285 pub fn from_frames_reversed(mut frames: Vec<Frame>) -> Option<Stacktrace> {
287 if frames.is_empty() {
288 None
289 } else {
290 frames.reverse();
291 Some(Stacktrace {
292 frames,
293 ..Default::default()
294 })
295 }
296 }
297}
298
299#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
301#[serde(untagged)]
302pub enum ThreadId {
303 Int(u64),
305 String(String),
307}
308
309impl Default for ThreadId {
310 fn default() -> ThreadId {
311 ThreadId::Int(0)
312 }
313}
314
315impl<'a> From<&'a str> for ThreadId {
316 fn from(id: &'a str) -> ThreadId {
317 ThreadId::String(id.to_string())
318 }
319}
320
321impl From<String> for ThreadId {
322 fn from(id: String) -> ThreadId {
323 ThreadId::String(id)
324 }
325}
326
327impl From<i64> for ThreadId {
328 fn from(id: i64) -> ThreadId {
329 ThreadId::Int(id as u64)
330 }
331}
332
333impl From<i32> for ThreadId {
334 fn from(id: i32) -> ThreadId {
335 ThreadId::Int(id as u64)
336 }
337}
338
339impl From<u32> for ThreadId {
340 fn from(id: u32) -> ThreadId {
341 ThreadId::Int(id as u64)
342 }
343}
344
345impl From<u16> for ThreadId {
346 fn from(id: u16) -> ThreadId {
347 ThreadId::Int(id as u64)
348 }
349}
350
351impl fmt::Display for ThreadId {
352 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
353 match *self {
354 ThreadId::Int(i) => write!(f, "{i}"),
355 ThreadId::String(ref s) => write!(f, "{s}"),
356 }
357 }
358}
359
360#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash)]
362pub struct Addr(pub u64);
363
364impl Addr {
365 pub fn is_null(&self) -> bool {
367 self.0 == 0
368 }
369}
370
371impl_hex_serde!(Addr, u64);
372
373impl From<u64> for Addr {
374 fn from(addr: u64) -> Addr {
375 Addr(addr)
376 }
377}
378
379impl From<i32> for Addr {
380 fn from(addr: i32) -> Addr {
381 Addr(addr as u64)
382 }
383}
384
385impl From<u32> for Addr {
386 fn from(addr: u32) -> Addr {
387 Addr(addr as u64)
388 }
389}
390
391impl From<usize> for Addr {
392 fn from(addr: usize) -> Addr {
393 Addr(addr as u64)
394 }
395}
396
397impl<T> From<*const T> for Addr {
398 fn from(addr: *const T) -> Addr {
399 Addr(addr as u64)
400 }
401}
402
403impl<T> From<*mut T> for Addr {
404 fn from(addr: *mut T) -> Addr {
405 Addr(addr as u64)
406 }
407}
408
409impl From<Addr> for u64 {
410 fn from(addr: Addr) -> Self {
411 addr.0
412 }
413}
414
415fn is_false(value: &bool) -> bool {
416 !*value
417}
418
419#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash)]
421pub struct RegVal(pub u64);
422
423impl_hex_serde!(RegVal, u64);
424
425impl From<u64> for RegVal {
426 fn from(addr: u64) -> RegVal {
427 RegVal(addr)
428 }
429}
430
431impl From<i32> for RegVal {
432 fn from(addr: i32) -> RegVal {
433 RegVal(addr as u64)
434 }
435}
436
437impl From<u32> for RegVal {
438 fn from(addr: u32) -> RegVal {
439 RegVal(addr as u64)
440 }
441}
442
443impl From<usize> for RegVal {
444 fn from(addr: usize) -> RegVal {
445 RegVal(addr as u64)
446 }
447}
448
449impl<T> From<*const T> for RegVal {
450 fn from(addr: *const T) -> RegVal {
451 RegVal(addr as u64)
452 }
453}
454
455impl<T> From<*mut T> for RegVal {
456 fn from(addr: *mut T) -> RegVal {
457 RegVal(addr as u64)
458 }
459}
460
461impl From<RegVal> for u64 {
462 fn from(reg: RegVal) -> Self {
463 reg.0
464 }
465}
466
467#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq)]
469pub struct Thread {
470 #[serde(default, skip_serializing_if = "Option::is_none")]
472 pub id: Option<ThreadId>,
473 #[serde(default, skip_serializing_if = "Option::is_none")]
475 pub name: Option<String>,
476 #[serde(default, skip_serializing_if = "Option::is_none")]
479 pub stacktrace: Option<Stacktrace>,
480 #[serde(default, skip_serializing_if = "Option::is_none")]
482 pub raw_stacktrace: Option<Stacktrace>,
483 #[serde(default, skip_serializing_if = "is_false")]
485 pub crashed: bool,
486 #[serde(default, skip_serializing_if = "is_false")]
489 pub current: bool,
490}
491
492#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, Eq)]
494pub struct CError {
495 pub number: i32,
497 #[serde(default, skip_serializing_if = "Option::is_none")]
499 pub name: Option<String>,
500}
501
502impl From<i32> for CError {
503 fn from(number: i32) -> CError {
504 CError { number, name: None }
505 }
506}
507
508impl From<CError> for i32 {
509 fn from(err: CError) -> Self {
510 err.number
511 }
512}
513
514#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, Eq)]
516pub struct MachException {
517 pub exception: i32,
519 pub code: u64,
521 pub subcode: u64,
523 #[serde(default, skip_serializing_if = "Option::is_none")]
525 pub name: Option<String>,
526}
527
528#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, Eq)]
530pub struct PosixSignal {
531 pub number: i32,
533 #[serde(default, skip_serializing_if = "Option::is_none")]
535 pub code: Option<i32>,
536 #[serde(default, skip_serializing_if = "Option::is_none")]
538 pub name: Option<String>,
539 #[serde(default, skip_serializing_if = "Option::is_none")]
541 pub code_name: Option<String>,
542}
543
544impl From<i32> for PosixSignal {
545 fn from(number: i32) -> PosixSignal {
546 PosixSignal {
547 number,
548 code: None,
549 name: None,
550 code_name: None,
551 }
552 }
553}
554
555impl From<(i32, i32)> for PosixSignal {
556 fn from(tuple: (i32, i32)) -> PosixSignal {
557 let (number, code) = tuple;
558 PosixSignal {
559 number,
560 code: Some(code),
561 name: None,
562 code_name: None,
563 }
564 }
565}
566
567impl From<PosixSignal> for i32 {
568 fn from(sig: PosixSignal) -> Self {
569 sig.number
570 }
571}
572
573#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq)]
575pub struct MechanismMeta {
576 #[serde(default, skip_serializing_if = "Option::is_none")]
578 pub errno: Option<CError>,
579 #[serde(default, skip_serializing_if = "Option::is_none")]
581 pub signal: Option<PosixSignal>,
582 #[serde(default, skip_serializing_if = "Option::is_none")]
584 pub mach_exception: Option<MachException>,
585}
586
587impl MechanismMeta {
588 fn is_empty(&self) -> bool {
589 self.errno.is_none() && self.signal.is_none() && self.mach_exception.is_none()
590 }
591}
592
593#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq)]
595pub struct Mechanism {
596 #[serde(rename = "type")]
598 pub ty: String,
599 #[serde(default, skip_serializing_if = "Option::is_none")]
601 pub description: Option<String>,
602 #[serde(default, skip_serializing_if = "Option::is_none")]
604 pub help_link: Option<Url>,
605 #[serde(default, skip_serializing_if = "Option::is_none")]
607 pub handled: Option<bool>,
608 #[serde(default, skip_serializing_if = "Option::is_none")]
610 pub synthetic: Option<bool>,
611 #[serde(default, skip_serializing_if = "Map::is_empty")]
613 pub data: Map<String, Value>,
614 #[serde(default, skip_serializing_if = "MechanismMeta::is_empty")]
616 pub meta: MechanismMeta,
617}
618
619#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq)]
621pub struct Exception {
622 #[serde(rename = "type")]
624 pub ty: String,
625 #[serde(skip_serializing_if = "Option::is_none")]
627 pub value: Option<String>,
628 #[serde(default, skip_serializing_if = "Option::is_none")]
630 pub module: Option<String>,
631 #[serde(default, skip_serializing_if = "Option::is_none")]
633 pub stacktrace: Option<Stacktrace>,
634 #[serde(default, skip_serializing_if = "Option::is_none")]
636 pub raw_stacktrace: Option<Stacktrace>,
637 #[serde(default, skip_serializing_if = "Option::is_none")]
639 pub thread_id: Option<ThreadId>,
640 #[serde(default, skip_serializing_if = "Option::is_none")]
642 pub mechanism: Option<Mechanism>,
643}
644
645#[derive(Debug, Error)]
647#[error("invalid level")]
648pub struct ParseLevelError;
649
650#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
652pub enum Level {
653 Debug,
655 #[default]
657 Info,
658 Warning,
660 Error,
662 Fatal,
664}
665
666impl str::FromStr for Level {
667 type Err = ParseLevelError;
668
669 fn from_str(string: &str) -> Result<Level, Self::Err> {
670 Ok(match string {
671 "debug" => Level::Debug,
672 "info" | "log" => Level::Info,
673 "warning" => Level::Warning,
674 "error" => Level::Error,
675 "fatal" => Level::Fatal,
676 _ => return Err(ParseLevelError),
677 })
678 }
679}
680
681impl fmt::Display for Level {
682 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
683 match *self {
684 Level::Debug => write!(f, "debug"),
685 Level::Info => write!(f, "info"),
686 Level::Warning => write!(f, "warning"),
687 Level::Error => write!(f, "error"),
688 Level::Fatal => write!(f, "fatal"),
689 }
690 }
691}
692
693impl Level {
694 pub fn is_debug(&self) -> bool {
696 *self == Level::Debug
697 }
698
699 pub fn is_info(&self) -> bool {
701 *self == Level::Info
702 }
703
704 pub fn is_warning(&self) -> bool {
706 *self == Level::Warning
707 }
708
709 pub fn is_error(&self) -> bool {
711 *self == Level::Error
712 }
713
714 pub fn is_fatal(&self) -> bool {
716 *self == Level::Fatal
717 }
718}
719
720impl_str_serde!(Level);
721
722mod breadcrumb {
723 use super::*;
724
725 pub fn default_type() -> String {
726 "default".to_string()
727 }
728
729 pub fn is_default_type(ty: &str) -> bool {
730 ty == "default"
731 }
732
733 pub fn default_level() -> Level {
734 Level::Info
735 }
736}
737
738#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
740pub struct Breadcrumb {
741 #[serde(default = "SystemTime::now", with = "ts_seconds_float")]
743 pub timestamp: SystemTime,
744 #[serde(
746 rename = "type",
747 default = "breadcrumb::default_type",
748 skip_serializing_if = "breadcrumb::is_default_type"
749 )]
750 pub ty: String,
751 #[serde(default, skip_serializing_if = "Option::is_none")]
753 pub category: Option<String>,
754 #[serde(
757 default = "breadcrumb::default_level",
758 skip_serializing_if = "Level::is_info"
759 )]
760 pub level: Level,
761 #[serde(default, skip_serializing_if = "Option::is_none")]
763 pub message: Option<String>,
764 #[serde(default, skip_serializing_if = "Map::is_empty")]
766 pub data: Map<String, Value>,
767}
768
769impl Default for Breadcrumb {
770 fn default() -> Breadcrumb {
771 Breadcrumb {
772 timestamp: SystemTime::now(),
773 ty: breadcrumb::default_type(),
774 category: Default::default(),
775 level: breadcrumb::default_level(),
776 message: Default::default(),
777 data: Default::default(),
778 }
779 }
780}
781
782#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
784pub enum IpAddress {
785 #[default]
787 Auto,
788 Exact(IpAddr),
790}
791
792impl PartialEq<IpAddr> for IpAddress {
793 fn eq(&self, other: &IpAddr) -> bool {
794 match *self {
795 IpAddress::Auto => false,
796 IpAddress::Exact(ref addr) => addr == other,
797 }
798 }
799}
800
801impl cmp::PartialOrd<IpAddr> for IpAddress {
802 fn partial_cmp(&self, other: &IpAddr) -> Option<cmp::Ordering> {
803 match *self {
804 IpAddress::Auto => None,
805 IpAddress::Exact(ref addr) => addr.partial_cmp(other),
806 }
807 }
808}
809
810impl fmt::Display for IpAddress {
811 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
812 match *self {
813 IpAddress::Auto => write!(f, "{{{{auto}}}}"),
814 IpAddress::Exact(ref addr) => write!(f, "{addr}"),
815 }
816 }
817}
818
819impl From<IpAddr> for IpAddress {
820 fn from(addr: IpAddr) -> IpAddress {
821 IpAddress::Exact(addr)
822 }
823}
824
825impl str::FromStr for IpAddress {
826 type Err = AddrParseError;
827
828 fn from_str(string: &str) -> Result<IpAddress, AddrParseError> {
829 match string {
830 "{{auto}}" => Ok(IpAddress::Auto),
831 other => other.parse().map(IpAddress::Exact),
832 }
833 }
834}
835
836impl_str_serde!(IpAddress);
837
838#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq)]
840pub struct User {
841 #[serde(default, skip_serializing_if = "Option::is_none")]
843 pub id: Option<String>,
844 #[serde(default, skip_serializing_if = "Option::is_none")]
846 pub email: Option<String>,
847 #[serde(default, skip_serializing_if = "Option::is_none")]
849 pub ip_address: Option<IpAddress>,
850 #[serde(default, skip_serializing_if = "Option::is_none")]
852 pub username: Option<String>,
853 #[serde(flatten)]
855 pub other: Map<String, Value>,
856}
857
858#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq)]
860pub struct Request {
861 #[serde(default, skip_serializing_if = "Option::is_none")]
863 pub url: Option<Url>,
864 #[serde(default, skip_serializing_if = "Option::is_none")]
866 pub method: Option<String>,
867 #[serde(default, skip_serializing_if = "Option::is_none")]
870 pub data: Option<String>,
871 #[serde(default, skip_serializing_if = "Option::is_none")]
873 pub query_string: Option<String>,
874 #[serde(default, skip_serializing_if = "Option::is_none")]
876 pub cookies: Option<String>,
877 #[serde(default, skip_serializing_if = "Map::is_empty")]
879 pub headers: Map<String, String>,
880 #[serde(default, skip_serializing_if = "Map::is_empty")]
882 pub env: Map<String, String>,
883}
884
885#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
890pub struct SystemSdkInfo {
891 pub sdk_name: String,
893 pub version_major: u32,
895 pub version_minor: u32,
897 pub version_patchlevel: u32,
899}
900
901#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
903#[serde(rename_all = "snake_case", tag = "type")]
904pub enum DebugImage {
905 Apple(AppleDebugImage),
908 Symbolic(SymbolicDebugImage),
910 Proguard(ProguardDebugImage),
912 Wasm(WasmDebugImage),
915}
916
917impl DebugImage {
918 pub fn type_name(&self) -> &str {
920 match *self {
921 DebugImage::Apple(..) => "apple",
922 DebugImage::Symbolic(..) => "symbolic",
923 DebugImage::Proguard(..) => "proguard",
924 DebugImage::Wasm(..) => "wasm",
925 }
926 }
927}
928
929macro_rules! into_debug_image {
930 ($kind:ident, $ty:ty) => {
931 impl From<$ty> for DebugImage {
932 fn from(data: $ty) -> DebugImage {
933 DebugImage::$kind(data)
934 }
935 }
936 };
937}
938
939#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
941pub struct AppleDebugImage {
942 pub name: String,
944 pub arch: Option<String>,
946 pub cpu_type: Option<u32>,
948 pub cpu_subtype: Option<u32>,
950 pub image_addr: Addr,
952 pub image_size: u64,
954 #[serde(default, skip_serializing_if = "Addr::is_null")]
956 pub image_vmaddr: Addr,
957 pub uuid: Uuid,
959}
960
961#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
963pub struct SymbolicDebugImage {
964 pub name: String,
969 pub arch: Option<String>,
971 pub image_addr: Addr,
975 pub image_size: u64,
979 #[serde(default, skip_serializing_if = "Addr::is_null")]
987 pub image_vmaddr: Addr,
988 pub id: DebugId,
992
993 #[serde(default, skip_serializing_if = "Option::is_none")]
995 pub code_id: Option<CodeId>,
996 #[serde(default, skip_serializing_if = "Option::is_none")]
998 pub debug_file: Option<String>,
999}
1000
1001#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
1003pub struct ProguardDebugImage {
1004 pub uuid: Uuid,
1006}
1007
1008#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
1010pub struct WasmDebugImage {
1011 pub name: String,
1013 pub debug_id: Uuid,
1015 #[serde(default, skip_serializing_if = "Option::is_none")]
1020 pub debug_file: Option<String>,
1021 #[serde(default, skip_serializing_if = "Option::is_none")]
1024 pub code_id: Option<String>,
1025 pub code_file: String,
1028}
1029
1030into_debug_image!(Apple, AppleDebugImage);
1031into_debug_image!(Symbolic, SymbolicDebugImage);
1032into_debug_image!(Proguard, ProguardDebugImage);
1033into_debug_image!(Wasm, WasmDebugImage);
1034
1035#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq)]
1037pub struct DebugMeta {
1038 #[serde(default, skip_serializing_if = "Option::is_none")]
1040 pub sdk_info: Option<SystemSdkInfo>,
1041 #[serde(default, skip_serializing_if = "Vec::is_empty")]
1043 pub images: Vec<DebugImage>,
1044}
1045
1046impl DebugMeta {
1047 pub fn is_empty(&self) -> bool {
1051 self.sdk_info.is_none() && self.images.is_empty()
1052 }
1053}
1054
1055#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
1057pub struct ClientSdkInfo {
1058 pub name: String,
1060 pub version: String,
1062 #[serde(default, skip_serializing_if = "Vec::is_empty")]
1064 pub integrations: Vec<String>,
1065 #[serde(default, skip_serializing_if = "Vec::is_empty")]
1067 pub packages: Vec<ClientSdkPackage>,
1068}
1069
1070#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
1072pub struct ClientSdkPackage {
1073 pub name: String,
1075 pub version: String,
1077}
1078
1079#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
1084#[serde(rename_all = "snake_case", tag = "type")]
1085#[non_exhaustive]
1086pub enum Context {
1087 Device(Box<DeviceContext>),
1089 Os(Box<OsContext>),
1091 Runtime(Box<RuntimeContext>),
1093 App(Box<AppContext>),
1095 Browser(Box<BrowserContext>),
1097 Trace(Box<TraceContext>),
1099 Gpu(Box<GpuContext>),
1101 #[serde(rename = "unknown")]
1103 Other(Map<String, Value>),
1104}
1105
1106impl Context {
1107 pub fn type_name(&self) -> &str {
1109 match *self {
1110 Context::Device(..) => "device",
1111 Context::Os(..) => "os",
1112 Context::Runtime(..) => "runtime",
1113 Context::App(..) => "app",
1114 Context::Browser(..) => "browser",
1115 Context::Trace(..) => "trace",
1116 Context::Gpu(..) => "gpu",
1117 Context::Other(..) => "unknown",
1118 }
1119 }
1120}
1121
1122#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
1124#[serde(rename_all = "lowercase")]
1125pub enum Orientation {
1126 Portrait,
1128 Landscape,
1130}
1131
1132#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
1134pub struct DeviceContext {
1135 #[serde(default, skip_serializing_if = "Option::is_none")]
1137 pub name: Option<String>,
1138 #[serde(default, skip_serializing_if = "Option::is_none")]
1140 pub family: Option<String>,
1141 #[serde(default, skip_serializing_if = "Option::is_none")]
1143 pub model: Option<String>,
1144 #[serde(default, skip_serializing_if = "Option::is_none")]
1146 pub model_id: Option<String>,
1147 #[serde(default, skip_serializing_if = "Option::is_none")]
1149 pub arch: Option<String>,
1150 #[serde(default, skip_serializing_if = "Option::is_none")]
1152 pub battery_level: Option<f32>,
1153 #[serde(default, skip_serializing_if = "Option::is_none")]
1155 pub orientation: Option<Orientation>,
1156 #[serde(default, skip_serializing_if = "Option::is_none")]
1158 pub simulator: Option<bool>,
1159 #[serde(default, skip_serializing_if = "Option::is_none")]
1161 pub memory_size: Option<u64>,
1162 #[serde(default, skip_serializing_if = "Option::is_none")]
1164 pub free_memory: Option<u64>,
1165 #[serde(default, skip_serializing_if = "Option::is_none")]
1167 pub usable_memory: Option<u64>,
1168 #[serde(default, skip_serializing_if = "Option::is_none")]
1170 pub storage_size: Option<u64>,
1171 #[serde(default, skip_serializing_if = "Option::is_none")]
1173 pub free_storage: Option<u64>,
1174 #[serde(default, skip_serializing_if = "Option::is_none")]
1176 pub external_storage_size: Option<u64>,
1177 #[serde(default, skip_serializing_if = "Option::is_none")]
1179 pub external_free_storage: Option<u64>,
1180 #[serde(
1182 default,
1183 skip_serializing_if = "Option::is_none",
1184 with = "ts_rfc3339_opt"
1185 )]
1186 pub boot_time: Option<SystemTime>,
1187 #[serde(default, skip_serializing_if = "Option::is_none")]
1189 pub timezone: Option<String>,
1190 #[serde(flatten)]
1192 pub other: Map<String, Value>,
1193}
1194
1195#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
1197pub struct OsContext {
1198 #[serde(default, skip_serializing_if = "Option::is_none")]
1200 pub name: Option<String>,
1201 #[serde(default, skip_serializing_if = "Option::is_none")]
1203 pub version: Option<String>,
1204 #[serde(default, skip_serializing_if = "Option::is_none")]
1206 pub build: Option<String>,
1207 #[serde(default, skip_serializing_if = "Option::is_none")]
1209 pub kernel_version: Option<String>,
1210 #[serde(default, skip_serializing_if = "Option::is_none")]
1212 pub rooted: Option<bool>,
1213 #[serde(flatten)]
1215 pub other: Map<String, Value>,
1216}
1217
1218#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
1220pub struct RuntimeContext {
1221 #[serde(default, skip_serializing_if = "Option::is_none")]
1223 pub name: Option<String>,
1224 #[serde(default, skip_serializing_if = "Option::is_none")]
1226 pub version: Option<String>,
1227 #[serde(flatten)]
1229 pub other: Map<String, Value>,
1230}
1231
1232#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
1234pub struct AppContext {
1235 #[serde(
1237 default,
1238 skip_serializing_if = "Option::is_none",
1239 with = "ts_rfc3339_opt"
1240 )]
1241 pub app_start_time: Option<SystemTime>,
1242 #[serde(default, skip_serializing_if = "Option::is_none")]
1244 pub device_app_hash: Option<String>,
1245 #[serde(default, skip_serializing_if = "Option::is_none")]
1247 pub build_type: Option<String>,
1248 #[serde(default, skip_serializing_if = "Option::is_none")]
1250 pub app_identifier: Option<String>,
1251 #[serde(default, skip_serializing_if = "Option::is_none")]
1253 pub app_name: Option<String>,
1254 #[serde(default, skip_serializing_if = "Option::is_none")]
1256 pub app_version: Option<String>,
1257 #[serde(default, skip_serializing_if = "Option::is_none")]
1259 pub app_build: Option<String>,
1260 #[serde(flatten)]
1262 pub other: Map<String, Value>,
1263}
1264
1265#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
1267pub struct BrowserContext {
1268 #[serde(default, skip_serializing_if = "Option::is_none")]
1270 pub name: Option<String>,
1271 #[serde(default, skip_serializing_if = "Option::is_none")]
1273 pub version: Option<String>,
1274 #[serde(flatten)]
1276 pub other: Map<String, Value>,
1277}
1278
1279#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
1281pub struct GpuContext {
1282 pub name: String,
1284 #[serde(default, skip_serializing_if = "Option::is_none")]
1286 pub version: Option<String>,
1287 #[serde(default, skip_serializing_if = "Option::is_none")]
1289 pub driver_version: Option<String>,
1290 #[serde(default, skip_serializing_if = "Option::is_none")]
1292 pub id: Option<String>,
1293 #[serde(default, skip_serializing_if = "Option::is_none")]
1295 pub vendor_id: Option<String>,
1296 #[serde(default, skip_serializing_if = "Option::is_none")]
1298 pub vendor_name: Option<String>,
1299 #[serde(default, skip_serializing_if = "Option::is_none")]
1301 pub memory_size: Option<u32>,
1302 #[serde(default, skip_serializing_if = "Option::is_none")]
1304 pub api_type: Option<String>,
1305 #[serde(default, skip_serializing_if = "Option::is_none")]
1307 pub multi_threaded_rendering: Option<bool>,
1308 #[serde(default, skip_serializing_if = "Option::is_none")]
1310 pub npot_support: Option<bool>,
1311 #[serde(default, skip_serializing_if = "Option::is_none")]
1313 pub max_texture_size: Option<u32>,
1314 #[serde(default, skip_serializing_if = "Option::is_none")]
1317 pub graphics_shader_level: Option<String>,
1318 #[serde(default, skip_serializing_if = "Option::is_none")]
1320 pub supports_draw_call_instancing: Option<bool>,
1321 #[serde(default, skip_serializing_if = "Option::is_none")]
1323 pub supports_ray_tracing: Option<bool>,
1324 #[serde(default, skip_serializing_if = "Option::is_none")]
1326 pub supports_compute_shaders: Option<bool>,
1327 #[serde(default, skip_serializing_if = "Option::is_none")]
1329 pub supports_geometry_shaders: Option<bool>,
1330 #[serde(flatten)]
1332 pub other: Map<String, Value>,
1333}
1334
1335#[derive(Serialize, Deserialize, Debug, Copy, Clone, Eq, PartialEq, Hash)]
1337#[serde(try_from = "String", into = "String")]
1338pub struct SpanId([u8; 8]);
1339
1340impl Default for SpanId {
1341 fn default() -> Self {
1342 Self(rand::random())
1343 }
1344}
1345
1346impl fmt::Display for SpanId {
1347 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1348 write!(fmt, "{}", hex::encode(self.0))
1349 }
1350}
1351
1352impl From<SpanId> for String {
1353 fn from(span_id: SpanId) -> Self {
1354 span_id.to_string()
1355 }
1356}
1357
1358impl str::FromStr for SpanId {
1359 type Err = hex::FromHexError;
1360
1361 fn from_str(input: &str) -> Result<Self, Self::Err> {
1362 let mut buf = [0; 8];
1363 hex::decode_to_slice(input, &mut buf)?;
1364 Ok(Self(buf))
1365 }
1366}
1367
1368impl TryFrom<String> for SpanId {
1369 type Error = hex::FromHexError;
1370
1371 fn try_from(value: String) -> Result<Self, Self::Error> {
1372 value.parse()
1373 }
1374}
1375
1376#[derive(Serialize, Deserialize, Debug, Copy, Clone, Eq, PartialEq, Hash)]
1378#[serde(try_from = "String", into = "String")]
1379pub struct TraceId([u8; 16]);
1380
1381impl Default for TraceId {
1382 fn default() -> Self {
1383 Self(rand::random())
1384 }
1385}
1386
1387impl fmt::Display for TraceId {
1388 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1389 write!(fmt, "{}", hex::encode(self.0))
1390 }
1391}
1392
1393impl From<TraceId> for String {
1394 fn from(trace_id: TraceId) -> Self {
1395 trace_id.to_string()
1396 }
1397}
1398
1399impl str::FromStr for TraceId {
1400 type Err = hex::FromHexError;
1401
1402 fn from_str(input: &str) -> Result<Self, Self::Err> {
1403 let mut buf = [0; 16];
1404 hex::decode_to_slice(input, &mut buf)?;
1405 Ok(Self(buf))
1406 }
1407}
1408
1409impl TryFrom<String> for TraceId {
1410 type Error = hex::FromHexError;
1411
1412 fn try_from(value: String) -> Result<Self, Self::Error> {
1413 value.parse()
1414 }
1415}
1416
1417#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
1419pub struct TraceContext {
1420 #[serde(default)]
1422 pub span_id: SpanId,
1423 #[serde(default)]
1425 pub trace_id: TraceId,
1426 #[serde(default, skip_serializing_if = "Option::is_none")]
1428 pub parent_span_id: Option<SpanId>,
1429 #[serde(default, skip_serializing_if = "Option::is_none")]
1431 pub op: Option<String>,
1432 #[serde(default, skip_serializing_if = "Option::is_none")]
1434 pub description: Option<String>,
1435 #[serde(default, skip_serializing_if = "Option::is_none")]
1437 pub status: Option<SpanStatus>,
1438}
1439
1440macro_rules! into_context {
1441 ($kind:ident, $ty:ty) => {
1442 impl From<$ty> for Context {
1443 fn from(data: $ty) -> Self {
1444 Context::$kind(Box::new(data))
1445 }
1446 }
1447 };
1448}
1449
1450into_context!(App, AppContext);
1451into_context!(Device, DeviceContext);
1452into_context!(Os, OsContext);
1453into_context!(Runtime, RuntimeContext);
1454into_context!(Browser, BrowserContext);
1455into_context!(Trace, TraceContext);
1456into_context!(Gpu, GpuContext);
1457
1458const INFERABLE_CONTEXTS: &[&str] = &["device", "os", "runtime", "app", "browser", "trace", "gpu"];
1459
1460struct ContextsVisitor;
1461
1462impl<'de> de::Visitor<'de> for ContextsVisitor {
1463 type Value = Map<String, Context>;
1464
1465 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1466 formatter.write_str("contexts object")
1467 }
1468
1469 fn visit_map<A>(self, mut access: A) -> Result<Self::Value, A::Error>
1470 where
1471 A: de::MapAccess<'de>,
1472 {
1473 let mut map: Map<String, Context> = Map::new();
1474
1475 while let Some((key, mut value)) = access.next_entry::<String, Value>()? {
1476 let typed_value = value
1477 .as_object_mut()
1478 .map(|ctx| {
1479 if !ctx.contains_key("type") {
1480 let type_key = if INFERABLE_CONTEXTS.contains(&key.as_str()) {
1481 key.clone().into()
1482 } else {
1483 Value::String("unknown".into())
1484 };
1485 ctx.insert(String::from("type"), type_key);
1486 }
1487 ctx.to_owned()
1488 })
1489 .ok_or_else(|| de::Error::custom("expected valid `context` object"))?;
1490
1491 match serde_json::from_value(serde_json::to_value(typed_value).unwrap()) {
1492 Ok(context) => {
1493 map.insert(key, context);
1494 }
1495 Err(e) => return Err(de::Error::custom(e.to_string())),
1496 }
1497 }
1498
1499 Ok(map)
1500 }
1501}
1502
1503fn deserialize_contexts<'de, D>(deserializer: D) -> Result<Map<String, Context>, D::Error>
1504where
1505 D: Deserializer<'de>,
1506{
1507 deserializer.deserialize_map(ContextsVisitor {})
1508}
1509
1510mod event {
1511 use super::*;
1512
1513 pub fn default_id() -> Uuid {
1514 crate::random_uuid()
1515 }
1516
1517 pub fn serialize_id<S: Serializer>(uuid: &Uuid, serializer: S) -> Result<S::Ok, S::Error> {
1518 serializer.serialize_some(&uuid.as_simple().to_string())
1519 }
1520
1521 pub fn default_level() -> Level {
1522 Level::Error
1523 }
1524
1525 pub fn default_platform() -> Cow<'static, str> {
1526 Cow::Borrowed("other")
1527 }
1528
1529 pub fn is_default_platform(value: &str) -> bool {
1530 value == "other"
1531 }
1532
1533 static DEFAULT_FINGERPRINT: &[Cow<'static, str>] = &[Cow::Borrowed("{{ default }}")];
1534
1535 pub fn default_fingerprint<'a>() -> Cow<'a, [Cow<'a, str>]> {
1536 Cow::Borrowed(DEFAULT_FINGERPRINT)
1537 }
1538
1539 pub fn is_default_fingerprint(fp: &[Cow<'_, str>]) -> bool {
1540 fp.len() == 1 && ((fp)[0] == "{{ default }}" || (fp)[0] == "{{default}}")
1541 }
1542}
1543
1544#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
1546pub struct Event<'a> {
1547 #[serde(default = "event::default_id", serialize_with = "event::serialize_id")]
1549 pub event_id: Uuid,
1550 #[serde(
1552 default = "event::default_level",
1553 skip_serializing_if = "Level::is_error"
1554 )]
1555 pub level: Level,
1556 #[serde(
1558 default = "event::default_fingerprint",
1559 skip_serializing_if = "event::is_default_fingerprint"
1560 )]
1561 pub fingerprint: Cow<'a, [Cow<'a, str>]>,
1562 #[serde(default, skip_serializing_if = "Option::is_none")]
1564 pub culprit: Option<String>,
1565 #[serde(default, skip_serializing_if = "Option::is_none")]
1567 pub transaction: Option<String>,
1568 #[serde(default, skip_serializing_if = "Option::is_none")]
1570 pub message: Option<String>,
1571 #[serde(default, skip_serializing_if = "Option::is_none")]
1574 pub logentry: Option<LogEntry>,
1575 #[serde(default, skip_serializing_if = "Option::is_none")]
1577 pub logger: Option<String>,
1578 #[serde(default, skip_serializing_if = "Map::is_empty")]
1580 pub modules: Map<String, String>,
1581 #[serde(
1583 default = "event::default_platform",
1584 skip_serializing_if = "event::is_default_platform"
1585 )]
1586 pub platform: Cow<'a, str>,
1587 #[serde(default = "SystemTime::now", with = "ts_seconds_float")]
1591 pub timestamp: SystemTime,
1592 #[serde(default, skip_serializing_if = "Option::is_none")]
1594 pub server_name: Option<Cow<'a, str>>,
1595 #[serde(default, skip_serializing_if = "Option::is_none")]
1597 pub release: Option<Cow<'a, str>>,
1598 #[serde(default, skip_serializing_if = "Option::is_none")]
1600 pub dist: Option<Cow<'a, str>>,
1601 #[serde(default, skip_serializing_if = "Option::is_none")]
1603 pub environment: Option<Cow<'a, str>>,
1604 #[serde(default, skip_serializing_if = "Option::is_none")]
1606 pub user: Option<User>,
1607 #[serde(default, skip_serializing_if = "Option::is_none")]
1609 pub request: Option<Request>,
1610 #[serde(
1612 default,
1613 skip_serializing_if = "Map::is_empty",
1614 deserialize_with = "deserialize_contexts"
1615 )]
1616 pub contexts: Map<String, Context>,
1617 #[serde(default, skip_serializing_if = "Values::is_empty")]
1619 pub breadcrumbs: Values<Breadcrumb>,
1620 #[serde(default, skip_serializing_if = "Values::is_empty")]
1622 pub exception: Values<Exception>,
1623 #[serde(default, skip_serializing_if = "Option::is_none")]
1625 pub stacktrace: Option<Stacktrace>,
1626 #[serde(default, skip_serializing_if = "Option::is_none")]
1628 pub template: Option<TemplateInfo>,
1629 #[serde(default, skip_serializing_if = "Values::is_empty")]
1631 pub threads: Values<Thread>,
1632 #[serde(default, skip_serializing_if = "Map::is_empty")]
1634 pub tags: Map<String, String>,
1635 #[serde(default, skip_serializing_if = "Map::is_empty")]
1637 pub extra: Map<String, Value>,
1638 #[serde(default, skip_serializing_if = "DebugMeta::is_empty")]
1640 pub debug_meta: Cow<'a, DebugMeta>,
1641 #[serde(default, skip_serializing_if = "Option::is_none")]
1643 pub sdk: Option<Cow<'a, ClientSdkInfo>>,
1644}
1645
1646impl Default for Event<'_> {
1647 fn default() -> Self {
1648 Event {
1649 event_id: event::default_id(),
1650 level: event::default_level(),
1651 fingerprint: event::default_fingerprint(),
1652 culprit: Default::default(),
1653 transaction: Default::default(),
1654 message: Default::default(),
1655 logentry: Default::default(),
1656 logger: Default::default(),
1657 modules: Default::default(),
1658 platform: event::default_platform(),
1659 timestamp: SystemTime::now(),
1660 server_name: Default::default(),
1661 release: Default::default(),
1662 dist: Default::default(),
1663 environment: Default::default(),
1664 user: Default::default(),
1665 request: Default::default(),
1666 contexts: Default::default(),
1667 breadcrumbs: Default::default(),
1668 exception: Default::default(),
1669 stacktrace: Default::default(),
1670 template: Default::default(),
1671 threads: Default::default(),
1672 tags: Default::default(),
1673 extra: Default::default(),
1674 debug_meta: Default::default(),
1675 sdk: Default::default(),
1676 }
1677 }
1678}
1679
1680impl<'a> Event<'a> {
1681 pub fn new() -> Event<'a> {
1683 Default::default()
1684 }
1685
1686 pub fn into_owned(self) -> Event<'static> {
1688 Event {
1689 event_id: self.event_id,
1690 level: self.level,
1691 fingerprint: Cow::Owned(
1692 self.fingerprint
1693 .iter()
1694 .map(|x| Cow::Owned(x.to_string()))
1695 .collect(),
1696 ),
1697 culprit: self.culprit,
1698 transaction: self.transaction,
1699 message: self.message,
1700 logentry: self.logentry,
1701 logger: self.logger,
1702 modules: self.modules,
1703 platform: Cow::Owned(self.platform.into_owned()),
1704 timestamp: self.timestamp,
1705 server_name: self.server_name.map(|x| Cow::Owned(x.into_owned())),
1706 release: self.release.map(|x| Cow::Owned(x.into_owned())),
1707 dist: self.dist.map(|x| Cow::Owned(x.into_owned())),
1708 environment: self.environment.map(|x| Cow::Owned(x.into_owned())),
1709 user: self.user,
1710 request: self.request,
1711 contexts: self.contexts,
1712 breadcrumbs: self.breadcrumbs,
1713 exception: self.exception,
1714 stacktrace: self.stacktrace,
1715 template: self.template,
1716 threads: self.threads,
1717 tags: self.tags,
1718 extra: self.extra,
1719 debug_meta: Cow::Owned(self.debug_meta.into_owned()),
1720 sdk: self.sdk.map(|x| Cow::Owned(x.into_owned())),
1721 }
1722 }
1723}
1724
1725impl fmt::Display for Event<'_> {
1726 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1727 write!(
1728 f,
1729 "Event(id: {}, ts: {})",
1730 self.event_id,
1731 crate::utils::to_rfc3339(&self.timestamp)
1732 )
1733 }
1734}
1735
1736#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
1738pub struct Span {
1739 #[serde(default)]
1741 pub span_id: SpanId,
1742 #[serde(default)]
1744 pub trace_id: TraceId,
1745 #[serde(default, skip_serializing_if = "Option::is_none")]
1747 pub parent_span_id: Option<SpanId>,
1748 #[serde(default, skip_serializing_if = "Option::is_none")]
1750 pub same_process_as_parent: Option<bool>,
1751 #[serde(default, skip_serializing_if = "Option::is_none")]
1753 pub op: Option<String>,
1754 #[serde(default, skip_serializing_if = "Option::is_none")]
1757 pub description: Option<String>,
1758 #[serde(
1760 default,
1761 skip_serializing_if = "Option::is_none",
1762 with = "ts_rfc3339_opt"
1763 )]
1764 pub timestamp: Option<SystemTime>,
1765 #[serde(default = "SystemTime::now", with = "ts_seconds_float")]
1767 pub start_timestamp: SystemTime,
1768 #[serde(default, skip_serializing_if = "Option::is_none")]
1770 pub status: Option<SpanStatus>,
1771 #[serde(default, skip_serializing_if = "Map::is_empty")]
1773 pub tags: Map<String, String>,
1774 #[serde(default, skip_serializing_if = "Map::is_empty")]
1776 pub data: Map<String, Value>,
1777}
1778
1779impl Default for Span {
1780 fn default() -> Self {
1781 Span {
1782 span_id: Default::default(),
1783 trace_id: Default::default(),
1784 timestamp: Default::default(),
1785 tags: Default::default(),
1786 start_timestamp: SystemTime::now(),
1787 description: Default::default(),
1788 status: Default::default(),
1789 parent_span_id: Default::default(),
1790 same_process_as_parent: Default::default(),
1791 op: Default::default(),
1792 data: Default::default(),
1793 }
1794 }
1795}
1796
1797impl Span {
1798 pub fn new() -> Span {
1800 Default::default()
1801 }
1802
1803 pub fn finish(&mut self) {
1805 self.timestamp = Some(SystemTime::now());
1806 }
1807}
1808
1809impl fmt::Display for Span {
1810 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1811 write!(
1812 f,
1813 "Span(id: {}, ts: {})",
1814 self.span_id,
1815 crate::utils::to_rfc3339(&self.start_timestamp)
1816 )
1817 }
1818}
1819
1820#[derive(Debug, Error)]
1822#[error("invalid status")]
1823pub struct ParseStatusError;
1824
1825#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, Hash)]
1827#[non_exhaustive]
1828pub enum SpanStatus {
1829 #[serde(rename = "ok")]
1831 Ok,
1832 #[serde(rename = "deadline_exceeded")]
1834 DeadlineExceeded,
1835 #[serde(rename = "unauthenticated")]
1837 Unauthenticated,
1838 #[serde(rename = "permission_denied")]
1840 PermissionDenied,
1841 #[serde(rename = "not_found")]
1843 NotFound,
1844 #[serde(rename = "resource_exhausted")]
1846 ResourceExhausted,
1847 #[serde(rename = "invalid_argument")]
1849 InvalidArgument,
1850 #[serde(rename = "unimplemented")]
1852 Unimplemented,
1853 #[serde(rename = "unavailable")]
1855 Unavailable,
1856 #[serde(rename = "internal_error")]
1858 InternalError,
1859 #[serde(rename = "unknown_error")]
1861 UnknownError,
1862 #[serde(rename = "cancelled")]
1864 Cancelled,
1865 #[serde(rename = "already_exists")]
1867 AlreadyExists,
1868 #[serde(rename = "failed_precondition")]
1870 FailedPrecondition,
1871 #[serde(rename = "aborted")]
1873 Aborted,
1874 #[serde(rename = "out_of_range")]
1876 OutOfRange,
1877 #[serde(rename = "data_loss")]
1879 DataLoss,
1880}
1881
1882impl str::FromStr for SpanStatus {
1883 type Err = ParseStatusError;
1884
1885 fn from_str(s: &str) -> Result<SpanStatus, Self::Err> {
1886 Ok(match s {
1887 "ok" => SpanStatus::Ok,
1888 "deadline_exceeded" => SpanStatus::DeadlineExceeded,
1889 "unauthenticated" => SpanStatus::Unauthenticated,
1890 "permission_denied" => SpanStatus::PermissionDenied,
1891 "not_found" => SpanStatus::NotFound,
1892 "resource_exhausted" => SpanStatus::ResourceExhausted,
1893 "invalid_argument" => SpanStatus::InvalidArgument,
1894 "unimplemented" => SpanStatus::Unimplemented,
1895 "unavailable" => SpanStatus::Unavailable,
1896 "internal_error" => SpanStatus::InternalError,
1897 "unknown_error" => SpanStatus::UnknownError,
1898 "cancelled" => SpanStatus::Cancelled,
1899 "already_exists" => SpanStatus::AlreadyExists,
1900 "failed_precondition" => SpanStatus::FailedPrecondition,
1901 "aborted" => SpanStatus::Aborted,
1902 "out_of_range" => SpanStatus::OutOfRange,
1903 "data_loss" => SpanStatus::DataLoss,
1904 _ => return Err(ParseStatusError),
1905 })
1906 }
1907}
1908
1909impl fmt::Display for SpanStatus {
1910 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1911 match self {
1912 SpanStatus::Ok => write!(f, "ok"),
1913 SpanStatus::DeadlineExceeded => write!(f, "deadline_exceeded"),
1914 SpanStatus::Unauthenticated => write!(f, "unauthenticated"),
1915 SpanStatus::PermissionDenied => write!(f, "permission_denied"),
1916 SpanStatus::NotFound => write!(f, "not_found"),
1917 SpanStatus::ResourceExhausted => write!(f, "resource_exhausted"),
1918 SpanStatus::InvalidArgument => write!(f, "invalid_argument"),
1919 SpanStatus::Unimplemented => write!(f, "unimplemented"),
1920 SpanStatus::Unavailable => write!(f, "unavailable"),
1921 SpanStatus::InternalError => write!(f, "internal_error"),
1922 SpanStatus::UnknownError => write!(f, "unknown_error"),
1923 SpanStatus::Cancelled => write!(f, "cancelled"),
1924 SpanStatus::AlreadyExists => write!(f, "already_exists"),
1925 SpanStatus::FailedPrecondition => write!(f, "failed_precondition"),
1926 SpanStatus::Aborted => write!(f, "aborted"),
1927 SpanStatus::OutOfRange => write!(f, "out_of_range"),
1928 SpanStatus::DataLoss => write!(f, "data_loss"),
1929 }
1930 }
1931}
1932
1933#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
1935pub struct Transaction<'a> {
1936 #[serde(default = "event::default_id", serialize_with = "event::serialize_id")]
1938 pub event_id: Uuid,
1939 #[serde(
1941 rename = "transaction",
1942 default,
1943 skip_serializing_if = "Option::is_none"
1944 )]
1945 pub name: Option<String>,
1946 #[serde(default, skip_serializing_if = "Option::is_none")]
1948 pub release: Option<Cow<'a, str>>,
1949 #[serde(default, skip_serializing_if = "Option::is_none")]
1951 pub environment: Option<Cow<'a, str>>,
1952 #[serde(default, skip_serializing_if = "Option::is_none")]
1954 pub user: Option<User>,
1955 #[serde(default, skip_serializing_if = "Map::is_empty")]
1957 pub tags: Map<String, String>,
1958 #[serde(default, skip_serializing_if = "Map::is_empty")]
1960 pub extra: Map<String, Value>,
1961 #[serde(default, skip_serializing_if = "Option::is_none")]
1963 pub sdk: Option<Cow<'a, ClientSdkInfo>>,
1964 #[serde(
1966 default = "event::default_platform",
1967 skip_serializing_if = "event::is_default_platform"
1968 )]
1969 pub platform: Cow<'a, str>,
1970 #[serde(
1972 default,
1973 skip_serializing_if = "Option::is_none",
1974 with = "ts_rfc3339_opt"
1975 )]
1976 pub timestamp: Option<SystemTime>,
1977 #[serde(default = "SystemTime::now", with = "ts_seconds_float")]
1979 pub start_timestamp: SystemTime,
1980 pub spans: Vec<Span>,
1982 #[serde(
1984 default,
1985 skip_serializing_if = "Map::is_empty",
1986 deserialize_with = "deserialize_contexts"
1987 )]
1988 pub contexts: Map<String, Context>,
1989 #[serde(default, skip_serializing_if = "Option::is_none")]
1991 pub request: Option<Request>,
1992 #[serde(default, skip_serializing_if = "Option::is_none")]
1994 pub server_name: Option<Cow<'a, str>>,
1995}
1996
1997impl Default for Transaction<'_> {
1998 fn default() -> Self {
1999 Transaction {
2000 event_id: event::default_id(),
2001 name: Default::default(),
2002 user: Default::default(),
2003 tags: Default::default(),
2004 extra: Default::default(),
2005 release: Default::default(),
2006 environment: Default::default(),
2007 sdk: Default::default(),
2008 platform: event::default_platform(),
2009 timestamp: Default::default(),
2010 start_timestamp: SystemTime::now(),
2011 spans: Default::default(),
2012 contexts: Default::default(),
2013 request: Default::default(),
2014 server_name: Default::default(),
2015 }
2016 }
2017}
2018
2019impl<'a> Transaction<'a> {
2020 pub fn new() -> Transaction<'a> {
2022 Default::default()
2023 }
2024
2025 pub fn into_owned(self) -> Transaction<'static> {
2027 Transaction {
2028 event_id: self.event_id,
2029 name: self.name,
2030 user: self.user,
2031 tags: self.tags,
2032 extra: self.extra,
2033 release: self.release.map(|x| Cow::Owned(x.into_owned())),
2034 environment: self.environment.map(|x| Cow::Owned(x.into_owned())),
2035 sdk: self.sdk.map(|x| Cow::Owned(x.into_owned())),
2036 platform: Cow::Owned(self.platform.into_owned()),
2037 timestamp: self.timestamp,
2038 start_timestamp: self.start_timestamp,
2039 spans: self.spans,
2040 contexts: self.contexts,
2041 request: self.request,
2042 server_name: self.server_name.map(|x| Cow::Owned(x.into_owned())),
2043 }
2044 }
2045
2046 pub fn finish(&mut self) {
2048 self.timestamp = Some(SystemTime::now());
2049 }
2050}
2051
2052impl fmt::Display for Transaction<'_> {
2053 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2054 write!(
2055 f,
2056 "Transaction(id: {}, ts: {})",
2057 self.event_id,
2058 crate::utils::to_rfc3339(&self.start_timestamp)
2059 )
2060 }
2061}