@@ -9,14 +9,21 @@ export function updateSpanFromRequest(span: Span, request: Request) {
99 span . updateName ( request . method ) ;
1010
1111 span . setAttribute ( "http.request.method" , request . method ) ;
12- const url = new URL ( request . url ) ;
1312 span . setAttribute ( "url.full" , request . url ) ;
14- span . setAttribute (
15- "url.scheme" ,
16- StringPrototypeSlice ( url . protocol , 0 , - 1 ) ,
17- ) ;
18- span . setAttribute ( "url.path" , url . pathname ) ;
19- span . setAttribute ( "url.query" , StringPrototypeSlice ( url . search , 1 ) ) ;
13+ // Malformed URLs (e.g. invalid hosts from internet scanners) would otherwise
14+ // throw here and crash the request handler before it can respond. Record the
15+ // raw URL above and skip the parsed attributes when parsing fails.
16+ try {
17+ const url = new URL ( request . url ) ;
18+ span . setAttribute (
19+ "url.scheme" ,
20+ StringPrototypeSlice ( url . protocol , 0 , - 1 ) ,
21+ ) ;
22+ span . setAttribute ( "url.path" , url . pathname ) ;
23+ span . setAttribute ( "url.query" , StringPrototypeSlice ( url . search , 1 ) ) ;
24+ } catch {
25+ span . setAttribute ( "url.parse_error" , "true" ) ;
26+ }
2027}
2128
2229export function updateSpanFromResponse ( span : Span , response : Response ) {
0 commit comments