@@ -5,6 +5,7 @@ use relay_event_schema::protocol::{
5
5
Attributes , NetworkReportRaw , OurLog , OurLogLevel , Timestamp , TraceId ,
6
6
} ;
7
7
use relay_protocol:: Annotated ;
8
+ use std:: borrow:: Cow ;
8
9
use std:: collections:: HashMap ;
9
10
use std:: sync:: LazyLock ;
10
11
use url:: Url ;
@@ -170,9 +171,13 @@ fn extract_server_address(server_address: &str) -> String {
170
171
171
172
/// Gets the human-readable description for a NEL error type
172
173
fn get_nel_culprit ( error_type : & str ) -> Option < & ' static str > {
173
- use std:: borrow:: Cow ;
174
+ NEL_CULPRITS_MAP . get ( error_type) . copied ( )
175
+ }
174
176
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 > > {
176
181
let template = get_nel_culprit ( error_type) ?;
177
182
178
183
if error_type == "http.error" {
@@ -182,15 +187,12 @@ fn get_nel_culprit_formatted(error_type: &str, status_code: Option<u16>) -> Opti
182
187
Some ( Cow :: Borrowed ( template) )
183
188
}
184
189
}
185
- Some ( template. replace ( "{}" , & code. to_string ( ) ) )
186
- } else {
187
- Some ( template. to_owned ( ) )
188
- }
189
- }
190
190
191
191
/// Creates a human-readable message for a NEL report
192
192
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 ( ) )
194
196
}
195
197
196
198
/// Creates a [`OurLog`] from the provided [`NetworkReportRaw`].
@@ -223,39 +225,44 @@ pub fn create_log(nel: Annotated<NetworkReportRaw>, received_at: DateTime<Utc>)
223
225
224
226
macro_rules! add_string_attribute {
225
227
( $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
+ }
227
232
} } ;
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
+
236
235
add_string_attribute ! ( "sentry.origin" , "auto.http.browser_report.nel" ) ;
237
236
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
+ }
240
244
add_attribute ! ( "http.request.duration" , body. elapsed_time) ;
241
245
add_attribute ! ( "http.request.method" , body. method) ;
242
246
add_attribute ! ( "http.request.header.referer" , body. referrer. clone( ) ) ;
243
247
add_attribute ! ( "http.response.status_code" , body. status_code) ;
244
248
// Split protocol into name and version components
245
249
if let Some ( protocol) = body. protocol . value ( ) {
246
250
let parts: Vec < & str > = protocol. split ( '/' ) . collect ( ) ;
247
- if !parts. is_empty ( ) {
251
+ if !parts. is_empty ( ) && !parts [ 0 ] . is_empty ( ) {
248
252
// e.g. "http"
249
253
add_string_attribute ! ( "network.protocol.name" , parts[ 0 ] ) ;
250
- if parts. len ( ) > 1 {
254
+ if parts. len ( ) > 1 && !parts [ 1 ] . is_empty ( ) {
251
255
// e.g. "1.1"
252
256
add_string_attribute ! ( "network.protocol.version" , parts[ 1 ] ) ;
253
257
}
254
258
}
255
259
}
256
260
// Server domain name if available without reverse DNS lookup; otherwise,
257
261
// 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
+ }
259
266
260
267
// NEL-specific attributes
261
268
add_attribute ! ( "nel.referrer" , body. referrer) ;
0 commit comments