From fef5395e512cd086f997f870d80151284107be68 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Fri, 12 Jan 2024 10:37:54 +0100 Subject: [PATCH] fix(core): Safely flush telemetry --- packages/bundler-plugin-core/src/debug-id-upload.ts | 3 ++- .../src/plugins/release-management.ts | 3 ++- .../bundler-plugin-core/src/plugins/telemetry.ts | 3 ++- packages/bundler-plugin-core/src/sentry/telemetry.ts | 12 ++++++++++++ 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/bundler-plugin-core/src/debug-id-upload.ts b/packages/bundler-plugin-core/src/debug-id-upload.ts index c9d89d86..1e7b4822 100644 --- a/packages/bundler-plugin-core/src/debug-id-upload.ts +++ b/packages/bundler-plugin-core/src/debug-id-upload.ts @@ -8,6 +8,7 @@ import { promisify } from "util"; import { Hub, NodeClient } from "@sentry/node"; import SentryCli from "@sentry/cli"; import { dynamicSamplingContextToSentryBaggageHeader } from "@sentry/utils"; +import { safeFlushTelemetry } from "./sentry/telemetry"; interface RewriteSourcesHook { (source: string, map: any): string; @@ -224,7 +225,7 @@ export function createDebugIdUploadFunction({ cleanupSpan.finish(); } artifactBundleUploadTransaction.finish(); - await sentryClient.flush(); + await safeFlushTelemetry(sentryClient); } }; } diff --git a/packages/bundler-plugin-core/src/plugins/release-management.ts b/packages/bundler-plugin-core/src/plugins/release-management.ts index eecd0a28..aaa177ed 100644 --- a/packages/bundler-plugin-core/src/plugins/release-management.ts +++ b/packages/bundler-plugin-core/src/plugins/release-management.ts @@ -2,6 +2,7 @@ import SentryCli, { SentryCliCommitsOptions, SentryCliNewDeployOptions } from "@ import { Hub, NodeClient } from "@sentry/node"; import { UnpluginOptions } from "unplugin"; import { Logger } from "../sentry/logger"; +import { safeFlushTelemetry } from "../sentry/telemetry"; import { IncludeEntry } from "../types"; import { arrayify } from "../utils"; @@ -93,7 +94,7 @@ export function releaseManagementPlugin({ } } catch (e) { sentryHub.captureException('Error in "releaseManagementPlugin" writeBundle hook'); - await sentryClient.flush(); + await safeFlushTelemetry(sentryClient); handleRecoverableError(e); } }, diff --git a/packages/bundler-plugin-core/src/plugins/telemetry.ts b/packages/bundler-plugin-core/src/plugins/telemetry.ts index 4b256b6b..68f3b0bf 100644 --- a/packages/bundler-plugin-core/src/plugins/telemetry.ts +++ b/packages/bundler-plugin-core/src/plugins/telemetry.ts @@ -1,6 +1,7 @@ import { Hub, NodeClient } from "@sentry/node"; import { UnpluginOptions } from "unplugin"; import { Logger } from "../sentry/logger"; +import { safeFlushTelemetry } from "../sentry/telemetry"; interface TelemetryPluginOptions { sentryHub: Hub; @@ -23,7 +24,7 @@ export function telemetryPlugin({ "Sending error and performance telemetry data to Sentry. To disable telemetry, set `options.telemetry` to `false`." ); sentryHub.startTransaction({ name: "Sentry Bundler Plugin execution" }).finish(); - await sentryClient.flush(3000); + await safeFlushTelemetry(sentryClient); } }, }; diff --git a/packages/bundler-plugin-core/src/sentry/telemetry.ts b/packages/bundler-plugin-core/src/sentry/telemetry.ts index e6f02b76..219eaf00 100644 --- a/packages/bundler-plugin-core/src/sentry/telemetry.ts +++ b/packages/bundler-plugin-core/src/sentry/telemetry.ts @@ -147,3 +147,15 @@ export async function allowedToSendTelemetry(options: NormalizedOptions): Promis return new URL(cliInfoUrl).hostname === SENTRY_SAAS_HOSTNAME; } + +/** + * Flushing the SDK client can fail. We never want to crash the plugin because of telemetry. + */ +export async function safeFlushTelemetry(sentryClient: NodeClient) { + try { + await sentryClient.flush(2000); + } catch { + // Noop when flushing fails. + // We don't even need to log anything because there's likely nothing the user can do and they likely will not care. + } +}