relay_base_schema/
data_category.rs

1//! Defines the [`DataCategory`] type that classifies data Relay can handle.
2
3use std::fmt;
4use std::str::FromStr;
5
6use serde::{Deserialize, Serialize};
7
8use crate::events::EventType;
9
10/// An error that occurs if a number cannot be converted into a [`DataCategory`].
11#[derive(Debug, PartialEq, thiserror::Error)]
12#[error("Unknown numeric data category {0} can not be converted into a DataCategory.")]
13pub struct UnknownDataCategory(pub u8);
14
15/// Classifies the type of data that is being ingested.
16#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
17#[serde(rename_all = "snake_case")]
18#[repr(i8)]
19pub enum DataCategory {
20    /// Reserved and unused.
21    Default = 0,
22    /// Error events and Events with an `event_type` not explicitly listed below.
23    Error = 1,
24    /// Transaction events.
25    Transaction = 2,
26    /// Events with an event type of `csp`, `hpkp`, `expectct` and `expectstaple`.
27    Security = 3,
28    /// An attachment. Quantity is the size of the attachment in bytes.
29    Attachment = 4,
30    /// Session updates. Quantity is the number of updates in the batch.
31    Session = 5,
32    /// Profile
33    ///
34    /// This is the category for processed profiles (all profiles, whether or not we store them).
35    Profile = 6,
36    /// Session Replays
37    Replay = 7,
38    /// DEPRECATED: A transaction for which metrics were extracted.
39    ///
40    /// This category is now obsolete because the `Transaction` variant will represent
41    /// processed transactions from now on.
42    TransactionProcessed = 8,
43    /// Indexed transaction events.
44    ///
45    /// This is the category for transaction payloads that were accepted and stored in full. In
46    /// contrast, `transaction` only guarantees that metrics have been accepted for the transaction.
47    TransactionIndexed = 9,
48    /// Monitor check-ins.
49    Monitor = 10,
50    /// Indexed Profile
51    ///
52    /// This is the category for indexed profiles that will be stored later.
53    ProfileIndexed = 11,
54    /// Span
55    ///
56    /// This is the category for spans from which we extracted metrics from.
57    Span = 12,
58    /// Monitor Seat
59    ///
60    /// Represents a monitor job that has scheduled monitor checkins. The seats are not ingested
61    /// but we define it here to prevent clashing values since this data category enumeration
62    /// is also used outside of Relay via the Python package.
63    MonitorSeat = 13,
64    /// User Feedback
65    ///
66    /// Represents a User Feedback processed.
67    /// Currently standardized on name UserReportV2 to avoid clashing with the old UserReport.
68    /// TODO(jferg): Rename this to UserFeedback once old UserReport is deprecated.
69    UserReportV2 = 14,
70    /// Metric buckets.
71    MetricBucket = 15,
72    /// SpanIndexed
73    ///
74    /// This is the category for spans we store in full.
75    SpanIndexed = 16,
76    /// ProfileDuration
77    ///
78    /// This data category is used to count the number of milliseconds per indexed profile chunk,
79    /// excluding UI profile chunks.
80    ProfileDuration = 17,
81    /// ProfileChunk
82    ///
83    /// This is a count of profile chunks received. It will not be used for billing but will be
84    /// useful for customers to track what's being dropped.
85    ProfileChunk = 18,
86    /// MetricSecond
87    ///
88    /// Reserved by billing to summarize the bucketed product of metric volume
89    /// and metric cardinality. Defined here so as not to clash with future
90    /// categories.
91    MetricSecond = 19,
92    /// Replay Video
93    ///
94    /// This is the data category for Session Replays produced via a video recording.
95    DoNotUseReplayVideo = 20,
96    /// This is the data category for Uptime monitors.
97    Uptime = 21,
98    /// Counts the number of individual attachments, as opposed to the number of bytes in an attachment.
99    AttachmentItem = 22,
100    /// LogItem
101    ///
102    /// This is the category for logs for which we store the count log events for users for measuring
103    /// missing breadcrumbs, and count of logs for rate limiting purposes.
104    LogItem = 23,
105    /// LogByte
106    ///
107    /// This is the category for logs for which we store log event total bytes for users.
108    LogByte = 24,
109    /// Profile duration of a UI profile.
110    ///
111    /// This data category is used to count the number of milliseconds per indexed UI profile
112    /// chunk.
113    ///
114    /// See also: [`Self::ProfileDuration`]
115    ProfileDurationUi = 25,
116    /// UI Profile Chunk.
117    ///
118    /// This data category is used to count the number of milliseconds per indexed UI profile
119    /// chunk.
120    ///
121    /// See also: [`Self::ProfileChunk`]
122    ProfileChunkUi = 26,
123    /// This is the data category to count Seer Autofix run events.
124    SeerAutofix = 27,
125    /// This is the data category to count Seer Scanner run events.
126    SeerScanner = 28,
127    /// PreventUser
128    ///
129    /// This is the data category to count the number of assigned Prevent Users.
130    PreventUser = 29,
131    /// PreventReview
132    ///
133    /// This is the data category to count the number of Prevent review events.
134    PreventReview = 30,
135    /// Size analysis
136    ///
137    /// This is the data category to count the number of size analyses performed.
138    /// 'Size analysis' a static binary analysis of a preprod build artifact
139    /// (e.g. the .apk of an Android app or MacOS .app).
140    /// When enabled there will typically be one such analysis per uploaded artifact.
141    SizeAnalysis = 31,
142    /// InstallableBuild
143    ///
144    /// This is the data category to count the number of installable builds.
145    /// It counts the number of artifacts uploaded *not* the number of times the
146    /// artifacts are downloaded for installation.
147    /// When enabled there will typically be one 'InstallableBuild' per uploaded artifact.
148    InstallableBuild = 32,
149    //
150    // IMPORTANT: After adding a new entry to DataCategory, go to the `relay-cabi` subfolder and run
151    // `make header` to regenerate the C-binding. This allows using the data category from Python.
152    // Rerun this step every time the **code name** of the variant is updated.
153    //
154    /// Any other data category not known by this Relay.
155    #[serde(other)]
156    Unknown = -1,
157}
158
159impl DataCategory {
160    /// Returns the data category corresponding to the given name.
161    pub fn from_name(string: &str) -> Self {
162        // TODO: This should probably use serde.
163        match string {
164            "default" => Self::Default,
165            "error" => Self::Error,
166            "transaction" => Self::Transaction,
167            "security" => Self::Security,
168            "attachment" => Self::Attachment,
169            "session" => Self::Session,
170            "profile" => Self::Profile,
171            "profile_indexed" => Self::ProfileIndexed,
172            "replay" => Self::Replay,
173            "transaction_processed" => Self::TransactionProcessed,
174            "transaction_indexed" => Self::TransactionIndexed,
175            "monitor" => Self::Monitor,
176            "span" => Self::Span,
177            "log_item" => Self::LogItem,
178            "log_byte" => Self::LogByte,
179            "monitor_seat" => Self::MonitorSeat,
180            "feedback" => Self::UserReportV2,
181            "user_report_v2" => Self::UserReportV2,
182            "metric_bucket" => Self::MetricBucket,
183            "span_indexed" => Self::SpanIndexed,
184            "profile_duration" => Self::ProfileDuration,
185            "profile_duration_ui" => Self::ProfileDurationUi,
186            "profile_chunk" => Self::ProfileChunk,
187            "profile_chunk_ui" => Self::ProfileChunkUi,
188            "metric_second" => Self::MetricSecond,
189            "replay_video" => Self::DoNotUseReplayVideo,
190            "uptime" => Self::Uptime,
191            "attachment_item" => Self::AttachmentItem,
192            "seer_autofix" => Self::SeerAutofix,
193            "seer_scanner" => Self::SeerScanner,
194            "prevent_user" => Self::PreventUser,
195            "prevent_review" => Self::PreventReview,
196            "size_analysis" => Self::SizeAnalysis,
197            "installable_build" => Self::InstallableBuild,
198            _ => Self::Unknown,
199        }
200    }
201
202    /// Returns the canonical name of this data category.
203    pub fn name(self) -> &'static str {
204        // TODO: This should probably use serde.
205        match self {
206            Self::Default => "default",
207            Self::Error => "error",
208            Self::Transaction => "transaction",
209            Self::Security => "security",
210            Self::Attachment => "attachment",
211            Self::Session => "session",
212            Self::Profile => "profile",
213            Self::ProfileIndexed => "profile_indexed",
214            Self::Replay => "replay",
215            Self::DoNotUseReplayVideo => "replay_video",
216            Self::TransactionProcessed => "transaction_processed",
217            Self::TransactionIndexed => "transaction_indexed",
218            Self::Monitor => "monitor",
219            Self::Span => "span",
220            Self::LogItem => "log_item",
221            Self::LogByte => "log_byte",
222            Self::MonitorSeat => "monitor_seat",
223            Self::UserReportV2 => "feedback",
224            Self::MetricBucket => "metric_bucket",
225            Self::SpanIndexed => "span_indexed",
226            Self::ProfileDuration => "profile_duration",
227            Self::ProfileDurationUi => "profile_duration_ui",
228            Self::ProfileChunk => "profile_chunk",
229            Self::ProfileChunkUi => "profile_chunk_ui",
230            Self::MetricSecond => "metric_second",
231            Self::Uptime => "uptime",
232            Self::AttachmentItem => "attachment_item",
233            Self::SeerAutofix => "seer_autofix",
234            Self::SeerScanner => "seer_scanner",
235            Self::PreventUser => "prevent_user",
236            Self::PreventReview => "prevent_review",
237            Self::SizeAnalysis => "size_analysis",
238            Self::InstallableBuild => "installable_build",
239            Self::Unknown => "unknown",
240        }
241    }
242
243    /// Returns true if the DataCategory refers to an error (i.e an error event).
244    pub fn is_error(self) -> bool {
245        matches!(self, Self::Error | Self::Default | Self::Security)
246    }
247
248    /// Returns the numeric value for this outcome.
249    pub fn value(self) -> Option<u8> {
250        // negative values (Internal and Unknown) cannot be sent as
251        // outcomes (internally so!)
252        (self as i8).try_into().ok()
253    }
254
255    /// Returns a dedicated category for indexing if this data can be converted to metrics.
256    ///
257    /// This returns `None` for most data categories.
258    pub fn index_category(self) -> Option<Self> {
259        match self {
260            Self::Transaction => Some(Self::TransactionIndexed),
261            Self::Span => Some(Self::SpanIndexed),
262            Self::Profile => Some(Self::ProfileIndexed),
263            _ => None,
264        }
265    }
266
267    /// Returns `true` if this data category is an indexed data category.
268    pub fn is_indexed(self) -> bool {
269        matches!(
270            self,
271            Self::TransactionIndexed | Self::SpanIndexed | Self::ProfileIndexed
272        )
273    }
274}
275
276impl fmt::Display for DataCategory {
277    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
278        write!(f, "{}", self.name())
279    }
280}
281
282impl FromStr for DataCategory {
283    type Err = ();
284
285    fn from_str(string: &str) -> Result<Self, Self::Err> {
286        Ok(Self::from_name(string))
287    }
288}
289
290impl From<EventType> for DataCategory {
291    fn from(ty: EventType) -> Self {
292        match ty {
293            EventType::Default | EventType::Error | EventType::Nel => Self::Error,
294            EventType::Transaction => Self::Transaction,
295            EventType::Csp | EventType::Hpkp | EventType::ExpectCt | EventType::ExpectStaple => {
296                Self::Security
297            }
298            EventType::UserReportV2 => Self::UserReportV2,
299        }
300    }
301}
302
303impl TryFrom<u8> for DataCategory {
304    type Error = UnknownDataCategory;
305
306    fn try_from(value: u8) -> Result<Self, UnknownDataCategory> {
307        match value {
308            0 => Ok(Self::Default),
309            1 => Ok(Self::Error),
310            2 => Ok(Self::Transaction),
311            3 => Ok(Self::Security),
312            4 => Ok(Self::Attachment),
313            5 => Ok(Self::Session),
314            6 => Ok(Self::Profile),
315            7 => Ok(Self::Replay),
316            8 => Ok(Self::TransactionProcessed),
317            9 => Ok(Self::TransactionIndexed),
318            10 => Ok(Self::Monitor),
319            11 => Ok(Self::ProfileIndexed),
320            12 => Ok(Self::Span),
321            13 => Ok(Self::MonitorSeat),
322            14 => Ok(Self::UserReportV2),
323            15 => Ok(Self::MetricBucket),
324            16 => Ok(Self::SpanIndexed),
325            17 => Ok(Self::ProfileDuration),
326            18 => Ok(Self::ProfileChunk),
327            19 => Ok(Self::MetricSecond),
328            20 => Ok(Self::DoNotUseReplayVideo),
329            21 => Ok(Self::Uptime),
330            22 => Ok(Self::AttachmentItem),
331            23 => Ok(Self::LogItem),
332            24 => Ok(Self::LogByte),
333            25 => Ok(Self::ProfileDurationUi),
334            26 => Ok(Self::ProfileChunkUi),
335            27 => Ok(Self::SeerAutofix),
336            28 => Ok(Self::SeerScanner),
337            29 => Ok(Self::PreventUser),
338            30 => Ok(Self::PreventReview),
339            31 => Ok(Self::SizeAnalysis),
340            32 => Ok(Self::InstallableBuild),
341            other => Err(UnknownDataCategory(other)),
342        }
343    }
344}
345
346#[cfg(test)]
347mod tests {
348    use super::*;
349
350    #[test]
351    pub fn test_last_variant_conversion() {
352        // If this test fails, update the numeric bounds so that the first assertion
353        // maps to the last variant in the enum and the second assertion produces an error
354        // that the DataCategory does not exist.
355        assert_eq!(
356            DataCategory::try_from(32),
357            Ok(DataCategory::InstallableBuild)
358        );
359        assert_eq!(DataCategory::try_from(33), Err(UnknownDataCategory(33)));
360    }
361}