Skip to content

Commit 393122c

Browse files
committed
rework how sentry metrics are collected
1 parent 50efd26 commit 393122c

File tree

4 files changed

+56
-42
lines changed

4 files changed

+56
-42
lines changed

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

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@ import {
88
import { type UnpluginContextMeta, type UnpluginOptions } from "unplugin";
99
import { getPreSignedURL } from "../utils/getPreSignedURL.ts";
1010
import { uploadStats } from "../utils/uploadStats.ts";
11-
import { type SentryClient } from "../sentry.ts";
11+
import { type SentryMetrics } from "../sentry.ts";
1212
import { type NormalizedOptions } from "../utils/normalizeOptions.ts";
1313
import { createGauge } from "../utils/fetchWithRetry.ts";
1414

1515
interface BundleAnalysisUploadPluginArgs {
1616
userOptions: NormalizedOptions;
1717
unpluginMetaContext: UnpluginContextMeta;
1818
bundleAnalysisUploadPlugin: BundleAnalysisUploadPlugin;
19-
sentryClient: SentryClient;
19+
sentryMetrics: SentryMetrics;
2020
handleRecoverableError: (error: unknown) => void;
2121
}
2222

2323
export const bundleAnalysisPluginFactory = ({
2424
userOptions,
2525
unpluginMetaContext,
2626
bundleAnalysisUploadPlugin,
27-
sentryClient,
27+
sentryMetrics,
2828
handleRecoverableError,
2929
}: BundleAnalysisUploadPluginArgs): UnpluginOptions => {
3030
const output: Output = {
@@ -66,7 +66,7 @@ export const bundleAnalysisPluginFactory = ({
6666
let url = "";
6767
const gauge = createGauge({
6868
bundler: unpluginMetaContext.framework,
69-
sentryClient,
69+
sentryMetrics,
7070
});
7171
const getPreSignedURLStart = Date.now();
7272
try {
@@ -77,27 +77,18 @@ export const bundleAnalysisPluginFactory = ({
7777
retryCount: userOptions?.retryCount,
7878
gauge,
7979
});
80-
sentryClient?.metricsAggregator?.add(
81-
"c",
82-
"request_presigned_url.success",
83-
1,
84-
"none",
85-
{ bundler: unpluginMetaContext.framework },
86-
);
80+
sentryMetrics?.increment("request_presigned_url.success", 1, "none", {
81+
bundler: unpluginMetaContext.framework,
82+
});
8783
} catch (error) {
88-
sentryClient?.metricsAggregator?.add(
89-
"c",
90-
"request_presigned_url.error",
91-
1,
92-
"none",
93-
{ bundler: unpluginMetaContext.framework },
94-
);
84+
sentryMetrics?.increment("request_presigned_url.error", 1, "none", {
85+
bundler: unpluginMetaContext.framework,
86+
});
9587

9688
handleRecoverableError(error);
9789
return;
9890
} finally {
99-
sentryClient?.metricsAggregator?.add(
100-
"d",
91+
sentryMetrics?.distribution(
10192
"request_presigned_url",
10293
Date.now() - getPreSignedURLStart,
10394
"millisecond",
@@ -113,24 +104,15 @@ export const bundleAnalysisPluginFactory = ({
113104
retryCount: userOptions?.retryCount,
114105
gauge,
115106
});
116-
sentryClient?.metricsAggregator?.add(
117-
"c",
118-
"upload_bundle_stats.success",
119-
1,
120-
"none",
121-
{ bundler: unpluginMetaContext.framework },
122-
);
107+
sentryMetrics?.increment("upload_bundle_stats.success", 1, "none", {
108+
bundler: unpluginMetaContext.framework,
109+
});
123110
} catch (error) {
124-
sentryClient?.metricsAggregator?.add(
125-
"c",
126-
"upload_bundle_stats.error",
127-
1,
128-
);
111+
sentryMetrics?.increment("upload_bundle_stats.error", 1);
129112
handleRecoverableError(error);
130113
return;
131114
} finally {
132-
sentryClient?.metricsAggregator?.add(
133-
"d",
115+
sentryMetrics?.distribution(
134116
"upload_bundle_stats",
135117
Date.now() - uploadStart,
136118
"millisecond",

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function codecovUnpluginFactory({
4242

4343
const options = normalizeOptions(userOptions);
4444

45-
const { sentryClient, sentryHub } = createSentryInstance(
45+
const { sentryHub, sentryMetrics } = createSentryInstance(
4646
options,
4747
unpluginMetaContext.framework,
4848
);
@@ -90,7 +90,7 @@ function codecovUnpluginFactory({
9090
userOptions: options,
9191
unpluginMetaContext,
9292
bundleAnalysisUploadPlugin,
93-
sentryClient,
93+
sentryMetrics,
9494
handleRecoverableError,
9595
}),
9696
);

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,28 @@ import {
77
} from "@sentry/node";
88
import { type Options } from "./types";
99
import { type NormalizedOptions } from "./utils/normalizeOptions";
10+
import { type Primitive } from "zod";
1011

1112
export type SentryClient = ReturnType<
1213
typeof createSentryInstance
1314
>["sentryClient"];
1415

16+
export type SentryMetrics = ReturnType<
17+
typeof createSentryInstance
18+
>["sentryMetrics"];
19+
1520
export const createSentryInstance = (
1621
options: NormalizedOptions,
1722
bundler: string,
1823
) => {
1924
const telemetry = options.telemetry ?? true;
2025

2126
if (telemetry === false || !!options.dryRun) {
22-
return { sentryClient: undefined, sentryHub: undefined };
27+
return {
28+
sentryClient: undefined,
29+
sentryHub: undefined,
30+
sentryMetrics: undefined,
31+
};
2332
}
2433

2534
const client = new NodeClient({
@@ -75,7 +84,30 @@ export const createSentryInstance = (
7584
// increment the counter for the bundler
7685
client.metricsAggregator?.add("c", `bundler-${bundler}`, 1);
7786

78-
return { sentryClient: client, sentryHub: hub };
87+
type MetricFunction = (
88+
key: string,
89+
value: number,
90+
unit?: string,
91+
tags?: Record<string, Primitive>,
92+
) => void;
93+
94+
const gauge: MetricFunction = (key, value, unit, tags) =>
95+
client.metricsAggregator?.add("g", key, value, unit, tags);
96+
const distribution: MetricFunction = (key, value, unit, tags) =>
97+
client.metricsAggregator?.add("d", key, value, unit, tags);
98+
const increment: MetricFunction = (key, value, unit, tags) =>
99+
client.metricsAggregator?.add("c", key, value, unit, tags);
100+
const set: MetricFunction = (key, value, unit, tags) =>
101+
client.metricsAggregator?.add("s", key, value, unit, tags);
102+
103+
const sentryMetrics = {
104+
distribution,
105+
increment,
106+
gauge,
107+
set,
108+
};
109+
110+
return { sentryClient: client, sentryHub: hub, sentryMetrics };
79111
};
80112

81113
export const setTelemetryDataOnHub = (

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
import { type SentryClient } from "../sentry.ts";
1+
import { type SentryMetrics } from "../sentry.ts";
22
import { BadResponseError } from "../errors/BadResponseError";
33
import { DEFAULT_RETRY_DELAY } from "./constants";
44
import { delay } from "./delay";
55
import { debug, red } from "./logging";
66

77
interface CreateGaugeArgs {
88
bundler: string;
9-
sentryClient: SentryClient;
9+
sentryMetrics: SentryMetrics;
1010
}
1111

1212
export type Gauge = ReturnType<typeof createGauge>;
1313

1414
export const createGauge =
15-
({ bundler, sentryClient }: CreateGaugeArgs) =>
15+
({ bundler, sentryMetrics }: CreateGaugeArgs) =>
1616
(name: string, count: number) => {
17-
sentryClient?.metricsAggregator?.add("g", `fetch.${name}`, count, "none", {
17+
sentryMetrics?.gauge(`fetch.${name}`, count, "none", {
1818
bundler,
1919
});
2020
};

0 commit comments

Comments
 (0)