relay_event_normalization/normalize/
utils.rs1use std::f64::consts::SQRT_2;
7
8use relay_conventions::attributes::HTTP__RESPONSE__STATUS_CODE;
9use relay_event_schema::protocol::{Event, ResponseContext, Span, TraceContext, User};
10use relay_protocol::Value;
11
12pub const MOBILE_SDKS: [&str; 4] = [
14 "sentry.cocoa",
15 "sentry.dart.flutter",
16 "sentry.java.android",
17 "sentry.javascript.react-native",
18];
19
20pub const MAIN_THREAD_NAME: &str = "main";
22
23pub const MAX_DURATION_MOBILE_MS: f64 = 180_000.0;
28
29pub fn http_status_code_from_span(span: &Span) -> Option<String> {
31 if let Some(status_code) = span
33 .data
34 .value()
35 .and_then(|data| data.get_value(HTTP__RESPONSE__STATUS_CODE))
36 .map(|v| match v {
37 Value::String(s) => Some(s.as_str().to_owned()),
38 Value::I64(i) => Some(i.to_string()),
39 Value::U64(u) => Some(u.to_string()),
40 _ => None,
41 })
42 {
43 return status_code;
44 }
45
46 if let Some(status_code) = span
48 .tags
49 .value()
50 .and_then(|tags| tags.get("http.status_code"))
51 .and_then(|v| v.as_str())
52 .map(|v| v.to_owned())
53 {
54 return Some(status_code);
55 }
56
57 None
58}
59
60pub fn extract_http_status_code(event: &Event) -> Option<String> {
62 if let Some(status_code) = event.tag_value("http.status_code") {
64 return Some(status_code.to_owned());
65 }
66
67 if let Some(spans) = event.spans.value() {
68 for span in spans {
69 if let Some(span_value) = span.value()
70 && let Some(status_code) = http_status_code_from_span(span_value)
71 {
72 return Some(status_code);
73 }
74 }
75 }
76
77 if let Some(breadcrumbs) = event.breadcrumbs.value()
79 && let Some(values) = breadcrumbs.values.value()
80 {
81 for breadcrumb in values {
82 if let Some(crumb) = breadcrumb
84 .value()
85 .filter(|bc| bc.ty.as_str() == Some("http"))
86 {
87 if let Some(status_code) = crumb.data.value().and_then(|v| v.get("status_code")) {
89 return status_code.value().and_then(|v| v.as_str()).map(Into::into);
90 }
91 }
92 }
93 }
94
95 if let Some(response_context) = event.context::<ResponseContext>() {
97 let status_code = response_context
98 .status_code
99 .value()
100 .map(|code| code.to_string());
101 return status_code;
102 }
103
104 None
105}
106
107pub fn get_event_user_tag(user: &User) -> Option<String> {
109 format_user_tag(
110 user.id.as_str(),
111 user.username.as_str(),
112 user.email.as_str(),
113 user.ip_address.as_str(),
114 )
115}
116
117pub(crate) fn format_user_tag(
147 id: Option<&str>,
148 username: Option<&str>,
149 email: Option<&str>,
150 ip_address: Option<&str>,
151) -> Option<String> {
152 if let Some(id) = id {
153 return Some(format!("id:{id}"));
154 }
155
156 if let Some(username) = username {
157 return Some(format!("username:{username}"));
158 }
159
160 if let Some(email) = email {
161 return Some(format!("email:{email}"));
162 }
163
164 if let Some(ip_address) = ip_address {
165 return Some(format!("ip:{ip_address}"));
166 }
167
168 None
169}
170
171pub fn extract_transaction_op(trace_context: &TraceContext) -> Option<String> {
173 let op = trace_context.op.value()?;
174 if op == "default" {
175 return None;
181 }
182 Some(op.to_string())
183}
184
185fn erf(x: f64) -> f64 {
189 let a1 = 0.254829592;
191 let a2 = -0.284496736;
192 let a3 = 1.421413741;
193 let a4 = -1.453152027;
194 let a5 = 1.061405429;
195 let p = 0.3275911;
196 let sign = if x < 0.0 { -1.0 } else { 1.0 };
198 let x = x.abs();
199 let t = 1.0 / (1.0 + p * x);
201 let y = 1.0 - ((((a5 * t + a4) * t + a3) * t + a2) * t + a1) * t * (-x * x).exp();
202 sign * y
203}
204
205fn calculate_cdf_sigma(p10: f64, p50: f64) -> f64 {
207 (p10.ln() - p50.ln()).abs() / (SQRT_2 * 0.9061938024368232)
208}
209
210pub fn calculate_cdf_score(value: f64, p10: f64, p50: f64) -> f64 {
216 0.5 * (1.0 - erf((f64::ln(value) - f64::ln(p50)) / (SQRT_2 * calculate_cdf_sigma(p50, p10))))
217}
218
219#[cfg(test)]
220mod tests {
221 use crate::utils::{get_event_user_tag, http_status_code_from_span};
222 use relay_event_schema::protocol::{Span, User};
223 use relay_protocol::Annotated;
224
225 #[test]
226 fn test_get_event_user_tag() {
227 let user = User {
231 id: Annotated::new("ident".to_owned().into()),
232 username: Annotated::new("username".to_owned().into()),
233 email: Annotated::new("email".to_owned()),
234 ip_address: Annotated::new("127.0.0.1".parse().unwrap()),
235 ..User::default()
236 };
237
238 assert_eq!(get_event_user_tag(&user).unwrap(), "id:ident");
239
240 let user = User {
241 username: Annotated::new("username".to_owned().into()),
242 email: Annotated::new("email".to_owned()),
243 ip_address: Annotated::new("127.0.0.1".parse().unwrap()),
244 ..User::default()
245 };
246
247 assert_eq!(get_event_user_tag(&user).unwrap(), "username:username");
248
249 let user = User {
250 email: Annotated::new("email".to_owned()),
251 ip_address: Annotated::new("127.0.0.1".parse().unwrap()),
252 ..User::default()
253 };
254
255 assert_eq!(get_event_user_tag(&user).unwrap(), "email:email");
256
257 let user = User {
258 ip_address: Annotated::new("127.0.0.1".parse().unwrap()),
259 ..User::default()
260 };
261
262 assert_eq!(get_event_user_tag(&user).unwrap(), "ip:127.0.0.1");
263
264 let user = User::default();
265
266 assert!(get_event_user_tag(&user).is_none());
267 }
268
269 #[test]
270 fn test_extracts_http_status_code_when_int() {
271 let span = Annotated::<Span>::from_json(
272 r#"{
273 "data": {
274 "http.response.status_code": 400
275 }
276 }"#,
277 )
278 .unwrap()
279 .into_value()
280 .unwrap();
281
282 let result = http_status_code_from_span(&span);
283
284 assert_eq!(result, Some("400".to_owned()));
285 }
286}