@@ -27,15 +27,30 @@ export const handleRequest = async (
2727 const intent = await getIntent ( decodedPathname , configuration , exists ) ;
2828
2929 if ( ! intent ) {
30- return new NotFoundResponse ( ) ;
30+ return env . JAEGER . enterSpan ( "no_intent" , ( span ) => {
31+ span . setTags ( {
32+ decodedPathname,
33+ configuration : JSON . stringify ( configuration ) ,
34+ status : 404 ,
35+ } ) ;
36+
37+ return new NotFoundResponse ( ) ;
38+ } ) ;
3139 }
3240
3341 // if there was a POST etc. to a route without an asset
3442 // this should be passed onto a user worker if one exists
3543 // so prioritise returning a 404 over 405?
3644 const method = request . method . toUpperCase ( ) ;
3745 if ( ! [ "GET" , "HEAD" ] . includes ( method ) ) {
38- return new MethodNotAllowedResponse ( ) ;
46+ return env . JAEGER . enterSpan ( "method_not_allowed" , ( span ) => {
47+ span . setTags ( {
48+ method,
49+ status : 405 ,
50+ } ) ;
51+
52+ return new MethodNotAllowedResponse ( ) ;
53+ } ) ;
3954 }
4055
4156 const decodedDestination = intent . redirect ?? decodedPathname ;
@@ -47,11 +62,29 @@ export const handleRequest = async (
4762 * We combine this with other redirects (e.g. for html_handling) to avoid multiple redirects.
4863 */
4964 if ( ( encodedDestination !== pathname && intent . asset ) || intent . redirect ) {
50- return new TemporaryRedirectResponse ( encodedDestination + search ) ;
65+ return env . JAEGER . enterSpan ( "redirect" , ( span ) => {
66+ span . setTags ( {
67+ originalPath : pathname ,
68+ location :
69+ encodedDestination !== pathname
70+ ? encodedDestination
71+ : intent . redirect ?? "<unknown>" ,
72+ status : 307 ,
73+ } ) ;
74+
75+ return new TemporaryRedirectResponse ( encodedDestination + search ) ;
76+ } ) ;
5177 }
5278
5379 if ( ! intent . asset ) {
54- return new InternalServerErrorResponse ( new Error ( "Unknown action" ) ) ;
80+ return env . JAEGER . enterSpan ( "unknown_action" , ( span ) => {
81+ span . setTags ( {
82+ pathname,
83+ status : 500 ,
84+ } ) ;
85+
86+ return new InternalServerErrorResponse ( new Error ( "Unknown action" ) ) ;
87+ } ) ;
5588 }
5689
5790 const asset = await env . JAEGER . enterSpan ( "getByETag" , async ( span ) => {
@@ -70,16 +103,31 @@ export const handleRequest = async (
70103 const weakETag = `W/${ strongETag } ` ;
71104 const ifNoneMatch = request . headers . get ( "If-None-Match" ) || "" ;
72105 if ( [ weakETag , strongETag ] . includes ( ifNoneMatch ) ) {
73- return new NotModifiedResponse ( null , { headers } ) ;
74- }
106+ return env . JAEGER . enterSpan ( "matched_etag" , ( span ) => {
107+ span . setTags ( {
108+ matchedEtag : ifNoneMatch ,
109+ status : 304 ,
110+ } ) ;
75111
76- const body = method === "HEAD" ? null : asset . readableStream ;
77- switch ( intent . asset . status ) {
78- case 404 :
79- return new NotFoundResponse ( body , { headers } ) ;
80- case 200 :
81- return new OkResponse ( body , { headers } ) ;
112+ return new NotModifiedResponse ( null , { headers } ) ;
113+ } ) ;
82114 }
115+
116+ return env . JAEGER . enterSpan ( "response" , ( span ) => {
117+ span . setTags ( {
118+ etag : intent . asset . eTag ,
119+ status : intent . asset . status ,
120+ head : method === "HEAD" ,
121+ } ) ;
122+
123+ const body = method === "HEAD" ? null : asset . readableStream ;
124+ switch ( intent . asset . status ) {
125+ case 404 :
126+ return new NotFoundResponse ( body , { headers } ) ;
127+ case 200 :
128+ return new OkResponse ( body , { headers } ) ;
129+ }
130+ } ) ;
83131} ;
84132
85133type Intent =
@@ -90,6 +138,7 @@ type Intent =
90138 | { asset : null ; redirect : string }
91139 | null ;
92140
141+ // TODO: Trace this
93142export const getIntent = async (
94143 pathname : string ,
95144 configuration : Required < AssetConfig > ,
0 commit comments