sentry_types/protocol/
profile.rs

1use std::{collections::HashMap, time::SystemTime};
2
3use super::v7::{DebugMeta, TraceId};
4use crate::utils::ts_rfc3339;
5
6use serde::{Deserialize, Serialize, Serializer};
7use uuid::Uuid;
8
9fn serialize_id<S: Serializer>(uuid: &Uuid, serializer: S) -> Result<S::Ok, S::Error> {
10    serializer.serialize_some(&uuid.as_simple())
11}
12#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
13/// Metadata about the transaction associated with the profile
14pub struct TransactionMetadata {
15    #[serde(serialize_with = "serialize_id")]
16    /// Transaction ID
17    pub id: Uuid,
18    /// Transaction Name
19    pub name: String,
20    /// Trace ID
21    pub trace_id: TraceId,
22    /// Transaction start timestamp in nanoseconds relative to the start of the profiler
23    pub relative_start_ns: u64,
24    /// Transaction end timestamp in nanoseconds relative to the start of the profiler
25    pub relative_end_ns: u64,
26    /// ID of the thread in which the transaction started
27    #[serde(default)]
28    pub active_thread_id: u64,
29}
30
31#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq, Hash)]
32/// Single frame of a Sample
33pub struct RustFrame {
34    /// Instruction address
35    pub instruction_addr: String,
36}
37
38#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
39/// Single sample of a profile
40pub struct Sample {
41    /// ID of the relative stack
42    pub stack_id: u32,
43    /// Thread ID
44    pub thread_id: u64,
45    /// Timestamp at which this sample was collected relative to the start of the profiler
46    pub elapsed_since_start_ns: u64,
47}
48
49#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
50/// Thread metadata
51pub struct ThreadMetadata {
52    #[serde(default, skip_serializing_if = "Option::is_none")]
53    /// Thread name
54    pub name: Option<String>,
55}
56
57#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
58/// Collected Profile
59pub struct Profile {
60    /// list of samples in a profile
61    pub samples: Vec<Sample>,
62    /// List of stacks: each stacks is a vec of indexed frames
63    pub stacks: Vec<Vec<u32>>,
64    /// List of frames
65    pub frames: Vec<RustFrame>,
66    /// Thread metadata
67    pub thread_metadata: HashMap<String, ThreadMetadata>,
68}
69
70#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
71/// Operating System metadata
72pub struct OSMetadata {
73    /// OS Name
74    pub name: String,
75    /// OS Version
76    pub version: String,
77
78    #[serde(default, skip_serializing_if = "Option::is_none")]
79    /// Build number
80    pub build_number: Option<String>,
81}
82
83#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
84/// Runtime metadata
85pub struct RuntimeMetadata {
86    /// Runtime name (rustc)
87    pub name: String,
88    /// Runtime version
89    pub version: String,
90}
91
92#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
93/// Device metadata
94pub struct DeviceMetadata {
95    /// Architecture
96    pub architecture: Option<String>,
97}
98
99#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
100/// Profile format version
101pub enum Version {
102    #[serde(rename = "1")]
103    /// First version
104    V1,
105}
106
107#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
108/// Represents a Profile Envelope ItemType
109pub struct SampleProfile {
110    /// Format version of the SampleProfile
111    pub version: Version,
112
113    #[serde(skip_serializing_if = "Option::is_none")]
114    /// Debug meta information
115    pub debug_meta: Option<DebugMeta>,
116
117    /// Device metadata information
118    pub device: DeviceMetadata,
119    /// OS metadata information
120    pub os: OSMetadata,
121    #[serde(skip_serializing_if = "Option::is_none")]
122    /// Runtime metadata information
123    pub runtime: Option<RuntimeMetadata>,
124
125    #[serde(default, skip_serializing_if = "String::is_empty")]
126    /// Environment
127    pub environment: String,
128    #[serde(serialize_with = "serialize_id")]
129    /// Event ID or Profile ID
130    pub event_id: Uuid,
131    /// Platform
132    pub platform: String,
133    /// Collected profile
134    pub profile: Profile,
135    /// Release
136    pub release: String,
137    #[serde(with = "ts_rfc3339")]
138    /// Timestamp at which the profiler started
139    pub timestamp: SystemTime,
140
141    #[serde(default, skip_serializing_if = "Vec::is_empty")]
142    /// List of transactions associated with this profile
143    pub transactions: Vec<TransactionMetadata>,
144}