11import { AxiosRequestConfig , AxiosResponse } from 'axios'
2+ import { Span } from 'opentracing'
23
34import { CacheLayer } from '../../caches/CacheLayer'
45import { LOCALE_HEADER , SEGMENT_HEADER , SESSION_HEADER } from '../../constants'
6+ import { IOContext } from '../../service/worker/runtime/typings'
7+ import { ErrorReport } from '../../tracing'
58import { HttpLogEvents } from '../../tracing/LogEvents'
69import { HttpCacheLogFields } from '../../tracing/LogFields'
710import { CustomHttpTags } from '../../tracing/Tags'
@@ -90,7 +93,7 @@ export const cacheMiddleware = ({ type, storage }: CacheOptions) => {
9093 return await next ( )
9194 }
9295
93- const span = ctx . tracing ?. rootSpan
96+ const { rootSpan : span , tracer , logger } = ctx . tracing ?? { }
9497
9598 const key = cacheKey ( ctx . config )
9699 const segmentToken = ctx . config . headers [ SEGMENT_HEADER ]
@@ -103,8 +106,18 @@ export const cacheMiddleware = ({ type, storage }: CacheOptions) => {
103106 [ HttpCacheLogFields . KEY_WITH_SEGMENT ] : keyWithSegment ,
104107 } )
105108
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+ }
108121
109122 if ( cached && cached . response ) {
110123 const { etag : cachedEtag , response, expiration, responseType, responseEncoding} = cached as Cached
@@ -208,24 +221,32 @@ export const cacheMiddleware = ({ type, storage }: CacheOptions) => {
208221 return
209222 }
210223
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+ }
229250
230251 return
231252 }
@@ -234,6 +255,12 @@ export const cacheMiddleware = ({ type, storage }: CacheOptions) => {
234255 }
235256}
236257
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+
237264export interface Cached {
238265 etag : string
239266 expiration : number
0 commit comments