1
1
import { AxiosRequestConfig , AxiosResponse } from 'axios'
2
+ import { Span } from 'opentracing'
2
3
3
4
import { CacheLayer } from '../../caches/CacheLayer'
4
5
import { LOCALE_HEADER , SEGMENT_HEADER , SESSION_HEADER } from '../../constants'
6
+ import { IOContext } from '../../service/worker/runtime/typings'
7
+ import { ErrorReport } from '../../tracing'
5
8
import { HttpLogEvents } from '../../tracing/LogEvents'
6
9
import { HttpCacheLogFields } from '../../tracing/LogFields'
7
10
import { CustomHttpTags } from '../../tracing/Tags'
@@ -90,7 +93,7 @@ export const cacheMiddleware = ({ type, storage }: CacheOptions) => {
90
93
return await next ( )
91
94
}
92
95
93
- const span = ctx . tracing ?. rootSpan
96
+ const { rootSpan : span , tracer , logger } = ctx . tracing ?? { }
94
97
95
98
const key = cacheKey ( ctx . config )
96
99
const segmentToken = ctx . config . headers [ SEGMENT_HEADER ]
@@ -103,8 +106,18 @@ export const cacheMiddleware = ({ type, storage }: CacheOptions) => {
103
106
[ HttpCacheLogFields . KEY_WITH_SEGMENT ] : keyWithSegment ,
104
107
} )
105
108
106
- const cacheHasWithSegment = await storage . has ( keyWithSegment )
107
- const cached = cacheHasWithSegment ? await storage . get ( keyWithSegment ) : await storage . get ( key )
109
+
110
+ const cacheReadSpan = createCacheSpan ( cacheType , 'read' , tracer , span )
111
+ let cached : void | Cached
112
+ try {
113
+ const cacheHasWithSegment = await storage . has ( keyWithSegment )
114
+ cached = cacheHasWithSegment ? await storage . get ( keyWithSegment ) : await storage . get ( key )
115
+ } catch ( error ) {
116
+ ErrorReport . create ( { originalError : error } ) . injectOnSpan ( cacheReadSpan )
117
+ logger ?. warn ( { message : 'Error reading from the HttpClient cache' , error } )
118
+ } finally {
119
+ cacheReadSpan ?. finish ( )
120
+ }
108
121
109
122
if ( cached && cached . response ) {
110
123
const { etag : cachedEtag , response, expiration, responseType, responseEncoding} = cached as Cached
@@ -208,24 +221,32 @@ export const cacheMiddleware = ({ type, storage }: CacheOptions) => {
208
221
return
209
222
}
210
223
211
- await storage . set ( setKey , {
212
- etag,
213
- expiration,
214
- response : { data : cacheableData , headers, status} ,
215
- responseEncoding,
216
- responseType,
217
- } )
218
-
219
- span ?. log ( {
220
- event : HttpLogEvents . LOCAL_CACHE_SAVED ,
221
- [ HttpCacheLogFields . CACHE_TYPE ] : cacheType ,
222
- [ HttpCacheLogFields . KEY_SET ] : setKey ,
223
- [ HttpCacheLogFields . AGE ] : currentAge ,
224
- [ HttpCacheLogFields . ETAG ] : etag ,
225
- [ HttpCacheLogFields . EXPIRATION_TIME ] : ( expiration - Date . now ( ) ) / 1000 ,
226
- [ HttpCacheLogFields . RESPONSE_ENCONDING ] : responseEncoding ,
227
- [ HttpCacheLogFields . RESPONSE_TYPE ] : responseType ,
228
- } )
224
+ const cacheWriteSpan = createCacheSpan ( cacheType , 'write' , tracer , span )
225
+ try {
226
+ await storage . set ( setKey , {
227
+ etag,
228
+ expiration,
229
+ response : { data : cacheableData , headers, status} ,
230
+ responseEncoding,
231
+ responseType,
232
+ } )
233
+
234
+ span ?. log ( {
235
+ event : HttpLogEvents . LOCAL_CACHE_SAVED ,
236
+ [ HttpCacheLogFields . CACHE_TYPE ] : cacheType ,
237
+ [ HttpCacheLogFields . KEY_SET ] : setKey ,
238
+ [ HttpCacheLogFields . AGE ] : currentAge ,
239
+ [ HttpCacheLogFields . ETAG ] : etag ,
240
+ [ HttpCacheLogFields . EXPIRATION_TIME ] : ( expiration - Date . now ( ) ) / 1000 ,
241
+ [ HttpCacheLogFields . RESPONSE_ENCONDING ] : responseEncoding ,
242
+ [ HttpCacheLogFields . RESPONSE_TYPE ] : responseType ,
243
+ } )
244
+ } catch ( error ) {
245
+ ErrorReport . create ( { originalError : error } ) . injectOnSpan ( cacheWriteSpan )
246
+ logger ?. warn ( { message : 'Error writing to the HttpClient cache' , error } )
247
+ } finally {
248
+ cacheWriteSpan ?. finish ( )
249
+ }
229
250
230
251
return
231
252
}
@@ -234,6 +255,12 @@ export const cacheMiddleware = ({ type, storage }: CacheOptions) => {
234
255
}
235
256
}
236
257
258
+ const createCacheSpan = ( cacheType : string , operation : 'read' | 'write' , tracer ?: IOContext [ 'tracer' ] , parentSpan ?: Span ) => {
259
+ if ( tracer && tracer . isTraceSampled && cacheType === 'disk' ) {
260
+ return tracer . startSpan ( `${ operation } -disk-cache` , { childOf : parentSpan } )
261
+ }
262
+ }
263
+
237
264
export interface Cached {
238
265
etag : string
239
266
expiration : number
0 commit comments