Skip to content

Commit c493dae

Browse files
author
Luca Forstner
authored
fix(core): Safely flush telemetry (#462)
1 parent 1c6adf0 commit c493dae

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

packages/bundler-plugin-core/src/debug-id-upload.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { promisify } from "util";
88
import { Hub, NodeClient } from "@sentry/node";
99
import SentryCli from "@sentry/cli";
1010
import { dynamicSamplingContextToSentryBaggageHeader } from "@sentry/utils";
11+
import { safeFlushTelemetry } from "./sentry/telemetry";
1112

1213
interface RewriteSourcesHook {
1314
(source: string, map: any): string;
@@ -224,7 +225,7 @@ export function createDebugIdUploadFunction({
224225
cleanupSpan.finish();
225226
}
226227
artifactBundleUploadTransaction.finish();
227-
await sentryClient.flush();
228+
await safeFlushTelemetry(sentryClient);
228229
}
229230
};
230231
}

packages/bundler-plugin-core/src/plugins/release-management.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import SentryCli, { SentryCliCommitsOptions, SentryCliNewDeployOptions } from "@
22
import { Hub, NodeClient } from "@sentry/node";
33
import { UnpluginOptions } from "unplugin";
44
import { Logger } from "../sentry/logger";
5+
import { safeFlushTelemetry } from "../sentry/telemetry";
56
import { IncludeEntry } from "../types";
67
import { arrayify } from "../utils";
78

@@ -93,7 +94,7 @@ export function releaseManagementPlugin({
9394
}
9495
} catch (e) {
9596
sentryHub.captureException('Error in "releaseManagementPlugin" writeBundle hook');
96-
await sentryClient.flush();
97+
await safeFlushTelemetry(sentryClient);
9798
handleRecoverableError(e);
9899
}
99100
},

packages/bundler-plugin-core/src/plugins/telemetry.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Hub, NodeClient } from "@sentry/node";
22
import { UnpluginOptions } from "unplugin";
33
import { Logger } from "../sentry/logger";
4+
import { safeFlushTelemetry } from "../sentry/telemetry";
45

56
interface TelemetryPluginOptions {
67
sentryHub: Hub;
@@ -23,7 +24,7 @@ export function telemetryPlugin({
2324
"Sending error and performance telemetry data to Sentry. To disable telemetry, set `options.telemetry` to `false`."
2425
);
2526
sentryHub.startTransaction({ name: "Sentry Bundler Plugin execution" }).finish();
26-
await sentryClient.flush(3000);
27+
await safeFlushTelemetry(sentryClient);
2728
}
2829
},
2930
};

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,15 @@ export async function allowedToSendTelemetry(options: NormalizedOptions): Promis
147147

148148
return new URL(cliInfoUrl).hostname === SENTRY_SAAS_HOSTNAME;
149149
}
150+
151+
/**
152+
* Flushing the SDK client can fail. We never want to crash the plugin because of telemetry.
153+
*/
154+
export async function safeFlushTelemetry(sentryClient: NodeClient) {
155+
try {
156+
await sentryClient.flush(2000);
157+
} catch {
158+
// Noop when flushing fails.
159+
// We don't even need to log anything because there's likely nothing the user can do and they likely will not care.
160+
}
161+
}

0 commit comments

Comments
 (0)