Skip to content

Commit

Permalink
fix(core): Safely flush telemetry (#462)
Browse files Browse the repository at this point in the history
  • Loading branch information
lforst authored Jan 12, 2024
1 parent 1c6adf0 commit c493dae
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/bundler-plugin-core/src/debug-id-upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -224,7 +225,7 @@ export function createDebugIdUploadFunction({
cleanupSpan.finish();
}
artifactBundleUploadTransaction.finish();
await sentryClient.flush();
await safeFlushTelemetry(sentryClient);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -93,7 +94,7 @@ export function releaseManagementPlugin({
}
} catch (e) {
sentryHub.captureException('Error in "releaseManagementPlugin" writeBundle hook');
await sentryClient.flush();
await safeFlushTelemetry(sentryClient);
handleRecoverableError(e);
}
},
Expand Down
3 changes: 2 additions & 1 deletion packages/bundler-plugin-core/src/plugins/telemetry.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
},
};
Expand Down
12 changes: 12 additions & 0 deletions packages/bundler-plugin-core/src/sentry/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
}

0 comments on commit c493dae

Please sign in to comment.