Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Safely flush telemetry #462

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
}
}