Skip to content

Commit 662ed32

Browse files
committed
add guage metric to fetchWithRetry function
1 parent 2321f39 commit 662ed32

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

packages/bundler-plugin-core/src/utils/fetchWithRetry.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { type SentryClient } from "../sentry.ts";
12
import { BadResponseError } from "../errors/BadResponseError";
23
import { DEFAULT_RETRY_DELAY } from "./constants";
34
import { delay } from "./delay";
@@ -8,19 +9,22 @@ interface FetchWithRetryArgs {
89
retryCount: number;
910
requestData: RequestInit;
1011
name?: string;
12+
sentryClient?: SentryClient;
1113
}
1214

1315
export 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

Comments
 (0)