Skip to content

Commit 8167a07

Browse files
committed
swap to using gauge factory function for fetchWithRetry, and add tags for different bundler
1 parent 22e2fe1 commit 8167a07

File tree

5 files changed

+51
-22
lines changed

5 files changed

+51
-22
lines changed

packages/bundler-plugin-core/src/bundle-analysis/bundleAnalysisPluginFactory.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
1-
import { type UnpluginOptions } from "unplugin";
21
import {
32
type BundleAnalysisUploadPlugin,
43
type Output,
54
type ProviderUtilInputs,
65
type UploadOverrides,
76
} from "../types.ts";
7+
import { type UnpluginContextMeta, type UnpluginOptions } from "unplugin";
88
import { getPreSignedURL } from "../utils/getPreSignedURL.ts";
99
import { type NormalizedOptions } from "../utils/normalizeOptions.ts";
1010
import { detectProvider } from "../utils/provider.ts";
1111
import { uploadStats } from "../utils/uploadStats.ts";
1212
import { sendSentryBundleStats } from "../utils/sentryUtils.ts";
1313
import { type SentryClient } from "../sentry.ts";
14+
import { createGauge } from "../utils/fetchWithRetry.ts";
1415

1516
interface BundleAnalysisUploadPluginArgs {
1617
options: NormalizedOptions;
18+
unpluginMetaContext: UnpluginContextMeta;
1719
bundleAnalysisUploadPlugin: BundleAnalysisUploadPlugin;
1820
sentryClient: SentryClient;
1921
}
2022

