@@ -140,7 +140,6 @@ export default class extends WorkerEntrypoint<Env> {
140140 }
141141 }
142142
143- // TODO: Add observability to these methods
144143 async unstable_canFetch ( request : Request ) : Promise < boolean > {
145144 // TODO: Mock this with Miniflare
146145 this . env . JAEGER ??= mockJaegerBinding ( ) ;
@@ -162,28 +161,45 @@ export default class extends WorkerEntrypoint<Env> {
162161 cacheStatus : "HIT" | "MISS" ;
163162 } > {
164163 const performance = new PerformanceTimer ( this . env . UNSAFE_PERFORMANCE ) ;
165- const startTime = performance . now ( ) ;
166- const asset = await getAssetWithMetadataFromKV (
167- this . env . ASSETS_KV_NAMESPACE ,
168- eTag
169- ) ;
170- const endTime = performance . now ( ) ;
171- const assetFetchTime = endTime - startTime ;
172-
173- if ( ! asset || ! asset . value ) {
174- throw new Error (
175- `Requested asset ${ eTag } exists in the asset manifest but not in the KV namespace.`
164+ const jaeger = this . env . JAEGER ?? mockJaegerBinding ( ) ;
165+ return jaeger . enterSpan ( "unstable_getByETag" , async ( span ) => {
166+ const startTime = performance . now ( ) ;
167+ const asset = await getAssetWithMetadataFromKV (
168+ this . env . ASSETS_KV_NAMESPACE ,
169+ eTag
176170 ) ;
177- }
171+ const endTime = performance . now ( ) ;
172+ const assetFetchTime = endTime - startTime ;
173+
174+ if ( ! asset || ! asset . value ) {
175+ span . setTags ( {
176+ error : true ,
177+ } ) ;
178+ span . addLogs ( {
179+ error : `Requested asset ${ eTag } exists in the asset manifest but not in the KV namespace.` ,
180+ } ) ;
181+ throw new Error (
182+ `Requested asset ${ eTag } exists in the asset manifest but not in the KV namespace.`
183+ ) ;
184+ }
178185
179- return {
180- readableStream : asset . value ,
181- contentType : asset . metadata ?. contentType ,
182186 // KV does not yet provide a way to check if a value was fetched from cache
183187 // so we assume that if the fetch time is less than 100ms, it was a cache hit.
184188 // This is a reasonable assumption given the data we have and how KV works.
185- cacheStatus : assetFetchTime <= 100 ? "HIT" : "MISS" ,
186- } ;
189+ const cacheStatus = assetFetchTime <= 100 ? "HIT" : "MISS" ;
190+
191+ span . setTags ( {
192+ etag : eTag ,
193+ contentType : asset . metadata ?. contentType ?? "unknown" ,
194+ cacheStatus,
195+ } ) ;
196+
197+ return {
198+ readableStream : asset . value ,
199+ contentType : asset . metadata ?. contentType ,
200+ cacheStatus,
201+ } ;
202+ } ) ;
187203 }
188204
189205 async unstable_getByPathname (
@@ -194,12 +210,21 @@ export default class extends WorkerEntrypoint<Env> {
194210 contentType : string | undefined ;
195211 cacheStatus : "HIT" | "MISS" ;
196212 } | null > {
197- const eTag = await this . unstable_exists ( pathname , request ) ;
198- if ( ! eTag ) {
199- return null ;
200- }
213+ const jaeger = this . env . JAEGER ?? mockJaegerBinding ( ) ;
214+ return jaeger . enterSpan ( "unstable_getByPathname" , async ( span ) => {
215+ const eTag = await this . unstable_exists ( pathname , request ) ;
216+
217+ span . setTags ( {
218+ path : pathname ,
219+ found : eTag !== null ,
220+ } ) ;
201221
202- return this . unstable_getByETag ( eTag , request ) ;
222+ if ( ! eTag ) {
223+ return null ;
224+ }
225+
226+ return this . unstable_getByETag ( eTag , request ) ;
227+ } ) ;
203228 }
204229
205230 async unstable_exists (
@@ -208,25 +233,37 @@ export default class extends WorkerEntrypoint<Env> {
208233 ) : Promise < string | null > {
209234 const analytics = new ExperimentAnalytics ( this . env . EXPERIMENT_ANALYTICS ) ;
210235 const performance = new PerformanceTimer ( this . env . UNSAFE_PERFORMANCE ) ;
236+ const jaeger = this . env . JAEGER ?? mockJaegerBinding ( ) ;
237+ return jaeger . enterSpan ( "unstable_exists" , async ( span ) => {
238+ if (
239+ this . env . COLO_METADATA &&
240+ this . env . VERSION_METADATA &&
241+ this . env . CONFIG
242+ ) {
243+ analytics . setData ( {
244+ accountId : this . env . CONFIG . account_id ,
245+ experimentName : "manifest-read-timing" ,
246+ } ) ;
247+ }
211248
212- if (
213- this . env . COLO_METADATA &&
214- this . env . VERSION_METADATA &&
215- this . env . CONFIG
216- ) {
217- analytics . setData ( {
218- accountId : this . env . CONFIG . account_id ,
219- experimentName : "manifest-read-timing" ,
220- } ) ;
221- }
249+ const startTimeMs = performance . now ( ) ;
250+ try {
251+ const assetsManifest = new AssetsManifest ( this . env . ASSETS_MANIFEST ) ;
252+ const eTag = await assetsManifest . get ( pathname ) ;
222253
223- const startTimeMs = performance . now ( ) ;
224- try {
225- const assetsManifest = new AssetsManifest ( this . env . ASSETS_MANIFEST ) ;
226- return await assetsManifest . get ( pathname ) ;
227- } finally {
228- analytics . setData ( { manifestReadTime : performance . now ( ) - startTimeMs } ) ;
229- analytics . write ( ) ;
230- }
254+ span . setTags ( {
255+ path : pathname ,
256+ found : eTag !== null ,
257+ etag : eTag ?? "" ,
258+ } ) ;
259+
260+ return eTag ;
261+ } finally {
262+ analytics . setData ( {
263+ manifestReadTime : performance . now ( ) - startTimeMs ,
264+ } ) ;
265+ analytics . write ( ) ;
266+ }
267+ } ) ;
231268 }
232269}
0 commit comments