@@ -5,7 +5,7 @@ import { Buffer } from 'node:buffer'
55
66import { getDeployStore , Store } from '@netlify/blobs'
77import { purgeCache } from '@netlify/functions'
8- import { trace , type Span , SpanStatusCode } from '@opentelemetry/api'
8+ import { type Span } from '@opentelemetry/api'
99import { NEXT_CACHE_TAGS_HEADER } from 'next/dist/lib/constants.js'
1010import type {
1111 CacheHandler ,
@@ -15,6 +15,7 @@ import type {
1515} from 'next/dist/server/lib/incremental-cache/index.js'
1616
1717import { getRequestContext } from './request-context.cjs'
18+ import { getTracer } from './tracer.cjs'
1819
1920type TagManifest = { revalidatedAt : number }
2021
@@ -26,7 +27,7 @@ export class NetlifyCacheHandler implements CacheHandler {
2627 options : CacheHandlerContext
2728 revalidatedTags : string [ ]
2829 blobStore : Store
29- tracer = trace . getTracer ( 'Netlify Cache Handler' )
30+ tracer = getTracer ( )
3031 tagManifestsFetchedFromBlobStoreInCurrentRequest : TagManifestBlobCache
3132
3233 constructor ( options : CacheHandlerContext ) {
@@ -90,77 +91,67 @@ export class NetlifyCacheHandler implements CacheHandler {
9091 }
9192
9293 async get ( ...args : Parameters < CacheHandler [ 'get' ] > ) : ReturnType < CacheHandler [ 'get' ] > {
93- return this . tracer . startActiveSpan ( 'get cache key' , async ( span ) => {
94- try {
95- const [ key , ctx = { } ] = args
96- console . debug ( `[NetlifyCacheHandler.get]: ${ key } ` )
97-
98- const blobKey = await this . encodeBlobKey ( key )
99- span . setAttributes ( { key, blobKey } )
100- const blob = ( await this . blobStore . get ( blobKey , {
101- type : 'json' ,
102- } ) ) as CacheHandlerValue | null
94+ return this . tracer . withActiveSpan ( 'get cache key' , async ( span ) => {
95+ const [ key , ctx = { } ] = args
96+ console . debug ( `[NetlifyCacheHandler.get]: ${ key } ` )
10397
104- // if blob is null then we don't have a cache entry
105- if ( ! blob ) {
106- span . addEvent ( 'Cache miss' , { key, blobKey } )
107- return null
108- }
98+ const blobKey = await this . encodeBlobKey ( key )
99+ span . setAttributes ( { key, blobKey } )
109100
110- const staleByTags = await this . checkCacheEntryStaleByTags ( blob , ctx . tags , ctx . softTags )
101+ const blob = ( await this . tracer . withActiveSpan ( 'blobStore.get' , async ( blobGetSpan ) => {
102+ blobGetSpan . setAttributes ( { key, blobKey } )
103+ return await this . blobStore . get ( blobKey , {
104+ type : 'json' ,
105+ } )
106+ } ) ) as CacheHandlerValue | null
111107
112- if ( staleByTags ) {
113- span . addEvent ( 'Stale' , { staleByTags } )
114- return null
115- }
108+ // if blob is null then we don't have a cache entry
109+ if ( ! blob ) {
110+ span . addEvent ( 'Cache miss' , { key, blobKey } )
111+ return null
112+ }
116113
117- this . captureResponseCacheLastModified ( blob , key , span )
114+ const staleByTags = await this . checkCacheEntryStaleByTags ( blob , ctx . tags , ctx . softTags )
118115
119- switch ( blob . value ?. kind ) {
120- case 'FETCH' :
121- span . addEvent ( 'FETCH' , { lastModified : blob . lastModified , revalidate : ctx . revalidate } )
122- return {
123- lastModified : blob . lastModified ,
124- value : blob . value ,
125- }
126-
127- case 'ROUTE' :
128- span . addEvent ( 'ROUTE' , { lastModified : blob . lastModified , status : blob . value . status } )
129- return {
130- lastModified : blob . lastModified ,
131- value : {
132- ...blob . value ,
133- body : Buffer . from ( blob . value . body as unknown as string , 'base64' ) ,
134- } ,
135- }
136- case 'PAGE' :
137- span . addEvent ( 'PAGE' , { lastModified : blob . lastModified } )
138- return {
139- lastModified : blob . lastModified ,
140- value : blob . value ,
141- }
142- default :
143- span . recordException ( new Error ( `Unknown cache entry kind: ${ blob . value ?. kind } ` ) )
144- // TODO: system level logging not implemented
145- }
116+ if ( staleByTags ) {
117+ span . addEvent ( 'Stale' , { staleByTags } )
146118 return null
147- } catch ( error ) {
148- if ( error instanceof Error ) {
149- span . recordException ( error )
150- }
151- span . setStatus ( {
152- code : SpanStatusCode . ERROR ,
153- message : error instanceof Error ? error . message : String ( error ) ,
154- } )
155- throw error
156- } finally {
157- span . end ( )
158119 }
120+
121+ this . captureResponseCacheLastModified ( blob , key , span )
122+
123+ switch ( blob . value ?. kind ) {
124+ case 'FETCH' :
125+ span . addEvent ( 'FETCH' , { lastModified : blob . lastModified , revalidate : ctx . revalidate } )
126+ return {
127+ lastModified : blob . lastModified ,
128+ value : blob . value ,
129+ }
130+
131+ case 'ROUTE' :
132+ span . addEvent ( 'ROUTE' , { lastModified : blob . lastModified , status : blob . value . status } )
133+ return {
134+ lastModified : blob . lastModified ,
135+ value : {
136+ ...blob . value ,
137+ body : Buffer . from ( blob . value . body as unknown as string , 'base64' ) ,
138+ } ,
139+ }
140+ case 'PAGE' :
141+ span . addEvent ( 'PAGE' , { lastModified : blob . lastModified } )
142+ return {
143+ lastModified : blob . lastModified ,
144+ value : blob . value ,
145+ }
146+ default :
147+ span . recordException ( new Error ( `Unknown cache entry kind: ${ blob . value ?. kind } ` ) )
148+ }
149+ return null
159150 } )
160151 }
161152
162153 async set ( ...args : Parameters < IncrementalCache [ 'set' ] > ) {
163- return this . tracer . startActiveSpan ( 'set cache key' , async ( span ) => {
154+ return this . tracer . withActiveSpan ( 'set cache key' , async ( span ) => {
164155 const [ key , data ] = args
165156 const blobKey = await this . encodeBlobKey ( key )
166157 const lastModified = Date . now ( )
@@ -189,7 +180,6 @@ export class NetlifyCacheHandler implements CacheHandler {
189180 } )
190181 }
191182 }
192- span . end ( )
193183 } )
194184 }
195185
@@ -264,22 +254,9 @@ export class NetlifyCacheHandler implements CacheHandler {
264254
265255 if ( ! tagManifestPromise ) {
266256 tagManifestPromise = this . encodeBlobKey ( tag ) . then ( ( blobKey ) => {
267- return this . tracer . startActiveSpan ( `get tag manifest` , async ( span ) => {
257+ return this . tracer . withActiveSpan ( `get tag manifest` , async ( span ) => {
268258 span . setAttributes ( { tag, blobKey } )
269- try {
270- return await this . blobStore . get ( blobKey , { type : 'json' } )
271- } catch ( error ) {
272- if ( error instanceof Error ) {
273- span . recordException ( error )
274- }
275- span . setStatus ( {
276- code : SpanStatusCode . ERROR ,
277- message : error instanceof Error ? error . message : String ( error ) ,
278- } )
279- throw error
280- } finally {
281- span . end ( )
282- }
259+ return this . blobStore . get ( blobKey , { type : 'json' } )
283260 } )
284261 } )
285262
0 commit comments