2019-03-12 22:07:57 +00:00
|
|
|
/**
|
|
|
|
* @license
|
2020-03-04 16:31:54 +00:00
|
|
|
* Copyright 2019-2020 Balena Ltd.
|
2019-03-12 22:07:57 +00:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-03-04 16:31:54 +00:00
|
|
|
import * as packageJSON from '../package.json';
|
2022-02-11 15:23:36 +00:00
|
|
|
import { stripIndent } from './utils/lazy';
|
2020-05-04 13:40:12 +00:00
|
|
|
|
2020-03-04 16:31:54 +00:00
|
|
|
/**
|
2021-09-29 22:00:53 +00:00
|
|
|
* Track balena CLI usage events (product improvement analytics).
|
2020-03-04 16:31:54 +00:00
|
|
|
*
|
|
|
|
* @param commandSignature A string like, for example:
|
2021-07-15 13:41:38 +00:00
|
|
|
* "push <fleetOrDevice>"
|
|
|
|
* That's literally so: "fleetOrDevice" is NOT replaced with the actual
|
2022-07-15 15:01:37 +00:00
|
|
|
* fleet slug or device uuid. The purpose is to find out the most / least
|
2020-03-04 16:31:54 +00:00
|
|
|
* used command verbs, so we can focus our development effort where it is most
|
|
|
|
* beneficial to end users.
|
|
|
|
*
|
|
|
|
* The username and command signature are also added as extra context
|
|
|
|
* information in Sentry.io error reporting, for CLI debugging purposes
|
|
|
|
* (mainly unexpected/unhandled exceptions -- see also `lib/errors.ts`).
|
2020-10-20 21:11:17 +00:00
|
|
|
*
|
|
|
|
* For more details on the data collected by balena generally, check this page:
|
|
|
|
* https://www.balena.io/docs/learn/more/collected-data/
|
2020-03-04 16:31:54 +00:00
|
|
|
*/
|
2020-05-04 09:04:44 +00:00
|
|
|
export async function trackCommand(commandSignature: string) {
|
2020-07-01 14:26:40 +00:00
|
|
|
try {
|
2020-10-09 12:06:09 +00:00
|
|
|
let Sentry: typeof import('@sentry/node');
|
|
|
|
if (!process.env.BALENARC_NO_SENTRY) {
|
|
|
|
Sentry = await import('@sentry/node');
|
|
|
|
Sentry.configureScope((scope) => {
|
|
|
|
scope.setExtra('command', commandSignature);
|
|
|
|
});
|
|
|
|
}
|
2022-02-11 15:23:36 +00:00
|
|
|
const { getCachedUsername } = await import('./utils/bootstrap');
|
|
|
|
let username: string | undefined;
|
|
|
|
try {
|
|
|
|
username = (await getCachedUsername())?.username;
|
|
|
|
} catch {
|
|
|
|
// ignore
|
|
|
|
}
|
2020-10-09 12:06:09 +00:00
|
|
|
if (!process.env.BALENARC_NO_SENTRY) {
|
|
|
|
Sentry!.configureScope((scope) => {
|
|
|
|
scope.setUser({
|
|
|
|
id: username,
|
|
|
|
username,
|
|
|
|
});
|
2020-03-04 16:31:54 +00:00
|
|
|
});
|
2020-10-09 12:06:09 +00:00
|
|
|
}
|
2020-11-26 12:47:48 +00:00
|
|
|
// Don't actually call mixpanel.track() while running test cases, or if suppressed
|
|
|
|
if (
|
|
|
|
!process.env.BALENA_CLI_TEST_TYPE &&
|
|
|
|
!process.env.BALENARC_NO_ANALYTICS
|
|
|
|
) {
|
2022-02-11 15:23:36 +00:00
|
|
|
const settings = await import('balena-settings-client');
|
2021-09-29 22:00:53 +00:00
|
|
|
const balenaUrl = settings.get<string>('balenaUrl');
|
|
|
|
await sendEvent(balenaUrl, `[CLI] ${commandSignature}`, username);
|
2020-07-09 18:48:06 +00:00
|
|
|
}
|
2020-07-01 14:26:40 +00:00
|
|
|
} catch {
|
|
|
|
// ignore
|
|
|
|
}
|
2018-01-04 14:07:55 +00:00
|
|
|
}
|
2021-09-29 22:00:53 +00:00
|
|
|
|
2022-10-21 16:07:39 +00:00
|
|
|
const TIMEOUT = 4000;
|
|
|
|
|
2021-09-29 22:00:53 +00:00
|
|
|
/**
|
|
|
|
* Make the event tracking HTTPS request to balenaCloud's '/mixpanel' endpoint.
|
|
|
|
*/
|
|
|
|
async function sendEvent(balenaUrl: string, event: string, username?: string) {
|
|
|
|
const { default: got } = await import('got');
|
|
|
|
const trackData = {
|
2022-10-13 22:32:55 +00:00
|
|
|
api_key: 'balena-main',
|
|
|
|
events: [
|
|
|
|
{
|
|
|
|
event_type: event,
|
|
|
|
user_id: username,
|
|
|
|
version_name: packageJSON.version,
|
2022-10-14 13:50:12 +00:00
|
|
|
event_properties: {
|
|
|
|
balenaUrl, // e.g. 'balena-cloud.com' or 'balena-staging.com'
|
|
|
|
arch: process.arch,
|
|
|
|
platform: process.platform,
|
|
|
|
node: process.version,
|
|
|
|
},
|
2022-10-13 22:32:55 +00:00
|
|
|
},
|
|
|
|
],
|
2021-09-29 22:00:53 +00:00
|
|
|
};
|
2022-10-14 13:50:12 +00:00
|
|
|
const url = `https://data.${balenaUrl}/amplitude/2/httpapi`;
|
2022-10-13 22:32:55 +00:00
|
|
|
|
2021-09-29 22:00:53 +00:00
|
|
|
try {
|
2022-10-13 22:32:55 +00:00
|
|
|
await got.post(url, {
|
2022-10-14 13:50:12 +00:00
|
|
|
json: trackData,
|
2022-10-13 22:32:55 +00:00
|
|
|
retry: 0,
|
2022-10-21 16:07:39 +00:00
|
|
|
timeout: {
|
|
|
|
// Starts when the request is initiated.
|
|
|
|
request: TIMEOUT,
|
|
|
|
// Starts when request has been flushed.
|
|
|
|
// Exits the request as soon as it's sent.
|
|
|
|
response: 0,
|
|
|
|
},
|
2022-10-13 22:32:55 +00:00
|
|
|
});
|
2021-09-29 22:00:53 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (process.env.DEBUG) {
|
2022-10-17 13:07:51 +00:00
|
|
|
console.error(`[debug] Event tracking error: ${e.message || e}`);
|
2021-09-29 22:00:53 +00:00
|
|
|
}
|
2021-11-12 16:03:29 +00:00
|
|
|
|
2022-10-21 16:07:39 +00:00
|
|
|
if (
|
|
|
|
e instanceof got.TimeoutError &&
|
|
|
|
TIMEOUT < (e.timings.phases.total ?? 0)
|
|
|
|
) {
|
2021-11-12 16:03:29 +00:00
|
|
|
console.error(stripIndent`
|
|
|
|
Timeout submitting analytics event to balenaCloud/openBalena.
|
|
|
|
If you are using the balena CLI in an air-gapped environment with a filtered
|
|
|
|
internet connection, set the BALENARC_OFFLINE_MODE=1 environment variable
|
|
|
|
when using CLI commands that do not strictly require access to balenaCloud.
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
// Note: You can simulate a timeout using non-routable address 10.0.0.0
|
2021-09-29 22:00:53 +00:00
|
|
|
}
|
|
|
|
}
|