relay_server/utils/
statsd.rs1use 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
8pub 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
21pub 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
46pub fn client_name_tag(client_name: ClientName<'_>) -> &'static str {
48 client_name.as_static_str().unwrap_or("other")
49}
50
51pub fn log_transaction_name_metrics<F, R>(event: &mut Annotated<Event>, mut f: F) -> R
56where
57 F: FnMut(&mut Annotated<Event>) -> R,
58{
59 let Some(inner) = event.value() else {
60 return f(event);
61 };
62
63 if inner.ty.value() != Some(&EventType::Transaction) {
64 return f(event);
65 }
66
67 let old_source = transaction_source_tag(inner).to_owned();
68 let old_remarks = inner.transaction.meta().iter_remarks().count();
69
70 let res = f(event);
71
72 let Some(inner) = event.value() else {
74 return res;
75 };
76
77 let mut pattern_based_changes = false;
78 let mut rule_based_changes = false;
79 let remarks = inner.transaction.meta().iter_remarks().skip(old_remarks);
80 for remark in remarks {
81 if remark.ty() == RemarkType::Substituted {
82 if remark.range().is_some() {
83 pattern_based_changes = true;
84 } else {
85 rule_based_changes = true;
86 }
87 }
88 }
89
90 let changes = match (pattern_based_changes, rule_based_changes) {
91 (true, true) => "both",
92 (true, false) => "pattern",
93 (false, true) => "rule",
94 (false, false) => "none",
95 };
96
97 let new_source = transaction_source_tag(inner);
98 let is_404 = extract_http_status_code(inner).is_some_and(|s| s == "404");
99
100 relay_statsd::metric!(
101 counter(RelayCounters::TransactionNameChanges) += 1,
102 source_in = old_source.as_str(),
103 changes = changes,
104 source_out = new_source,
105 is_404 = if is_404 { "true" } else { "false" },
106 );
107
108 res
109}