Skip to content

Commit

Permalink
feat(skytrace): add event pings
Browse files Browse the repository at this point in the history
  • Loading branch information
hassy committed Oct 19, 2023
1 parent 9eaec9f commit 41ac4bd
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/skytrace/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@
"artillery-plugin-expect": "*",
"chalk": "^4.1.2",
"cheerio": "^1.0.0-rc.10",
"ci-info": "^3.9.0",
"cli-highlight": "^2.1.11",
"debug": "^4.3.1",
"gradient-string": "^2.0.2",
"ip": "^1.1.8",
"jmespath": "^0.16.0",
"js-yaml": "^3.13.1",
"mime-types": "2.1.35",
"posthog-node": "^3.1.2",
"sprintf-js": "^1.1.2",
"temp": "^0.9.4"
},
Expand Down
10 changes: 10 additions & 0 deletions packages/skytrace/src/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { Plugin, formatters } from 'artillery-plugin-expect';

import * as gradientString from 'gradient-string';

import * as telemetry from '../telemetry';

class RunCommand extends Command {
static aliases = ['test'];
static strict = false;
Expand Down Expand Up @@ -122,6 +124,12 @@ SKYTRACE ──━━★

const opts = { target: flags.target, showHTTPTimings: flags.timings };

const ping = telemetry.init();
await ping.capture('run-flow', {
cliTarget: flags.target,
cliHTTPTimings: flags.timings,
});

if (flags.reload) {
console.log('> Running flow (reload mode on)');
console.log();
Expand Down Expand Up @@ -161,6 +169,8 @@ SKYTRACE ──━━★
console.log('');
await this.runFlow(flowFilePaths[0], opts);
}

await ping.shutdown();
}
}

Expand Down
115 changes: 115 additions & 0 deletions packages/skytrace/src/telemetry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

'use strict';

const { version: skytraceVersion } = require('../package.json');
const { isCI, name: ciName } = require('ci-info');
const chalk = require('chalk');
const debug = require('debug')('telemetry');

const POSTHOG_TOKEN = '_uzX-_WJoVmE_tsLvu0OFD2tpd0HGz72D5sU1zM2hbs';

const noop = () => {};

const notice = () => {
console.log(
'Anonymized telemetry is on. Learn more: https://skytrace.dev/docs/resources/core/telemetry.html'
);
};

const isEnabled = () => {
return typeof process.env.SKYTRACE_DISABLE_TELEMETRY === 'undefined';
};

const init = () => {
const telemetryDisabled = !isEnabled();

const debugEnabled =
typeof process.env.SKYTRACE_TELEMETRY_DEBUG !== 'undefined';

let telemetryDefaults = {};
try {
telemetryDefaults = JSON.parse(process.env.SKYTRACE_TELEMETRY_DEFAULTS);
} catch (err) {
// fail silently
}

const telemetry: any = {
capture: noop,
shutdown: noop
};

const capture = (client) => {
return (event, data: any = {}) => {
let eventPayload;

if (telemetryDisabled) {
eventPayload = {
event,
distinctId: 'skytrace'
};
} else {
eventPayload = {
event,
distinctId: data.distinctId || 'skytrace',
properties: {
...data,
version: skytraceVersion,
os: process.platform,
isCi: isCI,
$ip: null
}
};

eventPayload.properties = Object.assign(
eventPayload.properties,
telemetryDefaults
);

if (isCI) {
eventPayload.properties.ciName = ciName;
}
}

if (debugEnabled) {
console.log(
chalk.yellow(`Telemetry data: ${JSON.stringify(eventPayload)}`)
);
}

try {
debug({ eventPayload });
client.capture(eventPayload);
} catch (err) {
debug(err);
}
};
};

const shutdown = (client) => async () => {
try {
await client.shutdownAsync();
} catch (err) {
debug(err);
}
};

try {
const PostHog = require('posthog-node').PostHog;
const client = new PostHog(POSTHOG_TOKEN, {
flushInterval: 100
});

telemetry.capture = capture(client);
telemetry.shutdown = shutdown(client);
} catch (err) {
debug(err);
}

return telemetry;
};

export { init, notice, isEnabled };

0 comments on commit 41ac4bd

Please sign in to comment.