2123
export const bundleAnalysisPluginFactory = ({
2224
options,
25+
unpluginMetaContext,
2326
bundleAnalysisUploadPlugin,
2427
sentryClient,
2528
}: BundleAnalysisUploadPluginArgs): UnpluginOptions => {
@@ -69,25 +72,34 @@ export const bundleAnalysisPluginFactory = ({
6972
const inputs: ProviderUtilInputs = { envs, args };
7073
const provider = await detectProvider(inputs);
7174

72-
const getPreSignedURLStart = Date.now();
7375
let url = "";
76+
const gauge = createGauge({
77+
bundler: unpluginMetaContext.framework,
78+
sentryClient,
79+
});
80+
const getPreSignedURLStart = Date.now();
7481
try {
7582
url = await getPreSignedURL({
7683
apiURL: options?.apiUrl,
7784
uploadToken: options?.uploadToken,
7885
serviceParams: provider,
7986
retryCount: options?.retryCount,
87+
gauge,
8088
});
8189
sentryClient?.metricsAggregator?.add(
8290
"c",
8391
"request_presigned_url.success",
8492
1,
93+
"none",
94+
{ bundler: unpluginMetaContext.framework },
8595
);
8696
} catch (error) {
8797
sentryClient?.metricsAggregator?.add(
8898
"c",
8999
"request_presigned_url.error",
90100
1,
101+
"none",
102+
{ bundler: unpluginMetaContext.framework },
91103
);
92104
return;
93105
} finally {
@@ -96,6 +108,7 @@ export const bundleAnalysisPluginFactory = ({
96108
"request_presigned_url",
97109
Date.now() - getPreSignedURLStart,
98110
"millisecond",
111+
{ bundler: unpluginMetaContext.framework },
99112
);
100113
}
101114

@@ -106,12 +119,14 @@ export const bundleAnalysisPluginFactory = ({
106119
bundleName: output.bundleName,
107120
message: JSON.stringify(output),
108121
retryCount: options?.retryCount,
109-
sentryClient,
122+
gauge,
110123
});
111124
sentryClient?.metricsAggregator?.add(
112125
"c",
113126
"upload_bundle_stats.success",
114127
1,
128+
"none",
129+
{ bundler: unpluginMetaContext.framework },
115130
);
116131
} catch {
117132
sentryClient?.metricsAggregator?.add(
@@ -126,6 +141,7 @@ export const bundleAnalysisPluginFactory = ({
126141
"upload_bundle_stats",
127142
Date.now() - uploadStart,
128143
"millisecond",
144+
{ bundler: unpluginMetaContext.framework },
129145
);
130146
}
131147
},

packages/bundler-plugin-core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ function codecovUnpluginFactory({
6060
plugins.push(
6161
bundleAnalysisPluginFactory({
6262
options,
63+
unpluginMetaContext,
6364
bundleAnalysisUploadPlugin,
6465
sentryClient,
6566
}),

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

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,35 @@ import { DEFAULT_RETRY_DELAY } from "./constants";
44
import { delay } from "./delay";
55
import { debug, red } from "./logging";
66

7+
interface CreateGaugeArgs {
8+
bundler: string;
9+
sentryClient: SentryClient;
10+
}
11+
12+
export type Gauge = ReturnType<typeof createGauge>;
13+
14+
export const createGauge =
15+
({ bundler, sentryClient }: CreateGaugeArgs) =>
16+
(name: string, count: number) => {
17+
sentryClient?.metricsAggregator?.add("g", `fetch.${name}`, count, "none", {
18+
bundler,
19+
});
20+
};
21+
722
interface FetchWithRetryArgs {
823
url: string;
924
retryCount: number;
1025
requestData: RequestInit;
1126
name?: string;
12-
sentryClient?: SentryClient;
27+
gauge?: Gauge;
1328
}
1429

1530
export const fetchWithRetry = async ({
1631
url,
1732
retryCount,
1833
requestData,
1934
name,
20-
sentryClient,
35+
gauge,
2136
}: FetchWithRetryArgs) => {
2237
let response = new Response(null, { status: 400 });
2338
let retryCounter = 0;
@@ -43,17 +58,16 @@ export const fetchWithRetry = async ({
4358
if (!(err instanceof BadResponseError)) {
4459
throw err;
4560
}
46-
47-
sentryClient?.metricsAggregator?.add(
48-
"g",
49-
`fetch.${name}`,
50-
retryCounter,
51-
);
61+
if (gauge && name) {
62+
gauge(name, retryCounter + 1);
63+
}
5264
return response;
5365
}
5466
}
5567
}
5668

57-
sentryClient?.metricsAggregator?.add("g", `fetch.${name}`, retryCounter);
69+
if (gauge && name) {
70+
gauge(name, retryCounter + 1);
71+
}
5872
return response;
5973
};

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@ import { NoUploadTokenError } from "../errors/NoUploadTokenError.ts";
55
import { UploadLimitReachedError } from "../errors/UploadLimitReachedError.ts";
66
import { type ProviderServiceParams } from "../types.ts";
77
import { DEFAULT_RETRY_COUNT } from "./constants.ts";
8-
import { fetchWithRetry } from "./fetchWithRetry.ts";
8+
import { type Gauge, fetchWithRetry } from "./fetchWithRetry.ts";
99
import { green, red } from "./logging.ts";
1010
import { preProcessBody } from "./preProcessBody.ts";
11-
import { type SentryClient } from "../sentry.ts";
1211

1312
interface GetPreSignedURLArgs {
1413
apiURL: string;
1514
uploadToken?: string;
1615
serviceParams: Partial<ProviderServiceParams>;
1716
retryCount?: number;
18-
sentryClient?: SentryClient;
17+
gauge?: Gauge;
1918
}
2019

2120
const PreSignedURLSchema = z.object({
@@ -27,7 +26,7 @@ export const getPreSignedURL = async ({
2726
uploadToken,
2827
serviceParams,
2928
retryCount = DEFAULT_RETRY_COUNT,
30-
sentryClient,
29+
gauge,
3130
}: GetPreSignedURLArgs) => {
3231
if (!uploadToken) {
3332
red("No upload token found");
@@ -41,7 +40,7 @@ export const getPreSignedURL = async ({
4140
response = await fetchWithRetry({
4241
url,
4342
retryCount,
44-
sentryClient,
43+
gauge,
4544
name: "get-pre-signed-url",
4645
requestData: {
4746
method: "POST",

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,25 @@ import { ReadableStream, TextEncoderStream } from "node:stream/web";
22

33
import { FailedUploadError } from "../errors/FailedUploadError";
44
import { green, red } from "./logging";
5-
import { fetchWithRetry } from "./fetchWithRetry";
5+
import { type Gauge, fetchWithRetry } from "./fetchWithRetry";
66
import { DEFAULT_RETRY_COUNT } from "./constants";
77
import { UploadLimitReachedError } from "../errors/UploadLimitReachedError";
88
import { FailedFetchError } from "../errors/FailedFetchError";
9-
import { type SentryClient } from "../sentry.ts";
109

1110
interface UploadStatsArgs {
1211
message: string;
1312
bundleName: string;
1413
preSignedUrl: string;
1514
retryCount?: number;
16-
sentryClient?: SentryClient;
15+
gauge?: Gauge;
1716
}
1817

1918
export async function uploadStats({
2019
message,
2120
bundleName,
2221
preSignedUrl,
2322
retryCount = DEFAULT_RETRY_COUNT,
24-
sentryClient,
23+
gauge,
2524
}: UploadStatsArgs) {
2625
const iterator = message[Symbol.iterator]();
2726
const stream = new ReadableStream({
@@ -42,7 +41,7 @@ export async function uploadStats({
4241
url: preSignedUrl,
4342
retryCount,
4443
name: "upload-stats",
45-
sentryClient,
44+
gauge,
4645
requestData: {
4746
method: "PUT",
4847
headers: {

0 commit comments

Comments
 (0)