1+ import { type SentryClient } from "../sentry.ts" ;
12import { BadResponseError } from "../errors/BadResponseError" ;
23import { DEFAULT_RETRY_DELAY } from "./constants" ;
34import { delay } from "./delay" ;
@@ -8,19 +9,22 @@ interface FetchWithRetryArgs {
89 retryCount : number ;
910 requestData : RequestInit ;
1011 name ?: string ;
12+ sentryClient ?: SentryClient ;
1113}
1214
1315export const fetchWithRetry = async ( {
1416 url,
1517 retryCount,
1618 requestData,
1719 name,
20+ sentryClient,
1821} : FetchWithRetryArgs ) => {
1922 let response = new Response ( null , { status : 400 } ) ;
23+ let retryCounter = 0 ;
2024
2125 for ( let i = 0 ; i < retryCount + 1 ; i ++ ) {
2226 try {
23- debug ( `Attempting to fetch ${ name } , attempt: ${ i } ` ) ;
27+ debug ( `Attempting to fetch \` ${ name } \` , attempt: ${ i } ` ) ;
2428 await delay ( DEFAULT_RETRY_DELAY * i ) ;
2529 response = await fetch ( url , requestData ) ;
2630
@@ -29,19 +33,27 @@ export const fetchWithRetry = async ({
2933 }
3034 break ;
3135 } catch ( err ) {
32- debug ( `${ name } fetch attempt ${ i } failed` ) ;
36+ debug ( `\` ${ name } \` fetch attempt ${ i } failed` ) ;
3337 const isLastAttempt = i + 1 === retryCount ;
38+ retryCounter = i ;
3439
3540 if ( isLastAttempt ) {
3641 red ( `${ name } failed after ${ i } attempts` ) ;
3742
3843 if ( ! ( err instanceof BadResponseError ) ) {
3944 throw err ;
4045 }
46+
47+ sentryClient ?. metricsAggregator ?. add (
48+ "g" ,
49+ `fetch.${ name } ` ,
50+ retryCounter ,
51+ ) ;
4152 return response ;
4253 }
4354 }
4455 }
4556
57+ sentryClient ?. metricsAggregator ?. add ( "g" , `fetch.${ name } ` , retryCounter ) ;
4658 return response ;
4759} ;
0 commit comments