@@ -5,7 +5,7 @@ import { Buffer } from 'node:buffer'
5
5
6
6
import { getDeployStore , Store } from '@netlify/blobs'
7
7
import { purgeCache } from '@netlify/functions'
8
- import { trace , type Span , SpanStatusCode } from '@opentelemetry/api'
8
+ import { type Span } from '@opentelemetry/api'
9
9
import { NEXT_CACHE_TAGS_HEADER } from 'next/dist/lib/constants.js'
10
10
import type {
11
11
CacheHandler ,
@@ -15,6 +15,7 @@ import type {
15
15
} from 'next/dist/server/lib/incremental-cache/index.js'
16
16
17
17
import { getRequestContext } from './request-context.cjs'
18
+ import { getTracer } from './tracer.cjs'
18
19
19
20
type TagManifest = { revalidatedAt : number }
20
21
@@ -26,7 +27,7 @@ export class NetlifyCacheHandler implements CacheHandler {
26
27
options : CacheHandlerContext
27
28
revalidatedTags : string [ ]
28
29
blobStore : Store
29
- tracer = trace . getTracer ( 'Netlify Cache Handler' )
30
+ tracer = getTracer ( )
30
31
tagManifestsFetchedFromBlobStoreInCurrentRequest : TagManifestBlobCache
31
32
32
33
constructor ( options : CacheHandlerContext ) {
@@ -90,77 +91,67 @@ export class NetlifyCacheHandler implements CacheHandler {
90
91
}
91
92
92
93
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 } ` )
103
97
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 } )
109
100
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
111
107
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
+ }
116
113
117
- this . captureResponseCacheLastModified ( blob , key , span )
114
+ const staleByTags = await this . checkCacheEntryStaleByTags ( blob , ctx . tags , ctx . softTags )
118
115
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 } )
146
118
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 ( )
158
119
}
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
159
150
} )
160
151
}
161
152
162
153
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 ) => {
164
155
const [ key , data ] = args
165
156
const blobKey = await this . encodeBlobKey ( key )
166
157
const lastModified = Date . now ( )
@@ -189,7 +180,6 @@ export class NetlifyCacheHandler implements CacheHandler {
189
180
} )
190
181
}
191
182
}
192
- span . end ( )
193
183
} )
194
184
}
195
185
@@ -264,22 +254,9 @@ export class NetlifyCacheHandler implements CacheHandler {
264
254
265
255
if ( ! tagManifestPromise ) {
266
256
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 ) => {
268
258
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' } )
283
260
} )
284
261
} )
285
262
0 commit comments