Skip to content

Commit

Permalink
feat(sveltekit): Add telemetry collection
Browse files Browse the repository at this point in the history
  • Loading branch information
Lms24 committed Sep 14, 2023
1 parent 06b067c commit 05b2855
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 35 deletions.
21 changes: 15 additions & 6 deletions src/sveltekit/sdk-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import * as path from 'path';
import * as url from 'url';
import chalk from 'chalk';

import * as Sentry from '@sentry/node';

// @ts-ignore - clack is ESM and TS complains about that. It works though
import clack from '@clack/prompts';
// @ts-ignore - magicast is ESM and TS complains about that. It works though
Expand Down Expand Up @@ -59,19 +61,25 @@ export async function createOrMergeSvelteKitFiles(

const { dsn } = projectInfo;

Sentry.setTag(
'server-hooks-file-strategy',
originalClientHooksFile ? 'merge' : 'create',
);
if (!originalClientHooksFile) {
clack.log.info('No client hooks file found, creating a new one.');
await createNewHooksFile(`${clientHooksPath}.${fileEnding}`, 'client', dsn);
} else {
await mergeHooksFile(originalClientHooksFile, 'client', dsn);
}

Sentry.setTag(
'client-hooks-file-strategy',
originalServerHooksFile ? 'merge' : 'create',
);
if (!originalServerHooksFile) {
clack.log.info('No server hooks file found, creating a new one.');
await createNewHooksFile(`${serverHooksPath}.${fileEnding}`, 'server', dsn);
}

if (originalClientHooksFile) {
await mergeHooksFile(originalClientHooksFile, 'client', dsn);
}
if (originalServerHooksFile) {
} else {
await mergeHooksFile(originalServerHooksFile, 'server', dsn);
}

Expand Down Expand Up @@ -436,6 +444,7 @@ Skipping adding Sentry functionality to.`,
viteConfigPath,
getViteConfigCodeSnippet(org, project, selfHosted, url),
);
Sentry.captureException('Sveltekit Vite Config Modification Fail');
}
}

Expand Down
94 changes: 69 additions & 25 deletions src/sveltekit/sveltekit-wizard.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// @ts-ignore - clack is ESM and TS complains about that. It works though
import clack from '@clack/prompts';
import * as clack from '@clack/prompts';
import chalk from 'chalk';

import * as Sentry from '@sentry/node';

import {
abort,
addSentryCliConfig,
Expand All @@ -12,46 +14,82 @@ import {
installPackage,
printWelcome,
} from '../utils/clack-utils';
import { hasPackageInstalled } from '../utils/package-json';
import { getPackageVersion, hasPackageInstalled } from '../utils/package-json';
import { WizardOptions } from '../utils/types';
import { createExamplePage } from './sdk-example';
import { createOrMergeSvelteKitFiles, loadSvelteConfig } from './sdk-setup';
import { traceStep, withTelemetry } from '../telemetry';
import { getKitVersionBucket, getSvelteVersionBucket } from './utils';

export async function runSvelteKitWizard(
options: WizardOptions,
): Promise<void> {
return withTelemetry(
{
enabled: options.telemetryEnabled,
integration: 'sveltekit',
},
() => runSvelteKitWizardWithTelemetry(options),
);
}

export async function runSvelteKitWizardWithTelemetry(
options: WizardOptions,
): Promise<void> {
printWelcome({
wizardName: 'Sentry SvelteKit Wizard',
promoCode: options.promoCode,
telemetryEnabled: options.telemetryEnabled,
});

await confirmContinueEvenThoughNoGitRepo();
await traceStep('detect-git', confirmContinueEvenThoughNoGitRepo);

const packageJson = await getPackageDotJson();
await ensurePackageIsInstalled(packageJson, '@sveltejs/kit', 'Sveltekit');
await traceStep('detect-framework-version', () =>
ensurePackageIsInstalled(packageJson, '@sveltejs/kit', 'Sveltekit'),
);

Sentry.setTag(
'sveltekit-version',
getKitVersionBucket(getPackageVersion('@sveltejs/kit', packageJson)),
);
Sentry.setTag(
'svelte-version',
getSvelteVersionBucket(getPackageVersion('svelte', packageJson)),
);

const { selectedProject, selfHosted, sentryUrl, authToken } =
await getOrAskForProjectData(options, 'javascript-sveltekit');

await installPackage({
packageName: '@sentry/sveltekit',
alreadyInstalled: hasPackageInstalled('@sentry/sveltekit', packageJson),
});
const sdkAlreadyInstalled = hasPackageInstalled(
'@sentry/sveltekit',
packageJson,
);
Sentry.setTag('sdk-already-installed', sdkAlreadyInstalled);

await traceStep('install-sdk', () =>
installPackage({
packageName: '@sentry/sveltekit',
alreadyInstalled: sdkAlreadyInstalled,
}),
);

await addSentryCliConfig(authToken);
await traceStep('add-cli-config', () => addSentryCliConfig(authToken));

const svelteConfig = await loadSvelteConfig();
const svelteConfig = await traceStep('load-svelte-config', loadSvelteConfig);

try {
await createOrMergeSvelteKitFiles(
{
dsn: selectedProject.keys[0].dsn.public,
org: selectedProject.organization.slug,
project: selectedProject.slug,
selfHosted,
url: sentryUrl,
},
svelteConfig,
await traceStep('configure-sdk', () =>
createOrMergeSvelteKitFiles(
{
dsn: selectedProject.keys[0].dsn.public,
org: selectedProject.organization.slug,
project: selectedProject.slug,
selfHosted,
url: sentryUrl,
},
svelteConfig,
),
);
} catch (e: unknown) {
clack.log.error('Error while setting up the SvelteKit SDK:');
Expand All @@ -64,17 +102,20 @@ export async function runSvelteKitWizard(
: 'Unknown error',
),
);
Sentry.captureException('Error while setting up the SvelteKit SDK');
await abort('Exiting Wizard');
return;
}

try {
await createExamplePage(svelteConfig, {
selfHosted,
url: sentryUrl,
orgSlug: selectedProject.organization.slug,
projectId: selectedProject.id,
});
await traceStep('create-example-page', () =>
createExamplePage(svelteConfig, {
selfHosted,
url: sentryUrl,
orgSlug: selectedProject.organization.slug,
projectId: selectedProject.id,
}),
);
} catch (e: unknown) {
clack.log.error('Error while creating an example page to test Sentry:');
clack.log.info(
Expand All @@ -86,6 +127,9 @@ export async function runSvelteKitWizard(
: 'Unknown error',
),
);
Sentry.captureException(
'Error while creating an example Svelte page to test Sentry',
);
await abort('Exiting Wizard');
return;
}
Expand Down
34 changes: 34 additions & 0 deletions src/sveltekit/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { lt } from 'semver';

export function getKitVersionBucket(version: string | undefined): string {
if (!version) {
return 'none';
}
if (lt(version, '1.0.0')) {
return '0.x';
} else if (lt(version, '1.24.0')) {
return '>=1.0.0 <1.24.0';
} else {
// This is the version when the client-side invalidation fix was released
// https://github.com/sveltejs/kit/releases/tag/%40sveltejs%2Fkit%401.24.0
// https://github.com/sveltejs/kit/pull/10576
return '>=1.24.0';
}
}

export function getSvelteVersionBucket(version: string | undefined): string {
if (!version) {
return 'none';
}
if (lt(version, '3.0.0')) {
return '<3.0.0';
}
if (lt(version, '4.0.0')) {
return '3.x';
}
if (lt(version, '5.0.0')) {
return '4.x';
}
// Svelte 5 isn't released yet but it's being worked on
return '>=5.0.0';
}
16 changes: 12 additions & 4 deletions src/utils/clack-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,23 @@ export function printWelcome(options: {

let welcomeText =
options.message ||
'This Wizard will help you set up Sentry for your application.\nThank you for using Sentry :)';
`The ${options.wizardName} will help you set up Sentry for your application.\nThank you for using Sentry :)`;

if (options.promoCode) {
welcomeText += `\n\nUsing promo-code: ${options.promoCode}`;
welcomeText = `${welcomeText}\n\nUsing promo-code: ${options.promoCode}`;
}

if (wizardPackage.version) {
welcomeText += `\n\nVersion: ${wizardPackage.version}`;
welcomeText = `${welcomeText}\n\nVersion: ${wizardPackage.version}`;
}

if (options.telemetryEnabled) {
welcomeText += `\n\nYou are using the Sentry Wizard with telemetry enabled. This helps us improve the Wizard.\nYou can disable it at any time by running \`sentry-wizard --disable-telemetry\`.`;
welcomeText = `${welcomeText}
This wizard sends telemetry data and crash reports to Sentry. This helps us improve the Wizard.
You can turn this off at any time by running ${chalk.cyanBright(
'sentry-wizard --disable-telemetry',
)}.`;
}

clack.note(welcomeText);
Expand Down Expand Up @@ -419,6 +424,7 @@ export async function ensurePackageIsInstalled(
packageName: string,
) {
if (!hasPackageInstalled(packageId, packageJson)) {
Sentry.setTag('package-installed', false);
const continueWithoutPackage = await abortIfCancelled(
clack.confirm({
message: `${packageName} does not seem to be installed. Do you still want to continue?`,
Expand All @@ -429,6 +435,8 @@ export async function ensurePackageIsInstalled(
if (!continueWithoutPackage) {
await abort(undefined, 0);
}
} else {
Sentry.setTag('package-installed', true);
}
}

Expand Down

0 comments on commit 05b2855

Please sign in to comment.