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 telemetry unsupported async #1131

Merged
merged 4 commits into from
Aug 18, 2022
Merged
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
88 changes: 46 additions & 42 deletions bin/cml.js
Original file line number Diff line number Diff line change
@@ -8,8 +8,7 @@ const winston = require('winston');
const yargs = require('yargs');

const CML = require('../src/cml').default;
const { jitsuEventPayload } = require('../src/analytics');
let OPTS;
const { jitsuEventPayload, send } = require('../src/analytics');

const setupOpts = (opts) => {
const legacyEnvironmentVariables = {
@@ -33,8 +32,6 @@ const setupOpts = (opts) => {
opts.markdownFile = markdownfile;
opts.cmlCommand = opts._[0];
opts.cml = new CML(opts);

OPTS = opts;
};

const setupLogger = (opts) => {
@@ -68,48 +65,55 @@ const setupTelemetry = async (opts) => {

const runPlugin = async ({ $0: executable, command }) => {
if (command === undefined) throw new Error('no command');
const { argv } = process.argv;
const path = which.sync(`${basename(executable)}-${command}`);
const parameters = process.argv.slice(process.argv.indexOf(command) + 1); // HACK
const parameters = argv.slice(argv.indexOf(command) + 1); // HACK
await pseudoexec(path, parameters);
};

const handleError = async (message, error, yargs) => {
if (error) {
const { telemetryEvent, cml } = OPTS;
const event = { ...telemetryEvent, error: error.message };
await cml.telemetrySend({ event });
return;
const handleError = (message, error) => {
if (!error) {
yargs.showHelp();
console.error('\n' + message);
process.exit(1);
}

yargs.showHelp();
console.error('\n' + message);
};

process.on('uncaughtException', async (err) => {
await handleError('', err, yargs);
});

process.on('unhandledRejection', async (reason) => {
await handleError('', new Error(reason), yargs);
});

yargs
.env('CML')
.options({
log: {
type: 'string',
description: 'Maximum log level',
choices: ['error', 'warn', 'info', 'debug'],
default: 'info'
}
})
.fail(handleError)
.middleware(setupOpts)
.middleware(setupLogger)
.middleware(setupTelemetry)
.commandDir('./cml', { exclude: /\.test\.js$/ })
.command('$0 <command>', false, (builder) => builder.strict(false), runPlugin)
.recommendCommands()
.demandCommand()
.strict()
.parse();
(async () => {
0x2b3bfa0 marked this conversation as resolved.
Show resolved Hide resolved
try {
await yargs
.env('CML')
.options({
log: {
type: 'string',
description: 'Maximum log level',
choices: ['error', 'warn', 'info', 'debug'],
default: 'info'
}
})
.fail(handleError)
.middleware(setupOpts)
.middleware(setupLogger)
.middleware(setupTelemetry)
.commandDir('./cml', { exclude: /\.test\.js$/ })
.command(
'$0 <command>',
false,
(builder) => builder.strict(false),
runPlugin
)
.recommendCommands()
.demandCommand()
.strict()
.parse();

const { telemetryEvent } = yargs.parsed.argv;
await send({ event: telemetryEvent });
} catch (err) {
const { telemetryEvent } = yargs.parsed.argv;
const event = { ...telemetryEvent, error: err.message };
await send({ event });
winston.error({ err });
process.exit(1);
}
})();
3 changes: 1 addition & 2 deletions bin/cml/pr.js
Original file line number Diff line number Diff line change
@@ -11,10 +11,9 @@ exports.command = 'pr <glob path...>';
exports.description = 'Create a pull request with the specified files';

exports.handler = async (opts) => {
const { cml, telemetryEvent: event, globpath: globs } = opts;
const { cml, globpath: globs } = opts;
const link = await cml.prCreate({ ...opts, globs });
console.log(link);
await cml.telemetrySend({ event });
};

exports.builder = (yargs) =>
4 changes: 1 addition & 3 deletions bin/cml/publish.js
Original file line number Diff line number Diff line change
@@ -15,14 +15,12 @@ exports.handler = async (opts) => {
opts.native = true;
}

const { file, repo, native, asset: path, telemetryEvent: event } = opts;
const { file, repo, native, asset: path } = opts;
const cml = new CML({ ...opts, repo: native ? repo : 'cml' });
const output = await cml.publish({ ...opts, path });

if (!file) console.log(output);
else await fs.writeFile(file, output);

await cml.telemetrySend({ event });
};

exports.builder = (yargs) =>
3 changes: 1 addition & 2 deletions bin/cml/rerun-workflow.js
Original file line number Diff line number Diff line change
@@ -6,9 +6,8 @@ exports.description = 'Reruns a workflow given the jobId or workflow Id';
const { repoOptions } = require('../../src/cml');

exports.handler = async (opts) => {
const { cml, telemetryEvent: event } = opts;
const { cml } = opts;
await cml.pipelineRerun(opts);
await cml.telemetrySend({ event });
};

exports.builder = (yargs) =>
2 changes: 0 additions & 2 deletions bin/cml/runner.js
Original file line number Diff line number Diff line change
@@ -420,10 +420,8 @@ exports.description = 'Launch and register a self-hosted runner';

exports.handler = async (opts) => {
({ cml } = opts);
const { telemetryEvent: event } = opts;
try {
await run(opts);
await cml.telemetrySend({ event });
} catch (error) {
await shutdown({ ...opts, error });
}
5 changes: 2 additions & 3 deletions bin/cml/send-comment.js
Original file line number Diff line number Diff line change
@@ -6,9 +6,8 @@ exports.command = 'send-comment <markdown file>';
exports.description = 'Comment on a commit';

exports.handler = async (opts) => {
const { cml, telemetryEvent: event } = opts;
console.log(await opts.cml.commentCreate(opts));
await cml.telemetrySend({ event });
const { cml } = opts;
console.log(await cml.commentCreate(opts));
};

exports.builder = (yargs) =>
5 changes: 2 additions & 3 deletions bin/cml/send-github-check.js
Original file line number Diff line number Diff line change
@@ -7,10 +7,9 @@ exports.command = 'send-github-check <markdown file>';
exports.description = 'Create a check report';

exports.handler = async (opts) => {
const { cml, telemetryEvent: event, markdownfile } = opts;
const { cml, markdownfile } = opts;
const report = await fs.readFile(markdownfile, 'utf-8');
await opts.cml.checkCreate({ ...opts, report });
await cml.telemetrySend({ event });
await cml.checkCreate({ ...opts, report });
};

exports.builder = (yargs) =>
3 changes: 0 additions & 3 deletions bin/cml/tensorboard-dev.js
Original file line number Diff line number Diff line change
@@ -92,9 +92,6 @@ exports.handler = async (opts) => {
const url = await launchAndWaitLink({ ...opts, extraParams });
if (!file) console.log(url);
else await fs.appendFile(file, url);

const { cml, telemetryEvent: event } = opts;
await cml.telemetrySend({ event });
};

exports.builder = (yargs) =>
64 changes: 0 additions & 64 deletions package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -97,7 +97,6 @@
"strip-url-auth": "^1.0.1",
"tar": "^6.1.11",
"tempy": "^0.6.0",
"timeout-signal": "^1.1.0",
"timestring": "^6.0.0",
"unist-util-visit": "^2.0.3",
"uuid": "^8.3.2",
6 changes: 4 additions & 2 deletions src/analytics.js
Original file line number Diff line number Diff line change
@@ -3,7 +3,6 @@ const path = require('path');
const fs = require('fs').promises;

const fetch = require('node-fetch');
const timeoutSignal = require('timeout-signal');
const ProxyAgent = require('proxy-agent');
const { promisify } = require('util');
const { scrypt } = require('crypto');
@@ -187,8 +186,10 @@ const send = async ({
if (ITERATIVE_DO_NOT_TRACK) return;
if (!event.user_id || event.user_id === ID_DO_NOT_TRACK) return;

const controller = new AbortController();
const id = setTimeout(() => controller.abort(), 5 * 1000);
await fetch(endpoint, {
signal: timeoutSignal(5 * 1000),
signal: controller.signal,
method: 'POST',
headers: {
'X-Auth-Token': token,
@@ -197,6 +198,7 @@ const send = async ({
body: JSON.stringify(event),
agent: new ProxyAgent()
});
clearInterval(id);
} catch (err) {
winston.debug(`Send analytics failed: ${err.message}`);
}
5 changes: 0 additions & 5 deletions src/cml.js
Original file line number Diff line number Diff line change
@@ -20,7 +20,6 @@ const {
preventcacheUri,
waitForever
} = require('./utils');
const analytics = require('../src/analytics');

const { GITHUB_REPOSITORY, CI_PROJECT_URL, BITBUCKET_REPO_UUID } = process.env;

@@ -572,10 +571,6 @@ Automated commits for ${this.repo}/commit/${sha} created by CML.
return await this.getDriver().pipelineJobs(opts);
}

async telemetrySend({ event }) {
await analytics.send({ event });
}

logError(e) {
winston.error(e.message);
}