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 //
128 // IMPORTANT: After adding a new entry to DataCategory, go to the `relay-cabi` subfolder and run
129 // `make header` to regenerate the C-binding. This allows using the data category from Python.
130 // Rerun this step every time the **code name** of the variant is updated.
131 //
132 /// Any other data category not known by this Relay.
133 #[serde(other)]
134 Unknown = -1,
135}
136
137impl DataCategory {
138 /// Returns the data category corresponding to the given name.
139 pub fn from_name(string: &str) -> Self {
140 // TODO: This should probably use serde.
141 match string {
142 "default" => Self::Default,
143 "error" => Self::Error,
144 "transaction" => Self::Transaction,
145 "security" => Self::Security,
146 "attachment" => Self::Attachment,
147 "session" => Self::Session,
148 "profile" => Self::Profile,
149 "profile_indexed" => Self::ProfileIndexed,
150 "replay" => Self::Replay,
151 "transaction_processed" => Self::TransactionProcessed,
152 "transaction_indexed" => Self::TransactionIndexed,
153 "monitor" => Self::Monitor,
154 "span" => Self::Span,
155 "log_item" => Self::LogItem,
156 "log_byte" => Self::LogByte,
157 "monitor_seat" => Self::MonitorSeat,
158 "feedback" => Self::UserReportV2,
159 "user_report_v2" => Self::UserReportV2,
160 "metric_bucket" => Self::MetricBucket,
161 "span_indexed" => Self::SpanIndexed,
162 "profile_duration" => Self::ProfileDuration,
163 "profile_duration_ui" => Self::ProfileDurationUi,
164 "profile_chunk" => Self::ProfileChunk,
165 "profile_chunk_ui" => Self::ProfileChunkUi,
166 "metric_second" => Self::MetricSecond,
167 "replay_video" => Self::DoNotUseReplayVideo,
168 "uptime" => Self::Uptime,
169 "attachment_item" => Self::AttachmentItem,
170 "seer_autofix" => Self::SeerAutofix,
171 "seer_scanner" => Self::SeerScanner,
172 _ => Self::Unknown,
173 }
174 }
175
176 /// Returns the canonical name of this data category.
177 pub fn name(self) -> &'static str {
178 // TODO: This should probably use serde.
179 match self {
180 Self::Default => "default",
181 Self::Error => "error",
182 Self::Transaction => "transaction",
183 Self::Security => "security",
184 Self::Attachment => "attachment",
185 Self::Session => "session",
186 Self::Profile => "profile",
187 Self::ProfileIndexed => "profile_indexed",
188 Self::Replay => "replay",
189 Self::DoNotUseReplayVideo => "replay_video",
190 Self::TransactionProcessed => "transaction_processed",
191 Self::TransactionIndexed => "transaction_indexed",
192 Self::Monitor => "monitor",
193 Self::Span => "span",
194 Self::LogItem => "log_item",
195 Self::LogByte => "log_byte",
196 Self::MonitorSeat => "monitor_seat",
197 Self::UserReportV2 => "feedback",
198 Self::MetricBucket => "metric_bucket",
199 Self::SpanIndexed => "span_indexed",
200 Self::ProfileDuration => "profile_duration",
201 Self::ProfileDurationUi => "profile_duration_ui",
202 Self::ProfileChunk => "profile_chunk",
203 Self::ProfileChunkUi => "profile_chunk_ui",
204 Self::MetricSecond => "metric_second",
205 Self::Uptime => "uptime",
206 Self::AttachmentItem => "attachment_item",
207 Self::SeerAutofix => "seer_autofix",
208 Self::SeerScanner => "seer_scanner",
209 Self::Unknown => "unknown",
210 }
211 }
212
213 /// Returns true if the DataCategory refers to an error (i.e an error event).
214 pub fn is_error(self) -> bool {
215 matches!(self, Self::Error | Self::Default | Self::Security)
216 }
217
218 /// Returns the numeric value for this outcome.
219 pub fn value(self) -> Option<u8> {
220 // negative values (Internal and Unknown) cannot be sent as
221 // outcomes (internally so!)
222 (self as i8).try_into().ok()
223 }
224
225 /// Returns a dedicated category for indexing if this data can be converted to metrics.
226 ///
227 /// This returns `None` for most data categories.
228 pub fn index_category(self) -> Option<Self> {
229 match self {
230 Self::Transaction => Some(Self::TransactionIndexed),
231 Self::Span => Some(Self::SpanIndexed),
232 Self::Profile => Some(Self::ProfileIndexed),
233 _ => None,
234 }
235 }
236
237 /// Returns `true` if this data category is an indexed data category.
238 pub fn is_indexed(self) -> bool {
239 matches!(
240 self,
241 Self::TransactionIndexed | Self::SpanIndexed | Self::ProfileIndexed
242 )
243 }
244}
245
246impl fmt::Display for DataCategory {
247 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
248 write!(f, "{}", self.name())
249 }
250}
251
252impl FromStr for DataCategory {
253 type Err = ();
254
255 fn from_str(string: &str) -> Result<Self, Self::Err> {
256 Ok(Self::from_name(string))
257 }
258}
259
260impl From<EventType> for DataCategory {
261 fn from(ty: EventType) -> Self {
262 match ty {
263 EventType::Default | EventType::Error | EventType::Nel => Self::Error,
264 EventType::Transaction => Self::Transaction,
265 EventType::Csp | EventType::Hpkp | EventType::ExpectCt | EventType::ExpectStaple => {
266 Self::Security
267 }
268 EventType::UserReportV2 => Self::UserReportV2,
269 }
270 }
271}
272
273impl TryFrom<u8> for DataCategory {
274 type Error = UnknownDataCategory;
275
276 fn try_from(value: u8) -> Result<Self, UnknownDataCategory> {
277 match value {
278 0 => Ok(Self::Default),
279 1 => Ok(Self::Error),
280 2 => Ok(Self::Transaction),
281 3 => Ok(Self::Security),
282 4 => Ok(Self::Attachment),
283 5 => Ok(Self::Session),
284 6 => Ok(Self::Profile),
285 7 => Ok(Self::Replay),
286 8 => Ok(Self::TransactionProcessed),
287 9 => Ok(Self::TransactionIndexed),
288 10 => Ok(Self::Monitor),
289 11 => Ok(Self::ProfileIndexed),
290 12 => Ok(Self::Span),
291 13 => Ok(Self::MonitorSeat),
292 14 => Ok(Self::UserReportV2),
293 15 => Ok(Self::MetricBucket),
294 16 => Ok(Self::SpanIndexed),
295 17 => Ok(Self::ProfileDuration),
296 18 => Ok(Self::ProfileChunk),
297 19 => Ok(Self::MetricSecond),
298 20 => Ok(Self::DoNotUseReplayVideo),
299 21 => Ok(Self::Uptime),
300 22 => Ok(Self::AttachmentItem),
301 23 => Ok(Self::LogItem),
302 24 => Ok(Self::LogByte),
303 25 => Ok(Self::ProfileDurationUi),
304 26 => Ok(Self::ProfileChunkUi),
305 27 => Ok(Self::SeerAutofix),
306 28 => Ok(Self::SeerScanner),
307 other => Err(UnknownDataCategory(other)),
308 }
309 }
310}
311
312#[cfg(test)]
313mod tests {
314 use super::*;
315
316 #[test]
317 pub fn test_last_variant_conversion() {
318 // If this test fails, update the numeric bounds so that the first assertion
319 // maps to the last variant in the enum and the second assertion produces an error
320 // that the DataCategory does not exist.
321 assert_eq!(DataCategory::try_from(28), Ok(DataCategory::SeerScanner));
322 assert_eq!(DataCategory::try_from(29), Err(UnknownDataCategory(29)));
323 }
324}