Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

feat: Update Node SDK to latest v7 and use metrics #47

Merged
merged 5 commits into from
May 15, 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- ref: Use latest Sentry v7 SDK for telemetry collection (#47)

## 0.2.0

- feat: Add transform to extract node config into separate file (#41)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"dependencies": {
"@clack/prompts": "^0.7.0",
"@sentry/node": "^7.64.0",
"@sentry/node": "^7.114.0",
"chalk": "^5.3.0",
"globby": "^14.0.1",
"jscodeshift": "^0.15.2",
Expand Down
108 changes: 49 additions & 59 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { KEYS, WaitType, defaultRunner } from '../test-helpers/clet.js';
describe('index', () => {
it('works with an empty app & default options', async () => {
await defaultRunner('emptyApp')
.wait(WaitType.stdout, /Do you want to apply all transformers, or only selected ones/)
.stdin(/Do you want to apply all transformers, or only selected ones/, KEYS.ENTER)
.wait(WaitType.stdout, /Do you want to apply all code transforms, or only selected ones\?/)
.stdin(/Do you want to apply all code transforms, or only selected ones\?/, KEYS.ENTER)
.wait(WaitType.stdout, /Transformer (.*) completed./)
.wait(WaitType.stdout, /All transformers completed!/);
});
Expand Down
25 changes: 17 additions & 8 deletions src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ process.setMaxListeners(0);
* @param {import("types").RunOptions} options
*/
export async function run(options) {
withTelemetry({ enabled: !options.disableTelemetry }, () => runWithTelementry(options));
withTelemetry({ enabled: !options.disableTelemetry }, () => runWithTelemetry(options));
}

/**
* @param {import("types").RunOptions} options
*/
export async function runWithTelementry(options) {
export async function runWithTelemetry(options) {
Sentry.metrics.increment('executions');

traceStep('intro', () => {
intro(chalk.inverse('Welcome to sentry-migr8!'));
note(
Expand All @@ -38,16 +40,18 @@ export async function runWithTelementry(options) {
We will guide you through the process step by step.${
!options.disableTelemetry
? `
This tool collectes anonymous telemetry data and sends it to Sentry.
This tool collects anonymous telemetry data and sends it to Sentry.
You can disable this by passing the --disableTelemetry option.`
: ''
}`
);

note(`We will run transforms on files matching the following ${
note(`We will run code transforms on files matching the following ${
options.filePatterns.length > 1 ? 'patterns' : 'pattern'
}, ignoring any gitignored files:
}, ignoring any .gitignored files:

${options.filePatterns.join('\n')}

(You can change this by specifying the --filePatterns option)`);
});

Expand All @@ -74,7 +78,7 @@ export async function runWithTelementry(options) {
const applyAllTransformers = await traceStep('ask-apply-all-transformers', async () =>
abortIfCancelled(
select({
message: 'Do you want to apply all transformers, or only selected ones?',
message: 'Do you want to apply all code transforms, or only selected ones?',
options: [
{ value: true, label: 'Apply all transformations.', hint: 'Recommended' },
{ value: false, label: 'I want to select myself.' },
Expand All @@ -83,7 +87,7 @@ export async function runWithTelementry(options) {
)
);

Sentry.setTag('apply-all-transformers', applyAllTransformers);
Sentry.metrics.distribution('apply-all-transformers', applyAllTransformers ? 1 : 0, { unit: 'percent' });

let transformers = allTransformers;
if (!applyAllTransformers) {
Expand All @@ -105,6 +109,7 @@ export async function runWithTelementry(options) {

await traceStep('run-transformers', async () => {
for (const transformer of transformers) {
Sentry.metrics.set('transforms.selected', transformer.name);
const showSpinner = !transformer.requiresUserInput;

const s = spinner();
Expand All @@ -117,12 +122,14 @@ export async function runWithTelementry(options) {
if (showSpinner) {
s.stop(`Transformer "${transformer.name}" completed.`);
}
Sentry.metrics.set('transforms.success', transformer.name);
} catch (error) {
if (showSpinner) {
s.stop(`Transformer "${transformer.name}" failed to complete with error:`);
}
// eslint-disable-next-line no-console
console.error(error);
Sentry.metrics.set('transforms.fail', transformer.name);
}
}
});
Expand All @@ -133,6 +140,8 @@ export async function runWithTelementry(options) {
log.success('All transformers completed!');
outro('Thank you for using @sentry/migr8!');
});

Sentry.metrics.increment('executions.success');
}

/**
Expand All @@ -145,7 +154,7 @@ function detectSdk(packageJSON) {
const sdkName = sdkPackage ? sdkPackage.name : undefined;

if (sdkName) {
log.info(`Detected SDK: ${sdkName}`);
log.info(`Detected SDK: ${chalk.cyan(sdkName)}`);
} else {
log.info(
`No Sentry SDK detected. Skipping all import-rewriting transformations.
Expand Down
7 changes: 7 additions & 0 deletions src/transformers/addMigrationComments/transform.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const Sentry = require('@sentry/node');

const { wrapJscodeshift } = require('../../utils/dom.cjs');
const { addTodoComment } = require('../../utils/jscodeshift.cjs');

Expand Down Expand Up @@ -90,6 +92,11 @@ module.exports = function (fileInfo, api) {
}
});

if (hasChanges) {
Sentry.setTag('added-todo-comments', true);
Sentry.metrics.increment('added-todo-comments', 1);
}

return hasChanges ? tree.toSource() : undefined;
});
};
19 changes: 7 additions & 12 deletions src/utils/clackUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@ import { traceStep } from './telemetry.js';
export async function abortIfCancelled(input) {
if (isCancel(await input)) {
cancel('Migr8 run cancelled, see you next time :)');
const sentryHub = Sentry.getCurrentHub();
const sentryTransaction = sentryHub.getScope().getTransaction();
sentryTransaction?.setStatus('cancelled');
sentryTransaction?.finish();
sentryHub.captureSession(true);
Sentry.getActiveSpan()?.setStatus('ok');
Sentry.getActiveSpan()?.end();
await Sentry.flush(3000);
process.exit(0);
} else {
Expand Down Expand Up @@ -145,15 +142,13 @@ function hasChangedFiles() {
*/
async function abort(customMessage, exitCode = 0) {
cancel(customMessage ?? 'Exiting, see you next time :)');
const sentryHub = Sentry.getCurrentHub();
const sentryTransaction = sentryHub.getScope().getTransaction();
sentryTransaction?.setStatus('aborted');
sentryTransaction?.finish();
const sentrySession = sentryHub.getScope().getSession();
Sentry.getActiveSpan()?.setStatus('ok');
const sentrySession = Sentry.getCurrentScope().getSession();
if (sentrySession) {
sentrySession.status = exitCode === 0 ? 'abnormal' : 'crashed';
sentryHub.captureSession(true);
sentrySession.status = 'abnormal';
}
Sentry.endSession();

await Sentry.flush(3000);
process.exit(exitCode);
}
Expand Down
Loading
Loading