Skip to content

Commit 6ca1025

Browse files
committed
rework how sentry metrics are collected
1 parent 9486589 commit 6ca1025

File tree

4 files changed

+57
-43
lines changed

4 files changed

+57
-43
lines changed

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

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,26 @@ import {
66
} from "../types.ts";
77
import { type UnpluginContextMeta, type UnpluginOptions } from "unplugin";
88
import { getPreSignedURL } from "../utils/getPreSignedURL.ts";
9+
import { uploadStats } from "../utils/uploadStats.ts";
10+
import { type SentryMetrics } from "../sentry.ts";
911
import { type NormalizedOptions } from "../utils/normalizeOptions.ts";
1012
import { detectProvider } from "../utils/provider.ts";
11-
import { uploadStats } from "../utils/uploadStats.ts";
1213
import { sendSentryBundleStats } from "../utils/sentryUtils.ts";
13-
import { type SentryClient } from "../sentry.ts";
1414
import { createGauge } from "../utils/fetchWithRetry.ts";
1515

1616
interface BundleAnalysisUploadPluginArgs {
1717
options: NormalizedOptions;
1818
unpluginMetaContext: UnpluginContextMeta;
1919
bundleAnalysisUploadPlugin: BundleAnalysisUploadPlugin;
20-
sentryClient: SentryClient;
20+
sentryMetrics: SentryMetrics;
2121
handleRecoverableError: (error: unknown) => void;
2222
}
2323

2424
export const bundleAnalysisPluginFactory = ({
2525
options,
2626
unpluginMetaContext,
2727
bundleAnalysisUploadPlugin,
28-
sentryClient,
28+
sentryMetrics,
2929
handleRecoverableError,
3030
}: BundleAnalysisUploadPluginArgs): UnpluginOptions => {
3131
const output: Output = {
@@ -77,7 +77,7 @@ export const bundleAnalysisPluginFactory = ({
7777
let url = "";
7878
const gauge = createGauge({
7979
bundler: unpluginMetaContext.framework,
80-
sentryClient,
80+
sentryMetrics,
8181
});
8282
const getPreSignedURLStart = Date.now();
8383
try {
@@ -88,27 +88,18 @@ export const bundleAnalysisPluginFactory = ({
8888
retryCount: options?.retryCount,
8989
gauge,
9090
});
91-
sentryClient?.metricsAggregator?.add(
92-
"c",
93-
"request_presigned_url.success",
94-
1,
95-
"none",
96-
{ bundler: unpluginMetaContext.framework },
97-
);
91+
sentryMetrics?.increment("request_presigned_url.success", 1, "none", {
92+
bundler: unpluginMetaContext.framework,
93+
});
9894
} catch (error) {
99-
sentryClient?.metricsAggregator?.add(
100-
"c",
101-
"request_presigned_url.error",
102-
1,
103-
"none",
104-
{ bundler: unpluginMetaContext.framework },
105-
);
95+
sentryMetrics?.increment("request_presigned_url.error", 1, "none", {
96+
bundler: unpluginMetaContext.framework,
97+
});
10698

10799
handleRecoverableError(error);
108100
return;
109101
} finally {
110-
sentryClient?.metricsAggregator?.add(
111-
"d",
102+
sentryMetrics?.distribution(
112103
"request_presigned_url",
113104
Date.now() - getPreSignedURLStart,
114105
"millisecond",
@@ -125,24 +116,15 @@ export const bundleAnalysisPluginFactory = ({
125116
retryCount: options?.retryCount,
126117
gauge,
127118
});
128-
sentryClient?.metricsAggregator?.add(
129-
"c",
130-
"upload_bundle_stats.success",
131-
1,
132-
"none",
133-
{ bundler: unpluginMetaContext.framework },
134-
);
119+
sentryMetrics?.increment("upload_bundle_stats.success", 1, "none", {
120+
bundler: unpluginMetaContext.framework,
121+
});
135122
} catch (error) {
136-
sentryClient?.metricsAggregator?.add(
137-
"c",
138-
"upload_bundle_stats.error",
139-
1,
140-
);
123+
sentryMetrics?.increment("upload_bundle_stats.error", 1);
141124
handleRecoverableError(error);
142125
return;
143126
} finally {
144-
sentryClient?.metricsAggregator?.add(
145-
"d",
127+
sentryMetrics?.distribution(
146128
"upload_bundle_stats",
147129
Date.now() - uploadStart,
148130
"millisecond",

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function codecovUnpluginFactory({
5151
return plugins;
5252
}
5353

54-
const { sentryClient } = createSentryInstance(
54+
const { sentryHub, sentryMetrics } = createSentryInstance(
5555
options,
5656
unpluginMetaContext.framework,
5757
);
@@ -99,7 +99,7 @@ function codecovUnpluginFactory({
9999
options,
100100
unpluginMetaContext,
101101
bundleAnalysisUploadPlugin,
102-
sentryClient,
102+
sentryMetrics,
103103
handleRecoverableError,
104104
}),
105105
);

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)