genezio
Version:
Command line utility to interact with Genezio infrastructure.
128 lines (127 loc) • 6.94 kB
JavaScript
import { debugLogger } from "../utils/logging.js";
import { AnalyticsHandler } from "./sdk/analyticsHandler.sdk.js";
import { getTelemetrySessionId, saveTelemetrySessionId } from "./session.js";
import { v4 as uuidv4 } from "uuid";
import { AMPLITUDE_API_KEY, ENVIRONMENT } from "../constants.js";
import version from "../utils/version.js";
import { trackEvent } from "../requests/gaTrackEvent.js";
import { track, init } from "@amplitude/analytics-node";
import getUser from "../requests/getUser.js";
import { isCI } from "../utils/process.js";
init(AMPLITUDE_API_KEY, {
flushQueueSize: 1,
flushIntervalMillis: 100,
});
let user;
async function getCachedUser() {
if (!user) {
user = await getUser().catch(() => undefined);
}
return user;
}
export var TelemetryEventTypes;
(function (TelemetryEventTypes) {
TelemetryEventTypes["GENEZIO_CANCEL"] = "GENEZIO_CANCEL";
TelemetryEventTypes["GENEZIO_LOGIN_ERROR"] = "GENEZIO_LOGIN_ERROR";
TelemetryEventTypes["GENEZIO_ADD_CLASS_ERROR"] = "GENEZIO_ADD_CLASS_ERROR";
TelemetryEventTypes["GENEZIO_LOCAL_ERROR"] = "GENEZIO_LOCAL_ERROR";
TelemetryEventTypes["GENEZIO_GENERATE_SDK_ERROR"] = "GENEZIO_GENERATE_SDK_ERROR";
TelemetryEventTypes["GENEZIO_ADD_CLASS"] = "GENEZIO_ADD_CLASS";
TelemetryEventTypes["GENEZIO_DEPLOY_ERROR"] = "GENEZIO_DEPLOY_ERROR";
TelemetryEventTypes["GENEZIO_PRE_START_LOCAL_SCRIPT_ERROR"] = "GENEZIO_PRE_START_LOCAL_SCRIPT_ERROR";
TelemetryEventTypes["GENEZIO_POST_START_LOCAL_SCRIPT_ERROR"] = "GENEZIO_PRE_START_LOCAL_SCRIPT_ERROR";
TelemetryEventTypes["GENEZIO_PRE_RELOAD_LOCAL_SCRIPT_ERROR"] = "GENEZIO_POST_RELOAD_LOCAL_SCRIPT_ERROR";
TelemetryEventTypes["GENEZIO_PRE_BACKEND_DEPLOY_SCRIPT_ERROR"] = "GENEZIO_PRE_BACKEND_DEPLOY_SCRIPT_ERROR";
TelemetryEventTypes["GENEZIO_BACKEND_DEPLOY_START"] = "GENEZIO_BACKEND_DEPLOY_START";
TelemetryEventTypes["GENEZIO_BACKEND_DEPLOY_ERROR"] = "GENEZIO_BACKEND_DEPLOY_ERROR";
TelemetryEventTypes["GENEZIO_BACKEND_DEPLOY_END"] = "GENEZIO_BACKEND_DEPLOY_END";
TelemetryEventTypes["GENEZIO_POST_BACKEND_DEPLOY_SCRIPT_ERROR"] = "GENEZIO_POST_BACKEND_DEPLOY_SCRIPT_ERROR";
TelemetryEventTypes["GENEZIO_PRE_FRONTEND_DEPLOY_SCRIPT_ERROR"] = "GENEZIO_PRE_FRONTEND_DEPLOY_SCRIPT_ERROR";
TelemetryEventTypes["GENEZIO_FRONTEND_DEPLOY_START"] = "GENEZIO_FRONTEND_DEPLOY_START";
TelemetryEventTypes["GENEZIO_FRONTEND_DEPLOY_ERROR"] = "GENEZIO_FRONTEND_DEPLOY_ERROR";
TelemetryEventTypes["GENEZIO_FRONTEND_DEPLOY_END"] = "GENEZIO_FRONTEND_DEPLOY_END";
TelemetryEventTypes["GENEZIO_POST_FRONTEND_DEPLOY_SCRIPT_ERROR"] = "GENEZIO_POST_FRONTEND_DEPLOY_SCRIPT_ERROR";
TelemetryEventTypes["GENEZIO_GENERATE_SDK"] = "GENEZIO_GENERATE_SDK";
TelemetryEventTypes["GENEZIO_INIT"] = "GENEZIO_INIT";
TelemetryEventTypes["GENEZIO_INIT_ERROR"] = "GENEZIO_INIT_ERROR";
TelemetryEventTypes["GENEZIO_LOCAL"] = "GENEZIO_LOCAL";
TelemetryEventTypes["GENEZIO_LOCAL_RELOAD"] = "GENEZIO_LOCAL_RELOAD";
TelemetryEventTypes["GENEZIO_LOGIN"] = "GENEZIO_LOGIN";
TelemetryEventTypes["GENEZIO_LOGOUT"] = "GENEZIO_LOGOUT";
TelemetryEventTypes["GENEZIO_LS"] = "GENEZIO_LS";
TelemetryEventTypes["GENEZIO_LS_ERROR"] = "GENEZIO_LS_ERROR";
TelemetryEventTypes["GENEZIO_DELETE_PROJECT"] = "GENEZIO_DELETE_PROJECT";
TelemetryEventTypes["GENEZIO_DELETE_PROJECT_ERROR"] = "GENEZIO_DELETE_PROJECT_ERROR";
TelemetryEventTypes["GENEZIO_DEPLOY_LOAD_ENV_VARS"] = "GENEZIO_DEPLOY_LOAD_ENV_VARS";
TelemetryEventTypes["GENEZIO_COMMAND_ERROR"] = "GENEZIO_COMMAND_ERROR";
TelemetryEventTypes["GENEZIO_COMMAND"] = "GENEZIO_COMMAND";
TelemetryEventTypes["GENEZIO_CREATE"] = "GENEZIO_CREATE";
TelemetryEventTypes["GENEZIO_CREATE_ERROR"] = "GENEZIO_CREATE_ERROR";
TelemetryEventTypes["GENEZIO_CREATE_INTERACTIVE"] = "GENEZIO_CREATE_INTERACTIVE";
TelemetryEventTypes["GENEZIO_CREATE_INTERACTIVE_ERROR"] = "GENEZIO_CREATE_INTERACTIVE_ERROR";
TelemetryEventTypes["GENEZIO_CREATE_TEMPLATE_LIST"] = "GENEZIO_CREATE_TEMPLATE_LIST";
TelemetryEventTypes["GENEZIO_CREATE_TEMPLATE_LIST_ERROR"] = "GENEZIO_CREATE_TEMPLATE_LIST_ERROR";
})(TelemetryEventTypes || (TelemetryEventTypes = {}));
export class GenezioTelemetry {
static async getSessionId() {
const sessionId = await getTelemetrySessionId();
if (!sessionId) {
const newSessionId = uuidv4();
debugLogger.debug(`[GenezioTelemetry]`, `New session id: ${newSessionId}`);
saveTelemetrySessionId(newSessionId);
return newSessionId;
}
return sessionId;
}
static async sendEvent(eventRequest) {
if (process.env["GENEZIO_NO_TELEMETRY"] == "1") {
debugLogger.debug(`[GenezioTelemetry]`, `Telemetry disabled by user`);
return;
}
// get user language
const userLanguage = Intl.DateTimeFormat().resolvedOptions().locale;
// get user operating system
const operatingSystem = process.platform;
const sessionId = await this.getSessionId().catch((err) => {
debugLogger.debug(`[GenezioTelemetry]`, `Error getting session id: ${err}`);
return "";
});
if (!sessionId) {
return;
}
// get user country
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
// send event to telemetry
debugLogger.trace(`[GenezioTelemetry]`, `${timeZone} ${eventRequest.eventType} ${sessionId} ${userLanguage} ${operatingSystem} ${eventRequest.cloudProvider} ${eventRequest.errorTrace}`);
// send event to analytics
const analyticsData = {
env: ENVIRONMENT,
eventType: eventRequest.eventType,
sessionId,
operatingSystem,
userLanguage,
cloudProvider: eventRequest.cloudProvider,
errTrace: eventRequest.errorTrace,
timeZone: timeZone,
genezioVersion: version,
commandOptions: eventRequest.commandOptions || "",
isCI: isCI() ? true : false,
nodeVersion: process.version,
};
const user = await getCachedUser().catch(() => undefined);
const eventName = eventRequest.eventType.toLowerCase();
const amplitudePromise = ENVIRONMENT === "dev"
? Promise.resolve()
: track(eventName, undefined, {
device_id: sessionId,
user_id: user?.id,
}).promise;
const gaPromise = ENVIRONMENT === "dev"
? Promise.resolve()
: trackEvent(eventRequest.eventType.toLowerCase(), user?.id);
const analytics = AnalyticsHandler.sendEvent(analyticsData);
await Promise.all([gaPromise, analytics, amplitudePromise]).catch((err) => {
debugLogger.debug(`[GenezioTelemetry]`, `Error sending event to analytics: ${err}`);
});
}
}