Skip to content

Commit aab68c4

Browse files
committed
Apply suggestions
1 parent a8b570e commit aab68c4

File tree

1 file changed

+29
-22
lines changed
  • relay-event-normalization/src/normalize

1 file changed

+29
-22
lines changed

relay-event-normalization/src/normalize/nel.rs

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use relay_event_schema::protocol::{
55
Attributes, NetworkReportRaw, OurLog, OurLogLevel, Timestamp, TraceId,
66
};
77
use relay_protocol::Annotated;
8+
use std::borrow::Cow;
89
use std::collections::HashMap;
910
use std::sync::LazyLock;
1011
use url::Url;
@@ -170,9 +171,13 @@ fn extract_server_address(server_address: &str) -> String {
170171

171172
/// Gets the human-readable description for a NEL error type
172173
fn get_nel_culprit(error_type: &str) -> Option<&'static str> {
173-
use std::borrow::Cow;
174+
NEL_CULPRITS_MAP.get(error_type).copied()
175+
}
174176

175-
fn get_nel_culprit_formatted(error_type: &str, status_code: Option<u16>) -> Option<Cow<'static, str>> {
177+
fn get_nel_culprit_formatted(
178+
error_type: &str,
179+
status_code: Option<u16>,
180+
) -> Option<Cow<'static, str>> {
176181
let template = get_nel_culprit(error_type)?;
177182

178183
if error_type == "http.error" {
@@ -182,15 +187,12 @@ fn get_nel_culprit_formatted(error_type: &str, status_code: Option<u16>) -> Opti
182187
Some(Cow::Borrowed(template))
183188
}
184189
}
185-
Some(template.replace("{}", &code.to_string()))
186-
} else {
187-
Some(template.to_owned())
188-
}
189-
}
190190

191191
/// Creates a human-readable message for a NEL report
192192
fn create_message(error_type: &str, status_code: Option<u16>) -> String {
193-
get_nel_culprit_formatted(error_type, status_code).unwrap_or_else(|| error_type.to_owned())
193+
get_nel_culprit_formatted(error_type, status_code)
194+
.map(|cow| cow.into_owned())
195+
.unwrap_or_else(|| error_type.to_owned())
194196
}
195197

196198
/// Creates a [`OurLog`] from the provided [`NetworkReportRaw`].
@@ -223,39 +225,44 @@ pub fn create_log(nel: Annotated<NetworkReportRaw>, received_at: DateTime<Utc>)
223225

224226
macro_rules! add_string_attribute {
225227
($name:literal, $value:expr) => {{
226-
attributes.insert($name.to_owned(), $value.to_string());
228+
let val = $value.to_string();
229+
if !val.is_empty() {
230+
attributes.insert($name.to_owned(), val);
231+
}
227232
}};
228-
macro_rules! add_string_attribute {
229-
($name:literal, $value:expr) => {{
230-
let val = $value.to_string();
231-
if !val.is_empty() {
232-
attributes.insert($name.to_owned(), val);
233-
}
234-
}};
235-
}
233+
}
234+
236235
add_string_attribute!("sentry.origin", "auto.http.browser_report.nel");
237236
add_string_attribute!("browser.report.type", "network-error");
238-
add_attribute!("url.domain", url);
239-
add_attribute!("url.full", raw_report.url);
237+
238+
// Handle URL and extract server address if available
239+
add_attribute!("url.full", raw_report.url.clone());
240+
if let Some(url_str) = raw_report.url.value() {
241+
let server_address = extract_server_address(url_str);
242+
add_string_attribute!("url.domain", &server_address);
243+
}
240244
add_attribute!("http.request.duration", body.elapsed_time);
241245
add_attribute!("http.request.method", body.method);
242246
add_attribute!("http.request.header.referer", body.referrer.clone());
243247
add_attribute!("http.response.status_code", body.status_code);
244248
// Split protocol into name and version components
245249
if let Some(protocol) = body.protocol.value() {
246250
let parts: Vec<&str> = protocol.split('/').collect();
247-
if !parts.is_empty() {
251+
if !parts.is_empty() && !parts[0].is_empty() {
248252
// e.g. "http"
249253
add_string_attribute!("network.protocol.name", parts[0]);
250-
if parts.len() > 1 {
254+
if parts.len() > 1 && !parts[1].is_empty() {
251255
// e.g. "1.1"
252256
add_string_attribute!("network.protocol.version", parts[1]);
253257
}
254258
}
255259
}
256260
// Server domain name if available without reverse DNS lookup; otherwise,
257261
// IP address or Unix domain socket name.
258-
add_attribute!("server.address", server_address);
262+
if let Some(url_str) = raw_report.url.value() {
263+
let server_address = extract_server_address(url_str);
264+
add_string_attribute!("server.address", &server_address);
265+
}
259266

260267
// NEL-specific attributes
261268
add_attribute!("nel.referrer", body.referrer);

0 commit comments

Comments
 (0)