Skip to content

Commit bcb7d10

Browse files
committed
Apply suggestions
1 parent a8b570e commit bcb7d10

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

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

Lines changed: 25 additions & 21 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,31 +225,33 @@ 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
}

relay-event-normalization/src/normalize/snapshots/relay_event_normalization__normalize__nel__tests__create_log_basic.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ expression: "SerializableAnnotated(&Annotated::new(log))"
5858
},
5959
"server.address": {
6060
"type": "string",
61-
"value": "192.168.1.1"
61+
"value": "example.com"
6262
},
6363
"url.domain": {
6464
"type": "string",

relay-event-normalization/src/normalize/snapshots/relay_event_normalization__normalize__nel__tests__create_log_dns_error.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ expression: "SerializableAnnotated(&Annotated::new(log))"
4646
},
4747
"server.address": {
4848
"type": "string",
49-
"value": "10.0.0.1"
49+
"value": "api.example.com"
5050
},
5151
"url.domain": {
5252
"type": "string",

0 commit comments

Comments
 (0)