Skip to main content

relay_event_normalization/normalize/
utils.rs

1//! **Deprecated.** Utilities for extracting common event fields.
2//!
3//! This utility module is being phased out. Functionality in this module should be moved to the
4//! specific normalization file requiring this data access.
5
6use 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
12/// Used to decide when to extract mobile-specific tags.
13pub const MOBILE_SDKS: [&str; 4] = [
14    "sentry.cocoa",
15    "sentry.dart.flutter",
16    "sentry.java.android",
17    "sentry.javascript.react-native",
18];
19
20/// Allowed value for main thread name.
21pub const MAIN_THREAD_NAME: &str = "main";
22
23/// Maximum duration of a mobile measurement in milliseconds.
24///
25/// Mobile measurements (app start, TTID, TTFD) that exceed this threshold are considered
26/// outliers and removed.
27pub const MAX_DURATION_MOBILE_MS: f64 = 180_000.0;
28
29/// Extract the HTTP status code from the span data.
30pub fn http_status_code_from_span(span: &Span) -> Option<String> {
31    // For SDKs which put the HTTP status code into the span data.
32    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    // For SDKs which put the HTTP status code into the span tags.
47    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
60/// Extracts the HTTP status code.
61pub fn extract_http_status_code(event: &Event) -> Option<String> {
62    // For SDKs which put the HTTP status code in the event tags.
63    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    // For SDKs which put the HTTP status code into the breadcrumbs data.
78    if let Some(breadcrumbs) = event.breadcrumbs.value()
79        && let Some(values) = breadcrumbs.values.value()
80    {
81        for breadcrumb in values {
82            // We need only the `http` type.
83            if let Some(crumb) = breadcrumb
84                .value()
85                .filter(|bc| bc.ty.as_str() == Some("http"))
86            {
87                // Try to get the status code om the map.
88                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    // For SDKs which put the HTTP status code in the `Response` context.
96    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
107/// Computes the transaction event's "user" tag from the event's user.
108pub 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
117/// Formats the "user" tag from the first available user identifier, as close as possible to how
118/// users are determined in the transactions dataset in Snuba. This should produce the exact same
119/// user counts as the `user` column in Discover for Transactions, barring:
120///
121/// * imprecision caused by HLL sketching in Snuba, which we don't have in events
122/// * hash collisions in `BucketValue::set_from_display`, which we don't have in events
123/// * MD5-collisions caused by `EventUser.hash_from_tag`, which we don't have in metrics
124///
125///   MD5 is used to efficiently look up the current event user for an event, and if there is a
126///   collision it seems that this code will fetch an event user with potentially different values
127///   for everything that is in `defaults`:
128///   <https://github.com/getsentry/sentry/blob/f621cd76da3a39836f34802ba9b35133bdfbe38b/src/sentry/event_manager.py#L1058-L1060>
129///
130/// The performance product runs a discover query such as `count_unique(user)`, which maps to two
131/// things:
132///
133/// * `user` metric for the metrics dataset
134/// * the "promoted tag" column `user` in the transactions clickhouse table
135///
136/// A promoted tag is a tag that snuba pulls out into its own column. In this case it pulls out the
137/// `sentry:user` tag from the event payload:
138/// <https://github.com/getsentry/snuba/blob/430763e67e30957c89126e62127e34051eb52fd6/snuba/datasets/transactions_processor.py#L151>
139///
140/// Sentry's processing pipeline defers to `sentry.models.EventUser` to produce the `sentry:user` tag
141/// here: <https://github.com/getsentry/sentry/blob/f621cd76da3a39836f34802ba9b35133bdfbe38b/src/sentry/event_manager.py#L790-L794>
142///
143/// `sentry.models.eventuser.KEYWORD_MAP` determines which attributes are looked up in which order, here:
144/// <https://github.com/getsentry/sentry/blob/f621cd76da3a39836f34802ba9b35133bdfbe38b/src/sentry/models/eventuser.py#L18>
145/// If its order is changed, this function needs to be changed.
146pub(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
171/// Returns a normalized `op` from the given trace context.
172pub fn extract_transaction_op(trace_context: &TraceContext) -> Option<String> {
173    let op = trace_context.op.value()?;
174    if op == "default" {
175        // This was likely set by normalization, so let's treat it as None
176        // See https://github.com/getsentry/relay/blob/bb2ac4ee82c25faa07a6d078f93d22d799cfc5d1/relay-general/src/store/transactions.rs#L96
177
178        // Note that this is the opposite behavior of what we do for transaction.status, where
179        // we coalesce None to "unknown".
180        return None;
181    }
182    Some(op.to_string())
183}
184
185/// The Gauss error function.
186///
187/// See <https://en.wikipedia.org/wiki/Error_function>.
188fn erf(x: f64) -> f64 {
189    // constants
190    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    // Save the sign of x
197    let sign = if x < 0.0 { -1.0 } else { 1.0 };
198    let x = x.abs();
199    // A&S formula 7.1.26
200    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
205/// Sigma function for CDF score calculation.
206fn calculate_cdf_sigma(p10: f64, p50: f64) -> f64 {
207    (p10.ln() - p50.ln()).abs() / (SQRT_2 * 0.9061938024368232)
208}
209
210/// Computes the [cumulative distribution function](https://en.wikipedia.org/wiki/Cumulative_distribution_function)
211/// of a [log-normal distribution](https://en.wikipedia.org/wiki/Log-normal_distribution) with the given p10 and p50.
212///
213/// In other words, if `X` is log-normally distributed with 10th and 50th percentile `p10` and `p50`,
214/// then `calculate_cdf_score(x, p10, p50) = P(X ≤ x)`.
215pub 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        // Note: If this order changes,
228        // https://github.com/getsentry/sentry/blob/f621cd76da3a39836f34802ba9b35133bdfbe38b/src/sentry/models/eventuser.py#L18
229        // has to be changed. Though it is probably not a good idea!
230        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}