@@ -5,7 +5,7 @@ import { Buffer } from 'node:buffer'
55import { join } from 'node:path'
66import { join as posixJoin } from 'node:path/posix'
77
8- import { type Span } from '@opentelemetry/api '
8+ import type { Span } from '@netlify/otel/opentelemetry '
99import type { PrerenderManifest } from 'next/dist/build/index.js'
1010import { NEXT_CACHE_TAGS_HEADER } from 'next/dist/lib/constants.js'
1111
@@ -32,7 +32,7 @@ import {
3232 type RevalidateTagDurations ,
3333 type TagStaleOrExpiredStatus ,
3434} from './tags-handler.cjs'
35- import { getTracer , recordWarning } from './tracer.cjs'
35+ import { getTracer , recordWarning , withActiveSpan } from './tracer.cjs'
3636
3737let memoizedPrerenderManifest : PrerenderManifest
3838
@@ -74,7 +74,7 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
7474 private captureResponseCacheLastModified (
7575 cacheValue : NetlifyCacheHandlerValue ,
7676 key : string ,
77- getCacheKeySpan : Span ,
77+ getCacheKeySpan ? : Span ,
7878 ) {
7979 if ( cacheValue . value ?. kind === 'FETCH' ) {
8080 return
@@ -262,17 +262,17 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
262262 async get (
263263 ...args : Parameters < CacheHandlerForMultipleVersions [ 'get' ] >
264264 ) : ReturnType < CacheHandlerForMultipleVersions [ 'get' ] > {
265- return this . tracer . withActiveSpan ( 'get cache key' , async ( span ) => {
265+ return withActiveSpan ( this . tracer , 'get cache key' , async ( span ) => {
266266 const [ key , context = { } ] = args
267267 getLogger ( ) . debug ( `[NetlifyCacheHandler.get]: ${ key } ` )
268268
269- span . setAttributes ( { key } )
269+ span ? .setAttributes ( { key } )
270270
271271 const blob = await this . cacheStore . get < NetlifyCacheHandlerValue > ( key , 'blobStore.get' )
272272
273273 // if blob is null then we don't have a cache entry
274274 if ( ! blob ) {
275- span . addEvent ( 'Cache miss' , { key } )
275+ span ? .addEvent ( 'Cache miss' , { key } )
276276 return null
277277 }
278278
@@ -281,7 +281,7 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
281281 if ( getRequestContext ( ) ?. isBackgroundRevalidation && typeof ttl === 'number' && ttl < 0 ) {
282282 // background revalidation request should allow data that is not yet stale,
283283 // but opt to discard STALE data, so that Next.js generate fresh response
284- span . addEvent ( 'Discarding stale entry due to SWR background revalidation request' , {
284+ span ? .addEvent ( 'Discarding stale entry due to SWR background revalidation request' , {
285285 key,
286286 ttl,
287287 } )
@@ -303,14 +303,14 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
303303 )
304304
305305 if ( expiredByTags ) {
306- span . addEvent ( 'Expired' , { expiredByTags, key, ttl } )
306+ span ? .addEvent ( 'Expired' , { expiredByTags, key, ttl } )
307307 return null
308308 }
309309
310310 this . captureResponseCacheLastModified ( blob , key , span )
311311
312312 if ( staleByTags ) {
313- span . addEvent ( 'Stale' , { staleByTags, key, ttl } )
313+ span ? .addEvent ( 'Stale' , { staleByTags, key, ttl } )
314314 // note that we modify this after we capture last modified to ensure that Age is correct
315315 // but we still let Next.js know that entry is stale
316316 blob . lastModified = - 1 // indicate that the entry is stale
@@ -324,7 +324,7 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
324324
325325 switch ( blob . value ?. kind ) {
326326 case 'FETCH' :
327- span . addEvent ( 'FETCH' , {
327+ span ? .addEvent ( 'FETCH' , {
328328 lastModified : blob . lastModified ,
329329 revalidate : context . revalidate ,
330330 ttl,
@@ -336,7 +336,7 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
336336
337337 case 'ROUTE' :
338338 case 'APP_ROUTE' : {
339- span . addEvent ( blob . value ?. kind , {
339+ span ? .addEvent ( blob . value ?. kind , {
340340 lastModified : blob . lastModified ,
341341 status : blob . value . status ,
342342 revalidate : blob . value . revalidate ,
@@ -362,7 +362,7 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
362362 requestContext . pageHandlerRevalidate = revalidate
363363 }
364364
365- span . addEvent ( blob . value ?. kind , { lastModified : blob . lastModified , revalidate, ttl } )
365+ span ? .addEvent ( blob . value ?. kind , { lastModified : blob . lastModified , revalidate, ttl } )
366366
367367 await this . injectEntryToPrerenderManifest ( key , blob . value )
368368
@@ -379,7 +379,7 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
379379
380380 const { revalidate, rscData, segmentData, ...restOfPageValue } = blob . value
381381
382- span . addEvent ( blob . value ?. kind , { lastModified : blob . lastModified , revalidate, ttl } )
382+ span ? .addEvent ( blob . value ?. kind , { lastModified : blob . lastModified , revalidate, ttl } )
383383
384384 await this . injectEntryToPrerenderManifest ( key , blob . value )
385385
@@ -400,7 +400,7 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
400400 }
401401 }
402402 default :
403- span . recordException ( new Error ( `Unknown cache entry kind: ${ blob . value ?. kind } ` ) )
403+ span ? .recordException ( new Error ( `Unknown cache entry kind: ${ blob . value ?. kind } ` ) )
404404 }
405405 return null
406406 } )
@@ -452,10 +452,10 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
452452 }
453453
454454 async set ( ...args : Parameters < CacheHandlerForMultipleVersions [ 'set' ] > ) {
455- return this . tracer . withActiveSpan ( 'set cache key' , async ( span ) => {
455+ return withActiveSpan ( this . tracer , 'set cache key' , async ( span ?: Span ) => {
456456 const [ key , data , context ] = args
457457 const lastModified = Date . now ( )
458- span . setAttributes ( { key, lastModified } )
458+ span ? .setAttributes ( { key, lastModified } )
459459
460460 getLogger ( ) . debug ( `[NetlifyCacheHandler.set]: ${ key } ` )
461461
0 commit comments