Skip to content

Commit 0623113

Browse files
committed
swap to using gauge factory function for fetchWithRetry, and add tags for different bundler
1 parent fb14fff commit 0623113

File tree

5 files changed

+52
-23
lines changed

5 files changed

+52
-23
lines changed

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,23 @@ import {
55
type ProviderUtilInputs,
66
type UploadOverrides,
77
} from "../types.ts";
8-
import { type UnpluginOptions } from "unplugin";
8+
import { type UnpluginContextMeta, type UnpluginOptions } from "unplugin";
99
import { getPreSignedURL } from "../utils/getPreSignedURL.ts";
1010
import { uploadStats } from "../utils/uploadStats.ts";
1111
import { type SentryClient } from "../sentry.ts";
12-
import { type NormalizedOptions } from "src/utils/normalizeOptions.ts";
12+
import { type NormalizedOptions } from "../utils/normalizeOptions.ts";
13+
import { createGauge } from "../utils/fetchWithRetry.ts";
1314

1415
interface BundleAnalysisUploadPluginArgs {
1516
userOptions: NormalizedOptions;
17+
unpluginMetaContext: UnpluginContextMeta;
1618
bundleAnalysisUploadPlugin: BundleAnalysisUploadPlugin;
1719
sentryClient: SentryClient;
1820
}
1921

2022
export const bundleAnalysisPluginFactory = ({
2123
userOptions,
24+
unpluginMetaContext,
2225
bundleAnalysisUploadPlugin,
2326
sentryClient,
2427
}: BundleAnalysisUploadPluginArgs): UnpluginOptions => {
@@ -58,25 +61,34 @@ export const bundleAnalysisPluginFactory = ({
5861
const inputs: ProviderUtilInputs = { envs, args };
5962
const provider = await detectProvider(inputs);
6063

61-
const getPreSignedURLStart = Date.now();
6264
let url = "";
65+
const gauge = createGauge({
66+
bundler: unpluginMetaContext.framework,
67+
sentryClient,
68+
});
69+
const getPreSignedURLStart = Date.now();
6370
try {
6471
url = await getPreSignedURL({
6572
apiURL: userOptions?.apiUrl,
6673
uploadToken: userOptions?.uploadToken,
6774
serviceParams: provider,
6875
retryCount: userOptions?.retryCount,
76+
gauge,
6977
});
7078
sentryClient?.metricsAggregator?.add(
7179
"c",
7280
"request_presigned_url.success",
7381
1,
82+
"none",
83+
{ bundler: unpluginMetaContext.framework },
7484
);
7585
} catch (error) {
7686
sentryClient?.metricsAggregator?.add(
7787
"c",
7888
"request_presigned_url.error",
7989
1,
90+
"none",
91+
{ bundler: unpluginMetaContext.framework },
8092
);
8193
return;
8294
} finally {
@@ -85,6 +97,7 @@ export const bundleAnalysisPluginFactory = ({
8597
"request_presigned_url",
8698
Date.now() - getPreSignedURLStart,
8799
"millisecond",
100+
{ bundler: unpluginMetaContext.framework },
88101
);
89102
}
90103

@@ -94,12 +107,14 @@ export const bundleAnalysisPluginFactory = ({
94107
preSignedUrl: url,
95108
message: JSON.stringify(output),
96109
retryCount: userOptions?.retryCount,
97-
sentryClient,
110+
gauge,
98111
});
99112
sentryClient?.metricsAggregator?.add(
100113
"c",
101114
"upload_bundle_stats.success",
102115
1,
116+
"none",
117+
{ bundler: unpluginMetaContext.framework },
103118
);
104119
} catch {
105120
sentryClient?.metricsAggregator?.add(
@@ -114,6 +129,7 @@ export const bundleAnalysisPluginFactory = ({
114129
"upload_bundle_stats",
115130
Date.now() - uploadStart,
116131
"millisecond",
132+
{ bundler: unpluginMetaContext.framework },
117133
);
118134
}
119135
},

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ function codecovUnpluginFactory({
5151
plugins.push(
5252
bundleAnalysisPluginFactory({
5353
userOptions: options,
54+
unpluginMetaContext,
5455
bundleAnalysisUploadPlugin,
5556
sentryClient,
5657
}),

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,24 +2,23 @@ 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
preSignedUrl: string;
1413
retryCount?: number;
15-
sentryClient?: SentryClient;
14+
gauge?: Gauge;
1615
}
1716

1817
export async function uploadStats({
1918
message,
2019
preSignedUrl,
2120
retryCount = DEFAULT_RETRY_COUNT,
22-
sentryClient,
21+
gauge,
2322
}: UploadStatsArgs) {
2423
const iterator = message[Symbol.iterator]();
2524
const stream = new ReadableStream({
@@ -40,7 +39,7 @@ export async function uploadStats({
4039
url: preSignedUrl,
4140
retryCount,
4241
name: "upload-stats",
43-
sentryClient,
42+
gauge,
4443
requestData: {
4544
method: "PUT",
4645
headers: {

0 commit comments

Comments
 (0)