relay_event_normalization/normalize/
utils.rs1use std::f64::consts::SQRT_2;
7
8use relay_event_schema::protocol::{Event, ResponseContext, Span, TraceContext, User};
9use relay_protocol::Value;
10
11pub const MOBILE_SDKS: [&str; 4] = [
13 "sentry.cocoa",
14 "sentry.dart.flutter",
15 "sentry.java.android",
16 "sentry.javascript.react-native",
17];
18
19pub const MAIN_THREAD_NAME: &str = "main";
21
22pub const MAX_DURATION_MOBILE_MS: f64 = 180_000.0;
27
28pub fn http_status_code_from_span(span: &Span) -> Option<String> {
30 if let Some(status_code) = span
32 .data
33 .value()
34 .and_then(|data| data.http_response_status_code.value())
35 .map(|v| match v {
36 Value::String(s) => Some(s.as_str().to_owned()),
37 Value::I64(i) => Some(i.to_string()),
38 Value::U64(u) => Some(u.to_string()),
39 _ => None,
40 })
41 {
42 return status_code;
43 }
44
45 if let Some(status_code) = span
47 .tags
48 .value()
49 .and_then(|tags| tags.get("http.status_code"))
50 .and_then(|v| v.as_str())
51 .map(|v| v.to_owned())
52 {
53 return Some(status_code);
54 }
55
56 None
57}
58
59pub fn extract_http_status_code(event: &Event) -> Option<String> {
61 if let Some(status_code) = event.tag_value("http.status_code") {
63 return Some(status_code.to_owned());
64 }
65
66 if let Some(spans) = event.spans.value() {
67 for span in spans {
68 if let Some(span_value) = span.value()
69 && let Some(status_code) = http_status_code_from_span(span_value)
70 {
71 return Some(status_code);
72 }
73 }
74 }
75
76 if let Some(breadcrumbs) = event.breadcrumbs.value()
78 && let Some(values) = breadcrumbs.values.value()
79 {
80 for breadcrumb in values {
81 if let Some(crumb) = breadcrumb
83 .value()
84 .filter(|bc| bc.ty.as_str() == Some("http"))
85 {
86 if let Some(status_code) = crumb.data.value().and_then(|v| v.get("status_code")) {
88 return status_code.value().and_then(|v| v.as_str()).map(Into::into);
89 }
90 }
91 }
92 }
93
94 if let Some(response_context) = event.context::<ResponseContext>() {
96 let status_code = response_context
97 .status_code
98 .value()
99 .map(|code| code.to_string());
100 return status_code;
101 }
102
103 None
104}
105
106pub fn get_event_user_tag(user: &User) -> Option<String> {
136 if let Some(id) = user.id.as_str() {
137 return Some(format!("id:{id}"));
138 }
139
140 if let Some(username) = user.username.as_str() {
141 return Some(format!("username:{username}"));
142 }
143
144 if let Some(email) = user.email.as_str() {
145 return Some(format!("email:{email}"));
146 }
147
148 if let Some(ip_address) = user.ip_address.as_str() {
149 return Some(format!("ip:{ip_address}"));
150 }
151
152 None
153}
154
155pub fn extract_transaction_op(trace_context: &TraceContext) -> Option<String> {
157 let op = trace_context.op.value()?;
158 if op == "default" {
159 return None;
165 }
166 Some(op.to_string())
167}
168
169fn erf(x: f64) -> f64 {
173 let a1 = 0.254829592;
175 let a2 = -0.284496736;
176 let a3 = 1.421413741;
177 let a4 = -1.453152027;
178 let a5 = 1.061405429;
179 let p = 0.3275911;
180 let sign = if x < 0.0 { -1.0 } else { 1.0 };
182 let x = x.abs();
183 let t = 1.0 / (1.0 + p * x);
185 let y = 1.0 - ((((a5 * t + a4) * t + a3) * t + a2) * t + a1) * t * (-x * x).exp();
186 sign * y
187}
188
189fn calculate_cdf_sigma(p10: f64, p50: f64) -> f64 {
191 (p10.ln() - p50.ln()).abs() / (SQRT_2 * 0.9061938024368232)
192}
193
194pub fn calculate_cdf_score(value: f64, p10: f64, p50: f64) -> f64 {
196 0.5 * (1.0 - erf((f64::ln(value) - f64::ln(p50)) / (SQRT_2 * calculate_cdf_sigma(p50, p10))))
197}
198
199#[cfg(test)]
200mod tests {
201 use crate::utils::{get_event_user_tag, http_status_code_from_span};
202 use relay_event_schema::protocol::{Span, User};
203 use relay_protocol::Annotated;
204
205 #[test]
206 fn test_get_event_user_tag() {
207 let user = User {
211 id: Annotated::new("ident".to_owned().into()),
212 username: Annotated::new("username".to_owned().into()),
213 email: Annotated::new("email".to_owned()),
214 ip_address: Annotated::new("127.0.0.1".parse().unwrap()),
215 ..User::default()
216 };
217
218 assert_eq!(get_event_user_tag(&user).unwrap(), "id:ident");
219
220 let user = User {
221 username: Annotated::new("username".to_owned().into()),
222 email: Annotated::new("email".to_owned()),
223 ip_address: Annotated::new("127.0.0.1".parse().unwrap()),
224 ..User::default()
225 };
226
227 assert_eq!(get_event_user_tag(&user).unwrap(), "username:username");
228
229 let user = User {
230 email: Annotated::new("email".to_owned()),
231 ip_address: Annotated::new("127.0.0.1".parse().unwrap()),
232 ..User::default()
233 };
234
235 assert_eq!(get_event_user_tag(&user).unwrap(), "email:email");
236
237 let user = User {
238 ip_address: Annotated::new("127.0.0.1".parse().unwrap()),
239 ..User::default()
240 };
241
242 assert_eq!(get_event_user_tag(&user).unwrap(), "ip:127.0.0.1");
243
244 let user = User::default();
245
246 assert!(get_event_user_tag(&user).is_none());
247 }
248
249 #[test]
250 fn test_extracts_http_status_code_when_int() {
251 let span = Annotated::<Span>::from_json(
252 r#"{
253 "data": {
254 "http.response.status_code": 400
255 }
256 }"#,
257 )
258 .unwrap()
259 .into_value()
260 .unwrap();
261
262 let result = http_status_code_from_span(&span);
263
264 assert_eq!(result, Some("400".to_owned()));
265 }
266}