11import { WorkerEntrypoint } from "cloudflare:workers" ;
22import { PerformanceTimer } from "../../utils/performance" ;
3- import { InternalServerErrorResponse } from "../../utils/responses" ;
43import { setupSentry } from "../../utils/sentry" ;
54import { mockJaegerBinding } from "../../utils/tracing" ;
65import { Analytics } from "./analytics" ;
76import { AssetsManifest } from "./assets-manifest" ;
87import { applyConfigurationDefaults } from "./configuration" ;
98import { ExperimentAnalytics } from "./experiment-analytics" ;
109import { canFetch , handleRequest } from "./handler" ;
10+ import { handleError , submitMetrics } from "./utils/final-operations" ;
1111import { getAssetWithMetadataFromKV } from "./utils/kv" ;
1212import type {
1313 AssetConfig ,
@@ -16,44 +16,6 @@ import type {
1616 UnsafePerformanceTimer ,
1717} from "../../utils/types" ;
1818import type { Environment , ReadyAnalytics } from "./types" ;
19- import type { Toucan } from "toucan-js" ;
20-
21- function handleError (
22- sentry : Toucan | undefined ,
23- analytics : Analytics ,
24- err : unknown
25- ) {
26- try {
27- const response = new InternalServerErrorResponse ( err as Error ) ;
28-
29- // Log to Sentry if we can
30- if ( sentry ) {
31- sentry . captureException ( err ) ;
32- }
33-
34- if ( err instanceof Error ) {
35- analytics . setData ( { error : err . message } ) ;
36- }
37-
38- return response ;
39- } catch ( e ) {
40- console . error ( "Error handling error" , e ) ;
41- return new InternalServerErrorResponse ( e as Error ) ;
42- }
43- }
44-
45- function submitMetrics (
46- analytics : Analytics ,
47- performance : PerformanceTimer ,
48- startTimeMs : number
49- ) {
50- try {
51- analytics . setData ( { requestTime : performance . now ( ) - startTimeMs } ) ;
52- analytics . write ( ) ;
53- } catch ( e ) {
54- console . error ( "Error submitting metrics" , e ) ;
55- }
56- }
5719
5820export type Env = {
5921 /*
@@ -157,7 +119,8 @@ export default class extends WorkerEntrypoint<Env> {
157119 this . env ,
158120 config ,
159121 this . unstable_exists . bind ( this ) ,
160- this . unstable_getByETag . bind ( this )
122+ this . unstable_getByETag . bind ( this ) ,
123+ analytics
161124 ) ;
162125
163126 analytics . setData ( { status : response . status } ) ;
@@ -171,7 +134,7 @@ export default class extends WorkerEntrypoint<Env> {
171134 }
172135 }
173136
174- // TODO: Trace unstable methods
137+ // TODO: Add observability to these methods
175138 async unstable_canFetch ( request : Request ) : Promise < boolean > {
176139 // TODO: Mock this with Miniflare
177140 this . env . JAEGER ??= mockJaegerBinding ( ) ;
@@ -187,11 +150,16 @@ export default class extends WorkerEntrypoint<Env> {
187150 async unstable_getByETag ( eTag : string ) : Promise < {
188151 readableStream : ReadableStream ;
189152 contentType : string | undefined ;
153+ cacheStatus : "HIT" | "MISS" ;
190154 } > {
155+ const performance = new PerformanceTimer ( this . env . UNSAFE_PERFORMANCE ) ;
156+ const startTime = performance . now ( ) ;
191157 const asset = await getAssetWithMetadataFromKV (
192158 this . env . ASSETS_KV_NAMESPACE ,
193159 eTag
194160 ) ;
161+ const endTime = performance . now ( ) ;
162+ const assetFetchTime = endTime - startTime ;
195163
196164 if ( ! asset || ! asset . value ) {
197165 throw new Error (
@@ -202,12 +170,17 @@ export default class extends WorkerEntrypoint<Env> {
202170 return {
203171 readableStream : asset . value ,
204172 contentType : asset . metadata ?. contentType ,
173+ // KV does not yet provide a way to check if a value was fetched from cache
174+ // so we assume that if the fetch time is less than 100ms, it was a cache hit.
175+ // This is a reasonable assumption given the data we have and how KV works.
176+ cacheStatus : assetFetchTime <= 100 ? "HIT" : "MISS" ,
205177 } ;
206178 }
207179
208180 async unstable_getByPathname ( pathname : string ) : Promise < {
209181 readableStream : ReadableStream ;
210182 contentType : string | undefined ;
183+ cacheStatus : "HIT" | "MISS" ;
211184 } | null > {
212185 const eTag = await this . unstable_exists ( pathname ) ;
213186 if ( ! eTag ) {
0 commit comments