1
+ import { type SentryClient } from "../sentry.ts" ;
1
2
import { BadResponseError } from "../errors/BadResponseError" ;
2
3
import { DEFAULT_RETRY_DELAY } from "./constants" ;
3
4
import { delay } from "./delay" ;
@@ -8,19 +9,22 @@ interface FetchWithRetryArgs {
8
9
retryCount : number ;
9
10
requestData : RequestInit ;
10
11
name ?: string ;
12
+ sentryClient ?: SentryClient ;
11
13
}
12
14
13
15
export const fetchWithRetry = async ( {
14
16
url,
15
17
retryCount,
16
18
requestData,
17
19
name,
20
+ sentryClient,
18
21
} : FetchWithRetryArgs ) => {
19
22
let response = new Response ( null , { status : 400 } ) ;
23
+ let retryCounter = 0 ;
20
24
21
25
for ( let i = 0 ; i < retryCount + 1 ; i ++ ) {
22
26
try {
23
- debug ( `Attempting to fetch ${ name } , attempt: ${ i } ` ) ;
27
+ debug ( `Attempting to fetch \` ${ name } \` , attempt: ${ i } ` ) ;
24
28
await delay ( DEFAULT_RETRY_DELAY * i ) ;
25
29
response = await fetch ( url , requestData ) ;
26
30
@@ -29,19 +33,27 @@ export const fetchWithRetry = async ({
29
33
}
30
34
break ;
31
35
} catch ( err ) {
32
- debug ( `${ name } fetch attempt ${ i } failed` ) ;
36
+ debug ( `\` ${ name } \` fetch attempt ${ i } failed` ) ;
33
37
const isLastAttempt = i + 1 === retryCount ;
38
+ retryCounter = i ;
34
39
35
40
if ( isLastAttempt ) {
36
41
red ( `${ name } failed after ${ i } attempts` ) ;
37
42
38
43
if ( ! ( err instanceof BadResponseError ) ) {
39
44
throw err ;
40
45
}
46
+
47
+ sentryClient ?. metricsAggregator ?. add (
48
+ "g" ,
49
+ `fetch.${ name } ` ,
50
+ retryCounter ,
51
+ ) ;
41
52
return response ;
42
53
}
43
54
}
44
55
}
45
56
57
+ sentryClient ?. metricsAggregator ?. add ( "g" , `fetch.${ name } ` , retryCounter ) ;
46
58
return response ;
47
59
} ;
0 commit comments