relay_server/utils/
statsd.rs

1use relay_base_schema::events::EventType;
2use relay_event_normalization::utils::extract_http_status_code;
3use relay_event_schema::protocol::{Event, TransactionSource};
4use relay_protocol::{Annotated, RemarkType};
5
6use crate::{envelope::ClientName, statsd::RelayCounters};
7
8/// Maps the event's transaction source to a low-cardinality statsd tag.
9pub fn transaction_source_tag(event: &Event) -> &str {
10    let source = event
11        .transaction_info
12        .value()
13        .and_then(|i| i.source.value());
14    match source {
15        None => "none",
16        Some(TransactionSource::Other(_)) => "other",
17        Some(source) => source.as_str(),
18    }
19}
20
21/// Maps the event's platform to a low-cardinality statsd tag.
22pub fn platform_tag(event: &Event) -> &'static str {
23    let platform = event.platform.as_str();
24
25    match platform {
26        Some("cocoa") => "cocoa",
27        Some("csharp") => "csharp",
28        Some("edge") => "edge",
29        Some("go") => "go",
30        Some("java") => "java",
31        Some("javascript") => "javascript",
32        Some("julia") => "julia",
33        Some("native") => "native",
34        Some("node") => "node",
35        Some("objc") => "objc",
36        Some("perl") => "perl",
37        Some("php") => "php",
38        Some("python") => "python",
39        Some("ruby") => "ruby",
40        Some("swift") => "swift",
41        Some(_) => "other",
42        None => "missing",
43    }
44}
45
46/// Maps a client name to a low-cardinality statsd tag.
47pub fn client_name_tag(client_name: ClientName<'_>) -> &str {
48    match client_name {
49        ClientName::Other(_) => "other",
50        well_known => well_known.as_str(),
51    }
52}
53
54/// Log statsd metrics about transaction name modifications.
55///
56/// We have to look at event & meta before and after the modification is made,
57/// so we delegate to `f` in the middle of the function.
58pub fn log_transaction_name_metrics<F, R>(event: &mut Annotated<Event>, mut f: F) -> R
59where
60    F: FnMut(&mut Annotated<Event>) -> R,
61{
62    let Some(inner) = event.value() else {
63        return f(event);
64    };
65
66    if inner.ty.value() != Some(&EventType::Transaction) {
67        return f(event);
68    }
69
70    let old_source = transaction_source_tag(inner).to_owned();
71    let old_remarks = inner.transaction.meta().iter_remarks().count();
72
73    let res = f(event);
74
75    // Need to reborrow event so the reference's lifetime does not overlap with `f`:
76    let Some(inner) = event.value() else {
77        return res;
78    };
79
80    let mut pattern_based_changes = false;
81    let mut rule_based_changes = false;
82    let remarks = inner.transaction.meta().iter_remarks().skip(old_remarks);
83    for remark in remarks {
84        if remark.ty() == RemarkType::Substituted {
85            if remark.range().is_some() {
86                pattern_based_changes = true;
87            } else {
88                rule_based_changes = true;
89            }
90        }
91    }
92
93    let changes = match (pattern_based_changes, rule_based_changes) {
94        (true, true) => "both",
95        (true, false) => "pattern",
96        (false, true) => "rule",
97        (false, false) => "none",
98    };
99
100    let new_source = transaction_source_tag(inner);
101    let is_404 = extract_http_status_code(inner).is_some_and(|s| s == "404");
102
103    relay_statsd::metric!(
104        counter(RelayCounters::TransactionNameChanges) += 1,
105        source_in = old_source.as_str(),
106        changes = changes,
107        source_out = new_source,
108        is_404 = if is_404 { "true" } else { "false" },
109    );
110
111    res
112}